Skip to content

Commit 6ee0117

Browse files
committed
node: code review fixes
1 parent 5630eec commit 6ee0117

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

node/flatpak_node_generator/package.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ class GitSource(PackageSource):
123123
from_: Optional[str]
124124

125125

126+
@dataclass(frozen=True, eq=True)
127+
class NamedGitSource:
128+
package_name: str
129+
git_source: GitSource
130+
131+
126132
@dataclass(frozen=True, eq=True)
127133
class LocalSource(PackageSource):
128134
path: str

node/flatpak_node_generator/providers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def parse_git_source(self, version: str, from_: Optional[str] = None) -> GitSour
3838
# with https://[email protected]/ianstormtaylor/to-camel-case.git
3939
# for git+ssh URLs
4040
if ':' in new_url.netloc:
41-
netloc_split = new_url.netloc.split(':')
41+
netloc_split = new_url.netloc.split(':', 1)
4242
new_url = new_url._replace(
4343
netloc=netloc_split[0], path=f'/{netloc_split[1]}{new_url.path}'
4444
)

node/flatpak_node_generator/providers/npm.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
NamedTuple,
99
Optional,
1010
Set,
11-
Tuple,
1211
Type,
1312
)
1413

@@ -27,6 +26,7 @@
2726
from ..package import (
2827
GitSource,
2928
LocalSource,
29+
NamedGitSource,
3030
Package,
3131
PackageSource,
3232
PackageURLSource,
@@ -85,12 +85,11 @@ def _process_packages_v1(
8585
integrity = Integrity.parse(info['integrity'])
8686

8787
if 'resolved' in info:
88-
if info['resolved'].startswith('git+'):
89-
source = self.parse_git_source(info['resolved'])
88+
resolved = info['resolved']
89+
if resolved.startswith('git+'):
90+
source = self.parse_git_source(resolved)
9091
else:
91-
source = ResolvedSource(
92-
resolved=info['resolved'], integrity=integrity
93-
)
92+
source = ResolvedSource(resolved=resolved, integrity=integrity)
9493
elif version_url.scheme in {'http', 'https'}:
9594
source = PackageURLSource(resolved=version, integrity=integrity)
9695
else:
@@ -204,9 +203,10 @@ def __init__(
204203
] = {}
205204
self.index_entries: Dict[Path, str] = {}
206205
self.all_lockfiles: Set[Path] = set()
207-
# Mapping of lockfiles to a dict of the Git source target paths and GitSource objects.
206+
# Mapping of lockfiles to a dict of the Git source target paths and
207+
# NamedGitSource objects (package name + GitSource)
208208
self.git_sources: DefaultDict[
209-
Path, Dict[Path, Tuple[str, GitSource]]
209+
Path, Dict[Path, NamedGitSource]
210210
] = collections.defaultdict(lambda: {})
211211
# FIXME better pass the same provider object we created in main
212212
self.rcfile_provider = NpmRCFileProvider()
@@ -367,34 +367,36 @@ async def generate_package(self, package: Package) -> None:
367367
# Get a unique name to use for the Git repository folder.
368368
name = f'{package.name}-{source.commit}'
369369
path = self.gen.data_root / 'git-packages' / name
370-
self.git_sources[package.lockfile][path] = (package.name, source)
370+
self.git_sources[package.lockfile][path] = NamedGitSource(
371+
package.name, source
372+
)
371373
self.gen.add_git_source(source.url, source.commit, path)
372374

375+
git_suffix = re.compile(r'\.git$')
373376
url = urllib.parse.urlparse(source.url)
374-
if url.netloc == '[email protected]' or url.netloc == 'github.com':
377+
378+
if url.hostname == 'github.com':
375379
url = url._replace(
376-
netloc='codeload.github.com', path=re.sub('.git$', '', url.path)
377-
)
378-
tarball_url = url._replace(
379-
path=re.sub('$', f'/tar.gz/{source.commit}', url.path)
380+
netloc='codeload.github.com', path=git_suffix.sub('', url.path)
380381
)
382+
tarball_url = url._replace(path=url.path + f'/tar.gz/{source.commit}')
381383
index_url = tarball_url.geturl()
382-
elif url.netloc == '[email protected]' or url.netloc == 'gitlab.com':
384+
elif url.hostname == 'gitlab.com':
383385
url = url._replace(
384-
netloc='gitlab.com', path=re.sub('.git$', '', url.path)
386+
netloc='gitlab.com', path=git_suffix.sub('', url.path)
385387
)
386388
tarball_url = url._replace(
387-
path=re.sub(
388-
'$',
389-
f'/-/archive/{source.commit}/{package.name}-{source.commit}.tar.gz',
390-
url.path,
391-
)
389+
path=url.path
390+
+ f'/-/archive/{source.commit}/{package.name}-{source.commit}.tar.gz'
392391
)
393392
index_url = url._replace(
394-
path=re.sub(
395-
'$', f'/repository/archive.tar.gz?ref={source.commit}', url.path
396-
)
393+
path=url.path + f'/repository/archive.tar.gz?ref={source.commit}'
397394
).geturl()
395+
else:
396+
raise NotImplementedError(
397+
f"Don't know how to handle git source with url {url.geturl()}"
398+
)
399+
398400
metadata = await RemoteUrlMetadata.get(
399401
tarball_url.geturl(), cachable=True, integrity_algorithm='sha512'
400402
)
@@ -501,16 +503,23 @@ def _finalize(self) -> None:
501503
lockfile_v1 = json.load(fp)['lockfileVersion'] == 1
502504

503505
if lockfile_v1:
504-
for path, name_source in sources.items():
506+
for path, named_git_source in sources.items():
505507
GIT_URL_PREFIX = 'git+'
506-
name, source = name_source
507-
new_version = f'{path}#{source.commit}'
508-
data['package.json'][name] = new_version
509-
data['package-lock.json'][source.original] = new_version
510-
511-
if source.original.startswith(GIT_URL_PREFIX):
508+
new_version = f'{path}#{named_git_source.git_source.commit}'
509+
data['package.json'][
510+
named_git_source.package_name
511+
] = new_version
512+
data['package-lock.json'][
513+
named_git_source.git_source.original
514+
] = new_version
515+
516+
if named_git_source.git_source.original.startswith(
517+
GIT_URL_PREFIX
518+
):
512519
data['package-lock.json'][
513-
source.original[len(GIT_URL_PREFIX) :]
520+
named_git_source.git_source.original[
521+
len(GIT_URL_PREFIX) :
522+
]
514523
] = new_version
515524

516525
for filename, script in scripts.items():
@@ -547,7 +556,9 @@ def _finalize(self) -> None:
547556

548557
if not self.no_autopatch:
549558
# FLATPAK_BUILDER_BUILDDIR isn't defined yet for script sources.
550-
self.gen.add_command(f'FLATPAK_BUILDER_BUILDDIR="$PWD" {patch_all_dest}')
559+
self.gen.add_command(
560+
f'FLATPAK_BUILDER_BUILDDIR="$PWD" {patch_all_dest}'
561+
)
551562

552563
if self.index_entries:
553564
for path, entry in self.index_entries.items():

node/tests/test_providers.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
from conftest import FlatpakBuilder, ProviderFactorySpec
99
from flatpak_node_generator.manifest import ManifestGenerator
1010

11+
TEST_SCRIPT = """
12+
require("array-range");
13+
require("is-empty-object");
14+
require("is-number");
15+
require("person-lib");
16+
require("to-camel-case");
17+
require("to-capital-case");
18+
require("to-no-case");
19+
require("to-space-case");
20+
require("@flatpak-node-generator-tests/subdir").sayHello();
21+
"""
22+
1123

1224
async def test_minimal_git(
1325
flatpak_builder: FlatpakBuilder,
@@ -49,7 +61,7 @@ async def test_git(
4961
),
5062
commands=[
5163
provider_factory_spec.install_command,
52-
"""node -e 'require("array-range");require("is-empty-object");require("is-number");require("person-lib");require("to-camel-case");require("to-capital-case");require("to-no-case");require("to-space-case");require("@flatpak-node-generator-tests/subdir").sayHello()'""",
64+
f"""node -e '{TEST_SCRIPT}'""",
5365
],
5466
use_node=node_version,
5567
)

0 commit comments

Comments
 (0)