[go: up one dir, main page]

Menu

[r48]: / Prophet / __init__.py  Maximize  Restore  History

Download this file

343 lines (282 with data), 11.0 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
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
import types
import sys
import os
import os.path
import stat
import string
import fnmatch
import re
from Paths import ValidPathSet as VPS
from Keywords import Set as KwS, Keyword as Kw
from Prophet.Categories import *
verbose = 0
# Omit (whenever possible) the path to the executable if the latter is in
# the PATH
omitExecPath = True
skipMissExec = True # Skip the entries whose executables are not found
class NotFound(Exception):
"""Raised when an appropriate file is not found"""
class NotSet(Exception):
"""Raised when getter fails to return any meaningful result"""
def msg(s, newline=True):
if verbose:
sys.stderr.write(s)
if newline:
sys.stderr.write("\n")
sys.stderr.flush()
def warn(s, newline=True):
sys.stderr.write(s)
if newline:
sys.stderr.write("\n")
sys.stderr.flush()
def fatal(s, newline=True, code=1):
sys.stderr.write(s)
if newline:
sys.stderr.write("\n")
sys.stderr.flush()
sys.exit(code)
def isExe(exe):
"""Check whether the specified name can be considered executable"""
# Exec's have x-bit set for current uid, aren't directories, and don't
# have leading dot
try:
st = os.stat(exe)
return (stat.S_IMODE(st[stat.ST_MODE]) & stat.S_IXUSR) and not stat.S_ISDIR(
st[stat.ST_MODE]) and not os.path.basename(exe).startswith(".")
except OSError:
return False
class PathSet(VPS):
def adopt(self, path):
path = super(PathSet, self).adopt(path)
try:
for exe in os.listdir(path):
if isExe(os.path.join(path, exe)):
return path
except OSError:
pass
raise ValueError
def which(self, exe):
if os.path.isabs(exe):
exe = os.path.basename(exe)
for p in self:
x = os.path.join(p, exe)
if isExe(x):
return x
raise NotFound
class PrefixSet(VPS):
def __init__(self, *args, **kwds):
self.binpaths = {}
self.bins = ["bin", "sbin", "", "games"]
super(PrefixSet, self).__init__(*args, **kwds)
def adopt(self, path):
path = super(PrefixSet, self).adopt(path)
for bin in self.bins:
dir = os.path.join(path, bin)
# A valid bindir contains at least one executable
try:
for x in os.listdir(dir):
if isExe(os.path.join(dir, x)):
self.binpaths[path] = PathSet(
[os.path.join(path, x) for x in self.bins])
return path
except OSError:
pass
raise ValueError
# Set of valid directories containing executables extracted from PATH
paths = PathSet(os.environ["PATH"].split(":"))
# Set of valid prefixes
prefixes = PrefixSet([os.path.dirname(x) for x in paths]) \
+ ["~", "/", "/usr", "/usr/local", "/usr/X11", "/usr/X11R7",
"/usr/X11R6", "/usr/X11R5", "/opt", "$QTDIR", "$KDEDIR"]
# Match the most widespread shells
# TODO : detect absolute paths
_shStart = re.compile("^\s*(a|b|ba|c|k|pdk|tc|z)?sh\s+")
# Match complex shell command (i.e. with pipelines, and's or's etc.)
_shComplexCmd = re.compile("(;.*){2,}|\||&&|\(|\)|\{|\}")
class App(object):
"""Single application that has specific executable and thus can be put into a menu"""
Console = Kw("Console")
XWindow = Kw("XWindow")
GNOME = Kw("GNOME")
KDE = Kw("KDE")
Xfce = Kw("Xfce")
GNUstep = Kw("GNUstep")
def getPrefixes(self):
return prefixes
def getPaths(self):
return paths
def setKeywords(self):
# Make sure the keywords field always exists
try:
self.keywords
except AttributeError:
self.keywords = KwS()
def setName(self):
try:
self.name
except AttributeError:
# Deduce name from the executable
self.name = os.path.basename(self.exename).capitalize()
def setExecmd(self):
# If an absolute path is given, check it
# otherwise try to locate the executable via the PATH envar
path = self.exename
paths = self.getPaths()
if os.path.isabs(path):
if skipMissExec and not isExe(path):
raise NotSet("%s is not executable" % path)
if omitExecPath:
try:
# Check whether the specified filename is that one
# accessible via the PATH
located = paths.which(path)
try:
if os.path.samefile(path, located):
path = os.path.basename(path)
except OSError:
pass
except NotFound:
pass
else:
# Here and forth we're given a relative path
try:
located = paths.which(path)
# There is no reason to check for located<->path identity; we simply
# make sure the executable can be accessed via PATH
if not omitExecPath:
# Sice we're initially given a relative path, make it full
# by consuming the result of locateExec()
path = located
except NotFound:
if skipMissExec:
raise NotSet("executable %s not found in PATH" % path)
# Execmd is fully formatted command line for the application
try:
# exeargs may be missing, it's OK
self.execmd = path + " " + self.exeargs
except AttributeError:
self.execmd = path
def setTerminal(self):
try:
self.terminal
except AttributeError:
self.terminal = len(self.keywords & KwS(ConsoleOnly)) > 0
def setNativenv(self):
try:
self.nativenv
except AttributeError:
pass
if self.terminal:
self.nativenv = App.Console
else:
self.nativenv = App.XWindow
for x in self.keywords:
if fnmatch.fnmatchcase(x, "*GNOME*"):
self.nativenv = App.GNOME
break
elif fnmatch.fnmatchcase(x, "*KDE*"):
self.nativenv = App.KDE
break
elif fnmatch.fnmatchcase(x, "*XFCE*"):
self.nativenv = App.Xfce
break
# TODO : GNUstep???
# Attributes to set, mandatory(+), optional(-) :
# +exename -- executable, with or w/o path
# -exeargs -- arguments that follow the exename
# +execmd -- a fully formatted command line for the app
# +keywords -- a set of keywords, must not be empty
# +terminal -- True if the app requires console support
# +nativenv -- the environment required by the app
# -name -- short description of the app
# -comment -- a more detailed description
# NOTE : When determining whether the application should be run in terminal,
# consult the self.terminal and _not_ the self.nativenv
def testGoodness(self, cmd):
# Raise NotSet if cmd is not "good enough" and thus isn't worth further consideration
# Current implementation filters out complex commands and commands
# that are started with ?sh (a frequent Debian case)
# There is also a possibility to get None instead of string when the key is missing
if cmd is None or _shStart.search(cmd) or _shComplexCmd.search(cmd):
raise NotSet("not good enough : " + str(cmd))
def __setup__(self):
self.setExename()
self.setKeywords()
try:
self.setExeargs()
except AttributeError:
pass
self.setExecmd()
self.setName()
self.setTerminal()
self.setNativenv()
# Skip unwanted entries
if skip[self.nativenv]:
raise NotSet("unwanted %s environment" % self.nativenv)
try:
self.setComment()
except AttributeError:
pass
skip = {App.Console: False, App.XWindow: False,
App.GNOME: False, App.KDE: False, App.Xfce: False}
def merge(entries):
"""Removes repeating entries from the list using fuzzy matching technique"""
# NOTE : this is the deliberate violation of the immutability principle
# __matched attribute is set for the entries that have been successfully matched before
# Such entries should be skipped
# It is thought that this approach is faster than keeping them in separate
# list
msg("* merging...", newline=False)
result = []
esz = len(entries)
for i in range(0, esz - 1):
e = entries[i]
if hasattr(e, "__matched"):
continue
matching = [e] # List containing matching entries
for j in range(i + 1, esz):
x = entries[j]
if hasattr(x, "__matched"):
continue
# Matching e and x
if os.path.basename(e.exename) == os.path.basename(x.exename):
# The main criteria is the considence of the executables
try:
eargs = e.exeargs
except AttributeError:
eargs = None
try:
xargs = x.exeargs
except AttributeError:
xargs = None
if not eargs and not xargs:
# Both entries don't have arguments, consider them matching
matching.append(x)
elif eargs and xargs:
# Both entries do have arguments, match them
if Kw(eargs) == Kw(xargs):
# Squeeze the spaces and don't consider the case of the
# characters
matching.append(x)
if len(matching) < 2:
# Only one candidate, no choice
winner = e
else:
# Several candidates, must choose the most relevant
winner = None
for m in matching:
if winner:
if m.pref > winner.pref:
winner = m
elif m.pref == winner.pref:
# For now the longer name is the better
if len(m.name) > len(winner.name):
winner = m
else:
winner = m
result.append(winner)
# This is done in the last step to mark e which was put in matching
for m in matching:
m.__matched = True
msg(" %d coincidings detected" % (len(entries) - len(result)))
return result