-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyinstaller.py
135 lines (117 loc) · 4.62 KB
/
pyinstaller.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
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
from __future__ import print_function
import os
import platform
import subprocess
import shutil
from distutils import dir_util
from FileSort import __version__
def _install_pyinstaller(pyinstaller_path):
subprocess.call("pip install pyinstaller", shell=True)
# try to install pyinstaller if not installed
if not os.path.exists(pyinstaller_path):
os.mkdir(pyinstaller_path)
def _run_bin(pyinstaller_path):
# run the binary to test if working
file_sort_bin = os.path.join(pyinstaller_path, 'dist', 'FileSort',
'FileSort')
if platform.system() == 'Windows':
file_sort_bin += '.exe'
print("Trying to execute %s" % file_sort_bin)
retcode = os.system(file_sort_bin)
if retcode != 0:
raise Exception("Binary not working")
def _windows_version_file(VERSION):
template = """# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers={version_tuple},
prodvers={version_tuple},
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x0,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x4,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x1,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'000004b0',
[StringStruct(u'Comments', u'This executable was created with pyinstaller'),
StringStruct(u'CompanyName', u'Alexander Hanl'),
StringStruct(u'FileDescription', u'File sorter - to sort especially images by their date'),
StringStruct(u'FileVersion', u'{VERSION}'),
StringStruct(u'LegalCopyright', u'Copyright 2018 Alexander Hanl'),
StringStruct(u'ProductName', u'FileSort'),
StringStruct(u'ProductVersion', u'{VERSION}')])
]),
VarFileInfo([VarStruct(u'Translation', [0, 1200])])
]
)"""
if "-" in VERSION:
VERSION, _ = VERSION.split("-")
version_tuple = tuple([int(v) for v in VERSION.split(".")] + [0])
return template.format(VERSION=VERSION, version_tuple=version_tuple)
def pyinstall(source_folder):
pyinstaller_path = os.path.join(os.getcwd(), 'pyinstaller')
_install_pyinstaller(pyinstaller_path)
command = "pyinstaller" # "python pyinstaller.py"
try:
shutil.rmtree(os.path.join(pyinstaller_path))
except Exception as e:
print("Unable to remove old folder", e)
file_sort_path = os.path.join(source_folder, 'FileSort.py')
hidden = "--hidden-import=glob"
if platform.system() != "Windows":
hidden += " --hidden-import=setuptools.msvc"
win_ver = ""
else:
win_ver_file = os.path.join(pyinstaller_path, 'windows-version-file')
content = _windows_version_file(__version__)
def save(path, content):
"""
Saves a file with given content
Params:
path: path to write file to
load: contents to save in the file
"""
try:
os.makedirs(os.path.dirname(path))
except:
pass
with open(path, "wb") as handle:
try:
handle.write(bytes(content, "utf-8"))
except:
handle.write(bytes(content))
save(win_ver_file, content)
win_ver = "--version-file %s" % win_ver_file
if not os.path.exists(pyinstaller_path):
os.mkdir(pyinstaller_path)
subprocess.call('%s -y -p %s --console %s %s %s'
% (command, source_folder, file_sort_path, hidden, win_ver),
cwd=pyinstaller_path, shell=True)
_run_bin(pyinstaller_path)
file_sort_bin = os.path.join(pyinstaller_path, 'dist', 'build', 'FileSort')
return os.path.abspath(os.path.join(pyinstaller_path, 'dist', 'build', 'FileSort'))
if __name__ == "__main__":
source_folder = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
output_folder = pyinstall(source_folder)
print("\n**************FileSort binaries created!******************\n"
"\nAppend this folder to your system PATH: '%s'\n"
"Feel free to move the whole folder to another location." % output_folder)