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 =

TODO: can this be a more specific regexp so it doesn’t match something like “.yarn/cache/create-require-npm-1.0.0.zip”?

/.*require.*$/
PEP_508_NAME_REGEXP =
/^([A-Z0-9][A-Z0-9._-]*[A-Z0-9]|[A-Z0-9])/i
PEP_751_LOCKFILE_REGEXP =

A modified version of the regexp from the docs, to catch all cases: packaging.python.org/en/latest/specifications/pylock-toml/

/^pylock(\.[^.]+)?\.toml$/
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, source = nil) ⇒ Object



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
# File 'lib/bibliothecary/parsers/pypi.rb', line 154

def self.map_dependencies(packages, type, source = nil)
  return [] unless packages

  packages.flat_map do |name, package_info|
    local = true if package_info.is_a?(Hash) && (package_info.key?("path") || package_info.key?("file"))

    if package_info.is_a?(Array)
      # Poetry supports multiple requirements with differing specifiers for the same
      # package. Break these out into a separate dep per requirement.
      # https://python-poetry.org/docs/dependency-specification/#multiple-constraints-dependencies
      package_info.map do |info|
        Dependency.new(
          platform: platform_name,
          name: name,
          requirement: map_requirements(info),
          type: type,
          source: source,
          local: local
        )
      end
    else
      Dependency.new(
        platform: platform_name,
        name: name,
        requirement: map_requirements(package_info),
        type: type,
        source: source,
        local: local
      )
    end
  end
end

.map_requirements(info) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/bibliothecary/parsers/pypi.rb', line 187

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

.mappingObject



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
# File 'lib/bibliothecary/parsers/pypi.rb', line 27

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,
                    },
    ->(p) { PIP_COMPILE_REGEXP.match(p) } => {
      content_matcher: :pip_compile?,
      kind: "lockfile",
      parser: :parse_requirements_txt,
    },
    ->(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("pip-dependency-graph.json") => { # Exported from pipdeptree --json
      kind: "lockfile",
      parser: :parse_dependency_tree_json,
    },
    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,
    },
    # PEP-751: official python lockfile format (https://peps.python.org/pep-0751/)
    ->(p) { PEP_751_LOCKFILE_REGEXP.match(p) } => {
      kind: "lockfile",
      parser: :parser_pylock,
    },
  }
end

.normalize_name(name) ⇒ Object

Apply PyPa’s name normalization rules to the package name packaging.python.org/en/latest/specifications/name-normalization/#name-normalization



369
370
371
# File 'lib/bibliothecary/parsers/pypi.rb', line 369

def self.normalize_name(name)
  name.downcase.gsub(/[-_.]+/, "-")
end

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



277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/bibliothecary/parsers/pypi.rb', line 277

def self.parse_dependency_tree_json(file_contents, options: {})
  dependencies = JSON.parse(file_contents)
    .map do |pkg|
      Dependency.new(
        name: pkg.dig("package", "package_name"),
        requirement: pkg.dig("package", "installed_version"),
        type: "runtime",
        source: options.fetch(:filename, nil),
        platform: platform_name
      )
    end
    .uniq
  ParserResult.new(dependencies: dependencies)
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



360
361
362
363
364
365
# File 'lib/bibliothecary/parsers/pypi.rb', line 360

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

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



107
108
109
110
111
112
# File 'lib/bibliothecary/parsers/pypi.rb', line 107

def self.parse_pipfile(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)
  dependencies = map_dependencies(manifest["packages"], "runtime", options.fetch(:filename, nil)) +
                 map_dependencies(manifest["dev-packages"], "develop", options.fetch(:filename, nil))
  ParserResult.new(dependencies: dependencies)
end

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



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

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"
    deps += map_dependencies(dependencies, group, options.fetch(:filename, nil))
  end
  ParserResult.new(dependencies: deps)
end

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/bibliothecary/parsers/pypi.rb', line 213

def self.parse_poetry_lock(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)
  deps = []
  manifest["package"].each do |package|
    # next if group == "_meta"

    # Poetry <1.2.0 used singular "category" for kind
    # Poetry >=1.2.0 uses plural "groups" field for kind(s)
    groups = package.values_at("category", "groups").flatten.compact
      .map do |g|
        if g == "dev"
          "develop"
        else
          (g == "main" ? "runtime" : g)
        end
      end

    groups = ["runtime"] if groups.empty?

    groups.each do |group|
      # Poetry lockfiles should already contain normalizated names, but we'll
      # apply it here as well just to be consistent with pyproject.toml parsing.
      normalized_name = normalize_name(package["name"])
      deps << Dependency.new(
        name: normalized_name,
        original_name: normalized_name == package["name"] ? nil : package["name"],
        requirement: map_requirements(package),
        type: group,
        source: options.fetch(:filename, nil),
        platform: platform_name
      )
    end
  end
  ParserResult.new(dependencies: deps)
end

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



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
# File 'lib/bibliothecary/parsers/pypi.rb', line 114

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", options.fetch(:filename, nil))
  # Poetry 1.0.0-1.2.0 way of defining dev deps
  deps += map_dependencies(poetry_manifest["dev-dependencies"], "develop", options.fetch(:filename, nil))
  # Poetry's 1.2.0+ of defining dev deps
  poetry_manifest
    .fetch("group", {})
    .each_pair do |group_name, obj|
      group_name = "develop" if group_name == "dev"
      deps += map_dependencies(obj.fetch("dependencies", {}), group_name, options.fetch(:filename, nil))
    end

  # 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", options.fetch(:filename, nil))

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

  # Poetry normalizes names in lockfiles but doesn't provide the original, so we need to keep
  # track of the original name so the dep is connected between manifest+lockfile.
  dependencies = deps.map do |dep|
    normalized_name = normalize_name(dep.name)
    Dependency.new(
      **dep.to_h,
      name: normalized_name,
      original_name: normalized_name == dep.name ? nil : dep.name
    )
  end
  ParserResult.new(dependencies: dependencies)
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.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/bibliothecary/parsers/pypi.rb', line 296

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, type, options.fetch(:filename, nil))
      rescue URI::Error, NoEggSpecified
        next
      end

      deps << result
    elsif (match = line.delete(" ").match(REQUIREMENTS_REGEXP))
      deps << Dependency.new(
        name: match[1],
        requirement: match[-1],
        type: type,
        source: options.fetch(:filename, nil),
        platform: platform_name
      )
    end
  end

  dependencies = deps.uniq
  ParserResult.new(dependencies: dependencies)
end

.parse_requirements_txt_url(url, type = nil, source = nil) ⇒ Object

Raises:



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/bibliothecary/parsers/pypi.rb', line 331

def self.parse_requirements_txt_url(url, type = nil, source = nil)
  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]

  Dependency.new(
    name: name,
    requirement: requirement,
    type: type,
    source: source,
    platform: platform_name
  )
end

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bibliothecary/parsers/pypi.rb', line 249

def self.parse_setup_py(file_contents, options: {})
  match = file_contents.match(INSTALL_REGEXP)
  return ParserResult.new(dependencies: []) 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 << Dependency.new(
      name: match[1],
      requirement: match[-1],
      type: "runtime",
      source: options.fetch(:filename, nil),
      platform: platform_name
    )
  end
  ParserResult.new(dependencies: deps)
end

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bibliothecary/parsers/pypi.rb', line 91

def self.parser_pylock(file_contents, options: {})
  lockfile = Tomlrb.parse(file_contents)
  dependencies = lockfile["packages"].map do |d|
    is_local = true if d.key?("archive") || d.key?("directory")
    Dependency.new(
      platform: platform_name,
      name: d["name"],
      type: "runtime",
      source: options.fetch(:filename, nil),
      requirement: d["version"] || "*",
      local: is_local
    )
  end
  ParserResult.new(dependencies: dependencies)
end

.pip_compile?(file_contents) ⇒ Boolean



349
350
351
352
353
354
355
356
# File 'lib/bibliothecary/parsers/pypi.rb', line 349

def self.pip_compile?(file_contents)
  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