From ecdbca8fb6f40aed2ffce34b6aeb69643e35edd3 Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 2 Dec 2020 16:50:30 +0100 Subject: [PATCH 1/4] Make sync_url work relative to cwd like sane programs --- sync_url.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/sync_url.py b/sync_url.py index ddd239a..c327b9d 100755 --- a/sync_url.py +++ b/sync_url.py @@ -35,19 +35,15 @@ def main() -> None: cookie_jar.load_cookies() - if args.folder is not None: - folder = args.folder - # Initialize pferd at the *parent of the passed folder* - # This is needed so Pferd's internal protections against escaping the working directory - # do not trigger (e.g. if somebody names a file in ILIAS '../../bad thing.txt') - pferd = Pferd(Path(Path(__file__).parent, folder).parent, test_run=args.test_run) - else: - # fetch course name from ilias + folder = args.folder + if args.folder is None: folder = crawler.find_element_name(args.url) cookie_jar.save_cookies() - # Initialize pferd at the location of the script - pferd = Pferd(Path(__file__).parent, test_run=args.test_run) + # files may not escape the pferd_root with relative paths + # note: Path(Path.cwd, Path(folder)) == Path(folder) if it is an absolute path + pferd_root = Path(Path.cwd(), Path(folder)).parent + pferd = Pferd(pferd_root, test_run=args.test_run) def dir_filter(_: Path, element: IliasElementType) -> bool: if args.no_videos: From f3a46634913d4b9d7c21389195d8b10eb5488017 Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 2 Dec 2020 16:58:36 +0100 Subject: [PATCH 2/4] Add passive/no_prompt flag --- PFERD/organizer.py | 17 +++++++++++++---- PFERD/pferd.py | 7 +++++-- sync_url.py | 5 ++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 87bc684..346df76 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -26,7 +26,7 @@ class FileAcceptException(Exception): class Organizer(Location): """A helper for managing downloaded files.""" - def __init__(self, path: Path): + def __init__(self, path: Path, no_prompt: bool = False): """Create a new organizer for a given path.""" super().__init__(path) self._known_files: Set[Path] = set() @@ -36,6 +36,8 @@ class Organizer(Location): self.download_summary = DownloadSummary() + self.not_prompting = no_prompt + def accept_file(self, src: Path, dst: PurePath) -> Optional[Path]: """ Move a file to this organizer and mark it. @@ -67,13 +69,18 @@ class Organizer(Location): if self._is_marked(dst): PRETTY.warning(f"File {str(dst_absolute)!r} was already written!") - if not prompt_yes_no(f"Overwrite file?", default=False): + default_action: bool = False + if self.not_prompting and not default_action \ + or not self.not_prompting and not prompt_yes_no(f"Overwrite file?", default=default_action): PRETTY.ignored_file(dst_absolute, "file was written previously") return None # Destination file is directory if dst_absolute.exists() and dst_absolute.is_dir(): - if prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=False): + default_action: bool = False + if self.not_prompting and default_action \ + or not self.not_prompting \ + and prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=default_action): shutil.rmtree(dst_absolute) else: PRETTY.warning(f"Could not add file {str(dst_absolute)!r}") @@ -144,6 +151,8 @@ class Organizer(Location): def _delete_file_if_confirmed(self, path: Path) -> None: prompt = f"Do you want to delete {path}" - if prompt_yes_no(prompt, False): + default_action: bool = False + if self.not_prompting and default_action or \ + not self.not_prompting and prompt_yes_no(prompt, default_action): self.download_summary.add_deleted_file(path) path.unlink() diff --git a/PFERD/pferd.py b/PFERD/pferd.py index c01b5fd..57b15f6 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -76,12 +76,13 @@ class Pferd(Location): download_strategy: IliasDownloadStrategy, timeout: int, clean: bool = True, + no_prompt: bool = None ) -> Organizer: # pylint: disable=too-many-locals cookie_jar = CookieJar(to_path(cookies) if cookies else None) session = cookie_jar.create_session() tmp_dir = self._tmp_dir.new_subdir() - organizer = Organizer(self.resolve(to_path(target))) + organizer = Organizer(self.resolve(to_path(target)), no_prompt if no_prompt is not None else False) crawler = IliasCrawler(base_url, session, authenticator, dir_filter) downloader = IliasDownloader(tmp_dir, organizer, session, @@ -245,6 +246,7 @@ class Pferd(Location): download_strategy: IliasDownloadStrategy = download_modified_or_new, clean: bool = True, timeout: int = 5, + no_prompt: bool = None ) -> Organizer: """ Synchronizes a folder with a given folder on the ILIAS instance of the KIT. @@ -289,7 +291,8 @@ class Pferd(Location): transform=transform, download_strategy=download_strategy, clean=clean, - timeout=timeout + timeout=timeout, + no_prompt=no_prompt ) self._download_summary.merge(organizer.download_summary) diff --git a/sync_url.py b/sync_url.py index c327b9d..c2ffb93 100755 --- a/sync_url.py +++ b/sync_url.py @@ -21,6 +21,8 @@ def main() -> None: parser.add_argument("--test-run", action="store_true") parser.add_argument('-c', '--cookies', nargs='?', default=None, help="File to store cookies in") parser.add_argument('--no-videos', nargs='?', default=None, help="Don't download videos") + parser.add_argument('-p', '--passive', action="store_true", + help="Don't prompt for confirmations and use sane defaults") parser.add_argument('url', help="URL to the course page") parser.add_argument('folder', nargs='?', default=None, help="Folder to put stuff into") args = parser.parse_args() @@ -57,7 +59,8 @@ def main() -> None: full_url=args.url, cookies=args.cookies, dir_filter=dir_filter, - transform=sanitize_windows_path + transform=sanitize_windows_path, + no_prompt=args.passive ) From 49a0ca7a7c149399c93014a0de9769fd20e050bf Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 2 Dec 2020 16:59:29 +0100 Subject: [PATCH 3/4] Add myself to LICENSE This should've been done back when I added a PR for adding sync_url but people are lazy smh. --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 2e3fa8c..26bcc0a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2019-2020 Garmelon, I-Al-Istannen, danstooamerican, pavelzw +Copyright 2019-2020 Garmelon, I-Al-Istannen, danstooamerican, pavelzw, TheChristophe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in From 6426060804e0212795b59adff85dbced8cff4b5b Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 2 Dec 2020 18:40:45 +0100 Subject: [PATCH 4/4] Fix relative paths bug Introduced in 74ea03945876c94c260b590e6140a7ee50630477 --- sync_url.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sync_url.py b/sync_url.py index c2ffb93..14c2c9e 100755 --- a/sync_url.py +++ b/sync_url.py @@ -37,14 +37,15 @@ def main() -> None: cookie_jar.load_cookies() - folder = args.folder + folder = Path(args.folder) if args.folder is None: - folder = crawler.find_element_name(args.url) + folder = Path(crawler.find_element_name(args.url)) cookie_jar.save_cookies() # files may not escape the pferd_root with relative paths # note: Path(Path.cwd, Path(folder)) == Path(folder) if it is an absolute path pferd_root = Path(Path.cwd(), Path(folder)).parent + folder = folder.name pferd = Pferd(pferd_root, test_run=args.test_run) def dir_filter(_: Path, element: IliasElementType) -> bool: