-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.py
executable file
·148 lines (129 loc) · 5.11 KB
/
generate.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
143
144
145
146
147
148
#!/usr/bin/env python3
"""Generates files used to create Docker images
"""
import datetime
import algorithm_rgb
# Names of empty files to create
EMPTY_FILE_NAMES = ['requirements.txt', 'packages.txt']
# The name of the Docker build file
DOCKERFILE_NAME = 'Dockerfile'
# Template contents of the Docker build file
DOCKERFILE_CONTENTS = [
'FROM agdrone/rgb-plot-base-image:1.4',
'LABEL maintainer="Someone <[email protected]>"',
'',
'COPY requirements.txt packages.txt /home/extractor/',
'',
'USER root',
'',
'RUN [ -s /home/extractor/packages.txt ] && \\',
' (echo "Installing packages" && \\',
' apt-get update && \\',
' cat /home/extractor/packages.txt | xargs apt-get install -y --no-install-recommends && \\',
' rm /home/extractor/packages.txt && \\',
' apt-get autoremove -y && \\',
' apt-get clean && \\',
' rm -rf /var/lib/apt/lists/*) || \\',
' (echo "No packages to install" && \\',
' rm /home/extractor/packages.txt)',
'',
'RUN [ -s /home/extractor/requirements.txt ] && \\',
' (echo "Install python modules" && \\',
' python -m pip install -U --no-cache-dir pip && \\',
' python -m pip install --no-cache-dir setuptools && \\',
' python -m pip install --no-cache-dir -r /home/extractor/requirements.txt && \\',
' rm /home/extractor/requirements.txt) || \\',
' (echo "No python modules to install" && \\',
' rm /home/extractor/requirements.txt)',
'',
'USER extractor'
'',
'COPY algorithm_rgb.py /home/extractor/'
]
# Required variables in algorithm_rgb
REQUIRED_VARIABLES = [
'ALGORITHM_AUTHOR',
'ALGORITHM_AUTHOR_EMAIL',
'ALGORITHM_NAME',
'ALGORITHM_DESCRIPTION',
'VARIABLE_NAMES'
]
# Variables in algorithm_rgb that are required to not be empty
REQUIRED_NOT_EMPTY_VARIABLES = [
'VARIABLE_NAMES'
]
# Variables in algorithm_rgb that should be filled in, but aren't required to be
PREFERRED_NOT_EMPTY_VARIABLES = [
'ALGORITHM_AUTHOR',
'ALGORITHM_AUTHOR_EMAIL',
'ALGORITHM_NAME',
'ALGORITHM_DESCRIPTION',
'CITATION_AUTHOR',
'CITATION_TITLE',
'CITATION_YEAR'
]
def check_environment() -> bool:
"""Checks that we have the information we need to generate the files
Returns:
Returns True if everything appears to be OK and False if there's a problem detected
"""
# Check for missing definitions
bad_values = []
for one_attr in REQUIRED_VARIABLES:
if not hasattr(algorithm_rgb, one_attr):
bad_values.append(one_attr)
if bad_values:
print("The following variables are not globally defined in algorithm_rgb.py: %s" % ', '.join(bad_values))
print("Please add the variables and try again")
return False
# Check for empty values
for one_attr in REQUIRED_NOT_EMPTY_VARIABLES:
if not getattr(algorithm_rgb, one_attr, None):
bad_values.append(one_attr)
if bad_values:
print("The following variables are empty in algorithm_rgb.py: %s" % ', '.join(bad_values))
print("Please assign values to the variables and try again")
return False
# Warnings
for one_attr in PREFERRED_NOT_EMPTY_VARIABLES:
if not hasattr(algorithm_rgb, one_attr) or not getattr(algorithm_rgb, one_attr, None):
bad_values.append(one_attr)
if bad_values:
print("The following variables are missing or empty when it would be better to have them defined and filled in: %s" % \
','.join(bad_values))
print("Continuing to generate files ...")
return True
def generate_files() -> int:
"""Generated files needed to create a Docker image
Return:
Returns an integer representing success; zero indicates success and any other value represents failure.
"""
try:
for one_name in EMPTY_FILE_NAMES:
# pylint: disable=consider-using-with
open(one_name, 'a', encoding='utf-8').close()
except Exception as ex:
print("Exception caught while attempting to create files: %s" % str(ex))
print("Stopping file generation")
return -1
# Create the Dockerfile
try:
with open(DOCKERFILE_NAME, 'w', encoding='utf-8') as out_file:
out_file.write('# automatically generated: %s\n' % datetime.datetime.now().isoformat())
for line in DOCKERFILE_CONTENTS:
if line.startswith('LABEL maintainer='):
out_file.write("LABEL maintainer=\"{0} <{1}>\"\n".format(algorithm_rgb.ALGORITHM_AUTHOR,
algorithm_rgb.ALGORITHM_AUTHOR_EMAIL))
else:
out_file.write("{0}\n".format(line))
except Exception as ex:
print("Exception caught while attempting to create Docker build file: %s" % str(ex))
print("Stopping build file generation")
return -2
return 0
# Make the call to generate the files
if __name__ == "__main__":
print('Confirming the environment')
if check_environment():
print('Configuring files')
generate_files()