|
26 | 26 | from ..package import ( |
27 | 27 | GitSource, |
28 | 28 | LocalSource, |
| 29 | + Lockfile, |
29 | 30 | NamedGitSource, |
30 | 31 | Package, |
31 | 32 | PackageSource, |
@@ -54,7 +55,7 @@ def __init__(self, options: Options): |
54 | 55 | self.no_devel = options.no_devel |
55 | 56 |
|
56 | 57 | def _process_packages_v1( |
57 | | - self, lockfile: Path, entry: Dict[str, Dict[Any, Any]] |
| 58 | + self, lockfile: Lockfile, entry: Dict[str, Dict[Any, Any]] |
58 | 59 | ) -> Iterator[Package]: |
59 | 60 | for name, info in entry.get('dependencies', {}).items(): |
60 | 61 | if info.get('dev') and self.no_devel: |
@@ -95,13 +96,18 @@ def _process_packages_v1( |
95 | 96 | else: |
96 | 97 | source = RegistrySource(integrity=integrity) |
97 | 98 |
|
98 | | - yield Package(name=name, version=version, source=source, lockfile=lockfile) |
| 99 | + yield Package( |
| 100 | + name=name, |
| 101 | + version=version, |
| 102 | + source=source, |
| 103 | + lockfile=lockfile, |
| 104 | + ) |
99 | 105 |
|
100 | 106 | if 'dependencies' in info: |
101 | 107 | yield from self._process_packages_v1(lockfile, info) |
102 | 108 |
|
103 | 109 | def _process_packages_v2( |
104 | | - self, lockfile: Path, entry: Dict[str, Dict[Any, Any]] |
| 110 | + self, lockfile: Lockfile, entry: Dict[str, Dict[Any, Any]] |
105 | 111 | ) -> Iterator[Package]: |
106 | 112 | for install_path, info in entry.get('packages', {}).items(): |
107 | 113 | if (info.get('dev') or info.get('devOptional')) and self.no_devel: |
@@ -151,20 +157,20 @@ def _process_packages_v2( |
151 | 157 | source=source, |
152 | 158 | ) |
153 | 159 |
|
154 | | - def process_lockfile(self, lockfile: Path) -> Iterator[Package]: |
155 | | - with open(lockfile) as fp: |
| 160 | + def process_lockfile(self, lockfile_path: Path) -> Iterator[Package]: |
| 161 | + with open(lockfile_path) as fp: |
156 | 162 | data = json.load(fp) |
157 | 163 |
|
| 164 | + lockfile = Lockfile(lockfile_path, data['lockfileVersion']) |
| 165 | + |
158 | 166 | # TODO Once lockfile v2 syntax support is complete, use _process_packages_v2 |
159 | 167 | # for both v2 and v2 lockfiles |
160 | | - if data['lockfileVersion'] in {1, 2}: |
| 168 | + if lockfile.version in {1, 2}: |
161 | 169 | yield from self._process_packages_v1(lockfile, data) |
162 | | - elif data['lockfileVersion'] in {3}: |
| 170 | + elif lockfile.version in {3}: |
163 | 171 | yield from self._process_packages_v2(lockfile, data) |
164 | 172 | else: |
165 | | - raise NotImplementedError( |
166 | | - f'Unknown lockfile version {data["lockfileVersion"]}' |
167 | | - ) |
| 173 | + raise NotImplementedError(f'Unknown lockfile version {lockfile.version}') |
168 | 174 |
|
169 | 175 |
|
170 | 176 | class NpmRCFileProvider(RCFileProvider): |
@@ -202,11 +208,11 @@ def __init__( |
202 | 208 | str, asyncio.Future[NpmModuleProvider.RegistryPackageIndex] |
203 | 209 | ] = {} |
204 | 210 | self.index_entries: Dict[Path, str] = {} |
205 | | - self.all_lockfiles: Set[Path] = set() |
| 211 | + self.all_lockfiles: Set[Lockfile] = set() |
206 | 212 | # Mapping of lockfiles to a dict of the Git source target paths and |
207 | 213 | # NamedGitSource objects (package name + GitSource) |
208 | 214 | self.git_sources: DefaultDict[ |
209 | | - Path, Dict[Path, NamedGitSource] |
| 215 | + Lockfile, Dict[Path, NamedGitSource] |
210 | 216 | ] = collections.defaultdict(lambda: {}) |
211 | 217 | # FIXME better pass the same provider object we created in main |
212 | 218 | self.rcfile_provider = NpmRCFileProvider() |
@@ -433,7 +439,7 @@ def get_lockfile_rc(self, lockfile: Path) -> Dict[str, str]: |
433 | 439 |
|
434 | 440 | def get_package_registry(self, package: Package) -> str: |
435 | 441 | assert isinstance(package.source, RegistrySource) |
436 | | - rc = self.get_lockfile_rc(package.lockfile) |
| 442 | + rc = self.get_lockfile_rc(package.lockfile.path) |
437 | 443 | if rc and '/' in package.name: |
438 | 444 | scope, _ = package.name.split('/', maxsplit=1) |
439 | 445 | if f'{scope}:registry' in rc: |
@@ -493,16 +499,13 @@ def _finalize(self) -> None: |
493 | 499 | } |
494 | 500 |
|
495 | 501 | for lockfile, sources in self.git_sources.items(): |
496 | | - prefix = self.relative_lockfile_dir(lockfile) |
| 502 | + prefix = self.relative_lockfile_dir(lockfile.path) |
497 | 503 | data: Dict[str, Dict[str, str]] = { |
498 | 504 | 'package.json': {}, |
499 | 505 | 'package-lock.json': {}, |
500 | 506 | } |
501 | 507 |
|
502 | | - with open(lockfile) as fp: |
503 | | - lockfile_v1 = json.load(fp)['lockfileVersion'] == 1 |
504 | | - |
505 | | - if lockfile_v1: |
| 508 | + if lockfile.version == 1: |
506 | 509 | for path, named_git_source in sources.items(): |
507 | 510 | GIT_URL_PREFIX = 'git+' |
508 | 511 | new_version = f'{path}#{named_git_source.git_source.commit}' |
@@ -530,25 +533,27 @@ def _finalize(self) -> None: |
530 | 533 | .replace('\n', '') |
531 | 534 | ) |
532 | 535 | json_data = json.dumps(data[filename]) |
533 | | - patch_commands[lockfile].append( |
| 536 | + patch_commands[lockfile.path].append( |
534 | 537 | 'jq' |
535 | 538 | ' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"' |
536 | 539 | f' --argjson data {shlex.quote(json_data)}' |
537 | 540 | f' {shlex.quote(script)} {target}' |
538 | 541 | f' > {target}.new' |
539 | 542 | ) |
540 | | - patch_commands[lockfile].append(f'mv {target}{{.new,}}') |
| 543 | + patch_commands[lockfile.path].append(f'mv {target}{{.new,}}') |
541 | 544 |
|
542 | 545 | if len(patch_commands) > 0: |
543 | 546 | patch_all_commands: List[str] = [] |
544 | 547 | for lockfile in self.all_lockfiles: |
545 | 548 | patch_dest = ( |
546 | | - self.gen.data_root / 'patch' / self.relative_lockfile_dir(lockfile) |
| 549 | + self.gen.data_root |
| 550 | + / 'patch' |
| 551 | + / self.relative_lockfile_dir(lockfile.path) |
547 | 552 | ) |
548 | 553 | # Don't use with_extension to avoid problems if the package has a . in its name. |
549 | 554 | patch_dest = patch_dest.with_name(patch_dest.name + '.sh') |
550 | 555 |
|
551 | | - self.gen.add_script_source(patch_commands[lockfile], patch_dest) |
| 556 | + self.gen.add_script_source(patch_commands[lockfile.path], patch_dest) |
552 | 557 | patch_all_commands.append(f'"$FLATPAK_BUILDER_BUILDDIR"/{patch_dest}') |
553 | 558 |
|
554 | 559 | patch_all_dest = self.gen.data_root / 'patch-all.sh' |
|
0 commit comments