Created
October 31, 2023 22:16
-
-
Save arthur-mts/fb00a5fc41c92b9a6cc2d1efd46d8b51 to your computer and use it in GitHub Desktop.
uuid_replacer helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import argparse | |
from uuid import uuid4 | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(prog = "uuid_replacer", description="Update all UUIDs from JSON file for random UUIDs") | |
parser.add_argument("input", help = ".json file name") | |
parser.add_argument("-o", "--output", help = "output filename. default = output.json", default="output.json", dest="output", required=False) | |
parser.add_argument("-i", "--ids", help="UUIDs to be replaced. (If ommited all UUIDs will be replaced)", required = False, nargs="*", action="store") | |
args = parser.parse_args() | |
file = open(args.input) | |
data = file.read() | |
file.close() | |
if (args.ids != None and len(args.ids) > 0): | |
ids = list(set(args.ids)) | |
else: | |
ids = list(set(re.findall(r'[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}', data))) | |
for c_id in ids: | |
data = re.sub(re.compile(str(c_id)), str(uuid4()), data) | |
output_file = open(args.output, 'w') | |
output_file.write(data) | |
output_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment