[SCM] ci-tooling packaging branch, master, updated. e4846a1a73331205f946668f8bfeb969b0efa98d

Harald Sitter apachelogger-guest at moszumanska.debian.org
Mon Jan 19 14:22:20 UTC 2015


Gitweb-URL: http://git.debian.org/?p=pkg-kde/ci-tooling.git;a=commitdiff;h=e4846a1

The following commit has been merged in the master branch:
commit e4846a1a73331205f946668f8bfeb969b0efa98d
Author: Harald Sitter <sitter at kde.org>
Date:   Mon Jan 19 15:22:16 2015 +0100

    cleanup lp
---
 lib/lp.rb | 320 ++++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 177 insertions(+), 143 deletions(-)

diff --git a/lib/lp.rb b/lib/lp.rb
index c2bce45..5942d8d 100644
--- a/lib/lp.rb
+++ b/lib/lp.rb
@@ -1,156 +1,190 @@
+# Copyright (C) 2014-2015 Harald Sitter <sitter at kde.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License or (at your option) version 3 or any later version
+# accepted by the membership of KDE e.V. (or its successor approved
+# by the membership of KDE e.V.), which shall act as a proxy
+# defined in Section 14 of version 3 of the license.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require 'json'
+require 'ostruct'
+require 'net/http'
+require 'net/http/exceptions'
+
+require 'oauth'
+require 'oauth/signature/plaintext'
+
+# A simple launchpad REST API wrapper.
 module Launchpad
-    require 'json'
-    require 'ostruct'
-    require 'net/http'
-    require 'net/http/exceptions'
-
-    require 'oauth'
-    require 'oauth/signature/plaintext'
-
-    @token = nil
-
-    def self.token()
-        return @token
+  @token = nil
+
+  def self.token
+    @token
+  end
+
+  # Get or load OAuth token.
+  def self.authenticate
+    consumer_options = {
+      :signature_method   => 'PLAINTEXT',
+      :request_token_path => '/+request-token',
+      :authorize_path     => '/+authorize-token',
+      :access_token_path  => '/+access-token',
+    }
+
+    # Fun story, during auth for some reason launchpad needs the flags in the
+    # body while at usage it only works with flags in the header...
+
+    token_hash = {}
+    conf = "#{ENV['HOME']}/.config/lp-tokens.json"
+    if File.exist?(conf)
+      token_hash = JSON.parse(File.read(conf), symbolize_names: true)
+    else
+      consumer = OAuth::Consumer.new('kubuntu-ci', '',
+                                     consumer_options.merge(scheme: :body,
+                                                            site: 'https://launchpad.net'))
+      request_token = consumer.get_request_token(oauth_callback: '')
+      puts request_token.authorize_url(oauth_callback: '')
+      loop do
+        puts 'Type "done" and hit enter when done'
+        break if gets.strip == 'done'
+      end
+      access_token = request_token.get_access_token
+
+      # Get the hash, write to file.
+      token_hash = access_token.params
+      File.write(conf, JSON.fast_generate(token_hash))
     end
 
-    def self.authenticate()
-        consumer_options = {
-            :signature_method => 'PLAINTEXT',
-            :request_token_path => '/+request-token',
-            :authorize_path     => '/+authorize-token',
-            :access_token_path  => '/+access-token',
-        }
-
-        # Fun story, during auth for some reason launchpad needs the flags in the body
-        # while at usage it only works with flags in the header...
-
-        token_hash = {}
-        conf = "#{ENV['HOME']}/.config/lp-tokens.json"
-        if File.exist?(conf)
-            token_hash = JSON::parse(File.read(conf), :symbolize_names => true)
-        else
-            consumer = OAuth::Consumer.new('kubuntu-ci', '', consumer_options.merge(:scheme => :body,
-                                                                                    :site => "https://launchpad.net"))
-            # consumer.http.set_debug_output($stdout)
-            request_token = consumer.get_request_token(:oauth_callback => '')
-            p request_token.authorize_url(:oauth_callback => '')
-            sleep 15
-            access_token = request_token.get_access_token
-
-            # Get the hash, write to file.
-            token_hash = access_token.params
-            File.write(conf, JSON::fast_generate(token_hash))
-        end
-
-        consumer = OAuth::Consumer.new('kubuntu-ci', '', consumer_options.merge(:scheme => :header,
-                                                                                :site => "https://api.launchpad.net"))
-        # consumer.http.set_debug_output($stdout)
-        access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
-        @token = access_token
-        return @token
+    consumer = OAuth::Consumer.new('kubuntu-ci', '',
+                                   consumer_options.merge(scheme: :header,
+                                                          site: 'https://api.launchpad.net'))
+    access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
+    @token = access_token
+    @token
+  end
+
+  def self.get(uri)
+    if @token
+      # Token internally URIfies again without checking if it already has
+      # a URI, so simply give it a string...
+      response = @token.get(uri.to_s)
+      fail Net::HTTPRetriableError.new(response.body, response) unless response.is_a? Net::HTTPSuccess
+      return response.body
     end
 
-    def self.get(uri)
-        if @token
-            # Token internally URIfies again without checking if it already has
-            # a URI, so simply give it a string...
-            response = @token.get(uri.to_s)
-            raise Net::HTTPRetriableError.new(response.body, response) unless response.kind_of? Net::HTTPSuccess
-            return response.body
+    # Set cache control.
+    # Launchpad employs server-side caching, which is nice but for our purposes
+    # 90% of the time we need current data, otherwise we wouldn't be polling
+    # on a schedule.
+    d = nil
+    Net::HTTP.start(uri.hostname,
+                    uri.port,
+                    use_ssl: (uri.scheme == 'https')) do |http|
+                      d = http.request_get(uri, 'Cache-Control' => 'max-age=0').body
+                    end
+    d
+  end
+
+  def self.post(uri)
+    # Posting always requires a token.
+    response = @token.post(uri.path, uri.query)
+    fail Net::HTTPRetriableError.new(response.body, response) unless response.is_a? Net::HTTPSuccess
+    response.body
+  end
+
+  # Unlike launchpadlib we strive for minimal overhead by minimal validation.
+  # Launchpadlib internally will lookup the representation spec through WADL
+  # files offered by the API. We don't. We also have no advanced caching in
+  # place, if one wants caching it ought to be implemented client-side.
+  #  - Properties are all accessible through accessors.
+  #  - Methods that do not exist will trigger a HTTP GET on the identifier
+  #  - Unless the method ends with a ! in which case a HTTP POST will be done
+  #  - TODO: Property changes not implemented. Would probably go through a save
+  #    accessor.
+  class Rubber < OpenStruct
+    # @!visibility private
+    def method_missing(name, *args, &block)
+      #             puts "++++++++++++++ method missing! #{name}"
+      # Pointer to different URL reflecting a different object.
+      if self["#{name}_link"]
+        data = Launchpad.get(URI(send("#{name}_link")))
+        return Rubber.from_json(data)
+      end
+
+      # Pointer to a different URL reflecting a collection of things.
+      if self["#{name}_collection_link"]
+        ret = []
+        uri = URI(send("#{name}_collection_link"))
+        loop do
+          obj = Rubber.from_json(Launchpad.get(uri))
+          ret += obj.entries
+          return ret unless obj['next_collection_link']
+          uri = URI(obj.next_collection_link)
         end
-
-        # Set cache control.
-        # Launchpad employs server-side caching, which is nice but for our purposes
-        # 90% of the time we need current data, otherwise we wouldn't be polling
-        # on a schedule.
-        d = nil
-        Net::HTTP.start(uri.hostname,
-                        uri.port,
-                        :use_ssl => (uri.scheme == 'https')) do |http|
-            d = http.request_get(uri, "Cache-Control" => "max-age=0").body
+      end
+
+      # Build parameters.
+      # For convenience reasons passing a parameter object that itself
+      # responds to self_link will result in its self_link being passed.
+      params = args[0]
+      params ||= {}
+      params['ws.op'] = name.to_s.chomp('!')
+      params.each do | key, value |
+        next unless value['self_link']
+        params[key] = value['self_link']
+      end
+
+      uri = URI(self['self_link'])
+      uri.query = URI.encode_www_form(params)
+
+      # Try to call as a 'function' on the API
+      if name.to_s.end_with?('!') # foo! causes a post
+        return Launchpad.post(uri)
+      else # foo causes a get
+        ret = []
+        loop do
+          obj = Rubber.from_json(Launchpad.get(uri))
+          if obj['entries']
+            ret += obj.entries
+          else
+            return obj
+          end
+          return ret unless obj['next_collection_link']
+          uri = URI(obj.next_collection_link)
         end
-        return d
+      end
     end
 
-    def self.post(uri)
-        # Posting always requires a token.
-        response = @token.post(uri.path, uri.query)
-        raise raise Net::HTTPRetriableError.new(response.body, response) unless response.kind_of? Net::HTTPSuccess
-        return response.body
+    # Construct a {Rubber} from a JSON string.
+    # @param json JSON string to construct a rubber from. Can also be a primitve
+    #   that doesn't qualify for a Rubber, in which case the return type will be
+    #   different.
+    # @return [Rubber, Object] a Rubber instance or a primitive type, depending
+    #   on what the JSON string contains
+    def self.from_json(json)
+      # Launchpad uses new-style JSON which can be any of the JSON literals
+      # in addition to objects. To make the parser pick this up we need
+      # to enable quirks mode.
+      JSON.parse(json, quirks_mode: true, object_class: Rubber)
     end
 
-    # Unlike launchpadlib we strive for minimal overhead by minimal validation.
-    # Launchpadlib internally will lookup the representation spec through WADL
-    # files offered by the API. We don't. We also have no advanced caching in
-    # place, if one wants caching it ought to be implemented client-side.
-    #  - Properties are all accessible through accessors.
-    #  - Methods that do not exist will trigger a HTTP GET on the identifier
-    #  - Unless the method ends with a ! in which case a HTTP POST will be done
-    #  - TODO: Property changes not implemented. Would probably go through a save accessor.
-    class Rubber < OpenStruct
-        def method_missing(name, *args, &block)
-#             puts "++++++++++++++ method missing! #{name}"
-            # Pointer to different URL reflecting a different object.
-            if self["#{name}_link"]
-                data = Launchpad::get(URI(self.send("#{name}_link")))
-                return Rubber::from_json(data)
-            end
-
-            # Pointer to a different URL reflecting a collection of things.
-            if self["#{name}_collection_link"]
-                ret = []
-                uri = URI(self.send("#{name}_collection_link"))
-                loop do
-                    obj = Rubber::from_json(Launchpad::get(uri))
-                    ret += obj.entries
-                    return ret unless obj['next_collection_link']
-                    uri = URI(obj.next_collection_link)
-                end
-            end
-
-            # Build parameters.
-            # For convenience reasons passing a parameter object that itself
-            # responds to self_link will result in its self_link being passed.
-            params = args[0]
-            params ||= {}
-            params['ws.op']= name.to_s.chomp('!')
-            params.each do | key, value |
-                next unless value["self_link"] rescue next
-                params[key] = value["self_link"]
-            end
-
-            uri = URI(self['self_link'])
-            uri.query = URI.encode_www_form(params)
-
-            # Try to call as a 'function' on the API
-            if name.to_s.end_with?('!') # foo! causes a post
-                return Launchpad::post(uri)
-            else # foo causes a get
-                ret = []
-                loop do
-                    obj = Rubber::from_json(Launchpad::get(uri))
-                    if obj['entries']
-                        ret += obj.entries
-                    else
-                        return obj
-                    end
-                    return ret unless obj['next_collection_link']
-                    uri = URI(obj.next_collection_link)
-                end
-            end
-        end
-
-        def self.from_json(json)
-            # Launchpad uses new-style JSON which can be any of the JSON literals
-            # in addition to objects. To make the parser pick this up we need
-            # to enable quirks mode.
-            return JSON::parse(json, quirks_mode: true, object_class: Rubber)
-        end
-
-        def self.from_url(url)
-            uri = URI(url)
-            reply = Launchpad::get(uri)
-            return Rubber::from_json(reply)
-        end
+    # Construct a {Rubber} from the JSON returned from an HTTP GET on an URL.
+    # @param url URL to HTTP GET
+    # @return [Rubber, Object] see {from_json}
+    def self.from_url(url)
+      uri = URI(url)
+      reply = Launchpad.get(uri)
+      Rubber.from_json(reply)
     end
+  end
 end

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list