This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.py
121 lines (93 loc) · 3.1 KB
/
utils.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import os
import shlex
import subprocess
import click
from nix_prefetch_github import nix_prefetch_github
HERE = os.path.dirname(__file__)
TO_IGNORE = ["pip", "setuptools", "wheel", "zc.buildout", "zc.recipe.egg"]
PYTHON_VERSIONS = {
"2.6": "python26Full",
"2.7": "python27Full",
"3.2": "python32",
"3.3": "python33",
"3.4": "python34",
"3.5": "python35",
"3.6": "python36",
"3.7": "python37",
"3": "python3",
"pypy": "pypy",
}
def pretty_option(option):
if option is None:
return ""
else:
return " [value: {}]".format(
type(option) in [list, tuple] and " ".join(option) or option
)
def safe(string):
return string.replace('"', '\\"')
def cmd(command, verbose=False, stderr=subprocess.STDOUT):
if isinstance(command, str):
command = shlex.split(command)
if verbose:
click.echo("|-> " + " ".join(command))
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=stderr)
out = []
while True:
line = p.stdout.readline().decode()
if line == "" and p.poll() is not None:
break
if line != "":
if verbose:
click.echo(" " + line.rstrip("\n"))
out.append(line)
return p.returncode, "\n".join(out)
def create_command_options(options):
command_options = []
for name, value in options.items():
if isinstance(value, str):
command_options.append('--argstr {} "{}"'.format(name, value))
elif isinstance(value, list) or isinstance(value, tuple):
value = "[ %s ]" % (" ".join(['"%s"' % x for x in value]))
command_options.append("--arg {} '{}'".format(name, value))
return " ".join(command_options)
def args_as_list(inputs):
return list(filter(lambda x: x != "", (" ".join(inputs)).split(" ")))
def prefetch_url(url):
output = cmd(["nix-prefetch-url", url], stderr=None)
return output[-1][:-1]
def prefetch_git(url, rev=None):
command = ["nix-prefetch-git", url]
if rev is not None:
command += ["--rev", rev]
try:
completed_proc = subprocess.run(
command,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except FileNotFoundError:
raise click.ClickException(
"Could not find executable `nix-prefetch-git`. "
"Make sure it is installed correctly and available in "
"$PATH."
)
returncode = completed_proc.returncode
if returncode != 0:
raise click.ClickException(
(
"Could not fetch git repository at {url}, git returncode was "
"{code}. stdout:\n{stdout}\nstderr:\n{stderr}"
).format(
url=url,
code=returncode,
stdout=completed_proc.stdout,
stderr=completed_proc.stderr,
)
)
repo_data = json.loads(completed_proc.stdout)
return repo_data
def prefetch_github(owner, repo, rev=None):
return nix_prefetch_github(owner, repo, rev=rev)