-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-tool.py
179 lines (136 loc) · 5.42 KB
/
build-tool.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
#!/usr/bin/env python3
"""
This is a script to assist with RV's package development in order to iterate and deploy easily.
It searches inside the 'plugin' folder and adds the contents to a zip ".rvpkg" file.
It can then install and add the rvpkg to the RV installation.
It can start/restart RV to pick up the latest changes.
"""
__author__ = "Gabriela Roque Lopez"
__version__ = "0.0.1"
__license__ = "GPL-3.0"
import argparse
import os
import platform
import subprocess
import zipfile
from pathlib import Path
PLATFORM_NAME = platform.system()
# Location of the rvpkg app on different platforms
MACOS_RVPKG = "/Applications/RV.app/Contents/MacOS/rvpkg"
WINDOWS_RVPKG = "C:\\Program Files\\Shotgun\\RV-2021.0.0\\bin\\rvpkg.exe"
LINUX_RVPKG = "rvpkg"
# Location of the installation directory on different platforms
MACOS_PATH_TO_AREA = "~/Library/Application\ Support/RV"
WINDOWS_PATH_TO_AREA = "{}\\AppData\\Roaming\\RV"
LINUX_PATH_TO_AREA = "~/.rv/"
def get_package_info():
"""
Looks up the Package name and Version from the plugin PACKAGE file
This is done manually to avoid having to install the pyyaml dependency
Assumes the PACKAGE file is inside the 'plugin' folder
Returns:
tuple (str, float): The name and latest version of this plugin
"""
package_filename = os.path.join('plugin', 'PACKAGE')
name = ""
version = 0.1
with open(package_filename) as file:
for line in file:
element = line.strip().split(':')
if element[0] == 'package':
name = element[1].strip()
elif element[0] == 'version':
version = element[1].strip()
return name, version
PACKAGE_NAME, PACKAGE_VERSION = get_package_info()
PACKAGE_FILENAME = "{}-{}.rvpkg".format(PACKAGE_NAME.lower().replace(" ", "_"), PACKAGE_VERSION)
if PLATFORM_NAME == "Windows":
RVPKG = WINDOWS_RVPKG
PATH_TO_AREA = WINDOWS_PATH_TO_AREA.format(str(Path.home()))
PACKAGED_FILEPATH = "build\\{}".format(PACKAGE_FILENAME)
elif PLATFORM_NAME == "Linux":
RVPKG = LINUX_RVPKG
PATH_TO_AREA = LINUX_PATH_TO_AREA
PACKAGED_FILEPATH = "build/{}".format(PACKAGE_FILENAME)
elif PLATFORM_NAME == "Darwin":
RVPKG = MACOS_RVPKG
PATH_TO_AREA = MACOS_PATH_TO_AREA
PACKAGED_FILEPATH = "build/{}".format(PACKAGE_FILENAME)
def write_rvpkg_file(path, rvpkg_file):
"""
It grabs all the files inside the path folder and adds them to the zip rvpkg file
Args:
path: Folder where the plugin files are stored
rvpkg_file: ZipFile instance of the rvpkg file
"""
for root, dirs, files in os.walk(path):
for file in files:
rvpkg_file.write(filename=os.path.join(root, file), arcname=file)
def build():
"""
Creates a 'build' folder and packages the script files into a zip .rvpkg file
"""
try:
os.mkdir("build")
except FileExistsError:
print("Build folder already exists")
with zipfile.ZipFile(PACKAGED_FILEPATH, mode='w') as rvpkg_file:
write_rvpkg_file("plugin", rvpkg_file)
def install():
"""
Runs the 'rvpkg' program to add and install the current rvpkg file that it's inside the build folder
It takes into account the current OS to find the correct folders
"""
clean_existing_installation()
process = subprocess.run([RVPKG, "-install", "-add", PATH_TO_AREA, PACKAGED_FILEPATH],
capture_output=True)
print(process)
def clean_existing_installation():
"""
Uninstalls the plugin from RV
"""
subprocess.run([RVPKG, "-remove", "-force", PACKAGE_NAME])
def restart_rv():
"""
Kills all current instances of RV and starts a new one
"""
# TODO: Implement other for other platforms
if PLATFORM_NAME == "Windows":
subprocess.run(["taskkill", "/F", "/IM", "rv.exe", "/T"])
subprocess.run(["start", "RV"], shell=True)
print("Signal sent to start RV")
if PLATFORM_NAME == "Linux":
print("Restart on Linux not implemented yet")
if PLATFORM_NAME == "Darwin":
print("Restart on MacOS not implemented yet")
def main(args):
""" Main entry point of the app """
if args.build:
build()
if args.install:
install()
if args.restart:
print("Restarting current open RV session")
restart_rv()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-b", "--build",
action="store_true",
default=True,
help="Creates the rvpkg file using the contents of the 'plugin' folder, defaults to True")
parser.add_argument("-i", "--install",
action="store_true",
default=False,
help="Runs rvpkg -add and -install on the corresponding folder by operating system, "
"defaults to False")
parser.add_argument("-r", "--restart",
action="store_true",
default=False,
help="Restarts the current open instance of RV in order to get the latest plugin changes, "
"defaults to False")
parser.add_argument("-t", "--test",
action="store_true",
default=False,
help="Run unit tests for plugin, defaults to False. NOT IMPLEMENTED")
args = parser.parse_args()
main(args)