Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: Create integrity file symlink for new electron/get versions #376

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions node/flatpak_node_generator/electron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from typing import Dict, Iterator, NamedTuple, Optional

import hashlib
import os.path
import urllib.parse

from .integrity import Integrity
from .package import SemVer
from .requests import Requests
Expand All @@ -17,6 +21,14 @@ class Binary(NamedTuple):

arch: Optional['ElectronBinaryManager.Arch'] = None

@property
def url_hash(self) -> str:
url = urllib.parse.urlparse(self.url)
url_dir = urllib.parse.urlunparse(
url._replace(path=os.path.dirname(url.path))
)
return hashlib.sha256(url_dir.encode()).hexdigest()

ELECTRON_ARCHES_TO_FLATPAK = {
'ia32': 'i386',
'x64': 'x86_64',
Expand Down
46 changes: 29 additions & 17 deletions node/flatpak_node_generator/providers/special.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path
from typing import List, NamedTuple, Optional
from typing import DefaultDict, List, NamedTuple, Optional, Tuple

import collections
import hashlib
import itertools
import json
import os
import re
Expand Down Expand Up @@ -56,6 +58,9 @@ def _add_electron_cache_downloads(
add_integrities: bool = True,
) -> None:
electron_cache_dir = self.electron_cache_dir
links_to_create: DefaultDict[
str, List[Tuple[ElectronBinaryManager.Binary, str]]
] = collections.defaultdict(lambda: [])

for binary in manager.find_binaries(binary_name):
assert binary.arch is not None
Expand All @@ -69,23 +74,9 @@ def _add_electron_cache_downloads(
if self.xdg_layout:
sanitized_url = ''.join(c for c in binary.url if c not in '/:')

links_to_create[sanitized_url].append((binary, binary.filename))
# And for @electron/get >= 1.12.4 its sha256 hash of url dirname
url = urllib.parse.urlparse(binary.url)
url_dir = urllib.parse.urlunparse(
url._replace(path=os.path.dirname(url.path))
)
url_hash = hashlib.sha256(url_dir.encode()).hexdigest()

self.gen.add_shell_source(
[
f'mkdir -p "{sanitized_url}"',
f'ln -s "../{binary.filename}" "{sanitized_url}/{binary.filename}"',
f'mkdir -p "{url_hash}"',
f'ln -s "../{binary.filename}" "{url_hash}/{binary.filename}"',
],
destination=electron_cache_dir,
only_arches=[binary.arch.flatpak],
)
links_to_create[binary.url_hash].append((binary, binary.filename))

if add_integrities:
integrity_file = manager.integrity_file
Expand All @@ -94,6 +85,27 @@ def _add_electron_cache_downloads(
integrity_file.integrity,
electron_cache_dir / integrity_file.filename,
)
links_to_create[integrity_file.url_hash].append(
(integrity_file, ElectronBinaryManager.INTEGRITY_BASE_FILENAME)
)

for dir, all_binaries in links_to_create.items():
for arch, binaries_it in itertools.groupby(
sorted(all_binaries, key=lambda b: str(b[0].arch)),
key=lambda b: b[0].arch,
):
binaries = list(binaries_it)
self.gen.add_shell_source(
[
f'mkdir -p "{dir}"',
*(
f'ln -s "../{binary.filename}" "{dir}/{dest}"'
for (binary, dest) in binaries
),
],
destination=electron_cache_dir,
only_arches=[arch.flatpak] if arch is not None else None,
)

async def _handle_electron(self, package: Package) -> None:
manager = await ElectronBinaryManager.for_version(package.version)
Expand Down
Loading