-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
65 lines (54 loc) · 2.23 KB
/
generator.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
import os
import sys
import json
from jinja2 import Environment, FileSystemLoader
home_dir = '/home/clowder'
data_dir = '/home/clowder/data'
# Exit if extractor_info.json does not exist
if not os.path.isfile(os.path.join(data_dir,'extractor_info.json')):
print("Must have 'extractor_info.json' file present to build extractor!")
sys.exit(1)
# Load extractor_info.json file
with open(os.path.join(data_dir,'extractor_info.json')) as f:
extractor_info = json.load(f)
# Selecting subset of extractor_info related to building Dockerfile
try:
extractor_build_info = extractor_info['build']
except:
print("""
Must specify build section in extractor_info.json!\n
For example:\n
"build": {
"language": "python",
"base": "python:3.8",
"dependencies": {
"pandas":"",
"jinja2":"3.0.1" },
"module": "wordcount",
"function": "wordcount"
}
""")
sys.exit(1)
# Set template environement for jinja2
env = Environment(loader = FileSystemLoader(
os.path.join(home_dir,"templates/{}".format(extractor_build_info['language']))),
trim_blocks=True,
lstrip_blocks=True)
# Create simple_extractor.py
extractor = env.get_template('simple_extractor_template.txt')
extractor_output = extractor.render(extractor_build_info = extractor_build_info)
with open(os.path.join(data_dir,"simple_extractor.py"), "w") as fe:
fe.write(extractor_output)
# Check for requirements.txt
if extractor_build_info['language'].lower() == 'python':
if os.path.isfile(os.path.join(data_dir,'requirements.txt')):
extractor_build_info['requirements.txt'] = True
if 'dependencies' in extractor_build_info:
print("Both requirements.txt file and dependencies in build section of the extractor_info.json file found! Will default to requirements.txt.")
else:
extractor_build_info['requirements.txt'] = False
# Create Dockerfile
dockerfile = env.get_template('docker_template.txt')
dockerfile_output = dockerfile.render(extractor_build_info = extractor_build_info)
with open(os.path.join(data_dir,"Dockerfile"), "w") as fd:
fd.write(dockerfile_output)