|
| 1 | +''' |
| 2 | +Created on Nov 8, 2019 |
| 3 | +
|
| 4 | +@author: ylei |
| 5 | +
|
| 6 | +Delete multiple projects and their scans. The project names to be deleted are kept in clean_project.json |
| 7 | +''' |
| 8 | + |
| 9 | +import argparse |
| 10 | +import csv |
| 11 | +import logging |
| 12 | +import sys |
| 13 | +import json |
| 14 | +import arrow |
| 15 | + |
| 16 | +from blackduck.HubRestApi import HubInstance |
| 17 | + |
| 18 | +parser = argparse.ArgumentParser("A program that will delete projects along with their scans") |
| 19 | +#parser.add_argument("project", help="Project name") |
| 20 | +parser.add_argument("-f", "--filename", default="clean_project.json", help="File to keep Project names") |
| 21 | +parser.add_argument("-k", "--keep_scans", action = 'store_true', default=False, help="Use this option if you want to keep scans associated with the project-versions. Default is False, scans will be deleted.") |
| 22 | +parser.add_argument("-b", "--backup_scans", action = 'store_true', default=False, help="Use this option if you want to backup scans associated with the project-versions. Default is False, scans will not be backuped.") |
| 23 | +parser.add_argument("-a", "--age", default=3, help="The age, in days. If a project is older than this age it will be deleted.") |
| 24 | +args = parser.parse_args() |
| 25 | + |
| 26 | +logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 27 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 28 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 29 | + |
| 30 | +hub = HubInstance() |
| 31 | + |
| 32 | +filename = args.filename |
| 33 | +with open(filename, 'r+') as f: |
| 34 | + d = json.load(f) |
| 35 | +print("Projects in maintain json file: ", d['clean_project']) |
| 36 | +for proj in d['clean_project']: |
| 37 | + now = arrow.now() |
| 38 | + clean_time = now.to('local').shift(days=-args.age) |
| 39 | + project = hub.get_project_by_name(proj) |
| 40 | + |
| 41 | + if clean_time > arrow.get(project['createdAt']).to('local'): |
| 42 | + print("Project ", project['name'], ' last for more than 3 days. Will be backup abd deleted.') |
| 43 | + hub.delete_project_by_name(proj, save_scans=args.keep_scans, backup_scans=args.backup_scans) |
0 commit comments