This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pydeps.rb
76 lines (66 loc) · 1.85 KB
/
pydeps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
require "open3"
require "fileutils"
require "dalli"
require "tmpdir"
module Pydeps
class Resolver
attr_accessor :name
attr_accessor :version
def initialize(name, version)
@name = name
@version = version
end
def find_dependencies
memcached_client.fetch(cache_key) do
output = run_fetch
output ? parse(output) : "err"
end
end
def to_json
find_dependencies.map do |dep|
match = dep.gsub(' ', '').match(/^([a-z0-9]+[a-z0-9\-_\.]+)([><=\d\.,]+)?/i)
name = match[1]
requirements = match[2]
{
name: name,
requirements: requirements
}
end.to_json
end
private
def memcached_client
Dalli::Client.new((ENV["MEMCACHIER_SERVERS"] || "localhost:11211").split(","),
{
username: ENV["MEMCACHIER_USERNAME"],
password: ENV["MEMCACHIER_PASSWORD"],
failover: true,
socket_timeout: 1.5,
socket_failure_delay: 0.2
})
end
def cache_key
[name, version].join('-')
end
def parse(output)
output.split("\n")
end
def command(tmpdir, pip_tool)
"#{pip_tool} download #{name}==#{version} -d #{tmpdir} --no-cache-dir --no-binary all --disable-pip-version-check | grep 'from #{name}' | cut -d' ' -f2"
end
def run_fetch
begin
tmpdir = Dir.mktmpdir
_stdin, stdout, stderr = Open3.popen3(command(tmpdir, 'pip3'))
result = { err: stderr.read, res: stdout.read }
unless result[:err].empty?
# fall back to python2 pip if errors exist
_stdin, stdout, stderr = Open3.popen3(command(tmpdir, 'pip2'))
result = { err: stderr.read, res: stdout.read }
end
return result[:res] if result[:err].empty?
ensure
FileUtils.rm_rf(tmpdir)
end
end
end
end