-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.py
105 lines (92 loc) · 2.27 KB
/
helper.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
"""
Holds some helper fumctions.
"""
import os
import platform
import sys
import pip
try:
input = raw_input
except NameError:
pass
def convertToPlatformPath(path):
listOfString = []
if '/' in path:
listOfString = path.split('/')
elif '\\\\' in path:
listOfString = path.split('\\\\')
elif '\\' in path:
listOfString = path.split('\\')
if len(listOfString) > 0:
#For win it is necessary to add \\ for drive letter
if ':' in listOfString[0]:
listOfString[0] = listOfString[0] + os.sep
return os.path.join(*listOfString)
return path
def getCPUFamily(machineType):
if machineType.lower() == 'i386':
return 'x86'
elif machineType.lower() == 'amd64':
return 'x64'
return 'x64'
def iterateDict(dictonary):
if hasattr(dict, 'iteritems'):
return dictonary.iteritems()
else:
return iter(dictonary.items())
def str_to_bool(value):
if value.lower() == 'true':
return True
else:
return False
def bool_to_str(value):
if value:
return 'True'
else:
return 'False'
def module_exists(module_name):
"""
:param module_name: name of the module that needs to be checked.
:return: True/False based on if the input module exists or not
"""
try:
__import__(module_name)
except ImportError:
return False
else:
return True
def install(package):
try:
import package
except:
import sys
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
def yes_no(question):
"""
Ask user a yes or no question and evaluate answer.
:param question: question user is asked in a form of a string.
:return answer: returns True/False based on input.
"""
yes = {'yes', 'y', 'ye', ''}
no = {'no', 'n'}
while True:
sys.stdout.write(question + ' (y/n) ')
choice = input().strip()
choice = choice.lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no'\n")
def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text
def remove_carriage_return(str):
if str.endswith("\r\n"):
str = str[:-2]
if str.endswith("\n") or str.endswith("\r"):
str = str[:-1]
return str