-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·172 lines (139 loc) · 3.87 KB
/
utils.py
File metadata and controls
executable file
·172 lines (139 loc) · 3.87 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
import os
import platform
import json
import sys
import re
import hashlib
import stat
# NOTE: These values MUST match the same values in the precachelib.h
PRECACHE_FILE_ARCH_BIT = 0
PRECACHE_FILE_PLATFORM_BIT = 4
PRECACHE_FILE_EXECUTE_BIT = 8
PRECACHE_FILE_DEFAULT_MODE = '755'
# -----------------------------------------------------------------------------
# Utils
# -----------------------------------------------------------------------------
def require_config( cfg, key ):
""" Alert the user when a config parameter is missing """
if key not in cfg:
print( 'ERROR: Missing config parameter %s' % (key) )
sys.exit(1)
def get_platform():
p = platform.platform().lower()
if 'linux' in p:
return "linux"
elif "darwin" in p:
return "macosx"
elif "nt" or "windows" in p:
return "windows"
else:
return "unknown"
def file_exists( path ):
return os.path.exists(path) and os.stat(path)
def md5_from_file( file ):
""" Calculate an md5 hash of a file """
hash = ''
block_size = 8192
m = hashlib.md5()
f = open( file, "rb" )
while True:
data = f.read( block_size )
if not data:
break
m.update(data)
f.close()
return m.hexdigest()
def load_config( file ):
"""
Load precache configuration
"""
f = open( file, "rb" )
data = f.read()
f.close()
d = json.loads( data )
require_config( d, 'local_project_path' )
require_config( d, 'install_path' )
require_config( d, 'remote_project_path' )
# extend the exclude list
exclude_list = []
if 'excludes' in d:
exclude_list.extend( d['excludes'] )
if 'precache_binary_path' in d:
exclude_list.append( ('*/%s/*' % d['precache_binary_path']) )
if 'precache_launcher_path' in d:
exclude_list.append( ('*/%s/*' % d['precache_launcher_path']) )
if 'platforms' not in d:
d['platforms'] = [ get_platform() ]
d['excludes'] = exclude_list
return d
def ignores_to_regex( excludes ):
""" Convert list of ignores to regex objects """
ignore_list = []
for e in excludes:
e = os.path.normpath( e )
e = e.replace('\\', '\\\\')
e = e.replace('.', '\\.')
e = e.replace('*', '.*')
pat = re.compile( e )
#print( 'e: %s -> %s' % (e, pat) )
ignore_list.append( pat )
return ignore_list
def get_platform_id( pstring ):
"""
determine platform id
NOTE: These values MUST match the same values in the precachelib.h
"""
if pstring == "windows":
return 1
elif pstring == "linux":
return 2
elif pstring == "macosx":
return 3
return None
def get_arch_id( arch ):
"""
determine architecture id
NOTE: These values MUST match the same values in the precachelib.h
"""
if arch == "x86":
return 1
elif arch == "x64":
return 2
return None
def create_flags( arch_id, os_id, execute_bit ):
""" condense these ids into a single value """
return (int(arch_id) << PRECACHE_FILE_ARCH_BIT | int(os_id) << PRECACHE_FILE_PLATFORM_BIT | int(execute_bit) << PRECACHE_FILE_EXECUTE_BIT)
def make_relative_to( inpath, relpath ):
""" return a relative path to the inpath, given a relative to source project path """
if relpath in inpath:
return inpath[ len(relpath): ]
else:
print( 'relpath NOT in inpath:' )
print( '\t%s <-> %s' % (relpath, inpath) )
def default_file_mode():
return PRECACHE_FILE_DEFAULT_MODE
# return a tuple of owner, group, other flags
def get_mode_for_file( fullpath ):
mode = os.stat( fullpath ).st_mode
owner = 0
group = 0
other = 0
if mode & stat.S_IRUSR:
owner |= 4
if mode & stat.S_IWUSR:
owner |= 2
if mode & stat.S_IXUSR:
owner |= 1
if mode & stat.S_IRGRP:
group |= 4
if mode & stat.S_IWGRP:
group |= 2
if mode & stat.S_IXGRP:
group |= 1
if mode & stat.S_IROTH:
other |= 4
if mode & stat.S_IWOTH:
other |= 2
if mode & stat.S_IXOTH:
other |= 1
return (owner, group, other)