forked from flatpak/flatpak-builder-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
321 lines (293 loc) · 10.7 KB
/
main.py
File metadata and controls
321 lines (293 loc) · 10.7 KB
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
import argparse
import asyncio
import json
import os
import sys
from collections.abc import Iterator
from pathlib import Path
from .cache import Cache, FilesystemBasedCache
from .manifest import DEFAULT_SPLIT_SIZE_KB, ManifestGenerator
from .node_headers import NodeHeaders
from .package import Package
from .progress import GeneratorProgress
from .providers import ProviderFactory
from .providers.npm import NpmLockfileProvider, NpmModuleProvider, NpmProviderFactory
from .providers.pnpm import (
PnpmLockfileProvider,
PnpmProviderFactory,
)
from .providers.special import SpecialSourceProvider
from .providers.yarn import YarnProviderFactory
from .requests import Requests, StubRequests
def _scan_for_lockfiles(base: Path, patterns: list[str]) -> Iterator[Path]:
for root, _, files in os.walk(base.parent):
if base.name in files:
lockfile = Path(root) / base.name
if not patterns or any(map(lockfile.match, patterns)):
yield lockfile
async def _async_main() -> None:
parser = argparse.ArgumentParser(description='Flatpak Node generator')
parser.add_argument('type', choices=['npm', 'yarn', 'pnpm'])
parser.add_argument(
'lockfile',
help='The lockfile path (package-lock.json, yarn.lock, or pnpm-lock.yaml)',
)
parser.add_argument(
'-o',
'--output',
help='The output sources file',
default='generated-sources.json',
)
parser.add_argument(
'-r',
'--recursive',
action='store_true',
help='Recursively process all files under the lockfile directory with '
'the lockfile basename',
)
parser.add_argument(
'-R',
'--recursive-pattern',
action='append',
help='Given -r, restrict files to those matching the given pattern.',
)
parser.add_argument(
'--registry',
help='The registry to use (npm/pnpm)',
default='https://registry.npmjs.org',
)
parser.add_argument(
'--no-trim-index',
action='store_true',
help="Don't trim npm package metadata (npm only)",
)
parser.add_argument(
'--no-devel',
action='store_true',
help="Don't include devel dependencies (npm/pnpm)",
)
parser.add_argument(
'--no-requests-cache',
action='store_true',
help='Disable the requests cache',
)
parser.add_argument(
'--max-parallel',
help='Maximium number of packages to process in parallel',
type=int,
default=64,
)
parser.add_argument(
'--retries',
type=int,
help='Number of retries of failed requests',
default=Requests.DEFAULT_RETRIES,
)
parser.add_argument(
'-P',
'--no-autopatch',
action='store_true',
help="Don't automatically patch Git sources from package*.json",
)
parser.add_argument(
'-s',
'--split',
action='store_true',
help='Split the sources file to fit onto GitHub.',
)
parser.add_argument(
'-S',
'--split-size',
type=int,
default=DEFAULT_SPLIT_SIZE_KB,
dest='split_size',
help=f'If splitting the sources file, split at this size in KB. Default is {DEFAULT_SPLIT_SIZE_KB} KB.',
)
parser.add_argument(
'--node-chromedriver-from-electron',
help='Use the ChromeDriver version associated with the given '
'Electron version for node-chromedriver',
)
# Deprecated alternative to --node-chromedriver-from-electron
parser.add_argument('--electron-chromedriver', help=argparse.SUPPRESS)
parser.add_argument(
'--electron-ffmpeg',
choices=['archive', 'lib'],
help='Download prebuilt ffmpeg for matching electron version',
)
parser.add_argument(
'--electron-node-headers',
action='store_true',
help='Download the electron node headers',
)
parser.add_argument(
'--nwjs-version',
help='Specify NW.js version (will use latest otherwise)',
)
parser.add_argument(
'--nwjs-node-headers',
action='store_true',
help='Download the NW.js node headers',
)
parser.add_argument(
'--nwjs-ffmpeg',
action='store_true',
help='Download prebuilt ffmpeg for current NW.js version',
)
# Deprecated, because this is now enabled by default.
parser.add_argument('--xdg-layout', default=True, help=argparse.SUPPRESS)
parser.add_argument(
'--no-xdg-layout',
action='store_false',
dest='xdg_layout',
help="Don't use the XDG layout for caches",
)
# Internal option, useful for testing.
parser.add_argument('--stub-requests', action='store_true', help=argparse.SUPPRESS)
parser.add_argument(
'--node-sdk-extension',
help='Flatpak node SDK extension (e.g. org.freedesktop.Sdk.Extension.node24//25.08)',
default=None,
)
args = parser.parse_args()
Requests.retries = args.retries
if args.type == 'yarn' and (args.no_devel or args.no_autopatch):
sys.exit('--no-devel and --no-autopatch do not apply to Yarn.')
if args.type == 'pnpm' and args.no_autopatch:
sys.exit('--no-autopatch does not apply to pnpm.')
if args.electron_chromedriver:
print('WARNING: --electron-chromedriver is deprecated', file=sys.stderr)
print(
' (Use --node-chromedriver-from-electron instead.)',
file=sys.stderr,
)
if args.stub_requests:
Requests.instance = StubRequests()
if not args.no_requests_cache:
Cache.instance = FilesystemBasedCache()
lockfiles: list[Path]
if args.recursive or args.recursive_pattern:
lockfiles = list(
_scan_for_lockfiles(Path(args.lockfile), args.recursive_pattern)
)
if not lockfiles:
sys.exit('No lockfiles found.')
print(f'Found {len(lockfiles)} lockfiles.')
else:
lockfiles = [Path(args.lockfile)]
lockfile_root = Path(args.lockfile).parent
provider_factory: ProviderFactory
if args.type == 'npm':
npm_options = NpmProviderFactory.Options(
NpmLockfileProvider.Options(no_devel=args.no_devel),
NpmModuleProvider.Options(
registry=args.registry,
no_autopatch=args.no_autopatch,
no_trim_index=args.no_trim_index,
),
)
provider_factory = NpmProviderFactory(lockfile_root, npm_options)
elif args.type == 'yarn':
provider_factory = YarnProviderFactory()
elif args.type == 'pnpm':
pnpm_options = PnpmProviderFactory.Options(
PnpmLockfileProvider.Options(
no_devel=args.no_devel,
registry=args.registry,
),
)
provider_factory = PnpmProviderFactory(lockfile_root, pnpm_options)
else:
assert False, args.type
print('Reading packages from lockfiles...')
packages: set[Package] = set()
rcfile_node_headers: set[NodeHeaders] = set()
for lockfile in lockfiles:
lockfile_provider = provider_factory.create_lockfile_provider()
rcfile_providers = provider_factory.create_rcfile_providers()
packages.update(lockfile_provider.process_lockfile(lockfile))
for rcfile_provider in rcfile_providers:
rcfile = lockfile.parent / rcfile_provider.RCFILE_NAME
if rcfile.is_file():
nh = rcfile_provider.get_node_headers(rcfile)
if nh is not None:
rcfile_node_headers.add(nh)
print(f'{len(packages)} packages read.')
gen = ManifestGenerator()
gen.split_size = args.split_size * 1000
with gen:
options = SpecialSourceProvider.Options(
node_chromedriver_from_electron=args.node_chromedriver_from_electron
or args.electron_chromedriver,
nwjs_version=args.nwjs_version,
nwjs_node_headers=args.nwjs_node_headers,
nwjs_ffmpeg=args.nwjs_ffmpeg,
xdg_layout=args.xdg_layout,
electron_ffmpeg=args.electron_ffmpeg,
electron_node_headers=args.electron_node_headers,
node_sdk_extension=args.node_sdk_extension,
)
special = SpecialSourceProvider(gen, options)
with (
provider_factory.create_module_provider(gen, special) as module_provider,
GeneratorProgress(
packages,
module_provider,
args.max_parallel,
) as progress,
):
await progress.run()
for headers in rcfile_node_headers:
print(f'Generating headers {headers.runtime} @ {headers.target}')
await special.generate_node_headers(headers)
if args.xdg_layout:
script_name = 'setup_sdk_node_headers.sh'
node_gyp_dir = gen.data_root / 'cache' / 'node-gyp'
gen.add_script_source(
[
'version=$(node --version | sed "s/^v//")',
'nodedir=$(dirname "$(dirname "$(which node)")")',
f'mkdir -p "{node_gyp_dir}/$version"',
f'ln -s "$nodedir/include" "{node_gyp_dir}/$version/include"',
f'echo 11 > "{node_gyp_dir}/$version/installVersion"',
],
destination=gen.data_root / script_name,
)
gen.add_command(f'bash {gen.data_root / script_name}')
if args.split:
sources = list(gen.ordered_sources())
await Requests.instance.upgrade_to_sha256(sources)
gen.set_upgraded_sources(sources)
i = 0
for i, part in enumerate(gen.split_sources()):
output = Path(args.output)
output = output.with_suffix(f'.{i}{output.suffix}')
await asyncio.to_thread(
output.write_text,
json.dumps(part, indent=ManifestGenerator.JSON_INDENT),
encoding='utf-8',
)
delattr(gen, '_upgraded_sources')
print(f'Wrote {gen.source_count} to {i + 1} file(s).')
else:
sources = list(gen.ordered_sources())
await Requests.instance.upgrade_to_sha256(sources)
output_path = Path(args.output)
data = json.dumps(
sources,
indent=ManifestGenerator.JSON_INDENT,
)
await asyncio.to_thread(
output_path.write_text,
data,
encoding='utf-8',
)
if len(data.encode('utf-8')) >= gen.split_size:
print(
'WARNING: generated-sources.json is too large for GitHub.',
file=sys.stderr,
)
print(' (Pass -s to enable splitting.)')
print(f'Wrote {gen.source_count} source(s).')
def main() -> None:
asyncio.run(_async_main())