-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
151 lines (132 loc) · 3.95 KB
/
utils.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
import os, shutil
from operator import itemgetter
import gzip
from Config import Configuration
conf=Configuration()
def myMakeDir(dirPath, pathTail=None):
'''
Equivalent to os.path.join but if the resultant path does not exists, then it is created as a directory.
:param dirPath: str. prefix of the path.
:param pathTail: str. tail of the path. Optional
:return the result of path concatenate dirPath and pathTail (which will be a directory)
'''
if pathTail != None:
dirPath= os.path.join(dirPath, pathTail)
if not os.path.isdir(dirPath):
try:
os.makedirs(dirPath)
except OSError as e:
if e.errno== 17: # Directory already exists
pass
return dirPath
def myMakeDirUnique(dirPath, pathTail=None):
'''
Equivalent to os.path.join but if the resultant path exists, it adds a suffix
of form _#Nun
P.e.
myMakeDirUnique("/hola/path", "foo")
will create dir "/hola/path/foo"
If executed again, myMakeDirUnique("/hola/path", "foo")
will create dir "/hola/path/foo_1"
If executed again, myMakeDirUnique("/hola/path", "foo")
will create dir "/hola/path/foo_2"
and so on
and will return the new name
:param dirPath: str. prefix of the path.
:param pathTail: str. tail of the path. Optional
:return the result of path concatenation of dirPath and pathTail
'''
if pathTail != None:
if pathTail.endswith("/"):
pathTail= pathTail[:-1]
dirPath= os.path.join(dirPath, pathTail)
origDirPath= dirPath
i=1
if os.path.isdir(origDirPath):
dirPath= origDirPath+"_"+str(i)
i+=1
else:
os.makedirs(dirPath)
return dirPath
while os.path.isdir(dirPath):
dirPath= origDirPath+"_"+str(i)
i+=1
os.makedirs(dirPath)
return dirPath
def tryToRemove( fname):
'''
Try to remove one file. If it is not possible, it does not do anything.
:param fname: str. file to be removed
'''
try:
os.remove( fname)
except OSError:
pass
def tryToMove(source, dest):
'''
Try to move source file to dest file. If it is not possible, it does not do anything.
:param source: str. path to source file
:param dest: str. path where source will be copied
'''
try:
os.rename( source, dest)
except IOError:
pass
def tryToCopy( source, dest):
'''
Try to copy source file to dest file. If it is not possible, it does not do anything.
:param source: str. path to source file
:param dest: str. path where source will be copied
'''
try:
shutil.copyfile( source, dest)
except IOError:
pass
def tryToSymlink( source, dest):
'''
Try to copy source file to dest file. If it is not possible, it does not do anything.
:param source: str. path to source file
:param dest: str. path where source will be copied
'''
try:
os.symlink( source, dest)
except (IOError, OSError):
pass
def tryToCleanDir(dirName, substr="_", rootDataDir=conf.computedFeatsRootDir):
for name in os.listdir(dirName):
if not substr or substr in name:
nameToRemove= os.path.join(dirName, name)
assert nameToRemove.startswith(rootDataDir ), "Error, trying to remove not allowed file %s"%(nameToRemove)
os.remove(os.path.join(dirName, name))
def openForReadingFnameOrGz( fname):
if fname.endswith(".gz"):
return gzip.open(fname)
elif os.path.isfile(fname+".gz"):
return gzip.open(fname+".gz")
else:
return open(fname)
def getItemsFromList(idxs, l):
l= itemgetter( *idxs )( l)
if len(idxs)==1:
l= [l]
return l
def checkFreeMemory():
'''
return free memory in GBytes
'''
import psutil
x=psutil.virtual_memory()
return x.free/(1024.0 ** 3)
def getTotalMemory():
'''
return free memory in GBytes
'''
import psutil
x=psutil.virtual_memory()
return x.total/(1024.0 ** 3)
def getFileSize(fname):
'''
return file disk usage in GBytes
'''
statinfo = os.stat(fname)
return statinfo.st_size/(1024.0 ** 3)