Skip to content

Commit

Permalink
Merge pull request #5 from JoshuaVandaele/1.1.4
Browse files Browse the repository at this point in the history
1.1.4
  • Loading branch information
JoshuaVandaele authored Feb 9, 2024
2 parents 01f415b + 302548c commit 3b0d106
Show file tree
Hide file tree
Showing 31 changed files with 308 additions and 221 deletions.
10 changes: 6 additions & 4 deletions modules/updaters/ArchLinux.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cache
import os
from pathlib import Path

import requests
from bs4 import BeautifulSoup
Expand All @@ -25,8 +25,8 @@ class ArchLinux(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str) -> None:
file_path = os.path.join(folder_path, FILE_NAME)
def __init__(self, folder_path: Path) -> None:
file_path = folder_path / FILE_NAME
super().__init__(file_path)

self.download_page = requests.get(DOWNLOAD_PAGE_URL)
Expand All @@ -51,7 +51,9 @@ def check_integrity(self) -> bool:
sha256_sums = requests.get(sha256_url).text

sha256_sum = parse_hash(
sha256_sums, [self._get_complete_normalized_file_path(absolute=False)], 0
sha256_sums,
[str(self._get_complete_normalized_file_path(absolute=False))],
0,
)

return sha256_hash_check(
Expand Down
27 changes: 16 additions & 11 deletions modules/updaters/ChromeOS.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import cache
import os
import zipfile
from functools import cache
from pathlib import Path

import requests

Expand All @@ -26,11 +26,11 @@ class ChromeOS(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str, edition: str) -> None:
def __init__(self, folder_path: Path, edition: str) -> None:
self.valid_editions = ["ltc", "ltr", "stable"]
self.edition = edition.lower()

file_path = os.path.join(folder_path, FILE_NAME)
file_path = Path(folder_path) / FILE_NAME
super().__init__(file_path)

self.chromium_releases_info: list[dict] = requests.get(
Expand All @@ -51,7 +51,7 @@ def check_integrity(self) -> bool:
sha1_sum = self.cur_edition_info["sha1"]

return sha1_hash_check(
self._get_complete_normalized_file_path(absolute=True) + ".zip",
self._get_complete_normalized_file_path(absolute=True).with_suffix(".zip"),
sha1_sum,
)

Expand All @@ -66,7 +66,7 @@ def install_latest_version(self) -> None:

new_file = self._get_complete_normalized_file_path(absolute=True)

archive_path = f"{new_file}.zip"
archive_path = Path(new_file).with_suffix(".zip")

local_file = self._get_local_file()

Expand All @@ -80,7 +80,7 @@ def install_latest_version(self) -> None:
) from e

if not integrity_check:
os.remove(archive_path)
archive_path.unlink()
raise IntegrityCheckError("Integrity check failed: Hashes do not match")

with zipfile.ZipFile(archive_path) as z:
Expand All @@ -91,12 +91,17 @@ def install_latest_version(self) -> None:
file for file in file_list if file.lower().endswith(file_ext)
)

extracted_file = z.extract(to_extract, path=os.path.dirname(new_file))
os.rename(extracted_file, new_file)
extracted_file = Path(z.extract(to_extract, path=new_file.parent))
try:
extracted_file.rename(new_file)
except FileExistsError:
# On Windows, files are not overwritten by default, so we need to remove the old file first
new_file.unlink()
extracted_file.rename(new_file)

os.remove(archive_path)
archive_path.unlink()
if local_file:
os.remove(local_file) # type: ignore
local_file.unlink()

@cache
def _get_latest_version(self) -> list[str]:
Expand Down
6 changes: 3 additions & 3 deletions modules/updaters/Clonezilla.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cache
import os
from pathlib import Path

import requests
from bs4 import BeautifulSoup, Tag
Expand All @@ -20,8 +20,8 @@ class Clonezilla(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str) -> None:
file_path = os.path.join(folder_path, FILE_NAME)
def __init__(self, folder_path: Path) -> None:
file_path = folder_path / FILE_NAME
super().__init__(file_path)

@cache
Expand Down
22 changes: 13 additions & 9 deletions modules/updaters/Debian.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cache
import os
from pathlib import Path

import requests
from bs4 import BeautifulSoup, Tag
Expand Down Expand Up @@ -28,7 +28,7 @@ class Debian(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str, edition: str) -> None:
def __init__(self, folder_path: Path, edition: str) -> None:
self.valid_editions = [
"cinnamon",
"gnome",
Expand All @@ -42,7 +42,7 @@ def __init__(self, folder_path: str, edition: str) -> None:

self.edition = edition.lower()

file_path = os.path.join(folder_path, FILE_NAME)
file_path = folder_path / FILE_NAME
super().__init__(file_path)

# Make the parameter case insensitive, and find back the correct case using valid_editions
Expand Down Expand Up @@ -77,7 +77,9 @@ def check_integrity(self) -> bool:
sha256_sums = requests.get(sha256_url).text

sha256_sum = parse_hash(
sha256_sums, [self._get_complete_normalized_file_path(absolute=False)], 0
sha256_sums,
[str(self._get_complete_normalized_file_path(absolute=False))],
0,
)

return sha256_hash_check(
Expand All @@ -94,11 +96,13 @@ def _get_latest_version(self) -> list[str]:
latest = next(
href
for a_tag in download_a_tags
if self._get_normalized_file_path(
absolute=False,
version=None,
edition=self.edition if self.has_edition() else None, # type: ignore
lang=self.lang if self.has_lang() else None, # type: ignore
if str(
self._get_normalized_file_path(
absolute=False,
version=None,
edition=self.edition if self.has_edition() else None, # type: ignore
lang=self.lang if self.has_lang() else None, # type: ignore
)
).split("[[VER]]")[-1]
in (href := a_tag.get("href"))
)
Expand Down
6 changes: 3 additions & 3 deletions modules/updaters/Fedora.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cache
import os
from pathlib import Path

import requests
from bs4 import BeautifulSoup, Tag
Expand Down Expand Up @@ -27,7 +27,7 @@ class Fedora(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str, edition: str) -> None:
def __init__(self, folder_path: Path, edition: str) -> None:
self.valid_editions = [
"Budgie",
"Cinnamon",
Expand All @@ -41,7 +41,7 @@ def __init__(self, folder_path: str, edition: str) -> None:
]
self.edition = edition

file_path = os.path.join(folder_path, FILE_NAME)
file_path = folder_path / FILE_NAME
super().__init__(file_path)

# Make the parameter case insensitive, and find back the correct case using valid_editions
Expand Down
38 changes: 24 additions & 14 deletions modules/updaters/FreeDOS.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from functools import cache
import glob
import os
import logging
import re
import zipfile
from functools import cache
from pathlib import Path

import requests
from bs4 import BeautifulSoup
Expand All @@ -28,7 +29,7 @@ class FreeDOS(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str, edition: str) -> None:
def __init__(self, folder_path: Path, edition: str) -> None:
self.valid_editions = [
"BonusCD",
"FloppyEdition",
Expand All @@ -39,7 +40,7 @@ def __init__(self, folder_path: str, edition: str) -> None:
]

self.edition = edition
file_path = os.path.join(folder_path, FILE_NAME)
file_path = folder_path / FILE_NAME
super().__init__(file_path)

# Make the parameter case insensitive, and find back the correct case using valid_editions
Expand Down Expand Up @@ -85,7 +86,7 @@ def check_integrity(self) -> bool:
return sha256_hash_check(
self._get_normalized_file_path(
True, self._get_latest_version(), self.edition
).replace("[[EXT]]", "zip"),
).with_suffix(".zip"),
sha256_sum,
)

Expand All @@ -99,7 +100,7 @@ def install_latest_version(self) -> None:
download_link = self._get_download_link()

new_file = self._get_complete_normalized_file_path(absolute=True)
archive_path = new_file.replace("[[EXT]]", "zip")
archive_path = new_file.with_suffix(".zip")

local_file = self._get_local_file()

Expand All @@ -115,7 +116,7 @@ def install_latest_version(self) -> None:
) from e

if not integrity_check:
os.remove(archive_path)
archive_path.unlink()
raise IntegrityCheckError("Integrity check failed: Hashes do not match")

with zipfile.ZipFile(archive_path) as z:
Expand All @@ -131,26 +132,35 @@ def install_latest_version(self) -> None:
file for file in file_list if file.upper().endswith(file_ext)
)

extracted_file = z.extract(to_extract, path=os.path.dirname(new_file))
os.rename(extracted_file, new_file.replace("[[EXT]]", file_ext))
extracted_file = Path(z.extract(to_extract, path=new_file.parent))
try:
extracted_file.rename(new_file.with_suffix(file_ext))
except FileExistsError:
# On Windows, files are not overwritten by default, so we need to remove the old file first
new_file.unlink()
extracted_file.rename(new_file.with_suffix(file_ext))

os.remove(archive_path)
archive_path.unlink()
if local_file:
os.remove(local_file) # type: ignore

def _get_local_file(self) -> str | None:
def _get_local_file(self) -> Path | None:
file_path = self._get_normalized_file_path(
absolute=True,
version=None,
edition=self.edition,
edition=self.edition if self.has_edition() else None, # type: ignore
lang=self.lang if self.has_lang() else None, # type: ignore
)

local_files = glob.glob(
file_path.replace("[[VER]]", "*").replace("[[EXT]]", "*")
str(file_path.with_suffix(".*")).replace("[[VER]]", "*")
)

if local_files:
return local_files[0]
return Path(local_files[0])
logging.debug(
f"[FreeDOS._get_local_file] No local file found for {self.__class__.__name__}"
)
return None

@cache
Expand Down
6 changes: 3 additions & 3 deletions modules/updaters/GPartedLive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cache
import os
from pathlib import Path

import requests

Expand All @@ -19,8 +19,8 @@ class GPartedLive(GenericUpdater):
This class inherits from the abstract base class GenericUpdater.
"""

def __init__(self, folder_path: str) -> None:
file_path = os.path.join(folder_path, FILE_NAME)
def __init__(self, folder_path: Path) -> None:
file_path = folder_path / FILE_NAME
super().__init__(file_path)

self.checksum_file: str = requests.get(
Expand Down
Loading

0 comments on commit 3b0d106

Please sign in to comment.