[DRE-commits] [gemwatch] 01/01: reimplement support for rails-assets.org with polymorphism

Antonio Terceiro terceiro at moszumanska.debian.org
Thu Aug 6 18:42:03 UTC 2015


This is an automated email from the git hooks/post-receive script.

terceiro pushed a commit to branch master
in repository gemwatch.

commit 0b40fef9d3b222775ac32f3a82c16ca573d92d93
Author: Antonio Terceiro <terceiro at debian.org>
Date:   Thu Aug 6 15:40:18 2015 -0300

    reimplement support for rails-assets.org with polymorphism
---
 gemwatch.rb | 94 +++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 66 insertions(+), 28 deletions(-)

diff --git a/gemwatch.rb b/gemwatch.rb
index 726d3bf..6c19558 100644
--- a/gemwatch.rb
+++ b/gemwatch.rb
@@ -37,28 +37,21 @@ end
 class GemWatch::Gem
   class NotFound < Exception; end
   class CommandFailed < Exception; end
+
+  def self.get(gemname, forced_version = nil)
+    if gemname =~ /^rails-assets/
+      GemWatch::RailsAssetsGem.new(gemname, forced_version)
+    else
+      new(gemname, forced_version)
+    end
+  end
+
   def initialize(gemname, forced_version = nil)
     begin
-      # rails-assets.org does not provide a soap api/json
-      # so we can find out latest version only by a gem fetch
-      if /^rails-assets-/.match(gemname)
-        @data = { "name" => "#{gemname}", "version" => "" }
-        # keep all rails assets gems in download base
-        Dir.chdir(DOWNLOAD_BASE) do
-          run("gem", "fetch", "--source", "http://rails-assets.org", gemname)
-          # Find out latest version from downloaded gem
-          Dir.glob("#{gemname}*.gem") {|file|
-            @data['version'] =  file.split(/.gem/)[0].split(/#{gemname}-/)[1]
-            Gem2Tgz.convert!(file, tarball)
-            FileUtils.rm_f(file)
-          }
-        end
-      else
-        @data = JSON.parse(RestClient.get("http://rubygems.org/api/v1/gems/#{gemname}.json").body)
-        if forced_version
-          @data['version'] = forced_version
-          @data['gem_uri'] =  File.dirname(@data['gem_uri']) + "/#{directory}.gem"
-        end
+      @data = JSON.parse(RestClient.get("http://rubygems.org/api/v1/gems/#{gemname}.json").body)
+      if forced_version
+        @data['version'] = forced_version
+        @data['gem_uri'] =  File.dirname(@data['gem_uri']) + "/#{directory}.gem"
       end
     rescue RestClient::ResourceNotFound
       raise GemWatch::Gem::NotFound
@@ -145,6 +138,56 @@ class GemWatch::Gem
     end
   end
 end
+
+class GemWatch::RailsAssetsGem < GemWatch::Gem
+  def initialize(gemname, forced_version = nil)
+    @data = {}
+    @data['name'] = gemname
+    @data['version'] = forced_version || latest_version
+    @data['gem_uri'] = "https://rails-assets.org/gems/#{directory}.gem"
+  end
+
+  protected
+
+  def latest_version
+    cache.find do |entry|
+      entry.first == name
+    end[1].to_s
+  end
+
+  def cache
+    cache_dir = DOWNLOAD_BASE
+    cache_file = File.join(cache_dir, 'rails-assets.marshall')
+
+    if File.exists?(cache_file)
+      seconds_since_last_update = Time.now.to_i - File.stat(cache_file).mtime.to_i
+      cache_timeout = 60*60 # 1 hour
+      if seconds_since_last_update < cache_timeout
+        return Marshal.load(File.read(cache_file))
+      end
+    end
+
+    lock_file = cache_file + '.lock'
+    File.open(lock_file, 'w') do |f|
+      if f.flock(File::LOCK_EX | File::LOCK_NB)
+        # download
+        run('wget', '-q', '-O', cache_file + '.gz', 'https://rails-assets.org/latest_specs.4.8.gz')
+        run('rm', '-f', cache_file)
+        run('gunzip', cache_file + '.gz')
+        run('touch', cache_file)
+      else
+        # another process already downloading, just wait
+        f.flock(File::LOCK_SH)
+      end
+      f.flock(File::LOCK_UN)
+
+      Marshal.load(File.read(cache_file))
+    end
+  end
+
+end
+
+
 module HostHelper
   def host_with_port
     if request.respond_to?(:host_with_port)
@@ -169,7 +212,7 @@ end
 get '/:gem' do
   expires 14400, :public # 4 hours
   begin
-    @gem = GemWatch::Gem.new(params[:gem])
+    @gem = GemWatch::Gem.get(params[:gem])
     haml :gem
   rescue GemWatch::Gem::NotFound
     not_found
@@ -183,13 +226,8 @@ get '/download/:tarball' do
     gem_name = $1
     gem_version = $2
 
-    gem = GemWatch::Gem.new(gem_name, gem_version)
-    if /^rails-assets-/.match(gem_name)
-      # rails-assets-* gems are downloaded during GemWatch object creation
-      send_file "#{DOWNLOAD_BASE}/" + "#{params[:tarball]}"
-    else
-      send_file gem.download_for(gem_version)
-    end
+    gem = GemWatch::Gem.get(gem_name, gem_version)
+    send_file gem.download_for(gem_version)
   rescue GemWatch::Gem::NotFound
     not_found
   end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/gemwatch.git



More information about the Pkg-ruby-extras-commits mailing list