|
| 1 | +''' |
| 2 | +Created on May 28, 2020 |
| 3 | +
|
| 4 | +bdio_update_project_name - updates project name referenced in bdio file |
| 5 | +
|
| 6 | +@parameters |
| 7 | +
|
| 8 | +bdio_in - original bdio file |
| 9 | +--bdio-out bdio_ot - output bdio file |
| 10 | +--project-name projetc_name - new Project name |
| 11 | +
|
| 12 | +Invoked without --bdio-out and --project-name will read project name referenced in bdio file |
| 13 | +
|
| 14 | +
|
| 15 | +@author: kumykov |
| 16 | +''' |
| 17 | +import errno |
| 18 | +import json |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import sys |
| 22 | +import zipfile |
| 23 | +from argparse import ArgumentParser |
| 24 | +from zipfile import ZIP_DEFLATED |
| 25 | +from email.policy import default |
| 26 | + |
| 27 | +bdio_in = '/Users/kumykov/Downloads/e9c96cab-8d7c-3247-ac17-fca205fabd62.bdio' |
| 28 | +bdio_out = '/Users/kumykov/Downloads/renamed.bdio' |
| 29 | +workdir = 'workdir' |
| 30 | +inputdir = 'workdir/input' |
| 31 | +outputdir = 'workdir/output' |
| 32 | + |
| 33 | +def zip_extract_files(zip_file, dir_name): |
| 34 | + print("Extracting content of {} into {}".format(zip_file, dir_name)) |
| 35 | + with zipfile.ZipFile(zip_file, 'r') as zip_ref: |
| 36 | + zip_ref.extractall(dir_name) |
| 37 | + |
| 38 | +def zip_create_archive(zip_file, dir_name): |
| 39 | + print ("writing content of {} into {} file".format(dir_name, zip_file)) |
| 40 | + with zipfile.ZipFile(zip_file, mode='w', compression=ZIP_DEFLATED) as zipObj: |
| 41 | + for folderName, subfolders, filenames in os.walk(dir_name): |
| 42 | + for filename in filenames: |
| 43 | + filePath = os.path.join(folderName, filename) |
| 44 | + zipObj.write(filePath, os.path.basename(filePath)) |
| 45 | + |
| 46 | +def read_json_object(filepath): |
| 47 | + with open(os.path.join(workdir,filepath)) as jsonfile: |
| 48 | + data = json.load(jsonfile) |
| 49 | + return data |
| 50 | + |
| 51 | +def write_json_file(filepath, data): |
| 52 | + with open(filepath, "w") as outfile: |
| 53 | + json.dump(data, outfile) |
| 54 | + |
| 55 | +def update_project_name(data, name): |
| 56 | + content_array = data['@graph'] |
| 57 | + |
| 58 | + for counter, array_entry in enumerate(content_array): |
| 59 | + if array_entry['@type'][0] == 'https://blackducksoftware.github.io/bdio#Project': |
| 60 | + #print (counter) |
| 61 | + #print (content_array[counter].keys()) |
| 62 | + if "https://blackducksoftware.github.io/bdio#hasName" in content_array[counter]: |
| 63 | + content_array[counter]["https://blackducksoftware.github.io/bdio#hasName"][0]['@value'] = name |
| 64 | + #print (content_array[counter]) |
| 65 | + |
| 66 | +def get_project_name(data): |
| 67 | + content_array = data['@graph'] |
| 68 | + names = [] |
| 69 | + for counter, array_entry in enumerate(content_array): |
| 70 | + if array_entry['@type'][0] == 'https://blackducksoftware.github.io/bdio#Project': |
| 71 | + #print (counter) |
| 72 | + #print (content_array[counter].keys()) |
| 73 | + if "https://blackducksoftware.github.io/bdio#hasName" in content_array[counter]: |
| 74 | + names.append(content_array[counter]["https://blackducksoftware.github.io/bdio#hasName"][0]['@value']) |
| 75 | + #print (content_array[counter]) |
| 76 | + return names |
| 77 | + |
| 78 | +def setup_workspace(): |
| 79 | + global workdir |
| 80 | + global inputdir |
| 81 | + global outputdir |
| 82 | + try: |
| 83 | + current_dir = os.getcwd() |
| 84 | + workdir = os.path.join(current_dir, 'workdir') |
| 85 | + inputdir = os.path.join(workdir, "input") |
| 86 | + outputdir = os.path.join(workdir, "output") |
| 87 | + if os.path.exists(inputdir): |
| 88 | + shutil.rmtree(inputdir) |
| 89 | + os.makedirs(inputdir) |
| 90 | + if os.path.exists(outputdir): |
| 91 | + shutil.rmtree(outputdir) |
| 92 | + os.makedirs(outputdir) |
| 93 | + except OSError as e: |
| 94 | + if e.errno != errno.EEXIST: |
| 95 | + raise |
| 96 | + |
| 97 | +def cleanup_workspace(): |
| 98 | + try: |
| 99 | + shutil.rmtree(workdir) |
| 100 | + except OSError as e: |
| 101 | + if e.errno != errno.EEXIST: |
| 102 | + raise |
| 103 | + |
| 104 | +def bdio_read_project_name(bdio_in): |
| 105 | + zip_extract_files(bdio_in, inputdir) |
| 106 | + filelist = os.listdir(inputdir) |
| 107 | + names = [] |
| 108 | + for filename in filelist: |
| 109 | + #print ("processing {}".format(filename)) |
| 110 | + filepath_in = os.path.join(inputdir, filename) |
| 111 | + data = read_json_object(filepath_in) |
| 112 | + names.extend(get_project_name(data)) |
| 113 | + return names |
| 114 | + |
| 115 | +def bdio_update_project_name(bdio_in, bdio_out, new_project_name): |
| 116 | + zip_extract_files(bdio_in, inputdir) |
| 117 | + filelist = os.listdir(inputdir) |
| 118 | + for filename in filelist: |
| 119 | + print ("processing {}".format(filename)) |
| 120 | + filepath_in = os.path.join(inputdir, filename) |
| 121 | + filepath_out = os.path.join(outputdir, filename) |
| 122 | + data = read_json_object(filepath_in) |
| 123 | + update_project_name(data,new_project_name) |
| 124 | + write_json_file(filepath_out, data) |
| 125 | + |
| 126 | + zip_create_archive(bdio_out, outputdir) |
| 127 | + |
| 128 | +def main(argv=None): |
| 129 | + |
| 130 | + if argv is None: |
| 131 | + argv = sys.argv |
| 132 | + else: |
| 133 | + sys.argv.extend(argv) |
| 134 | + |
| 135 | + try: |
| 136 | + # Setup argument parser |
| 137 | + parser = ArgumentParser() |
| 138 | + parser.add_argument("bdio_in", help="Path to the original BDIO file") |
| 139 | + parser.add_argument("--bdio-out", default=None, help="Path to the output file to be written") |
| 140 | + parser.add_argument("--project-name", default=None, help="New project name") |
| 141 | + |
| 142 | + args = parser.parse_args() |
| 143 | + |
| 144 | + if not args.bdio_in: |
| 145 | + parser.print_help(sys.stdout) |
| 146 | + sys.exit(1) |
| 147 | + |
| 148 | + if not args.project_name: |
| 149 | + if not args.bdio_out: |
| 150 | + setup_workspace() |
| 151 | + print ("Project names found {}".format(set(bdio_read_project_name(args.bdio_in)))) |
| 152 | + cleanup_workspace() |
| 153 | + else: |
| 154 | + parser.print_help(sys.stdout) |
| 155 | + sys.exit(1) |
| 156 | + else: |
| 157 | + if args.bdio_out: |
| 158 | + setup_workspace() |
| 159 | + bdio_update_project_name(args.bdio_in, args.bdio_out, args.project_name) |
| 160 | + cleanup_workspace() |
| 161 | + else: |
| 162 | + parser.print_help(sys.stdout) |
| 163 | + sys.exit(1) |
| 164 | + |
| 165 | + return 0 |
| 166 | + except Exception as e: |
| 167 | + pass |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + sys.exit(main()) |
0 commit comments