Class: Bibliothecary::Parsers::Pypi

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/pypi.rb

Constant Summary collapse

INSTALL_REGEXP =
/install_requires\s*=\s*\[([\s\S]*?)\]/
REQUIRE_REGEXP =

Capture Group 1 is package. Optional Group 2 is [extras]. Capture Group 3 is Version

/([a-zA-Z0-9]+[a-zA-Z0-9\-_\.]+)(?:\[.*?\])*([><=\w\.,]+)?/
REQUIREMENTS_REGEXP =
/^#{REQUIRE_REGEXP}/
MANIFEST_REGEXP =
/.*require[^\/]*(\/)?[^\/]*\.(txt|pip|in)$/
PIP_COMPILE_REGEXP =
/.*require.*$/
PEP_508_NAME_REGEX =
/^([A-Z0-9][A-Z0-9._-]*[A-Z0-9]|[A-Z0-9])/i
NoEggSpecified =

While the thing in the repo that PyPI is using might be either in egg format or wheel format, PyPI uses “egg” in the fragment of the VCS URL to specify what package in the PyPI index the VCS URL should be treated as.

Class.new(ArgumentError)

Class Method Summary collapse

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.map_dependencies(packages, type) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/bibliothecary/parsers/pypi.rb', line 134

def self.map_dependencies(packages, type)
  return [] unless packages
  packages.map do |name, info|
    {
      name: name,
      requirement: map_requirements(info),
      type: type
    }
  end
end

.map_requirements(info) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bibliothecary/parsers/pypi.rb', line 145

def self.map_requirements(info)
  if info.is_a?(Hash)
    if info['version']
      info['version']
    elsif info['git']
      info['git'] + '#' + info['ref']
    else
      '*'
    end
  else
    info || '*'
  end
end

.mappingObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bibliothecary/parsers/pypi.rb', line 20

def self.mapping
  {
    match_filenames('requirements-dev.txt', 'requirements/dev.txt',
                    'requirements-docs.txt', 'requirements/docs.txt',
                    'requirements-test.txt', 'requirements/test.txt',
                    'requirements-tools.txt', 'requirements/tools.txt') => {
      kind: 'manifest',
      parser: :parse_requirements_txt
    },
    lambda { |p| PIP_COMPILE_REGEXP.match(p) } => {
      content_matcher: :pip_compile?,
      kind: 'lockfile',
      parser: :parse_requirements_txt
    },
    lambda { |p| MANIFEST_REGEXP.match(p) } => {
      kind: 'manifest',
      parser: :parse_requirements_txt,
      can_have_lockfile: false
    },
    match_filename('requirements.frozen') => { # pattern exists to store frozen deps in requirements.frozen
      parser: :parse_requirements_txt,
      kind: 'lockfile'
    },
    match_filename('pip-resolved-dependencies.txt') => { # Inferred from pip
      kind: 'lockfile',
      parser: :parse_requirements_txt
    },
    match_filename("setup.py") => {
      kind: 'manifest',
      parser: :parse_setup_py,
      can_have_lockfile: false
    },
    match_filename("Pipfile") => {
      kind: 'manifest',
      parser: :parse_pipfile
    },
    match_filename("Pipfile.lock") => {
      kind: 'lockfile',
      parser: :parse_pipfile_lock
    },
    match_filename("pyproject.toml") => {
      kind: 'manifest',
      parser: :parse_pyproject
    },
    match_filename("poetry.lock") => {
      kind: 'lockfile',
      parser: :parse_poetry_lock
    },
    # Pip dependencies can be embedded in conda environment files
    match_filename("environment.yml") => {
      parser: :parse_conda,
      kind: "manifest"
    },
    match_filename("environment.yaml") => {
      parser: :parse_conda,
      kind: "manifest"
    },
    match_filename("environment.yml.lock") => {
      parser: :parse_conda,
      kind: "lockfile"
    },
    match_filename("environment.yaml.lock") => {
      parser: :parse_conda,
      kind: "lockfile"
    }
  }
end

.parse_conda(file_contents, options: {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/bibliothecary/parsers/pypi.rb', line 123

def self.parse_conda(file_contents, options: {})
  contents = YAML.safe_load(file_contents)
  return [] unless contents

  dependencies = contents["dependencies"]
  pip = dependencies.find { |dep| dep.is_a?(Hash) && dep["pip"]}
  return [] unless pip

  Pypi.parse_requirements_txt(pip["pip"].join("\n"))
end

.parse_pep_508_dep_spec(dep) ⇒ Object

Simply parses out the name of a PEP 508 Dependency specification: peps.python.org/pep-0508/ Leaves the rest as-is with any leading semicolons or spaces stripped



283
284
285
286
287
288
# File 'lib/bibliothecary/parsers/pypi.rb', line 283

def self.parse_pep_508_dep_spec(dep)
  name, requirement = dep.split(PEP_508_NAME_REGEX, 2).last(2).map(&:strip)
  requirement = requirement.sub(/^[\s;]*/, "")
  requirement = "*" if requirement == ""
  return name, requirement
end

.parse_pipfile(file_contents, options: {}) ⇒ Object



92
93
94
95
# File 'lib/bibliothecary/parsers/pypi.rb', line 92

def self.parse_pipfile(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)
  map_dependencies(manifest['packages'], 'runtime') + map_dependencies(manifest['dev-packages'], 'develop')
end

.parse_pipfile_lock(file_contents, options: {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/bibliothecary/parsers/pypi.rb', line 159

def self.parse_pipfile_lock(file_contents, options: {})
  manifest = JSON.parse(file_contents)
  deps = []
  manifest.each do |group, dependencies|
    next if group == "_meta"
    group = 'runtime' if group == 'default'
    dependencies.each do |name, info|
      deps << {
        name: name,
        requirement: map_requirements(info),
        type: group
      }
    end
  end
  deps
end

.parse_poetry(file_contents, options: {}) ⇒ Object

TODO: this was deprecated in 8.6.0. Remove this in any major version bump >= 9.*



118
119
120
121
# File 'lib/bibliothecary/parsers/pypi.rb', line 118

def self.parse_poetry(file_contents, options: {})
  puts "Warning: parse_poetry() is deprecated, use parse_pyproject() instead."
  parse_pyproject(file_contents, options)
end

.parse_poetry_lock(file_contents, options: {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bibliothecary/parsers/pypi.rb', line 176

def self.parse_poetry_lock(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)
  deps = []
  manifest["package"].each do |package|
    # next if group == "_meta"
    group = case package['category']
            when 'main'
              'runtime'
            when 'dev'
              'develop'
            end

    deps << {
      name: package['name'],
      requirement: map_requirements(package),
      type: group
    }
  end
  deps
end

.parse_pyproject(file_contents, options: {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bibliothecary/parsers/pypi.rb', line 97

def self.parse_pyproject(file_contents, options: {})
  deps = []

  file_contents = Tomlrb.parse(file_contents)

  # Parse poetry [tool.poetry] deps
  poetry_manifest = file_contents.fetch('tool', {}).fetch('poetry', {})
  deps += map_dependencies(poetry_manifest['dependencies'], 'runtime')
  deps += map_dependencies(poetry_manifest['dev-dependencies'], 'develop')

  # Parse PEP621 [project] deps
  pep621_manifest = file_contents.fetch('project', {})
  pep621_deps = pep621_manifest.fetch('dependencies', []).map { |d| parse_pep_508_dep_spec(d) }
  deps += map_dependencies(pep621_deps, 'runtime')

  # We're combining both poetry+PEP621 deps instead of making them mutually exclusive, until we
  # find a reason not to ingest them both.
  deps.uniq
end

.parse_requirements_txt(file_contents, options: {}) ⇒ Object

Parses a requirements.txt file, following the pip.pypa.io/en/stable/cli/pip_install/#requirement-specifiers and pip.pypa.io/en/stable/topics/vcs-support/#git. Invalid lines in requirements.txt are skipped.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/bibliothecary/parsers/pypi.rb', line 224

def self.parse_requirements_txt(file_contents, options: {})
  deps = []
  type = case options[:filename]
         when /dev/ || /docs/ || /tools/
           'development'
         when /test/
           'test'
         else
           'runtime'
         end

  file_contents.split("\n").each do |line|
    if line['://']
      begin
        result = parse_requirements_txt_url(line)
      rescue URI::Error, NoEggSpecified => e
        next
      end

      deps << result.merge(
        type: type
      )
    else
      match = line.delete(' ').match(REQUIREMENTS_REGEXP)
      next unless match

      deps << {
        name: match[1],
        requirement: match[-1] || '*',
        type: type
      }
    end
  end
  deps
end

.parse_requirements_txt_url(url) ⇒ Object

Raises:



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/bibliothecary/parsers/pypi.rb', line 260

def self.parse_requirements_txt_url(url)
  uri = URI.parse(url)
  raise NoEggSpecified, "No egg specified in #{url}" unless uri.fragment

  name = uri.fragment[/^egg=([^&]+)([&]|$)/, 1]
  raise NoEggSpecified, "No egg specified in #{url}" unless name

  requirement = uri.path[/@(.+)$/, 1]

  { name: name, requirement: requirement || "*" }
end

.parse_setup_py(file_contents, options: {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/bibliothecary/parsers/pypi.rb', line 197

def self.parse_setup_py(file_contents, options: {})
  match = file_contents.match(INSTALL_REGEXP)
  return [] unless match
  deps = []
  match[1].gsub(/',(\s)?'/, "\n").split("\n").each do |line|
    next if line.match(/^#/)
    match = line.match(REQUIRE_REGEXP)
    next unless match
    deps << {
      name: match[1],
      requirement: match[-1] || '*',
      type: 'runtime'
    }
  end
  deps
end

.pip_compile?(file_contents) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
279
# File 'lib/bibliothecary/parsers/pypi.rb', line 272

def self.pip_compile?(file_contents)
  return file_contents.include?("This file is autogenerated by pip-compile")
rescue Exception # rubocop:disable Lint/RescueException
  # We rescue exception here since native libs can throw a non-StandardError
  # We don't want to throw errors during the matching phase, only during
  # parsing after we match.
  false
end