-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
132 lines (107 loc) · 4.08 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
import distutils
import platform
import sys
import zipfile
from pathlib import Path
from typing import Union, Sequence, Dict
import os
from setuptools import find_packages, setup
NAME = "openopal"
EXE_NAME = "open-opal"
VERSION = "0.1.0"
required_packages = find_packages(exclude=["*demos.*", "*demos"])
with open('requirements.txt') as f:
required = [line for line in f.read().splitlines()
if not line.startswith("-") and not line.startswith("git+") and not line.startswith("#")]
def zip_dir(dir: Union[Path, str], filename: Union[Path, str]):
"""Zip the provided directory without navigating to that directory using `pathlib` module"""
# Convert to Path object
dir = Path(dir)
with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as zip_file:
for entry in dir.rglob("*"):
zip_file.write(entry, entry.relative_to(dir))
class Distribution(distutils.cmd.Command):
description = "Distribute with pyinstaller"
user_options = [
("zip", None, "Create a zip package"),
("macos-universal2", None, "Create MacOS universal2 package")
]
@staticmethod
def _create_cmd(command: str, modules: Sequence) -> Sequence[str]:
tokens = []
for module in modules:
tokens.append(command)
tokens.append(module)
return tokens
def run(self) -> None:
import PyInstaller.__main__
# find system name
system_name = sys.platform
system_arch = platform.machine()
# correct system name
if system_name.startswith("linux"):
system_name = "linux"
elif system_name.startswith("darwin"):
system_name = "macosx"
elif system_name.startswith("win"):
system_name = "windows"
# additional arguments
collect_binaries_modules = ["depthai", "pyvirtualcam"]
collect_data_modules = []
collect_all_modules = ["nanogui", "cv2"]
additional_files: Dict[str, str] = {}
additional_files["README.md"] = "."
system_asset_dir = Path(f"assets").joinpath(system_name)
if system_asset_dir.exists():
additional_files[str(system_asset_dir)] = "."
# create arguments
delimiter = ";" if sys.platform.startswith("win") else ":"
additional_files_args = [f"{src}{delimiter}{dsc}" for src, dsc in additional_files.items()]
arguments = [
f"{NAME}/__main__.py",
"--name", EXE_NAME,
"--onefile",
"--clean",
"--icon=assets/open-opal.ico",
"-y",
*self._create_cmd("--add-data", additional_files_args),
*self._create_cmd("--collect-binaries", collect_binaries_modules),
*self._create_cmd("--collect-data", collect_data_modules),
*self._create_cmd("--collect-all", collect_all_modules)
]
if sys.platform == "darwin" and self.macos_universal2:
print("building universal binary")
arguments.append("--target-arch")
arguments.append("universal2")
print("Arguments: pyinstaller %s" % " ".join(arguments))
PyInstaller.__main__.run(arguments)
if self.zip:
print("creating zip file...")
build_system_info = f"{system_name}-{system_arch}".lower()
release_dir = "release"
os.makedirs(release_dir, exist_ok=True)
zip_dir(f"dist/", f"{release_dir}/{EXE_NAME}-{build_system_info}.zip")
def initialize_options(self) -> None:
self.zip = False
self.macos_universal2 = False
def finalize_options(self) -> None:
pass
setup(
name=NAME,
version=VERSION,
packages=required_packages,
entry_points={
'console_scripts': [
f'{EXE_NAME} = openopal.__main__:main',
],
},
url='https://github.com/cansik/open-opal-c1',
license='MIT License',
author='Florian Bruggisser',
author_email='github@broox.ch',
description='A simple Opal C1 controller software.',
install_requires=required,
cmdclass={
"distribute": Distribution,
},
)