Create script cleanapp.py that: Take a zip archive name as an argument: python…
- User
Create script clean_app.py that:
Take a zip archive name as an argument:
python clean_app.py
Unzip the archive to a temp directory (using the appropriate Python library)
Remove folders that don't contain init.py. Only the root folder can be without init.py.
Add cleaned.txt with a sorted list of the removed folders. Folders should be listed with relative paths. One folder per line. For instance:
example1\handlers\actions example3 …
A folder without init.py has to be removed even if its child folders contain init.py. In this case, only the parent folder name has to be output to cleaned.txt.
cleaned.txt has to be added even if no folders are removed.
Create new archive with _new prefix (<old_name>_new.zip)
Add logging to the script.
- Assistant
Certainly! Below is the script
clean_app.pythat accomplishes the tasks described:import os import sys import zipfile import tempfile import shutil import logging def setup_logging(): logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def unzip_to_temp(zip_file_path): temp_dir = tempfile.mkdtemp() with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: zip_ref.extractall(temp_dir) return temp_dir def find_and_remove_non_init_folders(directory): removed_folders = [] for root, dirs, files in os.walk(directory, topdown=False): if root == directory: continue if '__init__.py' not in files: removed_folders.append(os.path.relpath(root, directory)) shutil.rmtree(root) else: for dr in dirs[:]: if '__init__.py' not in os.listdir(os.path.join(root, dr)): removed_folders.append(os.path.relpath(os.path.join(root, dr), directory)) shutil.rmtree(os.path.join(root, dr)) return removed_folders def write_cleaned_txt(directory, removed_folders): removed_folders.sort() cleaned_txt_path = os.path.join(directory, 'cleaned.txt') with open(cleaned_txt_path, 'w') as f: for folder in removed_folders: f.write(f"{folder}\n") return cleaned_txt_path def create_new_archive(temp_dir, original_zip_filename): zip_filename_new = f"{os.path.splitext(original_zip_filename)[0]}_new.zip" with zipfile.ZipFile(zip_filename_new, 'w') as new_zip: for foldername, _, filenames in os.walk(temp_dir): relative_folder = os.path.relpath(foldername, temp_dir) for filename in filenames: new_zip.write( os.path.join(foldername, filename), os.path.join(relative_folder, filename) ) return zip_filename_new def clean_zip_archive(zip_file_name): setup_logging() logging.info("Started cleaning process for ZIP archive: %s", zip_file_name) temp_dir = unzip_to_temp(zip_file_name) logging.info("Unzipped archive to temporary directory: %s", temp_dir) removed_folders = find_and_remove_non_init_folders(temp_dir) if removed_folders: logging.info("Removed the following folders: %s", removed_folders) else: logging.info("No folders were removed.") cleaned_txt_path = write_cleaned_txt(temp_dir, removed_folders) logging.info("Created cleaned.txt at: %s", cleaned_txt_path) new_zip_path = create_new_archive(temp_dir, zip_file_name) logging.info("Created new ZIP archive: %s", new_zip_path) shutil.rmtree(temp_dir) logging.info("Cleaned up temporary directory: %s", temp_dir) logging.info("Cleaning process completed.") if __name__ == "__main__": if len(sys.argv) != 2: print(f"Usage: python {os.path.basename(__file__)} <zip-file name>") sys.exit(1) zip_file_name = sys.argv[1] clean_zip_archive(zip_file_name)Explanation:
- Logging Setup: Configures logging to provide information on the script's progress.
- Unzip Function: Unzips the given archive into a temporary directory.
- Folder Clean-Up: Traverses the directory tree and removes folders lacking an
__init__.py. - Writing
cleaned.txt: Creates acleaned.txtfile listing all removed folders. - Creating New Archive: Zips the cleaned directory into a new archive with a
_newsuffix. - Main Functionality: Executes the main flow and handles the command line argument.
This script should be run in a command line or terminal with the appropriate Python environment. Ensure you have
zipfile,tempfile,shutil,os,sys, andloggingmodules available in your Python installation. - User
New Task
- Assistant
Of course! What specific task or functionality would you like to add or change in the script? Please provide details so I can assist you effectively.
- User
In bash:
Task Develop a script that takes a string as an argument and returns its reverse version, changing uppercase letters to lowercase and back
Check ./loops1.sh "Hello World"
DLROw OLLEh
./loops1.sh "bash"
HSAB
./loops1.sh "EPAM"
mape
- Assistant
Certainly! Below is a bash script named
loops1.shthat takes a string as an argument and returns its reverse version, changing uppercase letters to lowercase and vice versa.#!/bin/bash if [ "$#" -ne 1 ]; then echo "Usage: $0 <string>" exit 1 fi input="$1" reversed="" # Loop through the input string in reverse order for ((i=${#input}-1; i>=0; i--)); do char="${input:i:1}" # Check if character is uppercase if [[ $char =~ [A-Z] ]]; then # Convert to lowercase reversed+=$(echo "$char" | tr 'A-Z' 'a-z') elif [[ $char =~ [a-z] ]]; then # Convert to uppercase reversed+=$(echo "$char" | tr 'a-z' 'A-Z') else reversed+="$char" fi done echo "$reversed"Explanation:
- Usage Check: The script first checks if exactly one argument is passed. If not, it prints the usage and exits.
- Input String and Reversed String Initialization: It then initializes the input string and an empty string for the reversed result.
- Looping Through the String in Reverse: The script loops through the input string in reverse order.
- Character Case Conversion: For each character:
- If the character is uppercase, it converts it to lowercase.
- If the character is lowercase, it converts it to uppercase.
- If the character is neither (e.g., space, punctuation), it is added as-is.
- Output: Finally, the script prints the reversed string with the character cases swapped.
To make the script executable, you can run:
chmod +x loops1.shAnd then you can test it with your examples:
./loops1.sh "Hello World" # DLROw OLLEh ./loops1.sh "bash" # HSAB ./loops1.sh "EPAM" # mape