Guest User

Untitled

a guest
Feb 15th, 2023
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | Source Code | 0 0
  1. import os
  2. import re
  3. import argparse
  4. import shutil
  5. import zipfile
  6. from tqdm import tqdm
  7. import hashlib
  8. import base64
  9.  
  10. def base64_hash(s):
  11.     b64 = base64.b64encode(s.encode()).decode()
  12.     hash_str = ''.join(filter(lambda c: not c.isdigit(), b64))
  13.     return hash_str[:16].lower()
  14.  
  15.  
  16. def md5_hash_without_digits(text: str) -> str:
  17.     # Convert text to bytes and calculate MD5 hash
  18.     hash_object = hashlib.md5(text.encode())
  19.  
  20.     # Get hex representation of the hash, remove digits, and lowercase
  21.     hash_str = hash_object.hexdigest()
  22.     hash_str_no_digits = hash_str
  23.     # hash_str_no_digits = ''.join(c for c in hash_str if not c.isdigit())
  24.     hash_str_no_digits_lower = hash_str_no_digits.lower()
  25.  
  26.     # Return the first 8 characters of the hash string
  27.     return hash_str_no_digits_lower
  28.  
  29.  
  30. def process_js_files(extracted_folder):
  31.     # Step 2: Find all files in the specified folder that meet the *_interface.js mask
  32.     files_to_process = []
  33.     for root, dirs, files in os.walk(extracted_folder):
  34.         for file in files:
  35.             if file.endswith("_interface.js"):
  36.                 files_to_process.append(os.path.join(root, file))
  37.  
  38.     # Step 3: Map id and description inside a dictionary
  39.     dictionary = {}
  40.     for file_path in tqdm(files_to_process, desc="Processing files"):
  41.         with open(file_path, "r", encoding='utf-8') as f:
  42.             file_name = os.path.basename(file_path)
  43.             prefix = file_name.split("_")[0:-1]  # Get all parts of the filename before "_interface.js"
  44.             prefix = "_".join(prefix)
  45.             content = f.read()
  46.             matches = re.findall(r"\{id:\"(\w+)\", description:\"(\w+)\"", content)
  47.             for match in matches:
  48.                 if match[0] == 'Save':
  49.                     continue
  50.  
  51.                 dictionary[match[0]] = base64_hash(md5_hash_without_digits(match[1] + "_" + prefix))
  52.  
  53.     # Step 5: Find all js files inside folder and replace all substrings represented in dictionary as keys with their values inside dictionary
  54.     for root, dirs, files in os.walk(extracted_folder):
  55.         for file_path in tqdm(files, desc="Replacing substrings"):
  56.             if file_path.endswith(".js"):
  57.                 with open(os.path.join(root, file_path), "r+", encoding='utf-8') as f:
  58.                     content = f.read()
  59.                     for key in dictionary:
  60.                         content = content.replace(key, dictionary[key])
  61.                     f.seek(0)
  62.                     f.write(content)
  63.                     f.truncate()
  64.  
  65.  
  66. def create_updated_archive(input_archive_path):
  67.     # Step 1: Unzip the archive and get the path to the folder containing the JS files
  68.     with zipfile.ZipFile(input_archive_path, "r") as zip_ref:
  69.         extracted_folder = zip_ref.namelist()[0]
  70.         extracted_folder = os.path.join("temp", extracted_folder)
  71.         zip_ref.extractall("temp")
  72.  
  73.     process_js_files(extracted_folder)
  74.  
  75.     # Step 6: Zip the updated files and replace the original archive
  76.     output_archive_path = input_archive_path[:-4] + "_updated.zip"
  77.     with zipfile.ZipFile(output_archive_path, "w") as zip_ref:
  78.         for root, dirs, files in os.walk(extracted_folder):
  79.             for file in files:
  80.                 file_path = os.path.join(root, file)
  81.                 archive_path = os.path.join(input_archive_path[:-4], file_path[len(extracted_folder):])
  82.                 zip_ref.write(file_path, arcname=archive_path)
  83.  
  84.     # Clean up temporary files
  85.     shutil.rmtree("temp")
  86.  
  87.     # Replace the original archive with the updated archive
  88.     os.remove(input_archive_path)
  89.     os.rename(output_archive_path, input_archive_path)
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     # Parse command line arguments
  94.     parser = argparse.ArgumentParser(description="Process JS files inside a ZIP archive.")
  95.     parser.add_argument("archive", help="path to the ZIP archive containing the folder with JS files")
  96.     args = parser.parse_args()
  97.  
  98.     specified_archive_path = args.archive
  99.  
  100.     create_updated_archive(specified_archive_path)
Add Comment
Please, Sign In to add comment