forked from conan-io/conan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyinstaller.py
161 lines (137 loc) · 5.94 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
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
"""
This file is able to create a self contained Conan executable that contains all it needs,
including the Python interpreter, so it wouldnt be necessary to have Python installed
in the system
It is important to install the dependencies and the project first with "pip install -e ."
which configures the project as "editable", that is, to run from the current source folder
After creating the executable, it can be pip uninstalled
$ pip install -e .
$ python pyinstaller.py
This has to run in the same platform that will be using the executable, pyinstaller does
not cross-build
The resulting executable can be put in the system PATH of the running machine
"""
import argparse
import os
import platform
import shutil
import subprocess
import sys
from conans import __version__
from conans.util.files import save
def _install_pyinstaller(pyinstaller_path):
subprocess.call(f"{sys.executable} -m 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
conan_bin = os.path.join(pyinstaller_path, 'dist', 'conan', 'conan')
if platform.system() == 'Windows':
conan_bin = '"' + conan_bin + '.exe' + '"'
retcode = os.system(conan_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'JFrog'),
StringStruct(u'FileDescription', u'Conan C, C++ Open Source Package Manager'),
StringStruct(u'FileVersion', u'{version}'),
StringStruct(u'LegalCopyright', u'Copyright 2020 JFrog'),
StringStruct(u'ProductName', u'Conan'),
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, onefile=False):
pyinstaller_path = os.path.join(os.getcwd(), 'pyinstaller')
_install_pyinstaller(pyinstaller_path)
try:
shutil.rmtree(os.path.join(pyinstaller_path))
except Exception as e:
print("Unable to remove old folder", e)
conan_path = os.path.join(source_folder, 'conans', 'conan.py')
hidden = ("--hidden-import=glob " # core stdlib
"--hidden-import=pathlib "
# Modules that can be imported in ConanFile conan.tools and errors
"--collect-submodules=conan.cli.commands "
"--hidden-import=conan.errors "
"--hidden-import=conan.tools.microsoft "
"--hidden-import=conan.tools.gnu --hidden-import=conan.tools.cmake "
"--hidden-import=conan.tools.meson --hidden-import=conan.tools.apple "
"--hidden-import=conan.tools.build --hidden-import=conan.tools.env "
"--hidden-import=conan.tools.files "
"--hidden-import=conan.tools.google --hidden-import=conan.tools.intel "
"--hidden-import=conan.tools.layout --hidden-import=conan.tools.premake "
"--hidden-import=conan.tools.qbs --hidden-import=conan.tools.scm "
"--hidden-import=conan.tools.system --hidden-import=conan.tools.system.package_manager")
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__)
save(win_ver_file, content)
win_ver = ["--version-file", win_ver_file]
if not os.path.exists(pyinstaller_path):
os.mkdir(pyinstaller_path)
if onefile:
distpath = os.path.join(pyinstaller_path, "dist", "conan")
else:
distpath = os.path.join(pyinstaller_path, "dist")
command_args = [conan_path, "--noconfirm", f"--paths={source_folder}", "--console", f"--distpath={distpath}"]
command_args.extend(hidden.split(" "))
if win_ver:
command_args.extend(win_ver)
if onefile:
command_args.append("--onefile")
import PyInstaller.__main__
PyInstaller.__main__.run(command_args)
_run_bin(pyinstaller_path)
return os.path.abspath(os.path.join(pyinstaller_path, 'dist', 'conan'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--onefile', action='store_true')
parser.set_defaults(onefile=False)
args = parser.parse_args()
src_folder = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
output_folder = pyinstall(src_folder, args.onefile)
print("\n**************Conan binaries created!******************\n"
"\nAppend this folder to your system PATH: '%s'\n"
"Feel free to move the whole folder to another location." % output_folder)