diff --git a/README.md b/README.md index af462e6..34a158f 100644 --- a/README.md +++ b/README.md @@ -5,37 +5,39 @@ 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 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 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 slash. +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 slash. **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. ## Golang version diff --git a/butler.py b/butler.py index 51931dd..51574c4 100644 --- a/butler.py +++ b/butler.py @@ -30,38 +30,44 @@ ".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 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: + 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) - 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): @@ -70,11 +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: - if ".zip" in filename: - pass + if "butler" in filename: + print("Skipped " + filename) 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(