forked from jwdj/EasyABC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (57 loc) · 2.12 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
import sys
import os, os.path
def get_application_path():
if getattr(sys, 'frozen', False):
if sys.platform == "darwin":
return os.path.dirname(sys.argv[0])
else:
return os.path.dirname(sys.executable)
elif __file__:
return os.path.dirname(os.path.abspath(__file__))
else:
return os.getcwd()
def append_to_envpath(path):
prepend = path + ';'
envpath = os.environ.get('PATH', '')
if not envpath.startswith(prepend):
os.environ['PATH'] = prepend + envpath
def is_running_32bit():
import struct
bytesPerPointer = struct.calcsize("P")
return bytesPerPointer == 4
def search_files(dir, ext):
return [os.path.join(dp, f) for dp, dn, filenames in os.walk(dir) for f in filenames if os.path.splitext(f)[1] in ext]
def read_entire_file(path):
f = open(path, 'rb') # read in binary to avoid problem with EOL characters
result = f.read()
f.close()
return result
def read_text_if_file_exists(filepath):
''' reads the contents of the given file if it exists, otherwise returns the empty string '''
if filepath and os.path.exists(filepath):
return read_entire_file(filepath)
else:
return ''
def ensure_file_name_does_not_exist(file_path):
if not os.path.exists(file_path):
return file_path
i = 0
file_exists = True
path, file_name = os.path.split(file_path)
name, extension = os.path.splitext(file_name)
while file_exists:
i += 1
file_path = os.path.abspath(os.path.join(path, "{0}({1}){2}".format(name, i, extension)))
file_exists = os.path.exists(file_path)
return file_path
def generate_temp_file_name(path, ending, replace_ending=None):
i = 0
file_exists = True
while file_exists:
file_name = os.path.abspath(os.path.join(path, "temp{0:02d}{1}".format(i, ending)))
file_exists = os.path.exists(file_name)
if not file_exists and replace_ending is not None:
f = os.path.abspath(os.path.join(path, "temp{0:02d}{1}".format(i, replace_ending)))
file_exists = os.path.exists(f)
i += 1
return file_name