forked from python-poetry/poetry-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanza
executable file
·164 lines (125 loc) · 4.46 KB
/
stanza
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
#!/usr/bin/env python
import os
import subprocess
import tarfile
import zipfile
from pathlib import Path
from typing import Dict
from cleo import Application
from cleo import Command
from cleo import argument
from vendoring.configuration import Configuration
from vendoring.configuration import load_configuration
from vendoring.tasks.cleanup import cleanup_existing_vendored
from vendoring.tasks.license import find_and_extract_license
from vendoring.tasks.license import license_fallback
from vendoring.tasks.vendor import vendor_libraries
from vendoring.utils import remove_all
from vendoring.utils import run
def extract_license(
destination: Path,
sdist: Path,
license_directories: Dict[str, str],
license_fallback_urls: Dict[str, str],
) -> None:
def extract_from_source_tarfile(sdist: Path) -> bool:
ext = sdist.suffixes[-1][1:]
with tarfile.open(sdist, mode="r:{}".format(ext)) as tar:
return find_and_extract_license(
destination, tar, tar.getmembers(), license_directories,
)
def extract_from_source_zipfile(sdist: Path) -> bool:
with zipfile.ZipFile(sdist) as zip:
return find_and_extract_license(
destination, zip, zip.infolist(), license_directories,
)
if sdist.suffixes[-2] == ".tar":
found = extract_from_source_tarfile(sdist)
elif sdist.suffixes[-1] == ".zip":
found = extract_from_source_zipfile(sdist)
elif sdist.suffixes[-1] == ".whl":
found = extract_from_source_zipfile(sdist)
else:
raise NotImplementedError("new sdist type!")
if found:
return
license_fallback(
destination, sdist.name, license_directories, license_fallback_urls
)
def fetch_licenses(config: Configuration) -> None:
destination = config.destination
license_directories = config.license_directories
license_fallback_urls = config.license_fallback_urls
requirements = config.requirements
tmp_dir = destination / "__tmp__"
download_sources(tmp_dir, requirements)
for sdist in tmp_dir.iterdir():
extract_license(destination, sdist, license_directories, license_fallback_urls)
remove_all([tmp_dir])
def download_sources(location: Path, requirements: Path) -> None:
cmd = [
"pip",
"download",
"-r",
str(requirements),
"--no-deps",
"--dest",
str(location),
]
run(cmd, working_directory=None)
class VendorUpdateCommand(Command):
name = "update"
description = "Update one or more vendor packages"
arguments = [
argument("packages", "The packages to vendor.", optional=True, multiple=True)
]
def handle(self):
packages = self.argument("packages")
current_dir = os.getcwd()
base = os.path.dirname(__file__)
try:
os.chdir(base.join(["vendors"]))
if not packages:
subprocess.run(["poetry", "lock"])
else:
subprocess.run(["poetry", "update", "--lock"])
subprocess.run(["poetry", "show", "--all", "--tree"])
subprocess.run(
[
"poetry",
"export",
"-f",
"requirements.txt",
"-o",
"../poetry/core/_vendor/vendor.txt",
"--without-hashes",
]
)
finally:
os.chdir(current_dir)
lines = []
with open("poetry/core/_vendor/vendor.txt") as f:
for line in f.readlines():
if ";" in line:
line, _ = line.split(";", maxsplit=1)
if line.startswith("wheels/"):
line = "vendors/" + line
if line.startswith("enum34"):
line = "vendors/wheels/enum34-1.1.10-py2.py3-none-any.whl"
lines.append(line.strip())
with open("poetry/core/_vendor/vendor.txt", "w") as f:
f.write("\n".join(lines))
config = load_configuration(Path(base))
cleanup_existing_vendored(config)
vendor_libraries(config)
fetch_licenses(config)
class VendorCommand(Command):
name = "vendor"
description = "Vendor related commands."
commands = [VendorUpdateCommand()]
def handle(self):
return self.call("help", self.name)
app = Application("stanza")
app.add(VendorCommand())
if __name__ == "__main__":
app.run()