-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.py
363 lines (297 loc) · 11.8 KB
/
common.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#
# Common utility functions
#
import os, string, fsm, winreg, pefile, mmap, sys, traceback
import re
import pycmd_public
# Stop points when navigating one word at a time
word_sep = [' ', '\t', '\\', '-', '_', '.', '/', '$', '&', '=', '+', '@', ':', ';']
# Command splitting characters
sep_chars = [' ', '|', '&', '>', '<']
# Command sequencing tokens
seq_tokens = ['|', '||', '&', '&&']
# Redirection tokens
digit_chars = list(string.digits)
redir_file_simple = ['>', '>>', '<']
redir_file_ext = [c + d for d in digit_chars for c in ['<&', '>&']]
redir_file_all = redir_file_simple + redir_file_ext
redir_file_tokens = redir_file_all + [d + c for d in digit_chars for c in redir_file_all]
# print redir_file_tokens
# All command splitting tokens
sep_tokens = seq_tokens + redir_file_tokens
def parse_line(line):
"""Tokenize a command line based on whitespace while observing quotes"""
def accumulate(fsm):
"""Action: add current symbol to last token in list."""
fsm.memory[-1] = fsm.memory[-1] + fsm.input_symbol
def start_empty_token(fsm):
"""Action: start a new token."""
if fsm.memory[-1] != '':
fsm.memory.append('')
def start_token(fsm):
"""Action: start a new token and accumulate."""
start_empty_token(fsm)
accumulate(fsm)
def accumulate_last(fsm):
"""Action: accumulate and start new token."""
accumulate(fsm)
start_empty_token(fsm)
def error(fsm):
"""Action: handle uncovered transition (should never happen)."""
print('Unhandled transition:', (fsm.input_symbol, fsm.current_state))
accumulate(fsm)
f = fsm.FSM('init', [''])
f.set_default_transition(error, 'init')
# default
f.add_transition_list(string.whitespace, 'init', start_empty_token, 'whitespace')
f.add_transition('"', 'init', accumulate, 'in_string')
f.add_transition('|', 'init', start_token, 'pipe')
f.add_transition('&', 'init', start_token, 'amp')
f.add_transition('>', 'init', start_token, 'gt')
f.add_transition('<', 'init', accumulate, 'awaiting_&')
f.add_transition('^', 'init', accumulate, 'escape')
f.add_transition_list(string.digits, 'init', accumulate, 'redir')
f.add_transition_any('init', accumulate, 'init')
# whitespace
f.add_transition_list(string.whitespace, 'whitespace', None, 'whitespace')
f.add_empty_transition('whitespace', 'init')
# strings
f.add_transition('"', 'in_string', accumulate, 'init')
f.add_transition_any('in_string', accumulate, 'in_string')
# seen '|'
f.add_transition('|', 'pipe', accumulate_last, 'init')
f.add_empty_transition('pipe', 'init', start_empty_token)
# seen '&'
f.add_transition('&', 'amp', accumulate_last, 'init')
f.add_empty_transition('amp', 'init', start_empty_token)
# seen '>' or '1>' etc.
f.add_transition('>', 'gt', accumulate, 'awaiting_&')
f.add_transition('&', 'gt', accumulate, 'awaiting_nr')
f.add_empty_transition('gt', 'init', start_empty_token)
# seen digit
f.add_transition('<', 'redir', accumulate, 'awaiting_&')
f.add_transition('>', 'redir', accumulate, 'gt')
f.add_empty_transition('redir', 'init')
# seen '<' or '>>', '0<', '2>>' etc.
f.add_transition('&', 'awaiting_&', accumulate, 'awaiting_nr')
f.add_empty_transition('awaiting_&', 'init', start_empty_token)
# seen '<&' or '>&', '>>&', '0<&', '1>&', '2>>&' etc.
f.add_transition_list(string.digits, 'awaiting_nr', accumulate_last, 'init')
f.add_empty_transition('awaiting_nr', 'init', start_empty_token)
# seen '^'
f.add_transition_any('escape', accumulate, 'init')
f.process_list(line)
if len(f.memory) > 0 and f.memory[-1] == '':
del f.memory[-1]
return f.memory
def unescape(string):
"""Unescape string from ^ escaping. ^ inside double quotes is ignored"""
if (string == None):
return None
result = u''
in_quotes = False
escape_next = False
for c in string:
if in_quotes:
result += c
if c == '"':
in_quotes = False
elif escape_next:
result += c
escape_next = False
else:
if c == '^':
escape_next = True
else:
result += c
if c == '"':
in_quotes = True
return result
def expand_tilde(string):
"""
Return an expanded version of the string by replacing a leading tilde
with %HOME% (if defined) or %USERPROFILE%.
"""
if 'HOME' in os.environ.keys():
home_var = 'HOME'
else:
home_var = 'USERPROFILE'
if string.startswith('~') or string.startswith('"~'):
string = string.replace('~', '%' + home_var + '%', 1)
return string
def expand_env_vars(string):
"""
Return an expanded version of the string by inlining the values of the
environment variables. Also replaces ~ with %HOME% or %USERPROFILE%.
The provided string is expected to be a single token of a command.
"""
# Expand tilde
string = expand_tilde(string)
# Expand all %variable%s
begin = string.find('%')
while begin >= 0:
end = string.find('%', begin + 1)
if end >= 0:
# Found a %variable%
var = string[begin:end].strip('%')
if var.upper() in os.environ.keys():
string = string.replace('%' + var + '%', os.environ[var], 1)
begin = string.find('%', begin + 1)
return string
def split_nocase(string, separator):
"""Split a string based on the separator while ignoring case"""
chunks = []
seps = []
pos = string.lower().find(separator.lower())
while pos >= 0:
chunks.append(string[ : pos])
seps.append(string[pos : pos + len(separator)])
string = string[pos + len(separator) : ]
pos = string.lower().find(separator.lower())
chunks.append(string)
return (chunks, seps)
def fuzzy_match(substr, str, prefix_only = False):
"""
Check if a substring is part of a string, while ignoring case and
allowing for "fuzzy" matching, i.e. require that only the "words" in
substr be individually matched in str (instead of an full match of
substr). The prefix_only option only matches "words" in the substr at
word boundaries in str.
"""
#print '\n\nMatch "' + substr + '" in "' + str + '"\n\n'
words = substr.split(' ')
pattern = [('\\b' if prefix_only else '') + '(' + word + ').*' for word in words]
# print '\n\n', pattern, '\n\n'
pattern = ''.join(pattern)
matches = re.search(pattern, str, re.IGNORECASE)
return [matches.span(i) for i in range(1, len(words) + 1)] if matches else []
def abbrev_string(string):
"""Abbreviate a string by keeping uppercase and non-alphabetical characters"""
string_abbrev = ''
add_next_char = True
for char in string:
add_this_char = add_next_char
if char == ' ':
add_this_char = False
add_next_char = True
elif not char.isalpha():
add_this_char = True
add_next_char = True
elif char.isupper() and not string.isupper():
add_this_char = True
add_next_char = False
else:
add_next_char = False
if add_this_char:
string_abbrev += char
return string_abbrev
def has_exec_extension(file_name):
"""Check whether the specified file is executable, i.e. its extension is .exe, .com or .bat"""
return (file_name.endswith('.com')
or file_name.endswith('.exe')
or file_name.endswith('.bat')
or file_name.endswith('.cmd'))
def strip_extension(file_name):
"""Remove extension, if present"""
dot = file_name.rfind('.')
if dot > file_name.rfind('\\'):
return file_name[ : dot]
else:
return file_name
def contains_special_char(string):
"""Check whether the string contains a character that requires quoting"""
return string.find(' ') >= 0 or string.find('&') >= 0
def starts_with_special_char(string):
"""Check whether the string STARTS with a character that requires quoting"""
return string.find(' ') == 0 or string.find('&') == 0
def associated_application(ext):
"""
Scan the registry to find the application associated to a given file
extension.
"""
try:
file_class = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, ext) or ext
action = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, file_class + '\\shell') or 'open'
assoc_key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT,
file_class + '\\shell\\' + action + '\\command')
open_command = winreg.QueryValueEx(assoc_key, None)[0]
# We assume a value `similar to '<command> %1 %2'
return expand_env_vars(parse_line(open_command)[0])
except WindowsError as e:
return None
def full_executable_path(app_unicode):
"""
Compute the full path of the executable that will be spawned
for the given command
"""
app = app_unicode.encode(sys.getfilesystemencoding())
# Split the app into a dir, a name and an extension; we
# will configure our search for the actual executable based
# on these
dir, file = os.path.split(app.strip(b'"'))
name, ext = os.path.splitext(file)
# Determine possible executable extension
if ext != '':
extensions_to_search = [ext]
else:
extensions_to_search = ['.exe', '.com', '.bat', '.cmd']
# Determine the possible locations
if dir:
paths_to_search = [dir]
else:
paths_to_search = [os.getcwd()] + os.environ['PATH'].split(os.pathsep)
# Search for an app
# print 'D:', paths_to_search, 'N:', name, 'E:', extensions_to_search
for p in paths_to_search:
for e in extensions_to_search:
if type(p) == str:
p = p.encode('utf-8')
full_path = os.path.join(p, name) + e
if os.path.exists(full_path):
return full_path
# We could not find the executable; this might be an internal command,
# or a file that doesn't have a registered application
return None
def is_gui_application(executable):
"""
Try to guess if an executable is a GUI or console app.
Note that the full executable name of an .exe file is
required (use e.g. full_executable_path() to get it)
"""
result = False
try:
fd = os.open(executable, os.O_RDONLY)
m = mmap.mmap(fd, 0, access = mmap.ACCESS_READ)
try:
pe = pefile.PE(data = m, fast_load=True)
if pefile.SUBSYSTEM_TYPE[pe.OPTIONAL_HEADER.Subsystem] == 'IMAGE_SUBSYSTEM_WINDOWS_GUI':
# We only return true if all went well
result = True
except pefile.PEFormatError as e:
# There's not much we can do if pefile fails
pass
m.close()
os.close(fd)
except Exception as e:
# Not much we can do for exceptions
pass
# Return False when not sure
return result
def apply_settings(settings_file):
"""
Execute a configuration file (if it exists), overriding values from the
global configuration objects (created when this module is loaded)
"""
if os.path.exists(settings_file):
try:
# We initialize the dictionary to readily contain the settings
# structures; anything else needs to be explicitly imported
execfile(settings_file, pycmd_public.__dict__)
except Exception as e:
print('Error encountered when loading ' + settings_file)
print('Subsequent settings will NOT be applied!')
traceback.print_exc()
def sanitize_settings():
"""Sanitize all the configuration instances"""
pycmd_public.appearance.sanitize()
pycmd_public.behavior.sanitize()