Class: ExtractRuby::RubyExtractotr

Inherits:
Ex
  • Object
show all
Defined in:
lib/parser/gemfile_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path) ⇒ RubyExtractotr

Returns a new instance of RubyExtractotr.



25
26
27
28
29
30
31
32
33
# File 'lib/parser/gemfile_parser.rb', line 25

def initialize(repo_path)
  @repo_path = repo_path

  @package_list       = [] # [name, version, status] package list , not exist "\n"
  @rubygems_not_found = 30 # rubygemsDB not found
  @status_init = 10
  # gem server or git uri
  @status_private_source = 33
end

Instance Attribute Details

#package_listObject (readonly)

attr_reader :two_dependencies



23
24
25
# File 'lib/parser/gemfile_parser.rb', line 23

def package_list
  @package_list
end

Instance Method Details

#get_first_level_specsObject

gist.github.com/flavio/1722530 DOC: github.com/bundler/bundler/blob/master/lib/bundler/lockfile_parser.rb description : use bundler extract ruby package from gemfile.lcok repo_path : local repo path , type : String st_true : Correct extraction 10 s.source.options, [‘revision’]



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/parser/gemfile_parser.rb', line 41

def get_first_level_specs()
  all_specs = []
  lockfiles = Obtain_path.new(@repo_path, "gem", ".lock").get_data
  if lockfiles.size == 0
    $plog.info('Gemfile.lock files not found, Ruby packages not found')
    return all_specs
  end

  lockfiles.each do |pathname|
    $plog.debug("Gemfile.lock file pathname: #{pathname}")
    lockfile_parser = Bundler::LockfileParser.new(Bundler.read_file(pathname))
    lockfile_parser.specs.each do |s|
      all_specs.push(s)
      # The 2nd level dependencies
      # s.dependencies.each{|rows|
      #   rows.requirement.requirements.each{|row|
      #     tmp.concat [[row[0], row[1].version]]
      #   }
      # }
    end
  end
  all_specs
end

#select_rubygems_dbObject



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/parser/gemfile_parser.rb', line 141

def select_rubygems_db
  # FIXME: db connection
  require_relative '../lib/api/gem_data'
  rubygems_result = []
  ruby_gems = API::GemData.new
  @package_list.each do |row|
    pack_hash = {
        :pack_name => nil,
        :pack_version => nil,
        :homepage => nil,
        :source_url => nil,
        :license => nil,
        :status => 10,
        :cmt => nil,
        :language => nil
    }
    if row['uri'] != nil
      pack_hash[:language]     = row['uri']
      pack_hash[:pack_name]    = row['name']
      pack_hash[:pack_version] = row['version']
      pack_hash[:source_url]   = row['uri']
      rubygems_result << pack_hash
      next
    elsif row['remotes'] != nil
      pack_hash[:language] = row['remotes']
    end
    if 10 == row['st']
      result = ruby_gems.get_gemdata(row['name'],row['version'])
      if result != nil
        pack_hash[:pack_name]    = result[:name]
        pack_hash[:pack_version] = result[:version]
        pack_hash[:homepage]     = result[:homepage]
        pack_hash[:source_url]   = result[:source_url]
        pack_hash[:license]      = result[:license]
        pack_hash[:status]       = row['st']
      elsif nil == result
        pack_hash[:pack_name]    = row['name']
        if "" == row['version']
          pack_hash[:pack_version] = nil
        else
          pack_hash[:pack_version] = row['version']
        end
        pack_hash[:status]       = @rubygems_not_found
      end
    end

    rubygems_result << pack_hash
  end

  return rubygems_result
end

#startObject



65
66
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
# File 'lib/parser/gemfile_parser.rb', line 65

def start
  all_packs = []
  require_relative '../lib/api/gem_data'
  require_relative '../lib/api'
  local_gemdb = API::GemData.new
  rubygems_org = 'https://rubygems.org'
  rubygems_org_domain = 'rubygems.org'

  first_level_specs = get_first_level_specs
  first_level_specs.each {|s|
    # $plog.debug("git_uri: #{s.source.options['uri']}, remotes: #{s.source.options['remotes']}")
    # gem_server_remotes.join(',').gsub(/http[s]?:\/\//, '')
    pack = nil
    pack_name = s.name
    pack_version = s.version.to_s

    if s.source.class == Bundler::Source::Git
      source_url = s.source.options['uri']
      _status = @status_init
      _lang = source_url
      _cmt = nil

      if source_url =~ API::SOURCE_URL_PATTERN[:github]
        g = API::Github.new(source_url)
        source_url = g.repo_url
        _lang = 'https://github.com'
        if g.list_contents('').size == 0
          _status = @status_private_source
          _cmt = 'Private git uri, source code not found'
        end
      end
      pack = {
        pack_name: pack_name,
        pack_version: pack_version,
        homepage: nil,
        source_url: source_url,
        license: nil,
        status: _status,
        language: _lang,
        # This cmt require local rubygems.org database always up-to-date
        cmt: _cmt
      }
    elsif s.source.class == Bundler::Source::Rubygems
      gem_server_remotes = s.source.options['remotes']

      formatted_remotes = gem_server_remotes.collect {|r|
        r.gsub(/\/$/, '').gsub(/http:\/\/rubygems\.org/, rubygems_org)
      }
      # TODO: get Gemfile source section
      db_pack = local_gemdb.get_gem(pack_name, pack_version)
      if formatted_remotes.index(rubygems_org) and db_pack
        pack = db_pack.merge({
                                :status => 10,
                                :language => rubygems_org_domain,
                                :cmt => nil
                              })
      else
        formatted_remotes.delete(rubygems_org)
        pack = {
          pack_name: pack_name,
          pack_version: pack_version,
          homepage: nil,
          source_url: nil,
          license: 'UNKNOWN',
          status: @status_private_source,
          language: formatted_remotes.join(''),
          # This cmt require local rubygems.org database always up-to-date
          cmt: 'Private gem server or source code not found'
        }
      end
    end
    all_packs.push(pack)
  }
  all_packs
end