From a2ea9905acc9da037fe302d85956b647cd1b879e Mon Sep 17 00:00:00 2001 From: gotiniens <9089870+gotiniens@users.noreply.github.com> Date: Mon, 5 Jul 2021 22:44:30 +0200 Subject: [PATCH] ENH: Ignore files next to device dir (#175) * ignore files in path_to_garmin_device * Update tests, Add autorun file in path_to_garmin_device like in block device * Allow multiple directories * CLN: Remove unused variables --- CHANGELOG.md | 2 ++ tests/db_tests/conftest.py | 6 +++++- tests/db_tests/test_watchdogs.py | 2 ++ wkz/watchdogs.py | 11 +++++------ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dbe14e5..a72a7095 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added * Add support for mounting devices which identify as block device. * Add `check-for-update` command to `wkz` CLI. +### Changed +* When looking for activities on device allow files next to device dir. ### Fixed * Test for mounting devices fails sometimes, because it was not ready for block device mounting. diff --git a/tests/db_tests/conftest.py b/tests/db_tests/conftest.py index 08460448..c09771e9 100644 --- a/tests/db_tests/conftest.py +++ b/tests/db_tests/conftest.py @@ -170,6 +170,8 @@ def __init__(self, mount_path: LocalPath, device_dir, activity_dir: str, activit self.mount_path = mount_path self.device_path = self.mount_path / device_dir self.activity_path_on_device = self.device_path / activity_dir + self.auto_run_file = self.mount_path / "AUTORUN.INF" + self.extra_directory_path_on_device = self.mount_path / "random_extra_dir" self.mounted = False self.activity_files = activity_files @@ -178,6 +180,8 @@ def mount(self): if not self.mounted: print("mounting fake device...") self.activity_path_on_device.mkdir(parents=True) + self.extra_directory_path_on_device.mkdir() + self.auto_run_file.write_text("EMPTY") if self.activity_files: # copy activity files into activity dir copy_demo_fit_files_to_track_dir( @@ -190,7 +194,7 @@ def mount(self): def unmount(self): if self.mounted: print("unmounting fake device...") - shutil.rmtree(self.device_path) + shutil.rmtree(self.mount_path) self.mounted = False diff --git a/tests/db_tests/test_watchdogs.py b/tests/db_tests/test_watchdogs.py index 6db6a96f..a3fc09b4 100644 --- a/tests/db_tests/test_watchdogs.py +++ b/tests/db_tests/test_watchdogs.py @@ -77,6 +77,8 @@ def test_fake_device(db, fake_device, device_dir, activity_dir, fit_file): # now mount the fake device, which should also contain a fit file device.mount() + assert (mount_path / "AUTORUN.INF").is_file() + assert (mount_path / "random_extra_dir").is_dir() assert (mount_path / device_dir).is_dir() assert (mount_path / device_dir / activity_dir).is_dir() assert (mount_path / device_dir / activity_dir / fit_file).is_file() diff --git a/wkz/watchdogs.py b/wkz/watchdogs.py index daa5ab62..87843753 100644 --- a/wkz/watchdogs.py +++ b/wkz/watchdogs.py @@ -29,11 +29,12 @@ def trigger_device_watchdog(): def _watch_for_device(path_to_garmin_device: str, path_to_trace_dir: str, delete_files_after_import: bool): - device_mounted = False if Path(path_to_garmin_device).is_dir(): - sub_dirs = os.listdir(path_to_garmin_device) - if len(sub_dirs) == 1 and not device_mounted: - device_mounted = True + sub_dirs = [] + for filename in os.listdir(path_to_garmin_device): + if (Path(path_to_garmin_device) / filename).is_dir(): + sub_dirs.append(sub_dirs) + if len(sub_dirs) > 0: log.info(f"Found mounted device at {path_to_garmin_device}, triggering fit collector...") fit_collector = FitCollector( path_to_garmin_device=path_to_garmin_device, @@ -41,7 +42,5 @@ def _watch_for_device(path_to_garmin_device: str, path_to_trace_dir: str, delete delete_files_after_import=delete_files_after_import, ) fit_collector.copy_fit_files() - elif len(sub_dirs) == 0: - device_mounted = False else: log.warning(f"Device Watchdog: {path_to_garmin_device} is not a valid directory.")