forked from JacquesLucke/animation_nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
365 lines (283 loc) · 11 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'''
Copyright (C) 2016 Jacques Lucke
Created by Jacques Lucke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
'''
Command Line Arguments:
python setup.py
--all # recompile all
--export # make redistributable version
--nocopy # don't copy the build into Blenders addon directory
--noversioncheck # allow to create a build with any Python version
Generate .html files to debug cython code:
cython -a path/to/file.pyx
Cleanup Repository:
git clean -fdx # make sure you don't have uncommited files!
'''
import os
import sys
import shutil
import traceback
from os.path import abspath, dirname, join, relpath
addonName = "animation_nodes"
currentDirectory = dirname(abspath(__file__))
sourceDirectory = join(currentDirectory, addonName)
configPath = join(currentDirectory, "config.py")
defaultConfigPath = join(currentDirectory, "config.default.py")
compilationInfoPath = join(sourceDirectory, "compilation_info.json")
config = {}
initialArgs = sys.argv[:]
expectedArgs = {"--all", "--export", "--nocopy", "--noversioncheck"}
unknownArgs = set(initialArgs[1:]) - expectedArgs
if len(unknownArgs) > 0:
print("Unknown arguments:", unknownArgs)
print("Allowed arguments:", expectedArgs)
sys.exit()
v = sys.version_info
if "--noversioncheck" not in initialArgs and (v.major != 3 or v.minor != 5):
print("Blender 2.78/2.79 officially uses Python 3.5.x.")
print("You are using: {}".format(sys.version))
print()
print("Use the --noversioncheck argument to disable this check.")
sys.exit()
else:
print(sys.version)
print()
def main():
setupAndReadConfigFile()
if canCompile():
preprocessor()
if "--all" in initialArgs:
removeCFiles()
removeCompiledFiles()
compileCythonFiles()
writeCompilationInfoFile()
if "--export" in initialArgs:
export()
if not "--nocopy" in initialArgs:
if os.path.isdir(config["addonsDirectory"]):
copyToBlender()
else:
print("The path to Blenders addon directory does not exist")
print("Please correct the config.py file.")
def setupAndReadConfigFile():
if not os.path.isfile(configPath) and os.path.isfile(defaultConfigPath):
shutil.copyfile(defaultConfigPath, configPath)
print("Copied the config.default.py file to config.py")
print("Please change it manually if needed.")
print("Note: git ignores it, so depending on the settings of your editor")
print(" it might not be shown inside it.\n\n")
if os.path.isfile(configPath):
configCode = readFile(configPath)
exec(configCode, config, config)
else:
print("Cannot find any of these files: config.py, config.default.py ")
print("Make sure that at least the config.default.py exists.")
print("Maybe you have to clone the repository again.")
sys.exit()
def canCompile():
if "bpy" in sys.modules:
return False
if not os.path.isdir(sourceDirectory):
return False
correctSysPath()
try:
import Cython
return True
except:
print("Cython is not installed for this Python version.")
print(sys.version)
return False
def correctSysPath():
pathsToRemove = [path for path in sys.path if currentDirectory in path]
for path in pathsToRemove:
sys.path.remove(path)
print("Removed from sys.path:", path)
# Preprocess - execute .pre files
###################################################################
def preprocessor():
for path in iterPathsWithSuffix(".pre"):
code = readFile(path)
codeBlock = compile(code, path, "exec")
context = {
"__file__" : abspath(path),
"readFile" : readFile,
"writeFile" : writeFile,
"multiReplace" : multiReplace,
"dependenciesChanged" : dependenciesChanged,
"changeFileName" : changeFileName}
exec(codeBlock, context, context)
# Translate .pyx to .c files and compile extension modules
###################################################################
def compileCythonFiles():
from distutils.core import setup
from Cython.Build import cythonize
sys.argv = [sys.argv[0], "build_ext", "--inplace"]
extensions = cythonize(getPathsToCythonFiles())
setup(name = 'Animation Nodes', ext_modules = extensions)
print("Compilation Successful.")
def getPathsToCythonFiles():
return list(iterPathsWithSuffix(".pyx"))
def removeCFiles():
for path in iterPathsWithSuffix(".c"):
os.remove(path)
print("Remove generated .c files.")
def removeCompiledFiles():
for path in iterPathsWithSuffix(".so"):
os.remove(path)
for path in iterPathsWithSuffix(".pyd"):
os.remove(path)
print("Remove compiled files.")
# Compilation Info File
###################################################################
def writeCompilationInfoFile():
import Cython
info = {}
info["sys.version"] = sys.version
info["sys.platform"] = sys.platform
info["sys.api_version"] = sys.api_version
info["sys.version_info"] = sys.version_info
info["Cython.__version__"] = Cython.__version__
info["os.name"] = os.name
import json
writeFile(compilationInfoPath, json.dumps(info, indent = 4))
# Copy to Blenders addons directory
###################################################################
def copyToBlender():
print("\n\nCopy changes to addon folder")
targetPath = join(config["addonsDirectory"], addonName)
try:
copyAddonFiles(sourceDirectory, targetPath, verbose = True)
except PermissionError:
traceback.print_exc()
print("\n\nMaybe this error happens because Blender is running.")
sys.exit()
print("\nCopied all changes")
# Export Build
###################################################################
def export():
print("\nStart Export")
targetPath = join(currentDirectory, addonName + ".zip")
zipAddonDirectory(sourceDirectory, targetPath)
print("Finished Export")
print("Zipped file can be found here:")
print(" " + targetPath)
# Copy Addon Utilities
###################################################################
def copyAddonFiles(source, target, verbose = False):
if not os.path.isdir(target):
os.mkdir(target)
existingFilesInSource = set(iterRelativeAddonFiles(source))
existingFilesInTarget = set(iterRelativeAddonFiles(target))
counter = 0
filesToRemove = existingFilesInTarget - existingFilesInSource
for relativePath in filesToRemove:
path = join(target, relativePath)
removeFile(path)
if verbose: print("Removed File: ", path)
counter += 1
filesToCreate = existingFilesInSource - existingFilesInTarget
for relativePath in filesToCreate:
sourcePath = join(source, relativePath)
targetPath = join(target, relativePath)
copyFile(sourcePath, targetPath)
if verbose: print("Created File: ", targetPath)
counter += 1
filesToUpdate = existingFilesInSource.intersection(existingFilesInTarget)
for relativePath in filesToUpdate:
sourcePath = join(source, relativePath)
targetPath = join(target, relativePath)
sourceModificationTime = os.stat(sourcePath).st_mtime
targetModificationTime = os.stat(targetPath).st_mtime
if sourceModificationTime > targetModificationTime:
overwriteFile(sourcePath, targetPath)
if verbose: print("Updated File: ", targetPath)
counter += 1
print("Changed {} files.".format(counter))
def removeFile(path):
try:
os.remove(path)
except:
if tryGetFileAccessPermission(path):
os.remove(path)
def copyFile(source, target):
directory = dirname(target)
if not os.path.isdir(directory):
os.makedirs(directory)
shutil.copyfile(source, target)
def overwriteFile(source, target):
removeFile(target)
copyFile(source, target)
def iterRelativeAddonFiles(directory):
if not os.path.isdir(directory):
return
for root, folders, files in os.walk(directory, topdown = True):
for folder in folders:
if ignoreAddonDirectory(folder):
folders.remove(folder)
for fileName in files:
if not ignoreAddonFile(fileName):
yield relpath(join(root, fileName), directory)
def ignoreAddonFile(name):
return name.endswith(".c") or name.endswith(".html")
def ignoreAddonDirectory(name):
return name in {".git", "__pycache__"}
def tryRemoveDirectory(path):
try: shutil.rmtree(path, onerror = handlePermissionError)
except FileNotFoundError: pass
def handlePermissionError(function, path, excinfo):
if tryGetFileAccessPermission(path):
function(path)
else:
raise
def tryGetFileAccessPermission(path):
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
return True
return False
def zipAddonDirectory(sourcePath, targetPath):
try: os.remove(targetPath)
except FileNotFoundError: pass
import zipfile
with zipfile.ZipFile(targetPath, "w", zipfile.ZIP_DEFLATED) as zipFile:
for relativePath in iterRelativeAddonFiles(sourcePath):
absolutePath = join(sourcePath, relativePath)
zipFile.write(absolutePath, join(addonName, relativePath))
# Utils
###################################################################
def iterPathsWithSuffix(suffix):
for root, dirs, files in os.walk(sourceDirectory):
for fileName in files:
if fileName.endswith(suffix):
yield join(root, fileName)
def writeFile(path, content):
with open(path, "wt") as f:
f.write(content)
print("Changed File:", path)
def readFile(path):
with open(path, "rt") as f:
return f.read()
def changeFileName(path, newName):
return join(dirname(path), newName)
def multiReplace(text, **replacements):
for key, value in replacements.items():
text = text.replace(key, value)
return text
def dependenciesChanged(target, dependencies):
try: targetTime = os.stat(target).st_mtime
except FileNotFoundError: targetTime = 0
latestDependencyModification = max(os.stat(path).st_mtime for path in dependencies)
return targetTime < latestDependencyModification
main()