-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetadata_tilde.py
67 lines (53 loc) · 1.93 KB
/
metadata_tilde.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
# what a beautiful hack!
import sys
from glob import glob
from pathlib import Path
import tempfile
import tarfile
import zipfile
def replace_tilde_in_targz(tar_gz):
with tempfile.TemporaryDirectory() as td:
tdp = Path(td)
# extract archive to temporry directory
with tarfile.open(tar_gz) as r:
r.extractall(td)
pkg_info = next(tdp.glob('*/PKG-INFO'))
# modify PKG-INFO
with pkg_info.open() as f:
text = f.read()
text = text.replace(
'Author-Email: =?utf-8?q?Jo=C3=A3o_Faria?= <[email protected]>',
'Author-Email: "João Faria" <[email protected]>'
)
with pkg_info.open('wb') as f:
f.write(text.encode())
# replace archive, from all files in tempdir
with tarfile.open(tar_gz, "w:gz") as w:
for f in tdp.iterdir():
w.add(f, arcname=f.name)
def replace_tilde_in_whl(whl):
with tempfile.TemporaryDirectory() as td:
tdp = Path(td)
# extract archive to temporry directory
with zipfile.ZipFile(whl) as r:
r.extractall(td)
metadata = next(tdp.glob('*.dist-info/METADATA'))
# modify METADATA
with metadata.open() as f:
text = f.read()
text = text.replace(
'Author-Email: =?utf-8?q?Jo=C3=A3o_Faria?= <[email protected]>',
'Author-Email: "João Faria" <[email protected]>'
)
with metadata.open('wb') as f:
f.write(text.encode())
# replace archive, from all files in tempdir
with zipfile.ZipFile(whl, "w") as w:
for f in tdp.rglob('*'):
w.write(f, arcname=f.relative_to(tdp))
if 'dist' in sys.argv:
tar_gz = glob('dist/*.tar.gz')[0]
replace_tilde_in_targz(tar_gz)
if 'wheel' in sys.argv:
for whl in glob('wheelhouse/*.whl'):
replace_tilde_in_whl(whl)