-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__main__.py
110 lines (92 loc) · 3.24 KB
/
__main__.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
import argparse
import logging
import sys
import os
import json
depsPath = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, depsPath)
os.environ["PYTHONUNBUFFERED"] = "1"
import Zoe
from Zoe.utils import installSignalHandler
import Zoe.work
logger = None
def setupLogging():
global logger
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s | %(levelname)s | %(name)s | %(message)s', handlers=[
#logging.FileHandler("my_log.log", mode='w'),
stream_handler
])
logger = logging.getLogger('zoe')
if not os.environ.get('RUNNING_AS_WINDOWS_SERVICE', None) and not os.environ.get('NO_COLORLOG', None):
# only use color logs if not running as windows service
try:
import coloredlogs
coloredlogs.install(logging.INFO, fmt='%(asctime)s | %(levelname)s | %(name)s | %(message)s')
except ImportError:
pass
def loadDotEnv():
try:
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(filename=".env.default"))
load_dotenv(find_dotenv(filename=".env"))
except Exception as e:
logger.exception(e)
def loadLocalConfig():
try:
from uuid import uuid4
from appdirs import user_data_dir
appConfigPath = user_data_dir('Zoe', 'BeamNG')
jsonFilename = os.path.join(appConfigPath, 'config.json')
data = None
if os.path.exists(jsonFilename):
try:
with open(jsonFilename, 'r') as f:
data = json.load(f)
except:
pass
if data is None:
uuid = uuid4().hex
logger.info('Generated new UUID for this machine: {}'.format(uuid))
data = { 'machine_uuid': uuid }
data['zoe_version'] = Zoe.__version__
os.makedirs(appConfigPath, exist_ok = True)
with open(jsonFilename, 'w') as f:
json.dump(data, f, sort_keys=True, indent=2)
return data
except Exception as e:
logger.exception(e)
return {}
def zoeMain():
setupLogging()
logger.info(f"===== Welcome to Zoe v{Zoe.__version__} =====")
loadDotEnv()
parser = argparse.ArgumentParser(prog='zoe', description='The Zoe client and execution program suit')
# mode flags
parser.add_argument("jobfile", help="job filename to process", default=None, nargs='?')
# boolean flags
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("-q", "--quiet", help="decrease output verbosity", action="store_true")
parser.add_argument("-l", "--local", help="offline mode. No communication with the server.", action="store_true")
parser.add_argument("-u", "--autoupdate", help="Enable automatic updates", action="store_true")
args = parser.parse_args()
if args.verbose:
logger.setLevel(logging.DEBUG)
if args.quiet:
logger.setLevel(logging.ERROR)
env = loadLocalConfig()
env['autoupdate'] = args.autoupdate
if args.local:
env['localMode'] = True
if not args.jobfile:
logger.error('Local mode is not available when running as executor')
return 1
ex = Zoe.work.Executor(env)
if args.jobfile:
return ex.executeLocalJobs(args.jobfile.strip())
else:
installSignalHandler()
return ex.serveForever()
if __name__ == "__main__":
sys.exit(zoeMain())