-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.py
More file actions
90 lines (70 loc) · 3.17 KB
/
util.py
File metadata and controls
90 lines (70 loc) · 3.17 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
import sys
import time
import msvcrt
import requests
import win32api
def get_non_specialized_string(string):
return ''.join(e for e in string if e.isalnum())
def wait_enter_or_seconds(caption, timeout=5):
start_time = time.time()
sys.stdout.write('%s' % caption)
sys.stdout.flush()
input_string = ''
while True:
if msvcrt.kbhit():
byte_arr = msvcrt.getche()
if ord(byte_arr) == 13: # enter_key
break
elif ord(byte_arr) >= 32: # space_char
input_string += "".join(map(chr, byte_arr))
if len(input_string) == 0 and (time.time() - start_time) > timeout:
break
print('') # needed to move to next line
return input_string
def download_with_progress(url,save_path):
with open(save_path, "wb") as f:
print("Downloading %s" % save_path)
response = requests.get(url, stream=True)
total_length = response.headers.get('content-length')
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50 - done)))
sys.stdout.flush()
return
def get_file_properties(file_name):
"""
Read all properties of the given file return them as a dictionary.
"""
prop_names = ('Comments', 'InternalName', 'ProductName',
'CompanyName', 'LegalCopyright', 'ProductVersion',
'FileDescription', 'LegalTrademarks', 'PrivateBuild',
'FileVersion', 'OriginalFilename', 'SpecialBuild')
props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
try:
# backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc
fixedInfo = win32api.GetFileVersionInfo(file_name, '\\')
props['FixedFileInfo'] = fixedInfo
props['FileVersion'] = "%d.%d.%d.%d" % (fixedInfo['FileVersionMS'] / 65536,
fixedInfo['FileVersionMS'] % 65536,
fixedInfo['FileVersionLS'] / 65536,
fixedInfo['FileVersionLS'] % 65536)
# \VarFileInfo\Translation returns list of available (language, codepage)
# pairs that can be used to retreive string info. We are using only the first pair.
lang, codepage = win32api.GetFileVersionInfo(file_name, '\\VarFileInfo\\Translation')[0]
# any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle
# two are language/codepage pair returned from above
str_info = {}
for propName in prop_names:
str_info_path = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName)
str_info[propName] = win32api.GetFileVersionInfo(file_name, str_info_path)
props['StringFileInfo'] = str_info
except:
pass
return props