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 =
/([a-zA-Z0-9]+[a-zA-Z0-9\-_\.]+)([><=\d\.,]+)?/
REQUIREMENTS_REGEXP =
/^#{REQUIRE_REGEXP}/

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.is_requirements_file(filename) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/bibliothecary/parsers/pypi.rb', line 54

def self.is_requirements_file(filename)
  is_requirements_file = filename.match(/require.*\.(txt|pip)$/)
  if filename.match(/require.*\.(txt|pip)$/) and !filename.match(/^node_modules/)
    return true
  else
    return false
  end
end

.parse(filename, path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bibliothecary/parsers/pypi.rb', line 10

def self.parse(filename, path)
  is_valid_requirements_file = is_requirements_file(filename)
  if is_valid_requirements_file
    file_contents = File.open(path).read
    parse_requirements_txt(file_contents)
  elsif filename.match(/setup\.py$/)
    file_contents = File.open(path).read
    parse_setup_py(file_contents)
  else
    []
  end
end

.parse_requirements_txt(manifest) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bibliothecary/parsers/pypi.rb', line 40

def self.parse_requirements_txt(manifest)
  deps = []
  manifest.split("\n").each do |line|
    match = line.match(REQUIREMENTS_REGEXP)
    next unless match
    deps << {
      name: match[1],
      requirement: match[2] || '*',
      type: 'runtime'
    }
  end
  deps
end

.parse_setup_py(manifest) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bibliothecary/parsers/pypi.rb', line 23

def self.parse_setup_py(manifest)
  match = manifest.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[2] || '*',
      type: 'runtime'
    }
  end
  deps
end