-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoolbox.py
76 lines (76 loc) · 2.75 KB
/
toolbox.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
"""
generic python tools
"""
import os,sys
#-----------------------------------------------------
def isUnix():
import platform
uname = platform.uname()
return not (uname[0] == 'Windows' or uname[2] == 'Windows')
#-----------------------------------------------------
def fileToModule(file, verb=False):
if verb: print 'os.path.sep=',os.path.sep
file=os.path.abspath(file)
if verb: print 'file=',file
for dirname in sys.path:
dirname = os.path.abspath(dirname).lower()
if verb: print 'module in', dirname, '?',
common = os.path.commonprefix( (file.lower(), dirname) )
if common == dirname:
if verb: print 'YES'
strip = file[len(dirname):]
if strip[0]==os.path.sep:
strip=strip[1:]
strip = os.path.splitext(strip)[0]
strip = strip.replace(os.path.sep,'.')
if verb: print 'module=', strip
return strip
break
else:
if verb: print 'NO'
return ''
#-----------------------------------------------------
def moduleToFile(module):
for p in sys.path:
#print 'testing %s' % p
dir = os.path.join(p, module.replace('.',os.sep))
dirini = os.path.join(dir, '__init__.py')
file = '%s.py' % dir
#print '\tdir=%s\n\tfile=%s' %(dir, file)
if os.path.isfile(dirini):
return dir
elif os.path.isfile(file):
return file
return ''
#-----------------------------------------------------
def getWorkspace(file, baseName=os.getcwd(), verb=False):
module = fileToModule(file,verb)
wPath = os.path.join(baseName,os.path.join('workspace',module.replace('.','_')))
workspace = os.path.abspath(wPath)
if verb: print "toolbox.getWorkspace returns ",workspace
return workspace
#-----------------------------------------------------
def modulePath(local_function):
''' returns the module path without the use of __file__. Requires a function defined
locally in the module.
from http://stackoverflow.com/questions/729583/getting-file-path-of-imported-module
'''
import inspect
return os.path.abspath(inspect.getsourcefile(local_function))
#-----------------------------------------------------
def getFileName(fileWithExt):
import os
return os.path.basename(fileWithExt).split('.')[0]
#-----------------------------------------------------
def getFileExt(fileWithExt):
import os
return os.path.basename(fileWithExt).split('.')[-1]
#-----------------------------------------------------
def cleanDir(dir):
try:
import shutil
if os.path.isdir(dir):
print "cleaning ",dir
shutil.rmtree(dir)
except: # WindowsError if files in use
pass