-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
175 lines (151 loc) · 5.97 KB
/
setup.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding:Utf-8 -*
import os
import sys
import platform
import argparse
import glob
from zipfile import ZipFile, ZIP_DEFLATED
from cx_Freeze import setup, Executable
from my_pygame.resources import ResourcesCompiler
def zip_compress():
#pylint: disable=unused-variable
zip_filename = "{}-{}.zip".format(
executable_infos["project_name"],
platform.system(),
).replace(" ", "_")
print(f"Compressing executable in {zip_filename}...")
output_folder = options.get("build_exe", ".")
output_zip = os.path.join(output_folder, zip_filename)
all_files = list()
all_executables = [exec_file["name"] for exec_file in executable_infos["executables"]]
pattern_list = [*all_executables, "lib", *options["include_files"]]
if sys.platform.startswith("win"):
pattern_list.extend(["python*.dll", "vcruntime*.dll"])
if output_folder != ".":
for include in options["include_files"]:
ResourcesCompiler.compile(os.path.join(output_folder, include), delete=True)
for pattern in pattern_list:
pattern = os.path.join(output_folder, pattern)
for path in glob.glob(pattern):
if os.path.isfile(path):
all_files.append({"filename": path, "arcname": path.replace(output_folder, ".")})
elif os.path.isdir(path):
for root, folders, files in os.walk(path):
for file in files:
file = os.path.join(root, file)
all_files.append({"filename": file, "arcname": file.replace(output_folder, ".")})
with ZipFile(output_zip, "w", compression=ZIP_DEFLATED) as zip_file_fp:
for file in all_files:
zip_file_fp.write(**file)
def get_application_version(app: str) -> str:
app_globals = dict()
with open(os.path.join(app, "version.py")) as file:
exec(file.read(), app_globals)
return app_globals["__version__"]
#############################################################################
# Parsing des arguments
parser = argparse.ArgumentParser(prog="setup.py", description="Setup for executable freezing")
parser.add_argument("--zip", help="Create a zip file when it's finished", action="store_true")
parser.add_argument("--zip-no-build", help="Create a zip file without build project", action="store_true")
parser.add_argument("--version", help="Use a custom version instead of applcation version")
args = parser.parse_args()
#############################################################################
# Recupération des valeurs
application = "py_game_case"
dependencies = ["pygame", "my_pygame", "psutil", "navy", "four_in_a_row"]
additional_files = ["resources"]
executable_infos = {
"project_name": "Py-Game-Case",
"description": "Py-Game-Case - A library of board games using pygame",
"author": "Francis Clairicia-Rose-Claire-Josephine",
"version": args.version or get_application_version(application),
"executables": [
{
"script": "run.pyw",
"name": "Py_game_case",
"base": "Win32GUI",
"icon": "resources/py_game_case/img/icon.ico"
}
],
"copyright": "Copyright (c) 2020 Francis Clairicia-Rose-Claire-Josephine"
}
options = {
"path": sys.path,
"build_exe": os.path.join(sys.path[0], "build"),
"includes": [application, *dependencies],
"excludes": [],
"include_files": additional_files,
"optimize": 0,
"silent": True
}
print("-----------------------------------{ cx_Freeze }-----------------------------------")
print("Project Name: {project_name}".format(**executable_infos))
print("Author: {author}".format(**executable_infos))
print("Version: {version}".format(**executable_infos))
print("Description: {description}".format(**executable_infos))
print()
for i, infos in enumerate(executable_infos["executables"], start=1):
print(f"Executable number {i}")
print("Name: {name}".format(**infos))
print("Icon: {icon}".format(**infos))
print()
print("Dependencies: {includes}".format(**options))
print("Additional files/folders: {include_files}".format(**options))
print()
while True:
OK = input("Is this ok ? (y/n) : ").lower()
if OK in ("y", "n"):
break
if OK == "n":
sys.exit(0)
print("-----------------------------------------------------------------------------------")
for infos in executable_infos["executables"]:
if sys.platform.startswith("win"):
infos["name"] += ".exe"
else:
infos["base"] = None
if args.zip_no_build:
zip_compress()
sys.exit(0)
if "tkinter" not in options["includes"]:
options["excludes"].append("tkinter")
# pour inclure sous Windows les dll system de Windows necessaires
if sys.platform.startswith("win"):
options["include_msvcr"] = True
#############################################################################
# preparation de la cible
executables = list()
for infos in executable_infos["executables"]:
target = Executable(
script=os.path.join(sys.path[0], infos["script"]),
base=infos["base"],
targetName=infos["name"],
icon=infos["icon"],
copyright=executable_infos["copyright"]
)
executables.append(target)
#############################################################################
# creation du setup
sys.argv = [sys.argv[0], "build_exe"]
try:
result = str()
setup(
name=executable_infos["project_name"],
version=executable_infos["version"],
description=executable_infos["description"],
author=executable_infos["author"],
options={"build_exe": options},
executables=executables
)
except Exception as e:
result = f"{e.__class__.__name__}: {e}"
else:
result = "Build done"
if args.zip:
zip_compress()
finally:
print("-----------------------------------------------------------------------------------")
print(result)
print("-----------------------------------------------------------------------------------")
if sys.platform.startswith("win"):
os.system("pause")