-
Notifications
You must be signed in to change notification settings - Fork 79
/
run.py
executable file
·142 lines (113 loc) · 4.3 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
import argparse
from src.image import recipe
from src.image.image import Image
from src.docker_builder.docker_builder import DockerBuilder
from src.image import paths as p
class _HelpAction(argparse._HelpAction):
def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
# retrieve subparsers from parser
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)
]
# there will probably only be one subparser_action,
# but better save than sorry
for subparsers_action in subparsers_actions:
# get all subparsers and print help
for choice, subparser in subparsers_action.choices.items():
print("Subparser '{}'".format(choice))
print(subparser.format_help())
parser.exit()
def buildImages(paths):
builder = DockerBuilder()
images = []
for image_path in paths:
image = Image.createFromPath(image_path)
image.build_status = builder.build(image)
images.append(image)
return images
def build(args):
all_flag = args.all
paths = []
if (all_flag == True):
paths = p.getAllImagesPath()
else:
images_name = args.images
for image_name in images_name:
paths.append(p.getImagePath(image_name))
images = buildImages(paths)
for image in images:
print("Image: " + image.name + " " + str(image.build_status))
def update(args):
updateFlag = args.yes
all_flag = args.all
paths = []
if (all_flag == True):
paths = p.getAllImagesPath()
else:
images = args.images
for image in images:
paths.append(p.getImagePath(image))
for path in paths:
image = Image.createFromPath(path)
image.update(updateFlag)
def handleRecipe(args):
json_file = args.json
csv_file = args.csv
update = args.update_reference
dictionary = None
if json_file != None:
dictionary = recipe.readJsonFile(json_file)
elif csv_file != None:
dictionary = recipe.parseCsvFile(csv_file)
images = recipe.parseRecipeDict(dictionary)
if update == True:
csv = "Name,Version\n"
for image in images:
image.update(updateFlag=True)
csv += f"{image.name},{image.version}\n"
with open(csv_file, "w") as file:
file.writelines(csv)
else:
builder = DockerBuilder()
for image in images:
image.build_status = builder.build(image)
for image in images:
status = "Fail"
if image.build_status == True:
status = "Success"
print("Image | {:^30} | {:10} ".format(image.name, status))
parser = argparse.ArgumentParser(
description="A tool that builds and updates a set of docker images",
add_help=False)
parser.add_argument("-h",
"--help",
action=_HelpAction,
help="show this help message and exit")
subparsers = parser.add_subparsers()
build_subparser = subparsers.add_parser("build",
help="build one or more images")
build_subparser.add_argument("--all", action="store_true")
build_subparser.add_argument("images", nargs='+', type=str)
build_subparser.set_defaults(func=build)
update_subparser = subparsers.add_parser("update",
help="update one or more image")
update_subparser.add_argument("--all", action="store_true")
update_subparser.add_argument("-y", "--yes", action="store_true")
update_subparser.add_argument("images", nargs='+', type=str)
update_subparser.set_defaults(func=update)
recipe_subparser = subparsers.add_parser("recipe",
help="build images in a recipe file")
recipe_group = recipe_subparser.add_mutually_exclusive_group(required=True)
recipe_group.add_argument("--json", type=str)
recipe_group.add_argument("--csv", type=str)
recipe_subparser.add_argument(
"--update-reference",
help=
"update reference in a recipe file. Run without --update-reference to update the tool(s) afterwards",
action="store_true")
recipe_subparser.set_defaults(func=handleRecipe)
args = parser.parse_args()
args.func(args)