-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
164 lines (132 loc) · 4.35 KB
/
compile.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import json
import logging
import logging.config
import os, sys
from hammerhal.compilers import HeroCompiler, AdversaryCompiler
from hammerhal import ConfigLoader
def setup_logging(
default_path='configs/logging.json',
default_level=logging.INFO,
env_key='LOG_CFG'
):
"""Setup logging configuration
"""
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
def start(argv=sys.argv):
# Configure logger
setup_logging()
logger = logging.getLogger('hammerhal')
logger.info("Logger started")
ConfigLoader.load_configs()
if (argv == sys.argv):
if (len(argv) > 1):
args = argv[1:]
else:
logger.error("Command is required")
print_help(logger)
return 2
elif (len(argv) > 0):
args = argv
else:
logger.error("Command is required")
print_help(logger)
return 2
return run_command(logger, args)
def run_command(logger, args):
command = args[0].strip().lower()
arg = (' '.join(args[1:]) if len(args) > 1 else '').strip().lower()
if (command in commands):
logger.debug("Running command '{command}' with arguments '{arg}'".format(command=command, arg=arg))
return commands[command](logger, arg)
elif (command == 'exit'):
logger.debug("Exiting now")
return 0
else:
logger.error("Unsupported command: '{command}'".format(command=command))
print_help(logger)
return 3
def compile_all(logger, *args):
e_code = 0
e_code += compile_heroes(logger)
e_code += compile_adversaries(logger)
return 1 if e_code else 0
def compile_heroes(logger):
compiler = HeroCompiler()
heroes = compiler.search()
e_code = 0
logger.info("Gonna to compile heroes. There are {n} to deal with.".format(n=len(heroes)))
for hero in heroes:
e_code += compile_hero(logger, hero, compiler)
return e_code
def compile_hero(logger, hero, compiler=None):
if (not compiler):
compiler = HeroCompiler()
result = compiler.open(hero) and compiler.compile() and compiler.save(forced_width=720)
if (result):
logger.info("Success! File saved to '{filename}'".format(filename=result))
return 0
else:
logger.error("Cannot compile {hero}".format(hero=hero))
return 1
def compile_adversaries(logger):
compiler = AdversaryCompiler()
adversaries = compiler.search()
e_code = 0
logger.info("Gonna to compile adversaries. There are {n} to deal with.".format(n=len(adversaries)))
for adversary in adversaries:
e_code += compile_adversary(logger, adversary, compiler)
return e_code
def compile_adversary(logger, adversary, compiler=None):
if (not compiler):
compiler = AdversaryCompiler()
result = compiler.open(adversary) and compiler.compile() and compiler.save(forced_width=1080)
if (result):
logger.info("Success! File saved to '{filename}'".format(filename=result))
return 0
else:
logger.error("Cannot compile {adversary}".format(adversary=adversary))
return 1
def run_interactive(logger, *args):
while True:
print('> ', end='')
command = input()
_args = [ s for s in command.split() if s ]
run_command(logger, _args)
if (_args == [ "exit" ]):
return 0
print('')
def print_help(logger, *kwargs):
print("""
Hammerhal datasheet compiler tool v0.1
Usage:
python compile.py COMMAND [ ARGUMENT ]
Available commands (case-insensitive):
all Compiles all heroes and adversaries.
hero Compiles a specific hero (argument required).
adversary Compiles a specific adversary (argument required).
interactive Launches compiler in the interactive mode.
exit Exits the interactive mode.
help, ? Prints this message.
(c) 2017, USSX Hares, MIT License""")
return 0
commands = \
{
'all': compile_all,
'hero': compile_hero,
'adversary': compile_adversary,
'interactive': run_interactive,
'help': print_help,
'?': print_help
}
if (__name__ == "__main__"):
e_code = start()
exit(e_code)