-
Notifications
You must be signed in to change notification settings - Fork 41
/
api.py
529 lines (454 loc) · 15.4 KB
/
api.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
from __future__ import annotations
import logging
import shutil
import re
import os
import subprocess
import traceback
from typing import Dict, List, Union
from typing import Tuple, Optional, Iterable, Iterator
import fileinput
import tempfile
from pathlib import Path
from types import SimpleNamespace
import tarfile
import io
from collections.abc import Container
import requests
from myutils import at_dir
from htmlutils import parse_document_from_requests
from .cmd import run_cmd, git_pull, git_push, git_reset_hard
from . import const
from .const import _G, SPECIAL_FILES
from .typing import PkgRel
from .pypi2pkgbuild import gen_pkgbuild
from .pkgbuild import format_package_version as _format_package_version
git_push
logger = logging.getLogger(__name__)
_g = SimpleNamespace()
UserAgent = 'lilac/0.2b (package auto-build bot, by lilydjwg)'
s = requests.Session()
s.headers['User-Agent'] = UserAgent
VCS_SUFFIXES = ('-git', '-hg', '-svn', '-bzr')
AUR_URL = 'https://aur.archlinux.org/rpc/'
def _unquote_item(s: str) -> Optional[str]:
m = re.search(r'''[ \t'"]*([^ '"]+)[ \t'"]*''', s)
if m is not None:
return m.group(1)
else:
return None
def _add_into_array(line: str, values: Iterable[str]) -> str:
l = line.find('(')
r = line.rfind(')')
if r != -1:
line_l, line_m, line_r = line[:l+1], line[l+1:r], line[r:]
else:
line_l, line_m, line_r = line[:l+1], line[l+1:], ''
arr = {_unquote_item(x) for x in line_m.split(' ')}.union(values)
arr_nonone = [i for i in arr if i is not None]
arr_nonone.sort()
arr_elems_str = '"{}"'.format('" "'.join(arr_nonone))
line = line_l + arr_elems_str + line_r
return line
def add_into_array(which: str, extra_deps: Iterable[str]) -> None:
'''
Add more values into the shell array
'''
field_appeared = False
pattern = re.compile(r'\s*' + re.escape(which) + r'=')
for line in edit_file('PKGBUILD'):
if pattern.match(line):
line = _add_into_array(line, extra_deps)
field_appeared = True
print(line)
if not field_appeared:
with open('PKGBUILD', 'a') as f:
line = f'{which}=()'
line = _add_into_array(line, extra_deps)
f.write(line + '\n')
def add_arch(extra_arches: Iterable[str]) -> None:
add_into_array('arch', extra_arches)
def add_depends(extra_deps: Iterable[str]) -> None:
add_into_array('depends', extra_deps)
def add_makedepends(extra_deps: Iterable[str]) -> None:
add_into_array('makedepends', extra_deps)
def add_checkdepends(extra_deps: Iterable[str]) -> None:
add_into_array('checkdepends', extra_deps)
def add_conflicts(extra_conflicts: Iterable[str]) -> None:
add_into_array('conflicts', extra_conflicts)
def add_replaces(extra_replaces: Iterable[str]) -> None:
add_into_array('replaces', extra_replaces)
def add_provides(extra_provides: Iterable[str]) -> None:
add_into_array('provides', extra_provides)
def add_groups(extra_groups: Iterable[str]) -> None:
add_into_array('groups', extra_groups)
def edit_file(filename: str) -> Iterator[str]:
with fileinput.input(files=(filename,), inplace=True) as f:
for line in f:
yield line.rstrip('\n')
def obtain_array(name: str) -> Optional[List[str]]:
'''
Obtain an array variable from PKGBUILD.
Works by calling bash to source PKGBUILD, writing the array to a temporary file, and reading from the file.
'''
with tempfile.NamedTemporaryFile() as output_file:
command_write_array_out = """printf "%s\\0" "${{{}[@]}}" > {}""" \
.format(name, output_file.name)
command_export_array = ['bash', '-c', "source PKGBUILD && {}".format(
command_write_array_out)]
subprocess.run(command_export_array, stderr=subprocess.PIPE,
check=True)
res = output_file.read().decode()
if res == '\0':
return None
variable = res.split('\0')[:-1]
return variable
def obtain_depends() -> Optional[List[str]]:
return obtain_array('depends')
def obtain_makedepends() -> Optional[List[str]]:
return obtain_array('makedepends')
def obtain_optdepends(
parse_dict: bool=True
) -> Optional[Union[Dict[str, str], List[str]]]:
obtained_array = obtain_array('optdepends')
if not obtained_array:
return obtained_array
if parse_dict:
return {pkg.strip(): desc.strip() for (pkg, desc) in
(item.split(':', 1) for item in obtained_array)}
else:
return obtained_array
def vcs_update() -> None:
# clean up the old source tree
shutil.rmtree('src', ignore_errors=True)
run_cmd(['makepkg', '-od', '--noprepare', '-A'], use_pty=True)
def get_pkgver_and_pkgrel(
) -> Tuple[Optional[str], Optional[PkgRel]]:
pkgrel = None
pkgver = None
try:
with open('PKGBUILD') as f:
for l in f:
if l.startswith('pkgrel='):
pkgrel = l.rstrip().split(
'=', 1)[-1].strip('\'"')
try:
pkgrel = int(pkgrel) # type: ignore
except (ValueError, TypeError):
pass
elif l.startswith('pkgver='):
pkgver = l.rstrip().split('=', 1)[-1].strip('\'"')
except FileNotFoundError:
pass
return pkgver, pkgrel
def _next_pkgrel(rel: PkgRel) -> int:
if isinstance(rel, int):
return rel + 1
first_segment = rel.split('.')[0]
return int(first_segment) + 1
def update_pkgver_and_pkgrel(
newver: str, *, updpkgsums: bool = True) -> None:
pkgver, pkgrel = get_pkgver_and_pkgrel()
assert pkgver is not None and pkgrel is not None
for line in edit_file('PKGBUILD'):
if line.startswith('pkgver=') and pkgver != newver:
line = f'pkgver={newver}'
elif line.startswith('pkgrel='):
if pkgver != newver:
line = 'pkgrel=1'
else:
line = f'pkgrel={_next_pkgrel(pkgrel)}'
print(line)
if updpkgsums:
run_cmd(["updpkgsums"])
def update_pkgrel(
rel: Optional[PkgRel] = None,
) -> None:
with open('PKGBUILD') as f:
pkgbuild = f.read()
def replacer(m):
nonlocal rel
if rel is None:
rel = _next_pkgrel(m.group(1))
return str(rel)
pkgbuild = re.sub(r'''(?<=^pkgrel=)['"]?([\d.]+)['"]?''', replacer, pkgbuild, count=1, flags=re.MULTILINE)
with open('PKGBUILD', 'w') as f:
f.write(pkgbuild)
logger.info('pkgrel updated to %s', rel)
def pypi_pre_build(
depends: Optional[List[str]] = None,
python2: bool = False,
pypi_name: Optional[str] = None,
arch: Optional[Iterable[str]] = None,
makedepends: Optional[List[str]] = None,
depends_setuptools: bool = True,
provides: Optional[Iterable[str]] = None,
conflicts: Optional[Iterable[str]] = None,
prepare: Optional[str] = None,
check: Optional[str] = None,
optdepends: Optional[List[str]] = None,
license: Optional[str] = None,
license_file: Optional[str] = None,
) -> None:
if python2:
raise ValueError('pypi_pre_build no longer supports python2')
pkgname = os.path.basename(os.getcwd())
if pypi_name is None:
pypi_name = pkgname.split('-', 1)[-1]
_new_pkgver, pkgbuild = gen_pkgbuild(
pypi_name,
pkgname = pkgname,
depends = depends,
arch = arch,
makedepends = makedepends,
depends_setuptools = depends_setuptools,
provides = provides,
conflicts = conflicts,
prepare = prepare,
check = check,
optdepends = optdepends,
license = license,
license_file = license_file,
)
with open('PKGBUILD', 'w') as f:
f.write(pkgbuild)
def pypi_post_build() -> None:
git_add_files('PKGBUILD')
git_commit()
def git_add_files(
files: Union[str, List[str]], *, force: bool = False,
) -> None:
if isinstance(files, str):
files = [files]
try:
if force:
run_cmd(['git', 'add', '-f', '--'] + files)
else:
run_cmd(['git', 'add', '--'] + files)
except subprocess.CalledProcessError:
# on error, there may be a partial add, e.g. some files are ignored
run_cmd(['git', 'reset', '--'] + files)
raise
def git_commit(*, check_status: bool = True) -> None:
if check_status:
ret = [x for x in
run_cmd(["git", "status", "-s", "."]).splitlines()
if x.split(None, 1)[0] != '??']
if not ret:
return
pkgbase = os.path.split(os.getcwd())[1]
built_version = _format_package_version(
_G.epoch, _G.pkgver, _G.pkgrel)
msg = f'{pkgbase}: auto updated to {built_version}'
run_cmd(['git', 'commit', '-m', msg])
class AurDownloadError(Exception):
def __init__(self, pkgname: str) -> None:
self.pkgname = pkgname
def _allow_update_aur_repo(pkgname: str, diff: str) -> bool:
is_vcs = pkgname.endswith(VCS_SUFFIXES)
for line in diff.splitlines():
if not line.startswith(('+', '-')) or line.startswith(('+++', '---')):
# Not a changed line
continue
line = line[1:] # remove the +/- marker
if is_vcs and not line.startswith(('pkgver=', 'pkgrel=')):
return True
if not is_vcs and not line.startswith('pkgrel='):
return True
return False
def _update_aur_repo_real(pkgname: str) -> None:
aurpath = const.AUR_REPO_DIR / pkgname
if not aurpath.is_dir():
logger.info('cloning AUR repo: %s', aurpath)
with at_dir(const.AUR_REPO_DIR):
run_cmd(['git', 'clone', f'aur@aur.archlinux.org:{pkgname}.git'])
else:
with at_dir(aurpath):
git_reset_hard()
git_pull()
with at_dir(aurpath):
oldfiles = set(run_cmd(['git', 'ls-files']).splitlines())
newfiles = set()
logger.info('copying files to AUR repo: %s', aurpath)
files = run_cmd(['git', 'ls-files']).splitlines()
for f in files:
if f in const.SPECIAL_FILES:
continue
logger.debug('copying file %s', f)
shutil.copy(f, aurpath)
newfiles.add(f)
with at_dir(aurpath):
for f in oldfiles - newfiles:
if f in ['.SRCINFO', '.gitignore']:
continue
logger.debug('removing file %s', f)
try:
os.unlink(f)
except OSError as e:
logger.warning('failed to remove file %s: %s', f, e)
if not _allow_update_aur_repo(pkgname, run_cmd(['git', 'diff'])):
return
with open('.SRCINFO', 'wb') as srcinfo:
subprocess.run(
['makepkg', '--printsrcinfo'],
stdout = srcinfo,
check = True,
)
run_cmd(['git', 'add', '.'])
p = subprocess.run(['git', 'diff-index', '--quiet', 'HEAD'])
if p.returncode != 0:
built_version = _format_package_version(
_G.epoch, _G.pkgver, _G.pkgrel)
msg = f'[lilac] updated to {built_version}'
run_cmd(['git', 'commit', '-m', msg])
run_cmd(['git', 'push'])
def update_aur_repo() -> None:
pkgbase = _G.mod.pkgbase
try:
_update_aur_repo_real(pkgbase)
except subprocess.CalledProcessError as e:
tb = traceback.format_exc()
_G.repo.send_error_report(
_G.mod,
exc = (e, tb),
subject = '提交软件包 %s 到 AUR 时出错',
)
def git_pkgbuild_commit() -> None:
git_add_files('PKGBUILD')
git_commit()
def _prepend_self_path() -> None:
mydir = Path(__file__).resolve().parent.parent
path = os.environ['PATH']
os.environ['PATH'] = str(mydir / path)
def single_main(build_prefix: str = 'makepkg') -> None:
from nicelogger import enable_pretty_logging
from . import lilacpy
from .building import lilac_build
_prepend_self_path()
enable_pretty_logging('DEBUG')
with lilacpy.load_lilac(Path('.')) as mod:
lilac_build(
mod, None,
build_prefix = build_prefix,
accept_noupdate = True,
)
def clean_directory() -> List[str]:
'''clean all PKGBUILD and related files'''
files = run_cmd(['git', 'ls-files']).splitlines()
logger.info('clean directory')
ret = []
for f in files:
if f in SPECIAL_FILES:
continue
logger.debug('unlink file %s', f)
ret.append(f)
try:
os.unlink(f)
except FileNotFoundError:
pass
return ret
def _try_aur_url(name: str) -> bytes:
aur4url = 'https://aur.archlinux.org/cgit/aur.git/snapshot/{name}.tar.gz'
templates = [aur4url]
urls = [url.format(first_two=name[:2], name=name) for url in templates]
for url in urls:
response = s.get(url)
if response.status_code == 200:
logger.debug("downloaded aur tarball '%s' from url '%s'", name, url)
return response.content
logger.error("failed to find aur url for '%s'", name)
raise AurDownloadError(name)
def _download_aur_pkgbuild(name: str) -> List[str]:
content = io.BytesIO(_try_aur_url(name))
files = []
with tarfile.open(
name=name+".tar.gz", mode="r:gz", fileobj=content
) as tarf:
for tarinfo in tarf:
basename, remain = os.path.split(tarinfo.name)
if basename == '':
continue
if remain in ('.AURINFO', '.SRCINFO', '.gitignore'):
continue
tarinfo.name = remain
tarf.extract(tarinfo)
files.append(remain)
return files
def git_rm_files(files: List[str]) -> None:
if files:
run_cmd(['git', 'rm', '--cached', '--'] + files)
def aur_pre_build(
name: Optional[str] = None, *, do_vcs_update: Optional[bool] = None,
maintainers: Union[str, Container[str]] = (),
) -> None:
# import pyalpm here so that lilac can be easily used on non-Arch
# systems (e.g. Travis CI)
import pyalpm
if name is None:
name = os.path.basename(os.getcwd())
if maintainers:
info = s.get(AUR_URL, params={"v": 5, "type": "info", "arg[]": name}).json()
maintainer = info['results'][0]['Maintainer']
error = False
if isinstance(maintainers, str):
error = maintainer != maintainers
else:
error = maintainer not in maintainers
if error:
raise Exception('unexpected AUR package maintainer', maintainer)
pkgver, pkgrel = get_pkgver_and_pkgrel()
_g.aur_pre_files = clean_directory()
_g.aur_building_files = _download_aur_pkgbuild(name)
aur_pkgver, aur_pkgrel = get_pkgver_and_pkgrel()
if pkgver and pkgver == aur_pkgver:
if pyalpm.vercmp(f'1-{pkgrel}', f'1-{aur_pkgrel}') < 0:
# use aur pkgrel
pass
else:
# bump
update_pkgrel()
if do_vcs_update is None:
do_vcs_update = name.endswith(VCS_SUFFIXES)
if do_vcs_update:
vcs_update()
# recheck after sync, because AUR pkgver may lag behind
new_pkgver, new_pkgrel = get_pkgver_and_pkgrel()
if pkgver and pkgver == new_pkgver:
if pkgrel is None:
next_pkgrel = 1
else:
next_pkgrel = _next_pkgrel(pkgrel)
if pyalpm.vercmp(f'1-{next_pkgrel}', f'1-{new_pkgrel}') > 0:
update_pkgrel(next_pkgrel)
def aur_post_build() -> None:
git_rm_files(_g.aur_pre_files)
git_add_files(_g.aur_building_files, force=True)
output = run_cmd(["git", "status", "-s", "."]).strip()
if output:
git_commit()
del _g.aur_pre_files, _g.aur_building_files
def download_official_pkgbuild(name: str) -> List[str]:
url = 'https://www.archlinux.org/packages/search/json/?name=' + name
logger.info('download PKGBUILD for %s.', name)
info = s.get(url).json()
r = [r for r in info['results'] if r['repo'] != 'testing'][0]
repo = r['repo']
arch = r['arch']
if repo in ('core', 'extra'):
gitrepo = 'packages'
else:
gitrepo = 'community'
pkgbase = [r['pkgbase'] for r in info['results'] if r['repo'] != 'testing'][0]
tree_url = 'https://projects.archlinux.org/svntogit/%s.git/tree/repos/%s-%s?h=packages/%s' % (
gitrepo, repo, arch, pkgbase)
doc = parse_document_from_requests(tree_url, s)
blobs = doc.xpath('//div[@class="content"]//td/a[contains(concat(" ", normalize-space(@class), " "), " ls-blob ")]')
files = [x.text for x in blobs]
for filename in files:
blob_url = 'https://projects.archlinux.org/svntogit/%s.git/plain/repos/%s-%s/%s?h=packages/%s' % (
gitrepo, repo, arch, filename, pkgbase)
with open(filename, 'wb') as f:
logger.debug('download file %s.', filename)
data = s.get(blob_url).content
f.write(data)
return files