Skip to content

Commit

Permalink
Support setup-file name variations (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcapona authored Jul 30, 2024
1 parent e8d0b02 commit 9938aa7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ repos:
- id: check-symlinks

- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 24.4.2
hooks:
- id: black

Expand All @@ -26,7 +26,7 @@ repos:
- id: autopep8

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.1.0
hooks:
- id: flake8
name: flake8
Expand Down
8 changes: 7 additions & 1 deletion extra/handle-usb-drive-for-pi-top-setup
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ start_miniscreen_app() {
}

has_pitop_offline_setup_files() {
if [ -f "${MOUNT_POINT}/pi-top-usb-setup.tar.gz" ] || [ -f "${MOUNT_POINT}/pi-top-usb-setup/updates.tar.gz" ] || [ -d "${MOUNT_POINT}/pi-top-usb-setup/updates" ]; then
# look for setup files
if find "${MOUNT_POINT}" -maxdepth 1 -type f -name "pi-top-usb-setup*.tar.gz"| grep -q .; then
return 0
fi

# look for already extracted content
if [ -f "${MOUNT_POINT}/pi-top-usb-setup/updates.tar.gz" ] || [ -d "${MOUNT_POINT}/pi-top-usb-setup/updates" ]; then
return 0
fi
return 1
Expand Down
50 changes: 36 additions & 14 deletions pi_top_usb_setup/app_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@


class AppFilesystem:
USB_SETUP_FILENAME_GLOB = "pi-top-usb-setup*.tar.gz"
TEMP_FOLDER = "/tmp"

def __init__(self, mount_point: str) -> None:
self.MOUNT_POINT = mount_point
self.USB_SETUP_FILE = f"{mount_point}/pi-top-usb-setup.tar.gz"
self.TEMP_FOLDER = "/tmp"
self.SETUP_FOLDER = f"{self.TEMP_FOLDER}/pi-top-usb-setup"
self.DEVICE_CONFIG = f"{self.SETUP_FOLDER}/pi-top_config.json"
self.UPDATES_FOLDER = f"{self.SETUP_FOLDER}/updates"

if not any(
[
Path(self.USB_SETUP_FILE).exists(),
len(self._find_setup_files()) > 0,
Path(self.UPDATES_FOLDER).is_dir(),
]
):
raise Exception(
f"Files {self.USB_SETUP_FILE} or {self.UPDATES_FOLDER} not found, exiting ..."
f"Files '{self.MOUNT_POINT}/{self.USB_SETUP_FILENAME_GLOB}' or {self.UPDATES_FOLDER} not found, exiting ..."
)

self.requires_reboot = False
Expand All @@ -59,6 +61,14 @@ def __init__(self, mount_point: str) -> None:
f"findmnt -n -o SOURCE --target {mount_point}", timeout=5
).strip()

def _find_setup_files(self):
# find all setup files in mount point that match the glob pattern, sorted by date
return sorted(
Path(self.MOUNT_POINT).glob(self.USB_SETUP_FILENAME_GLOB),
key=lambda x: x.stat().st_mtime,
reverse=True,
)

@property
def usb_drive_is_present(self) -> bool:
return Path(self.device).exists() if self.device else False
Expand All @@ -67,29 +77,41 @@ def umount_usb_drive(self) -> None:
umount_usb_drive(self.MOUNT_POINT)

def extract_setup_file(self, on_progress: Optional[Callable] = None) -> None:
if not Path(self.USB_SETUP_FILE).exists():
logger.warning(
f"File {self.USB_SETUP_FILE} doesn't exist; skipping extraction"
)
files = self._find_setup_files()
if len(files) >= 1:
logger.info(f"Found {len(files)} setup files; will use '{files[0]}'...")
self._do_extract_setup_file(files[0], on_progress)
else:
logger.warning("No setup file found; skipping extraction")

def _do_extract_setup_file(
self, filename: str, on_progress: Optional[Callable] = None
) -> None:
if not Path(filename).exists():
logger.warning(f"File '{filename}' doesn't exist; skipping extraction")
return

# Get extracted size of the tar.gz file
try:
space = get_tar_gz_extracted_size(filename)
except Exception as e:
raise ExtractionError(f"Error getting extracted size of '{filename}', {e}")

# Check if there's enough free space in the SD card
if not drive_has_enough_free_space(
drive="/", space=get_tar_gz_extracted_size(self.USB_SETUP_FILE)
):
if not drive_has_enough_free_space(drive="/", space=space):
raise NotEnoughSpaceException(
f"Not enough space to extract {self.USB_SETUP_FILE} into {self.TEMP_FOLDER}"
f"Not enough space to extract '{filename}' into {self.TEMP_FOLDER}"
)

try:
extract_file(
file=self.USB_SETUP_FILE,
file=filename,
destination=self.TEMP_FOLDER,
on_progress=on_progress,
)
logger.info(f"File {self.USB_SETUP_FILE} extracted into {self.TEMP_FOLDER}")
logger.info(f"File {filename} extracted into {self.TEMP_FOLDER}")
except Exception as e:
raise ExtractionError(f"Error extracting '{self.USB_SETUP_FILE}': {e}")
raise ExtractionError(f"Error extracting '{filename}': {e}")

def configure_device(self, on_progress: Optional[Callable] = None) -> None:
if not Path(self.DEVICE_CONFIG).exists():
Expand Down

0 comments on commit 9938aa7

Please sign in to comment.