|
| 1 | +import os |
| 2 | +import zipfile |
| 3 | +import json |
| 4 | +import uuid |
| 5 | + |
| 6 | +# Directory path (current directory) |
| 7 | +cwd = os.getcwd() |
| 8 | + |
| 9 | +def resolve_and_repack(): |
| 10 | + ids_seen = set() |
| 11 | + |
| 12 | + for file in os.listdir(cwd): |
| 13 | + if file.endswith(".mstx"): |
| 14 | + temp_dir = os.path.join(cwd, "temp_extract") |
| 15 | + os.makedirs(temp_dir, exist_ok=True) |
| 16 | + |
| 17 | + try: |
| 18 | + with zipfile.ZipFile(file, 'r') as zip_ref: |
| 19 | + zip_ref.extractall(temp_dir) |
| 20 | + |
| 21 | + config_path = os.path.join(temp_dir, "config.json") |
| 22 | + if os.path.exists(config_path): |
| 23 | + with open(config_path, 'r') as config_file: |
| 24 | + config_data = json.load(config_file) |
| 25 | + |
| 26 | + if 'id' in config_data: |
| 27 | + original_id = config_data['id'] |
| 28 | + |
| 29 | + if original_id in ids_seen: |
| 30 | + # Generate a new UUID for the conflicting ID |
| 31 | + new_id = str(uuid.uuid4()) |
| 32 | + print(f"Conflict detected for ID '{original_id}' in {file}. Replacing with new ID: {new_id}") |
| 33 | + config_data['id'] = new_id |
| 34 | + |
| 35 | + # Update config.json with new ID |
| 36 | + with open(config_path, 'w') as config_file: |
| 37 | + json.dump(config_data, config_file, indent=4) |
| 38 | + |
| 39 | + ids_seen.add(config_data['id']) |
| 40 | + else: |
| 41 | + print(f"Warning: 'id' not found in config.json of {file}") |
| 42 | + |
| 43 | + # Repack the zip file with updated config.json |
| 44 | + new_zip_path = os.path.join(cwd, file) |
| 45 | + with zipfile.ZipFile(new_zip_path, 'w') as new_zip: |
| 46 | + for root, _, files in os.walk(temp_dir): |
| 47 | + for fname in files: |
| 48 | + file_path = os.path.join(root, fname) |
| 49 | + arcname = os.path.relpath(file_path, start=temp_dir) |
| 50 | + new_zip.write(file_path, arcname) |
| 51 | + |
| 52 | + print(f"Repacked and updated {file} successfully.") |
| 53 | + |
| 54 | + except zipfile.BadZipFile: |
| 55 | + print(f"Error: {file} is not a valid zip file.") |
| 56 | + except Exception as e: |
| 57 | + print(f"Error processing {file}: {e}") |
| 58 | + finally: |
| 59 | + # Clean up temporary directory |
| 60 | + for root, _, files in os.walk(temp_dir): |
| 61 | + for fname in files: |
| 62 | + os.remove(os.path.join(root, fname)) |
| 63 | + os.rmdir(temp_dir) |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + resolve_and_repack() |
0 commit comments