-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (160 loc) · 5.11 KB
/
main.py
File metadata and controls
188 lines (160 loc) · 5.11 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import locale
import os
import subprocess
import argvars
import cfg
import config
import dialog
import ewinst
import expand
import log
def check_workspace(ws):
if not ws:
return ws
if not os.path.isabs(ws):
log.die('path not absolute: ' + ws)
if not ws.endswith('.eww'):
ws += '.eww'
return ws
def get_workspace(ws):
def find_eww(dr):
for f in os.listdir(dr):
if f.endswith('.eww'):
return os.path.join(dr, f)
return ''
if not ws:
return ws
ws = os.path.abspath(ws)
if os.path.isdir(ws):
tmp = os.path.join(ws, os.path.basename(ws)) + '.eww'
if os.path.isfile(tmp):
return tmp
tmp2 = find_eww(ws)
if tmp2:
return tmp2
return tmp
if os.path.isfile(ws):
if ws.endswith('.eww'):
return ws
if ws.endswith('.custom_argvars'):
return ws.replace('.custom_argvars', '.eww')
log.die('Unexpected file name:' + ws)
if os.path.isfile(ws + '.eww'):
return ws + '.eww'
return check_workspace(ws)
def create_workspace(ws, exp):
if not os.path.isfile(ws):
dn = os.path.dirname(ws)
if not os.path.isdir(dn):
log.debug('Making directories')
os.makedirs(dn)
log.debug('Writing eww: ' + ws)
with open(ws, 'w', encoding='utf8') as f:
f.write(exp.expand(cfg.workspace_template))
def launch(ws, ew_sel, exp):
if cfg.subcmd == 'shell':
initenv = os.path.join(cfg.ewlaunch_dir, 'init_shell.bat')
exp.setenv()
if cfg.console:
proc = subprocess.Popen('cmd /k ' + initenv, shell=False)
cfg.wait_after_launch = True
else:
proc = subprocess.Popen('start cmd /k ' + initenv, shell=True)
else:
os.chdir(cfg.ewlaunch_dir)
if ew_sel.ide_exe is None:
log.die('EW version does not exist')
cmd_args = [ew_sel.ide_exe]
if ws:
cmd_args.append(ws)
log.debug('Launching: ' + cmd_args[0])
proc = subprocess.Popen(cmd_args, shell=False)
if cfg.wait_after_launch:
proc.wait()
def main():
config.read()
exp = expand.Expand()
arg_vars = argvars.ArgVars()
if cfg.subcmd == 'scan':
ewinst.scan(cfg.rest_args)
ewinst.dump(cfg.out_file)
return
if cfg.reg:
ewinst.add_from_reg()
if cfg.installations:
ewinst.add_from_file(exp.expand(cfg.installations))
if cfg.subcmd == 'dump':
ewinst.dump(cfg.out_file)
return
if cfg.subcmd == 'command':
for ew in ewinst.getlist(cfg.version_filter):
ew.check()
exp.set_ew(ew)
exp.setenv()
if cfg.rest_args:
if not cfg.noheading:
print(ew.key + ':')
args = [os.path.join(
cfg.ewlaunch_dir, 'init_env.bat'), '&&'] + cfg.rest_args
ret = subprocess.run(args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding=locale.getpreferredencoding(),
check=False)
print(ret.stdout.strip())
else:
print(ew.key)
return
ws = get_workspace(cfg.ws)
log.debug('workspace: ' + ws)
using_argvars = False
argvars_ver = None
if cfg.version:
selsrc = 'Version specified on command line'
ew_initial = ewinst.get(cfg.version)
else:
selsrc = ''
ew_initial = None
if os.path.isfile(ws):
argvars_ver = arg_vars.read(ws, exp)
if argvars_ver:
ew_initial = ewinst.get(argvars_ver)
if ew_initial:
selsrc = 'Using version from argvars file'
using_argvars = True
else:
selsrc = 'WARNING: Invalid version in argvars'
selsrc += f' ({argvars_ver}).'
else:
selsrc = 'No version in argvars file. '
if not ew_initial:
ew_initial = ewinst.get(cfg.default_version)
if not ew_initial:
selsrc += 'Default is invalid.'
else:
selsrc += 'Using default value (' + cfg.default_version + ').'
log.debug(selsrc)
ew_sel = ew_initial
if cfg.version:
selected_save = False
else:
selected_save = cfg.default_save
if cfg.always_show_dialog or not using_argvars:
dlg = dialog.Dialog()
if not dlg.show(ws, ew_initial, selsrc):
log.debug('Dialog exit')
return
ws = dlg.ws
ew_sel = dlg.ewi
selected_save = dlg.selected_save
ws = check_workspace(ws)
if not ew_sel:
log.die('could not find EW version')
ew_sel.check()
exp.set_ws(ws)
exp.set_ew(ew_sel)
if ws:
create_workspace(ws, exp)
if selected_save:
arg_vars.save(ew_sel.key, exp)
launch(ws, ew_sel, exp)