-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_script.py
133 lines (108 loc) · 5.06 KB
/
build_script.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
#!/usr/bin/env python3
"""
SPDX-License-Identifier: AGPL-3.0-or-later
Copyright (c) 2023 [email protected] https://github.com/eadf
This file is part of the hallr crate.
"""
# This script prepares and packages the blender addon files under the
# `blender_addon_exported` folder.
if __name__ == "__main__":
import subprocess
import time
import os
import platform
import shutil
import re
import glob
import argparse
parser = argparse.ArgumentParser(description="A script that packages the hallr blender addon files")
parser.add_argument(
"--dev_mode",
action="store_true",
help="Enable development mode"
)
args = parser.parse_args()
# Check if the current directory is a Rust project
if not os.path.isfile("Cargo.toml"):
print("Error: This directory does not contain a Cargo.toml file.")
exit(1)
# Validate the Cargo.toml file to ensure it's the correct Rust project
with open("Cargo.toml", "r") as f:
content = f.read()
if "name = \"hallr\"" not in content:
print("Error: The Cargo.toml file does not specify the project name as 'hallr'. Are you in the correct cwd?")
exit(1)
# Run the cargo build command
# Run the command
result = subprocess.run(["cargo", "build", "--release"])
# result = subprocess.run(["cargo", "build", "--release", "--features", "display_sdf_chunks"])
# Check the return status
if result.returncode != 0:
print(f"Cargo command failed with return code {result.returncode}.")
exit(1)
# Get the current timestamp
timestamp = str(int(time.time()))
# Determine the library extension based on the platform
system = platform.system()
library_extension = ".dylib" # Default to macOS
if system == "Linux":
library_extension = ".so"
elif system == "Windows":
library_extension = ".dll"
source_directory = 'blender_addon'
destination_directory = 'blender_addon_exported'
dest_lib_directory = os.path.join(destination_directory, "lib")
# Check if the destination directory exists, and create it if not
os.makedirs(destination_directory, exist_ok=True)
# Ensure the directory exists or create it if it doesn't
os.makedirs(dest_lib_directory, exist_ok=True)
# Rename and move the library file
lib_files = [f for f in os.listdir("target/release") if f.startswith("libhallr") and f.endswith(library_extension)]
if len(lib_files) == 0:
print(f"Could not find the libfile in ´target/release´.")
exit(1)
if args.dev_mode:
old_lib_files = [f for f in os.listdir(dest_lib_directory) if
f.startswith("libhallr_") and f.endswith(library_extension)]
for lib_file in old_lib_files:
old_file = os.path.join(dest_lib_directory, lib_file)
os.remove(old_file)
for lib_file in lib_files:
if args.dev_mode:
new_name = os.path.join(dest_lib_directory, f"libhallr_{timestamp}{library_extension}")
else:
new_name = os.path.join(dest_lib_directory,lib_file)
shutil.copy(f"target/release/{lib_file}", new_name)
file_extension = '.py'
# Get a list of all files with the specified extension in the source directory
source_files = glob.glob(f"{source_directory}/*{file_extension}")
# Copy each selected file to the destination directory
for source_file in source_files:
# Use shutil.copy to copy the file
shutil.copy(source_file, os.path.join(destination_directory, os.path.basename(source_file)))
base_directory = os.getcwd() # Get the current working directory
# Paths to be replaced
addon_exported_path = os.path.join(base_directory, 'blender_addon_exported')
target_release_path = os.path.join(base_directory, addon_exported_path, 'lib')
# List files in the directory (non-recursively)
file_list = [f for f in os.listdir(addon_exported_path) if
os.path.isfile(os.path.join(addon_exported_path, f)) and f.endswith(".py")]
# Do find and replace on the .py files
for file in file_list:
file_path = os.path.join(addon_exported_path, file)
with open(file_path, 'r') as f:
content = f.read()
content = re.sub(r'HALLR__BLENDER_ADDON_PATH', addon_exported_path, content)
content = re.sub(r'HALLR__TARGET_RELEASE', target_release_path, content)
if args.dev_mode:
content = re.sub(r'DEV_MODE = False', 'DEV_MODE = True', content)
content = re.sub(r'from . import', 'import', content)
with open(file_path, 'w') as f:
f.write(content)
if not args.dev_mode:
subprocess.run("mv blender_addon_exported hallr", shell=True)
subprocess.run("zip -r hallr.zip hallr", shell=True)#,cwd=addon_exported_path)
subprocess.run("mv hallr blender_addon_exported", shell=True)
print("Created a new hallr.zip file in the root, install it as an addon in blender.")
else:
print("Updated the files under blender_addon_exported, use blender to run __init__.py")