-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack.py
231 lines (183 loc) · 6.36 KB
/
jack.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#! /usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 13 17:48:20 2013
@author: gav
Module: jack.py
Description: The interactive commandline client
This is really a learning platform for interactive tools, but as tools develop
they will spun off, or incorporated further.
The first tool to be included is the check_winfates_results, with cross-machine
search and file movement extensions.
"""
from __future__ import print_function
import sys
import os
import os.path as osp
from pprint import pprint
from fnmatch import filter as fnfilter
from cmd2 import Cmd
### Constants
### Classes
def new_fetch_record():
"""Return a default fetch record (dict) for filling in
type: fetch - from Jack perhaps
header: Contains everything needed to generate the body
paths: List of directories to search
patterns: List of patterns to apply
match_type: glob or regex
clobber: Overwrite files? default: True
cleanup: Delete files after they are moved? default - False for the moment
body: The payload to be executed or fetched
dest_dir: The directory to move files to
files:
"""
return {"type": "fetch",
"header": {
"paths": [],
"patterns": [],
"match_type": "glob",
"clobber": True,
"cleanup": False,
},
"body": {
"files": [],
},
}
class Jack(Cmd):
"""Commandline interpreter to access various tools
"""
def __init__(self):
Cmd.__init__(self)
self.prompt = "jack:: "
def do_speak(self, line):
"""Tell me what you do"""
msg = "This is what I can do.\n"
sys.stdout.write(msg)
def do_fetch(self, line):
"""move to fetch subcommand"""
self.fetch_app = Fetch(self)
self.fetch_app.cmdloop()
class Fetch(Cmd):
"""Find, check and move winfates result files"""
def __init__(self, parent):
Cmd.__init__(self)
self.parent = parent
self.prompt = self.parent.prompt[:-2] + "fetch:: "
self.record = new_fetch_record()
def do_find(self, line):
"""move to find subcommand"""
self.find_app = Find(self)
self.find_app.cmdloop()
print(self.find_app.record)
self.record = self.find_app.record
class Find(Cmd):
"""Find winfates results files"""
LOCDATA_DRIVES = ['e$', 'c$', 'k$']
EXTENSIONS = set(""".ac3 .bys .clc .err .hc3 .hd3 .ht3 .if3 .in3 .jur
.log .lu3 .mmb .prf .sc3 .sf3 .sfa .shr .slk .sp3 .ss3 .tr3 .tsd .tu3 .wth""".split())
def __init__(self, parent):
Cmd.__init__(self)
self.parent = parent
self.prompt = self.parent.prompt[:-2] + "find:: "
self.machines = []
self.project = ""
self.locdata_dirs = []
self.project_dirs = []
self.record = self.parent.record
def _has_locdata(self, machines):
"""Check if the machines have a loc_data directory"""
result = []
tpl = r"\\{machine}\{drive}\loc_data"
for m in machines:
for d in self.LOCDATA_DRIVES:
locdata = tpl.format(machine=m, drive=d)
if osp.isdir(locdata):
result.append(locdata)
return result
def _has_project(self):
"""Return project dir if the locdata has a project in it"""
assert self.locdata_dirs
results = []
for d in self.locdata_dirs:
dirs = [ osp.join(d,f, "modelout")
for f in fnfilter(os.listdir(d), self.project)
if osp.isdir(osp.join(d,f, "modelout")) ]
results.extend(dirs)
return results
def _has_results(self):
"""Return files that match the pattern"""
assert self.project_dirs
assert self.patterns
results = []
for d in self.project_dirs:
for p in self.patterns:
files = [ osp.join(d, f) for f in fnfilter(os.listdir(d), p)
if osp.splitext(f)[1].lower() in self.EXTENSIONS ]
results.extend(files)
return results
def _display_files(self):
"""Show user the files that have been found"""
jprint("Files found were:\n")
for f in self.files:
print(f)
def _display_locdatas(self):
"""Show the locdatas found on machines"""
jprint("Loc_data_dirs found at:")
for d in self.locdata_dirs:
print(d)
def _display_state(self):
self._display_locdatas()
self._display_files()
def _refresh(self):
"""Update state"""
self._has_locdata()
self._has_project()
def _update(self):
"""Export state to fetch record"""
self.record['header'].update({'paths' : self.project_dirs,
'patterns': self.patterns})
self.record['body'].update({'files': self.files})
def do_export(self, line):
"""Return the fetch record to jack:fetch and quit"""
return self.record
def do_machines(self, line):
"""Display and set the machines to search within"""
if line:
self.machines = line.split()
if self.machines:
self.locdata_dirs = self._has_locdata(self.machines)
self._display_locdatas()
else:
print("Perhaps you could try :: machines manta")
def do_project(self, line):
"""Project can be the whole loc_data name or a pattern
eg. J0230 - GHD_Apache Phoenix South_1
or j0230*
"""
if line == "":
jprint(self.project)
else:
self.project = line
self.project_dirs = self._has_project()
jprint(self.project_dirs)
def do_patterns(self, line):
"""List of patterns to search modelout results"""
if line:
self.patterns = line.split()
else:
jprint(self.patterns)
if self.patterns:
self.files = self._has_results()
self._display_files()
def do_update(self, line):
"""Update the fetch record to reflect settings"""
self._update()
### Functions
def jprint(msg):
# sys.stdout.write("{}\n".format(msg))
pprint(msg)
if __name__ == "__main__":
app = Jack()
app.cmdloop()
print("Done __main__")