From a03519abdcb8c63cd7e3e24cebf14dc4054725fd Mon Sep 17 00:00:00 2001 From: CoolCoderCarl <123qwekir2wl@gmail.com> Date: Wed, 18 May 2022 01:14:42 +0300 Subject: [PATCH 1/4] Remove useless messages; add archive ignoring --- butler.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/butler.py b/butler.py index 51931dd..1383c27 100644 --- a/butler.py +++ b/butler.py @@ -30,37 +30,40 @@ ".allow", ] +archives_extension = [".zip", ".7z", ".gz", ".bz", ".gzip", ".bzip"] + target_dir_name = "ALL" -def clean_the_dir(directory_path: str): - for files in os.listdir(directory_path): - path = os.path.join(directory_path, files) - if "butler" in (path.split("/")[-1]): - print("Skipped " + path.split("/")[-1]) - else: - try: - shutil.rmtree(path) - except OSError: - os.remove(path) +def clean_the_dir(path_to_clean: str): + if args.clean == "/": + print("It is totally not great idea to remove all things") + exit(1) + else: + for files in os.listdir(path_to_clean): + path = os.path.join(path_to_clean, files) + if "butler" in (path.split("/")[-1]): + print("Skipped " + path.split("/")[-1]) + else: + try: + shutil.rmtree(path) + except OSError: + os.remove(path) def group_up_files(new_dir_name: str): - print(new_dir_name) for file in os.listdir(args.dir): for ext in files_extension: if file.endswith(ext): file_path = os.path.join(args.dir, file) - print(file_path) if args.dir == ".": new_dir_path = new_dir_name + ext.upper() else: new_dir_path = args.dir + new_dir_name + ext.upper() - print(new_dir_path) try: os.mkdir(new_dir_path) - except OSError as os_error: - print(os_error) + except OSError: + pass shutil.move(file_path, new_dir_path) @@ -70,11 +73,12 @@ def create_archive(dir_to_archive: str): with ZipFile(str(date_time) + ".zip", "w") as zip_obj: for folder_name, sub_folders, filenames in os.walk(dir_to_archive): for filename in filenames: - if ".zip" in filename: - pass - else: - zip_path = os.path.join(folder_name, filename) - zip_obj.write(zip_path, basename(zip_path)) + for a_ext in archives_extension: + if filename.endswith(a_ext): + pass + else: + zip_path = os.path.join(folder_name, filename) + zip_obj.write(zip_path, basename(zip_path)) parser = argparse.ArgumentParser( From a295d8efe40b0899db2a8c6dd267ddabbaf4d9d0 Mon Sep 17 00:00:00 2001 From: CoolCoderCarl <123qwekir2wl@gmail.com> Date: Wed, 18 May 2022 01:19:37 +0300 Subject: [PATCH 2/4] Add skipping for other functions --- butler.py | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/butler.py b/butler.py index 1383c27..51574c4 100644 --- a/butler.py +++ b/butler.py @@ -40,8 +40,8 @@ def clean_the_dir(path_to_clean: str): print("It is totally not great idea to remove all things") exit(1) else: - for files in os.listdir(path_to_clean): - path = os.path.join(path_to_clean, files) + for filename in os.listdir(path_to_clean): + path = os.path.join(path_to_clean, filename) if "butler" in (path.split("/")[-1]): print("Skipped " + path.split("/")[-1]) else: @@ -52,19 +52,22 @@ def clean_the_dir(path_to_clean: str): def group_up_files(new_dir_name: str): - for file in os.listdir(args.dir): - for ext in files_extension: - if file.endswith(ext): - file_path = os.path.join(args.dir, file) - if args.dir == ".": - new_dir_path = new_dir_name + ext.upper() - else: - new_dir_path = args.dir + new_dir_name + ext.upper() - try: - os.mkdir(new_dir_path) - except OSError: - pass - shutil.move(file_path, new_dir_path) + for filename in os.listdir(args.dir): + if "butler" in filename: + print("Skipped " + filename) + else: + for ext in files_extension: + if filename.endswith(ext): + file_path = os.path.join(args.dir, filename) + if args.dir == ".": + new_dir_path = new_dir_name + ext.upper() + else: + new_dir_path = args.dir + new_dir_name + ext.upper() + try: + os.mkdir(new_dir_path) + except OSError: + pass + shutil.move(file_path, new_dir_path) def create_archive(dir_to_archive: str): @@ -73,12 +76,15 @@ def create_archive(dir_to_archive: str): with ZipFile(str(date_time) + ".zip", "w") as zip_obj: for folder_name, sub_folders, filenames in os.walk(dir_to_archive): for filename in filenames: - for a_ext in archives_extension: - if filename.endswith(a_ext): - pass - else: - zip_path = os.path.join(folder_name, filename) - zip_obj.write(zip_path, basename(zip_path)) + if "butler" in filename: + print("Skipped " + filename) + else: + for a_ext in archives_extension: + if filename.endswith(a_ext): + pass + else: + zip_path = os.path.join(folder_name, filename) + zip_obj.write(zip_path, basename(zip_path)) parser = argparse.ArgumentParser( From cdcb6ce077492b8c5546e987ff4e8007fcba33e6 Mon Sep 17 00:00:00 2001 From: CoolCoderCarl <123qwekir2wl@gmail.com> Date: Wed, 18 May 2022 01:23:04 +0300 Subject: [PATCH 3/4] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index af462e6..66c832e 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ The author was tired of watching how a dump is constantly formed in his Download For this purpose this program was developed in the Python language. -In fact, this program is a working prototype for a similar program in golang, but you can already use it now. +In fact, this program is a working prototype for a similar Golang program, but you can already use it now. -You can download it and enjoy +Enjoy. ## How to use @@ -25,17 +25,17 @@ In case the first and third functions are already familiar, grouping may seem li ### In order **Cleaning** `butler.exe --clean /path/to/dir/` -Clean target dir. It is important to close slash. +Clean target dir. It is important to close slashes. **Grouping** `butler.exe --dir /path/to/dir/` Group up files by extensions to new directory named *ALL*.*EXT*. -For example, you have a lot of *.exe* files, after this command you will have one directory called *ALL.EXE* with all your *.exe* files in it. -It is important to close slash. +For example, you have a lot of *.exe* files, after this command you will have one directory called *ALL.EXE* with all your *.exe* files in it. +It is important to close slashes. **Archiving** `butler.exe --archive /path/to/dir/` -Create zip archive in current directory for target directory. It is important to close slash. +Create zip archive in current directory for target directory. It is important to close slashes. ## Golang version From 52555277cf0b06e2c450dfaab86157be7464ca9b Mon Sep 17 00:00:00 2001 From: CoolCoderCarl <123qwekir2wl@gmail.com> Date: Wed, 18 May 2022 01:31:41 +0300 Subject: [PATCH 4/4] Update README.md --- README.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 66c832e..34a158f 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ Clean your castle ## Prehistory This CLI program provides functions for maintaining order on a server or home computer. -The author was tired of watching how a dump is constantly formed in his Downloads directory (and not only) and he decided to end it. +> How tired I am of looking at this trash in Downloads. +> (c) Author -For this purpose this program was developed in the Python language. +To fix this problem this program was developed in the Python language. In fact, this program is a working prototype for a similar Golang program, but you can already use it now. @@ -16,26 +17,27 @@ Enjoy. ## How to use The program provides three functions: -**Cleaning** -**Grouping** -**Archiving** +1. **Cleaning** +2. **Grouping** +3. **Archiving** In case the first and third functions are already familiar, grouping may seem like an interesting function. -### In order +**It is important to close slashes.** + +### Examples **Cleaning** `butler.exe --clean /path/to/dir/` -Clean target dir. It is important to close slashes. +Clean target dir but not delete it. **Grouping** `butler.exe --dir /path/to/dir/` Group up files by extensions to new directory named *ALL*.*EXT*. -For example, you have a lot of *.exe* files, after this command you will have one directory called *ALL.EXE* with all your *.exe* files in it. -It is important to close slashes. +For example, you have a lot of *.exe* files, after this command you will have one directory called *ALL.EXE* with all your *.exe* files in it. **Archiving** `butler.exe --archive /path/to/dir/` -Create zip archive in current directory for target directory. It is important to close slashes. +Create zip archive in current directory for target directory. ## Golang version