Skip to content

Commit

Permalink
node: Create integrity file symlink for new electron/get versions
Browse files Browse the repository at this point in the history
This also updates the existing electron test & extends it to test this
out.

Fixes #370.

Signed-off-by: Ryan Gonzalez <[email protected]>
  • Loading branch information
refi64 committed Oct 7, 2023
1 parent 900a165 commit dcf5193
Show file tree
Hide file tree
Showing 8 changed files with 1,213 additions and 1,654 deletions.
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
55 changes: 38 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,36 @@ 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,
)
print(
[
f'mkdir -p "{dir}"',
*(
f'ln -s "../{binary.filename}" "{dir}/{dest}"'
for (binary, dest) in binaries
),
]
)

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

0 comments on commit dcf5193

Please sign in to comment.