forked from roam-qgis/Roam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
176 lines (160 loc) · 7.28 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
176
import os
import glob
import shutil
from setuptools import setup, find_packages
from setuptools.command.build import build
import subprocess
from cx_Freeze import Executable
import datetime
# cx_Freeze build configuration
icon = r"src\roam\resources\branding\icon.ico"
config_icon = r"src\roam\resources\branding\config.ico"
roam_exe = Executable(script=r"src\roam\__main__.py",
icon=icon,
targetName="Roam",
base="Win32GUI")
configmanager_exe = Executable(script=r"src\configmanager\__main__.py",
icon=config_icon,
targetName="Config Manager",
base="Win32GUI")
executables=[roam_exe, configmanager_exe]
build_path = os.path.join("build","Roam")
packages = find_packages("./src")
excludes = ["matplotlib", "scipy", "numpy", "disutils", "PIL"]
# Get environment variables
osgeo_path = os.environ["OSGEO4W_ROOT"]
qgis_path = os.environ["QGIS"]
python_path = os.environ["PYTHONHOME"]
try:
lrelease = os.path.join(os.environ["LRELEASE"], "lrelease.exe")
except KeyError:
lrelease = ""
# Get additional required files
def format_paths(wildcard_path, new_folder="lib"):
dist_paths = []
for path in glob.glob(wildcard_path):
dist_paths.append((path, os.path.join(new_folder, os.path.basename(path))))
return dist_paths
include_files = []
osgeo_utils = ["ogr2ogr.exe", "ogrinfo.exe", "gdalinfo.exe", "NCSEcw.dll"]
for osgeo_util in osgeo_utils:
include_files += format_paths(os.path.join(osgeo_path, "bin", osgeo_util))
include_files += format_paths(os.path.join(osgeo_path, "bin", "*.dll"))
include_files += format_paths(os.path.join(osgeo_path, "apps", "qt5", "bin", "*.dll"))
include_files += format_paths(os.path.join(osgeo_path, "apps", "qt5", "plugins", "sqldrivers", "*"))
include_files += format_paths(os.path.join(osgeo_path, "share", "proj", "proj.db"), new_folder=r"lib\proj")
include_files += format_paths(os.path.join(osgeo_path, "apps", "gdal", "share", "gdal","*"), new_folder=r"lib\gdal")
include_files += format_paths(os.path.join(osgeo_path, "apps", "gdal", "lib", "gdalplugins", "*.dll"))
include_files += format_paths(os.path.join(qgis_path, "bin", "*.dll"))
include_files += format_paths(os.path.join(qgis_path, "plugins", "*.dll"), new_folder=r"lib\qgis\plugins")
include_files += format_paths(os.path.join(qgis_path, "resources", "*.db"), new_folder=r"lib\qgis\resources")
include_files += format_paths(os.path.join(qgis_path, "svg", "*"), new_folder=r"lib\qgis\svg")
include_files += format_paths(os.path.join("lib", "*.dll"), new_folder=r"lib\qgis")
include_files += format_paths("gpl-3.0.rtf", new_folder=r".")
include_files += format_paths(os.path.join("src", "roam.config"), new_folder=r".")
include_files += format_paths(os.path.join("src", "plugins", "*"), new_folder=r"plugins")
include_files += format_paths(os.path.join("src", "roam", "templates", "*"), new_folder=r"lib\roam\templates")
include_files += format_paths(os.path.join("src", "configmanager", "templates", "*"), new_folder=r"lib\configmanager\templates")
qt_plugins = ["crypto", "generic", "iconengines", "imageformats", "mediaservice", "platforminputcontexts", "platforms", "position", "sqldrivers", "styles"]
for qt_plugin in qt_plugins:
include_files += format_paths(os.path.join(osgeo_path, "apps", "qt5", "plugins", qt_plugin, "*"), new_folder=os.path.join("lib", "qtplugins", qt_plugin))
print("Including extra files:")
for file, finalpath in sorted(include_files):
print(f"\t - {file} -> {finalpath}")
# Compile QT ui and ts files
def build_qt():
def _hashcheck(file):
import hashlib
hasher = hashlib.md5()
with open(file, "rb") as f:
buf = f.read()
hasher.update(buf)
hash = hasher.hexdigest()
filehash = hashes.get(file, "")
hashes[file] = hash
if hash != filehash:
print("Hash changed for: {0}, Got {2} wanted {1}".format(file, hash, filehash))
return False
else:
return True
import json
hashes = {}
try:
with open(".roambuild") as f:
hashes = json.load(f)
except Exception:
hashes = {}
print("Building QT ui and ts files")
pyuic5 = os.path.join(python_path, "Scripts", "pyuic5.exe")
pyrcc5 = os.path.join(python_path, "Scripts", "pyrcc5.exe")
HASHFILES = [".ui", ".qrc", ".ts"]
for folder_to_scan in [os.path.join("src", "roam"), os.path.join("src", "configmanager")]:
for root, _, files in os.walk(folder_to_scan):
for file in files:
file_path = os.path.join(root, file)
file_name, file_ext = os.path.splitext(file_path)
if file_ext in HASHFILES and _hashcheck(file_path):
print(f"Skipping {file_name} - Hash match")
continue
if file_ext == ".ui":
compiled_file = file_name + ".py"
subprocess.run([pyuic5, "-o", compiled_file, file_path], shell=True)
elif file_ext == ".qrc":
compiled_file = file_name + "_rc.py"
subprocess.run([pyrcc5, "-o", compiled_file, file_path], shell=True)
elif file_ext == ".ts" and lrelease != "":
compiled_file = file_name + ".qm"
subprocess.run([lrelease, file_path, "-qm", compiled_file], shell=True)
with open(".roambuild", "w") as f:
json.dump(hashes, f)
# Tidy up after setup
def tidy_up():
buildfolder = build_path
print("Removing pyqt5.uic extras")
shutil.rmtree(os.path.join(buildfolder, "PyQt5.uic.widget-plugins"))
print("Create projects folder")
os.mkdir(os.path.join(buildfolder, "projects"))
# Function to update files that have a modified date earlier than 1980 - Python zipfile won't work on these files
def update_modified_date(folder_path):
current_date = datetime.datetime.now()
for root, dirs, files in os.walk(folder_path):
for file_name in files:
file_path = os.path.join(root, file_name)
if os.path.isfile(file_path):
modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
if modified_date.year < 1980:
os.utime(file_path, (current_date.timestamp(), current_date.timestamp()))
print(f"Modified date of {file_path} updated to {current_date}")
# Class to enable tasks to be run before and after setuptools
class SetupCommandWrapper(build):
def run(self):
print("Running pre-setup commands...")
build_qt()
update_modified_date(osgeo_path)
super().run()
print("Running post-setup commands...")
tidy_up()
update_modified_date(build_path)
print("Build complete")
# Additional options for cx_Freeze
cx_freeze_options = {
"build_exe": build_path,
"packages": packages,
"include_files": include_files,
"excludes": excludes,
"include_msvcr": True
}
# Setuptools setup
setup(
name="roam",
version="3.1b0",
description="Roam Field Data Collection",
author="Nathan Woodrow",
maintainer="Terry McDonell",
license="GPLv3",
cmdclass={
"build": SetupCommandWrapper
},
options={"build_exe": cx_freeze_options},
executables=executables
)