-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmake_brew_formula.py
223 lines (161 loc) · 6.26 KB
/
make_brew_formula.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
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
153
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
brew.py
~~~~~~~
brew py is brew formula generator for the deadlinks package
"""
import json
from collections import defaultdict
from pathlib import Path
from re import compile as _compile
from textwrap import dedent
from typing import Dict, List, Optional, Tuple # pylint: disable-msg=W0611
import requests
from jinja2 import Template
try:
from packaging.version import parse
except ImportError:
from pip._vendor.packaging.version import parse
# -- Code ----------------------------------------------------------------------
DUNDER_REGEXP = _compile(r'(__(.*?)__ = "(.*?)")\n')
def read_data() -> Dict[str, str]:
""" Read data from __versions__ py """
init = Path(".").parent / "deadlinks" / "__version__.py"
if not Path(init).is_file():
raise RuntimeError("Can not find source for deadlinks/__version__.py")
values = dict() # type: Dict[str, str]
with open(str(init)) as fh:
content = "".join(fh.readlines())
for match in DUNDER_REGEXP.findall(content):
values[match[1]] = match[2]
return values
def require(section: str = "install") -> List[str]:
""" Requirements txt parser. """
require_txt = Path(".").parent / "requirements.txt"
if not Path(require_txt).is_file():
return []
requires = defaultdict(list) # type: Dict[str, List[str]]
with open(str(require_txt), "rb") as fh:
key = "" # type: str
for line in fh.read().decode("utf-8").split("\n"):
if not line.strip():
" empty line "
continue
if line[0] == "#":
" section key "
key = line[2:]
continue
# actual package
requires[key].append(line.strip())
return requires[section]
template = Template(
dedent(
"""\
class {{class}} < Formula
include Language::Python::Virtualenv
desc "{{description}}"
homepage "{{homepage}}"
url "{{url}}"
sha256 "{{digest}}"
depends_on "python"
{% for package in packages %}
resource "{{ package[0] }}" do
url "{{ package[2] }}"
sha256 "{{ package[1] }}"
end
{% endfor %}
def install
virtualenv_install_with_resources
end
test do
# version assertion
assert_match /#{version}/, shell_output("#{bin}/deadlinks --version")
# deaddomain expected result
(testpath/"localhost.localdomain.log").write <<~EOS
===========================================================================
URL=<http://localhost.localdomain>; External Checks=Off; Threads=1; Retry=0
===========================================================================
Links Total: 1; Found: 0; Not Found: 1; Ignored: 0; Redirects: 0
---------------------------------------------------------------------------\e[?25h
[ failed ] http://localhost.localdomain
EOS
# deaddomain assertion
output = shell_output("deadlinks localhost.localdomain --no-progress --no-colors")
assert_equal (testpath/"localhost.localdomain.log").read, output
end
end"""))
def build_formula(app, requirements, build_dev=False) -> str:
data = {
'class': app['app_package'][0].upper() + app['app_package'][1:],
'description': app['description'][:-1],
'homepage': app['app_website'],
'packages': [],
}
if build_dev:
data['url'], data['digest'] = get_local_pacage()
else:
data['url'], data['digest'] = info(app['app_package'], "", "")
for _package in requirements:
pkg, cmp, version = clean_version(_package)
url, digest = info(pkg, cmp, version)
data['packages'].append((pkg, digest, url))
return template.render(**data)
def get_local_pacage():
import glob
import hashlib
files = glob.glob("dist/deadlinks-*.tar.gz")
with open(files[0], "rb") as f:
data = f.read()
return f"http://localhost:8878/{files[0]}", hashlib.sha256(data).hexdigest()
def clean_version(package) -> Tuple[str, str, str]:
""" Splits the module from requirments to the tuple: (pkg, cmp_op, ver) """
separators = ["==", ">=", "<=", "!="]
for s in separators:
if s in package:
return package.partition(s)
return (package, "", "")
def info(pkg, cmp: Optional[str], version: Optional[str]) -> Tuple[str, str]:
""" Return version of package on pypi.python.org using json. """
package_info = f'https://pypi.python.org/pypi/{pkg}/json'
req = requests.get(package_info)
if req.status_code != requests.codes.ok:
raise RuntimeError("Can't request PiPy")
j = json.loads(req.text)
releases = j.get('releases', {})
versions = releases.keys()
version = release(versions, cmp, version)
def source(x):
return x.get('python_version') == 'source'
version = list(filter(source, releases[str(version)]))[0]
return version['url'], version["digests"]['sha256']
def release(releases, pkg_cmp, pkg_ver):
""" filter available release to pick one included in formula """
def not_prerelease(x):
return not x.is_prerelease
cmps = {
'!=': lambda x: x != parse(pkg_ver),
'==': lambda x: x == parse(pkg_ver),
'>=': lambda x: x >= parse(pkg_ver),
'<=': lambda x: x <= parse(pkg_ver),
}
_versions = list(releases)
_versions = map(parse, releases)
_versions = filter(not_prerelease, _versions)
if pkg_cmp and pkg_ver:
_versions = filter(cmps[pkg_cmp], _versions)
versions = sorted(_versions, reverse=True)
if not versions:
raise RuntimeError("No matching version found")
return versions[0]
if __name__ == "__main__":
import sys
data = read_data()
options = {
'app': data,
'requirements': require("install") + require("brew"),
}
options['requirements'] = list(
filter(lambda x: "python_full_version" not in x, options['requirements']))
if '--dev' in sys.argv[1:]:
options['build_dev'] = True
with open(f"{data['app_package']}.rb", 'w') as f:
print(build_formula(**options), file=f)