forked from zauberzeug/nicegui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm.py
executable file
·102 lines (79 loc) · 3.98 KB
/
npm.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
#!/usr/bin/env python3
"""Update dependencies according to npm.json configurations using the NPM packagist.
npm.json file is a JSON object key => dependency.
- key: is the key name of the dependency. It will be the folder name where the dependency will be stored.
- dependency: a JSON object key-pair value with the following meaning full keys:
- package (optional): if provided, this is the NPM package name. Otherwise, key is used as an NPM package name.
- version (optional): if provided, this will fix the version to use. Otherwise, the latest available NPM package version will be used.
- destination: the destination folder where the dependency should end up.
- keep: an array of regexp of files to keep within the downloaded NPM package.
- rename: an array of rename rules (string replace). Used to change the package structure after download to match NiceGUI expectations.
"""
import json
import re
import shutil
import tarfile
from pathlib import Path
import requests
def prepare(path: Path) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
return path
def cleanup(path: Path) -> Path:
shutil.rmtree(path, ignore_errors=True)
return path
def url_to_filename(url: str) -> str:
return re.sub(r'[^a-zA-Z0-9]', '_', url)
def download_buffered(url: str) -> Path:
path = Path('/tmp/nicegui_dependencies')
path.mkdir(exist_ok=True)
filepath = path / url_to_filename(url)
if not filepath.exists():
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=3)
filepath.write_bytes(response.content)
return filepath
DEPENDENCIES = (Path(__file__).parent / 'DEPENDENCIES.md').open('w')
DEPENDENCIES.write('# Included Web Dependencies\n\n')
KNOWN_LICENSES = {
'MIT': 'https://opensource.org/licenses/MIT',
'ISC': 'https://opensource.org/licenses/ISC',
'Apache-2.0': 'https://opensource.org/licenses/Apache-2.0',
}
# Create a hidden folder to work in.
tmp = cleanup(Path('.npm'))
dependencies: dict[str, dict] = json.loads(Path('npm.json').read_text())
for key, dependency in dependencies.items():
# Reset destination folder.
destination = prepare(Path('nicegui', dependency['destination'], key))
# Get package info from NPM.
package_name = dependency.get('package', key)
npm_data = json.loads(download_buffered(f'https://registry.npmjs.org/{package_name}').read_text())
npm_version = dependency.get('version', npm_data['dist-tags']['latest'])
npm_tarball = npm_data['versions'][npm_version]['dist']['tarball']
license_ = npm_data['versions'][npm_version]['license']
print(f'{key}: {npm_version} - {npm_tarball} ({license_})')
DEPENDENCIES.write(f'- {key}: {npm_version} ([{license_}]({KNOWN_LICENSES.get(license_, license_)}))\n')
# Handle the special case of tailwind. Hopefully remove this soon.
if 'download' in dependency:
download_path = download_buffered(dependency['download'])
shutil.copyfile(download_path, prepare(Path(destination, dependency['rename'])))
# Download and extract.
tgz_file = prepare(Path(tmp, key, f'{key}.tgz'))
tgz_download = download_buffered(npm_tarball)
shutil.copyfile(tgz_download, tgz_file)
with tarfile.open(tgz_file) as archive:
to_be_extracted: list[tarfile.TarInfo] = []
for tarinfo in archive.getmembers():
for keep in dependency['keep']:
if re.match(f'^{keep}$', tarinfo.name):
to_be_extracted.append(tarinfo) # TODO: simpler?
archive.extractall(members=to_be_extracted, path=Path(tmp, key))
for extracted in to_be_extracted:
filename: str = extracted.name
for rename in dependency['rename']:
filename = filename.replace(rename, dependency['rename'][rename])
newfile = prepare(Path(destination, filename))
Path(tmp, key, extracted.name).rename(newfile)
# Delete destination folder if empty.
if not any(destination.iterdir()):
destination.rmdir()
cleanup(tmp)