Class: API::RemoteSourcePackage

Inherits:
Object
  • Object
show all
Defined in:
lib/api/remote_source_package.rb

Overview

TODO: replace this impl Ruby mixin’s way

module Person

def self.included x
  ms = x.instance_methods(false)
  [:xx, :yy, :zz].each do |xn|
    raise "Please IMPL #{xn} Interface" unless ms.include?(xn)
  end
end

end

class Customer

def xx;end
def yy;end
def zz;end
include Person

end

c = Customer.new c.xx c.yy c.zz

Direct Known Subclasses

Launchpad, ManifestPackage

Instance Method Summary collapse

Constructor Details

#initializeRemoteSourcePackage

Returns a new instance of RemoteSourcePackage.



38
39
# File 'lib/api/remote_source_package.rb', line 38

def initialize()
end

Instance Method Details

#download_source_code(source_code_url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/api/remote_source_package.rb', line 47

def download_source_code(source_code_url)
  source_code_filename = source_code_url.split('/').last
  source_code_path = "#{LAUNCHPAD_SOURCE_DIR}/#{source_code_filename}"
  File.open(source_code_path, 'wb') do |f|
    f.binmode
    http_option = {
      :timeout => HTTPARTY_DOWNLOAD_TIMEOUT
    }
    http_proxy = Misc.get_http_proxy
    if http_proxy
      http_option[:http_proxyaddr] = http_proxy[:addr]
      http_option[:http_proxyport] = http_proxy[:port]
    end
    f.write(HTTParty.get(source_code_url, options=http_option).parsed_response)
  end

  return source_code_path
end

#fetch_license_info_from_local_sourceObject

Entry



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/api/remote_source_package.rb', line 67

def fetch_license_info_from_local_source()
  license = nil
  license_url = nil
  license_text = nil
  source_package_download_url = nil

  # Attention, source package but binary package
  source_package_homepage, source_package_download_url = find_source_package_homepage_and_download_url
  $plog.debug("source_package_homepage: #{source_package_homepage}")
  $plog.debug("source_package_download_url: #{source_package_download_url}")
  if source_package_download_url
    source_code_path = download_source_code(source_package_download_url)
    $plog.debug("#{source_code_path}")
    if source_code_path
      reader = nil
      if source_code_path =~ API::FILE_TYPE_PATTERN[:tar_gz]
        reader = Zlib::GzipReader
      elsif source_code_path =~ API::FILE_TYPE_PATTERN[:tar_xz]
        reader = XZ::StreamReader
      elsif source_code_path =~ API::FILE_TYPE_PATTERN[:tar_bz2]
        # Bash script demo, MacOSX tar is not compatible
        # $ tar --version
        # tar (GNU tar) 1.27.1
        # >>> Copyright (C) 2013 Free Software Foundation, Inc.
        # >>> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
        # >>> This is free software: you are free to change and redistribute it.
        # >> There is NO WARRANTY, to the extent permitted by law.
        # >> Written by John Gilmore and Jay Fenlason.
        # $ tar -tjvf bison_3.0.2.dfsg.orig.tar.bz2 | grep -i 'license\|copying\|readme' | awk '{ print $6 }'
        # $ tar -xj --file=bison_3.0.2.dfsg.orig.tar.bz2 bison-3.0.2.dfsg/COPYING
        # $ tar -xjO --file=bison_3.0.2.dfsg.orig.tar.bz2 bison-3.0.2.dfsg/COPYING
        # $ tar -xjO --file=bison_3.0.2.dfsg.orig.tar.bz2 bison-3.0.2.dfsg/COPYING -C /dev/null
        cmd_list_content = "tar -tjvf #{source_code_path} | grep -i 'license\\|copying' | awk '{ print $6 }'"
        # MacOSX
        # cmd_list_content = "tar -tjvf #{source_code_path} | grep -i 'license\\|copying' | awk '{ print $9 }'"
        $plog.debug(cmd_list_content)
        Open3.popen3(cmd_list_content) {|i,o,e,t|
          out = o.readlines
          error = e.readlines
          if error.length > 0
            # todo: move into exception.rb
            raise "decompress error: #{source_code_path}, #{error}"
          elsif out.length > 0
            out.each {|line|
              license_file_path = line.gsub(/\n/, '')
              if @root_license_only and !API::Helper.is_root_file(license_file_path)
                next
              end
              cmd_read_content = "tar -xjO --file=#{source_code_path} #{license_file_path} -C /dev/null"
              Open3.popen3(cmd_read_content) {|i,o,e,t|
                out2 = o.read
                error = e.readlines
                if error.length > 0
                  raise "cmd_read_content error: #{source_code_path}, #{license_file_path}, #{error}"
                elsif out2.length > 0
                  license_text = out2
                  license_url = license_file_path
                  $plog.debug(license_text)
                  break
                end
              }
            }
          end
        }
      else
        $plog.error("source_package_download_url: #{source_package_download_url}, can NOT be uncompressed.")
        return {}
      end

      if reader
        tar_extract = Gem::Package::TarReader.new(reader.open(source_code_path))
        tar_extract.rewind # The extract has to be rewinded after every iteration
        tar_extract.each do |entry|
          puts entry.full_name
          # puts entry.directory?
          # puts entry.file?
          # puts entry.read
          # Root dir files only
          if entry.directory? or !API::Helper.is_root_file(entry.full_name)
            # python-defaults-2.7.5/debian/copyright
            #if API::Helper.is_debian_copyright_file(entry.full_name)
            #  license_url = entry.full_name
            #  license_text = entry.read
            #  break
            #else
              next
           # end
          end

          if entry.file? and API::Helper.is_license_file(entry.full_name)
            license_url = entry.full_name
            license_text = entry.read

            $plog.debug(entry.full_name)
            $plog.debug(license_text)

            # TODO: parser license info
            break
          end

          # TODO:
          # if entry.file? and API::Helper.is_readme_file(entry.full_name)
          #   puts entry.full_name
          #   # puts entry.read
          #   # TODO: readme parser license info
          #   break
          # end

        end
        tar_extract.close ### to abstract out
      end
    end
  end

  if license_text
    license = License_recognition.new.similarity(license_text, STD_LICENSE_DIR)
  end
  {
    license: license,
    license_url: license_url,
    license_text: license_text,
    source_url: source_package_download_url,
    homepage: source_package_homepage
  }
end

#find_source_package_homepage_and_download_urlObject

OVERRIDE required return: homepage, download_url



43
44
45
# File 'lib/api/remote_source_package.rb', line 43

def find_source_package_homepage_and_download_url()
  raise 'Method must be overridden'
end