Class: Bibliothecary::Parsers::Pypi

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

Constant Summary collapse

PLATFORM_NAME =
'pypi'
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

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



19
20
21
22
# File 'lib/bibliothecary/parsers/pypi.rb', line 19

def self.analyse(folder_path, file_list)
  [analyse_requirements_txt(folder_path, file_list),
  analyse_setup_py(folder_path, file_list)]
end

.analyse_requirements_txt(folder_path, file_list) ⇒ Object



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

def self.analyse_requirements_txt(folder_path, file_list)
  path = file_list.find do |path|
    p = path.gsub(folder_path, '').gsub(/^\//, '')
    p.match(/require.*\.(txt|pip)$/) && !path.match(/^node_modules/)
  end
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_requirements_txt(manifest)
  }
end

.analyse_setup_py(folder_path, file_list) ⇒ Object



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

def self.analyse_setup_py(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^setup\.py$/) }
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_setup_py(manifest)
  }
end

.parse(filename, file_contents) ⇒ Object



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

def self.parse(filename, file_contents)
  if filename.match(/require.*\.(txt|pip)$/) && !filename.match(/^node_modules/)
    parse_requirements_txt(file_contents)
  elsif filename.match(/^setup\.py$/)
    parse_setup_py(file_contents)
  else
    []
  end
end

.parse_requirements_txt(manifest) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bibliothecary/parsers/pypi.rb', line 70

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bibliothecary/parsers/pypi.rb', line 53

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