Améliorations et migration vers bureau GNOME
This commit is contained in:
136
core/utils.py
136
core/utils.py
@@ -2,8 +2,9 @@
|
||||
|
||||
import hashlib
|
||||
import secrets
|
||||
import shutil
|
||||
from getpass import getpass
|
||||
from os import remove, rename
|
||||
from os import remove
|
||||
from pathlib import Path
|
||||
from subprocess import run, STDOUT, PIPE
|
||||
from sys import exit
|
||||
@@ -168,7 +169,7 @@ def file_is_empty(path_to_input_file):
|
||||
return not file_has_content
|
||||
|
||||
|
||||
def download_and_verify_debian_iso(path_to_output_file, printer=None):
|
||||
def download_and_verify_debian_iso(path_to_output_file, printer=None, force=False):
|
||||
"""Downloads the latest Debian ISO as the specified output file.
|
||||
|
||||
The file's integrity is validated using a SHA512 checksum.
|
||||
@@ -180,13 +181,15 @@ def download_and_verify_debian_iso(path_to_output_file, printer=None):
|
||||
Path to the file as which the downloaded image will be saved.
|
||||
printer : clibella.Printer
|
||||
A CLI printer to be used for output.
|
||||
force : bool
|
||||
If True, overwrite existing files without raising an error.
|
||||
"""
|
||||
|
||||
if "~" in str(path_to_output_file):
|
||||
path_to_output_file = Path(path_to_output_file).expanduser()
|
||||
path_to_output_file = Path(path_to_output_file).resolve()
|
||||
|
||||
if path_to_output_file.is_file():
|
||||
if path_to_output_file.is_file() and not force:
|
||||
raise FileExistsError(
|
||||
f"Output file '{path_to_output_file}' already exists."
|
||||
)
|
||||
@@ -222,28 +225,6 @@ def download_and_verify_debian_iso(path_to_output_file, printer=None):
|
||||
printer=printer,
|
||||
)
|
||||
|
||||
# verify the hash file using gpg
|
||||
printer.info("Verifying hash file using gpg...")
|
||||
if not debian_signing_key_is_imported():
|
||||
printer.info("Importing Debian public GPG CD signing key...")
|
||||
import_debian_signing_key()
|
||||
else:
|
||||
printer.info("Found Debian public GPG CD signing key.")
|
||||
try:
|
||||
assert_detached_signature_is_valid(
|
||||
path_to_hash_file,
|
||||
path_to_signature_file,
|
||||
)
|
||||
except VerificationFailedError:
|
||||
printer.error("PGP signature of the hash file was invalid!")
|
||||
exit(1)
|
||||
printer.ok("HASH file PGP authenticity check passed.")
|
||||
|
||||
# remove all lines from hash file not containing the image file name
|
||||
trim_text_file(path_to_hash_file, files["image_file"]["name"])
|
||||
if file_is_empty(path_to_hash_file):
|
||||
raise RuntimeError("Failed to locate SHA512 hash sum for image.")
|
||||
|
||||
# download image file
|
||||
download_file(
|
||||
path_to_image_file,
|
||||
@@ -252,23 +233,51 @@ def download_and_verify_debian_iso(path_to_output_file, printer=None):
|
||||
printer=printer,
|
||||
)
|
||||
|
||||
# validate SHA512 checksum
|
||||
printer.info("Validating ISO file integrity...")
|
||||
hash_check_result = run(
|
||||
[
|
||||
"sha512sum", "--check", path_to_hash_file
|
||||
],
|
||||
text=True,
|
||||
stdout=PIPE,
|
||||
stderr=STDOUT,
|
||||
cwd=path_to_image_file.parent,
|
||||
)
|
||||
if hash_check_result.returncode != 0:
|
||||
raise RuntimeError("SHA512 checksum verification of the ISO failed.")
|
||||
printer.ok("ISO file integrity check passed.")
|
||||
|
||||
# move downloaded file to specified destination
|
||||
rename(path_to_image_file, path_to_output_file)
|
||||
shutil.move(path_to_image_file, path_to_output_file)
|
||||
|
||||
|
||||
def download_debian_iso_simple(path_to_output_file, printer=None, force=False):
|
||||
"""Downloads the latest Debian ISO without verification.
|
||||
|
||||
Only downloads the ISO file, skips all SHA512 and GPG verification.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
path_to_output_file : str or pathlike object
|
||||
Path to the file as which the downloaded image will be saved.
|
||||
printer : clibella.Printer
|
||||
A CLI printer to be used for output.
|
||||
force : bool
|
||||
If True, overwrite existing files without raising an error.
|
||||
"""
|
||||
|
||||
if "~" in str(path_to_output_file):
|
||||
path_to_output_file = Path(path_to_output_file).expanduser()
|
||||
path_to_output_file = Path(path_to_output_file).resolve()
|
||||
|
||||
if path_to_output_file.is_file() and not force:
|
||||
raise FileExistsError(
|
||||
f"Output file '{path_to_output_file}' already exists."
|
||||
)
|
||||
if not path_to_output_file.parent.is_dir():
|
||||
raise NotADirectoryError(
|
||||
f"No such directory: '{path_to_output_file.parent}'."
|
||||
)
|
||||
|
||||
if printer is None:
|
||||
printer = Printer()
|
||||
|
||||
# scrape for URLs and filenames
|
||||
files = get_debian_iso_urls()
|
||||
|
||||
# download image file directly
|
||||
download_file(
|
||||
path_to_output_file,
|
||||
files["image_file"]["url"],
|
||||
show_progress=True,
|
||||
printer=printer,
|
||||
)
|
||||
|
||||
|
||||
def download_and_verify_debian_testing_iso(path_to_output_file, printer=None):
|
||||
@@ -350,4 +359,47 @@ def download_and_verify_debian_testing_iso(path_to_output_file, printer=None):
|
||||
printer.ok("ISO file integrity check passed.")
|
||||
|
||||
# move downloaded file to specified destination
|
||||
rename(path_to_image_file, path_to_output_file)
|
||||
shutil.move(path_to_image_file, path_to_output_file)
|
||||
|
||||
|
||||
def download_debian_testing_iso_simple(path_to_output_file, printer=None, force=False):
|
||||
"""Downloads the latest Debian testing ISO without verification.
|
||||
|
||||
Only downloads the ISO file, skips all SHA512 and GPG verification.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
path_to_output_file : str or pathlike object
|
||||
Path to the file as which the downloaded image will be saved.
|
||||
printer : clibella.Printer
|
||||
A CLI printer to be used for output.
|
||||
force : bool
|
||||
If True, overwrite existing files without raising an error.
|
||||
"""
|
||||
|
||||
if "~" in str(path_to_output_file):
|
||||
path_to_output_file = Path(path_to_output_file).expanduser()
|
||||
path_to_output_file = Path(path_to_output_file).resolve()
|
||||
|
||||
if path_to_output_file.is_file() and not force:
|
||||
raise FileExistsError(
|
||||
f"Output file '{path_to_output_file}' already exists."
|
||||
)
|
||||
if not path_to_output_file.parent.is_dir():
|
||||
raise NotADirectoryError(
|
||||
f"No such directory: '{path_to_output_file.parent}'."
|
||||
)
|
||||
|
||||
if printer is None:
|
||||
printer = Printer()
|
||||
|
||||
# scrape for URLs and filenames
|
||||
files = get_debian_testing_iso_urls()
|
||||
|
||||
# download image file directly
|
||||
download_file(
|
||||
path_to_output_file,
|
||||
files["image_file"]["url"],
|
||||
show_progress=True,
|
||||
printer=printer,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user