-
Notifications
You must be signed in to change notification settings - Fork 11
/
misc.py
305 lines (254 loc) · 9.98 KB
/
misc.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import re
import sys
import time
import shutil
import subprocess
import datetime
import yaml
import getpass
# Import gslab_scons modules
import _exception_classes
from size_warning import issue_size_warnings
def scons_debrief(target, env):
'''Execute functions after SCons has built all targets'''
# Log the state of the repo
env['CL_ARG'] = env['MAXIT']
maxit = int(command_line_args(env))
state_of_repo(maxit)
# Issue size warnings
look_in = env['look_in']
look_in = look_in.split(';')
file_MB_limit = float(env['file_MB_limit'])
total_MB_limit = float(env['total_MB_limit'])
issue_size_warnings(look_in, file_MB_limit, total_MB_limit)
return None
def state_of_repo(maxit):
outfile = 'state_of_repo.log'
with open(outfile, 'wb') as f:
f.write("WARNING: Information about .sconsign.dblite may be misleading \n" +
"as it can be edited after state_of_repo.log finishes running\n\n" +
"===================================\n\n GIT STATUS" +
"\n\n===================================\n")
f.write("Last commit:\n\n")
# https://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash
os.system("git log -n 1 >> state_of_repo.log 2>&1")
with open(outfile, 'ab') as f:
f.write("\n\nFiles changed since last commit:\n\n")
os.system("git diff --name-only >> state_of_repo.log 2>&1")
with open(outfile, 'ab') as f:
f.write("\n===================================\n\n FILE STATUS" +
"\n\n===================================\n")
for root, dirs, files in os.walk(".", followlinks = True):
i = 1
for name in files:
path = os.path.join(root, name).replace('\\', '/')
if i <= maxit and not \
re.search('\./\.', path) and not \
re.search('.DS_Store', name):
stat_info = os.stat(os.path.join(root, name))
f.write(os.path.join(root, name) + ':\n')
f.write(' modified on: %s\n' %
time.strftime('%d %b %Y %H:%M:%S',
time.localtime(stat_info.st_mtime)))
f.write(' size of file: %s\n' % stat_info.st_size)
i = i + 1
elif i > maxit:
f.write('MAX ITERATIONS (%s) HIT IN DIRECTORY: %s\n' % \
(maxit, root))
break
return None
def command_line_args(env):
'''
Return the content of env['CL_ARG'] as a string
with spaces separating entries. If env['CL_ARG']
doesn't exist, return an empty string.
'''
try:
cl_arg = env['CL_ARG']
if not isinstance(cl_arg, str):
try:
# Join arguments as strings by spaces
cl_arg = ' '.join(map(str, cl_arg))
except TypeError:
cl_arg = str(cl_arg)
except KeyError:
cl_arg = ''
return cl_arg
def get_stata_executable(env):
'''
This helper function returns the most common Stata executable
for Mac/Windows if the default executable is not available in env.
'''
# Get environment's user input executable. Empty default = None.
stata_executable = env['stata_executable']
if stata_executable not in [None, 'None', '']:
return stata_executable
else:
if is_unix():
return 'stata-mp'
elif sys.platform == 'win32':
return 'StataMP-64.exe'
return None
def get_stata_command(executable):
if is_unix():
command = stata_command_unix(executable)
elif sys.platform == 'win32':
command = stata_command_win(executable)
return command
def stata_command_unix(executable):
'''
This function returns the appropriate Stata command for a user's
Unix platform.
'''
options = {'darwin': '-e',
'linux' : '-b',
'linux2': '-b'}
option = options[sys.platform]
# %s will take filename and cl_arg later
command = executable + ' ' + option + ' %s %s'
return command
def stata_command_win(executable):
'''
This function returns the appropriate Stata command for a user's
Windows platform.
'''
command = executable + ' /e do' + ' %s %s' # %s will take filename later
return command
def is_unix():
'''
This function return True if the user's platform is Unix and false
otherwise.
'''
unix = ['darwin', 'linux', 'linux2']
return sys.platform in unix
def is_64_windows():
'''
This function return True if the user's platform is Windows (64 bit)
and False otherwise.
'''
return 'PROGRAMFILES(X86)' in os.environ
def is_in_path(program):
'''
This general helper function checks whether `program` exists in the
user's path environment variable.
'''
if os.access(program, os.X_OK):
return program
else:
for path in os.environ['PATH'].split(os.pathsep):
path = path.strip("'")
exe = os.path.join(path, program)
if os.access(exe, os.X_OK):
return exe
return False
def make_list_if_string(source):
'''Convert a string input into a singleton list containing that string.'''
if not isinstance(source, list):
if isinstance(source, str):
source = [source]
else:
message = "SCons source/target input must be either list or string. " + \
"Here, it is %s, a %s." % (str(source), str(type(source)))
raise TypeError(message)
return source
def check_code_extension(source_file, extensions):
'''
This function raises an exception if the extension in `source_file`
does not match the software package specified by `software`.
'''
if not isinstance(extensions, list):
extensions = [extensions]
source_file = str.lower(str(source_file))
error = True
for extension in extensions:
extension = str.lower(str(extension))
if source_file.endswith(extension):
error = False
if error:
error_message = 'First argument, %s, must be a %s file.' % \
(source_file, extension)
raise _exception_classes.BadExtensionError(error_message)
return None
def command_error_msg(executable, call):
''' This function prints an informative message given a CalledProcessError.'''
return '''%s did not run successfully.
Please check that the executable, source, and target files
Check SConstruct.log for errors.
Command tried: %s''' % (executable, call)
def current_time():
'''Return the current time in a Y-M-D H:M:S format.'''
now = datetime.datetime.now()
return datetime.datetime.strftime(now, '%Y-%m-%d %H:%M:%S')
def lyx_scan(node, env, path):
contents = node.get_contents()
SOURCE = []
for ext in env.EXTENSIONS:
src_find = re.compile(r'filename\s(\S+%s)' % ext, re.M)
SOURCE = SOURCE + [source.replace('"', '') \
for source in src_find.findall(contents)]
return SOURCE
def load_yaml_value(path, key):
'''
Load the yaml value indexed by the key argument in the file
specified by the path argument.
'''
if key == "stata_executable":
prompt = "Enter %s or None to search for defaults: "
elif key == "github_token":
prompt = "(Optional) Enter %s to be stored in config_user.yaml.\n"
prompt = prompt + "Github token can also be entered without storing to file later:"
else:
prompt = "Enter %s: "
# Check if file exists and is not corrupted. If so, load yaml contents.
yaml_contents = None
if os.path.isfile(path):
try:
yaml_contents = yaml.load(open(path, 'rU'))
if not isinstance(yaml_contents, dict):
raise yaml.scanner.ScannerError()
except yaml.scanner.ScannerError:
message = "%s is a corrupted yaml file. Delete file and recreate? (y/n) "
response = str(raw_input(message % path))
if response.lower() == 'y':
os.remove(path)
yaml_contents = None
else:
message = "%s is a corrupted yaml file. Please fix." % path
raise _exception_classes.PrerequisiteError(message)
# If key exists, return value. Otherwise, add key-value to file.
try:
if yaml_contents[key] == "None":
return None
else:
return yaml_contents[key]
except:
with open(path, 'ab') as f:
if key == "github_token":
val = getpass.getpass(prompt = (prompt % key))
else:
val = str(raw_input(prompt % key))
if re.sub('"', '', re.sub('\'', '', val.lower())) == "none":
val = None
f.write('%s: %s\n' % (key, val))
return val
def check_and_expand_path(path):
error_message = " The directory provided, '%s', cannot be found. " % path + \
"Please manually create before running\n" + \
"or fix the path in config_user.yaml.\n"
try:
path = os.path.expanduser(path)
if not os.path.isdir(path):
raise _exception_classes.PrerequisiteError(error_message)
return path
except:
raise _exception_classes.PrerequisiteError(error_message)
def get_directory(path):
'''
Determine the directory of a file. This function returns
'./' rather than '' when `path` does not include a directory.
'''
directory = os.path.dirname(path)
if directory == '':
directory = './'
return directory