-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
73 lines (56 loc) · 2.82 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
import ntpath
import traceback
import win32api
from PyQt5.QtWidgets import QMessageBox
def show_error(title="An error has occurred!", text="Task failed successfully", details=True):
message_box = QMessageBox(QMessageBox.Critical, str(title), str(text))
if details:
error = str(traceback.format_exc())
print(error)
message_box.setDetailedText(error)
else:
print(title, text)
message_box.exec_()
def get_file_name(path):
file_name = None
prop_name = 'ProductName'
if path.endswith(".exe"):
try:
# \VarFileInfo\Translation returns list of available (language, codepage)
# pairs that can be used to retrieve string info. We are using only the first pair.
lang, codepage = win32api.GetFileVersionInfo(path, '\\VarFileInfo\\Translation')[0]
str_info_path = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, prop_name)
file_name = win32api.GetFileVersionInfo(path, str_info_path)
except Exception as e:
print(e)
return file_name if file_name else ntpath.split(path)[1]
def get_file_properties(fname):
"""
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
fixed_info = win32api.GetFileVersionInfo(fname, '\\')
props['FixedFileInfo'] = fixed_info
props['FileVersion'] = "%d.%d.%d.%d" % (fixed_info['FileVersionMS'] / 65536,
fixed_info['FileVersionMS'] % 65536,
fixed_info['FileVersionLS'] / 65536,
fixed_info['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(fname, '\\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(fname, str_info_path)
props['StringFileInfo'] = str_info
except:
pass
return props