-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatron.py
109 lines (78 loc) · 2.61 KB
/
automatron.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
#!/usr/bin/env/python
"""
automatron.py :
Automatron is a Python Batch Script automatic code generation tool.
"""
__author__ = "Mohit Kumar"
__credits__ = ["Mohit Kumar"]
__version__ = "1.0.0"
__maintainer__ = "Mohit Kumar"
__email__ = "[email protected]"
__status__ = "Production"
import os
import sys
import argparse
import yaml
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument( '-c', '--create', help='Name of the job.' )
args = argument_parser.parse_args()
CONFIG_FILE_NAME = 'config.yaml'
SNIPPETS_DIR = "snippets"
BASE_DIR = "base"
SNIPPET_FILE_NAMES = (
"imports.py",
"globals.py",
"methods.py",
)
SNIPPETS_PATH = None
BASIC_CONFIG = {
"logging": "DEBUG"
}
HEADER_SNIPPET = """#!/usr/bin/env/python
\"\"\" %s : This script is generated by Automatron \"\"\"
__author__ = "John Doe"
__credits__ = ["John Doe"]
__version__ = "1.0.0"
__maintainer__ = "John Doe"
__email__ = "[email protected]"
__status__ = "Prototype"
"""
TAILER_SNIPPET = """
if __name__ == "__main__":
initialize()
execute()
finalize()
"""
def fetch_env_path():
""" Method to fetch and populate the global value in SNIPPETS_PATH """
global SNIPPETS_PATH
path_data = os.environ['PATH']
SNIPPETS_PATH = [ x for x in path_data.split(';') if 'automatron' in x ][0]
def create_config_file():
""" Method to create a configuration file """
with open( os.path.join( JOB_NAME, CONFIG_FILE_NAME ), 'w' ) as config_file:
data = yaml.dump( BASIC_CONFIG, config_file )
print( f"Created the configuration file : { JOB_NAME }/{ CONFIG_FILE_NAME }" )
def create_basic_file():
""" Method to create a basic python batch script """
with open( os.path.join( JOB_NAME, JOB_NAME+'.py' ), "w" ) as mainScript:
mainScript.write( HEADER_SNIPPET%( args.create ) )
for SNIPPET_FILE in SNIPPET_FILE_NAMES:
with open( os.path.join( SNIPPETS_PATH, SNIPPETS_DIR, BASE_DIR, SNIPPET_FILE ) ) as snippet_file:
mainScript.write( "".join( snippet_file.readlines() ) )
mainScript.write( "\n\n" )
mainScript.write( TAILER_SNIPPET )
print( f"Created Job Script : { JOB_NAME }/{ JOB_NAME }.py" )
if __name__ == "__main__":
global JOB_NAME
if args.create:
JOB_NAME = args.create
print( f"Starting project generation for Job Name : { JOB_NAME }" )
if not os.path.exists( JOB_NAME ):
os.mkdir( JOB_NAME )
fetch_env_path()
create_config_file()
create_basic_file()
else:
print( f"No job name provided, exiting!" )
sys.exit( os.EX_NOINPUT )