-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqdo
More file actions
executable file
·327 lines (301 loc) · 12.7 KB
/
qdo
File metadata and controls
executable file
·327 lines (301 loc) · 12.7 KB
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
#!/usr/bin/python2.7
import argparse
import os
import subprocess
import sys
import glob
import tarfile
import datetime
import shutil
import re
# Declare global variable name
args = None
def build_cmd():
"""
Build asset from source.
"""
for f in files():
if os.path.isdir(f):
debug('Processing: ' + f)
if f[-len(args.extension):] == args.extension:
src_name = os.path.basename(f)
section = os.path.basename(os.path.dirname(f))
otl_name = src_name[:-len(args.extension)] + '.otl'
otl = os.path.join(args.binary_dir, section, otl_name)
print "Building '{0}' to '{1}'".format(f, otl)
try:
output = subprocess.check_output(['hotl', '-C', f, otl],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print 'ERROR: The build process of {0} failed.'.format(otl)
print e.output
break
except OSError as e:
print 'ERROR: The build process of {0} failed.'.format(otl)
print '{0} ({1})'.format(e.strerror, e.errno)
break
def extract_cmd():
"""
Extracts source from binary asset.
"""
for f in files():
if os.path.isfile(f):
debug('Processing: ' + f)
if f[-4:] == '.otl':
otl_name = os.path.basename(f)
section = os.path.basename(os.path.dirname(f))
src_name = otl_name[:-4] + args.extension
src = os.path.join(args.source_dir, section, src_name)
print "Extracting '{0}' to '{1}'.".format(f, src)
try:
if not args.keep and os.path.exists(src):
shutil.rmtree(src)
output = subprocess.check_output(['hotl', '-X', src, f],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print 'ERROR: The extraction process of {0} failed.'.format(f)
print e.output
break
except OSError as e:
print 'ERROR: The extraction process of {0} failed.'.format(f)
print '{0} ({1})'.format(e.strerror, e.errno)
break
def clean_cmd():
"""
Removes compiled assets or sources.
"""
for f in files():
if args.source:
if os.path.isdir(f):
debug('Processing: ' + f)
if f[-len(args.extension):] == args.extension:
print 'Deleting:', f
try:
output = subprocess.check_output(['rm', '-r', f],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print 'ERROR: Removing {0} failed.'.format(otl)
print e.output
break
except OSError as e:
print 'ERROR: Removing {0} failed.'.format(otl)
print '{0} ({1})'.format(e.strerror, e.errno)
break
else:
if os.path.isfile(f):
debug('Processing: ' + f)
if f[-4:] == '.otl':
dir = f[:-4] + args.extension
print 'Deleting:', f
try:
output = subprocess.check_output(['rm', f],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print 'ERROR: Removing {0} failed.'.format(f)
print e.output
break
except OSError as e:
print 'ERROR: Removing {0} failed.'.format(f)
print '{0} ({1})'.format(e.strerror, e.errno)
break
def pack_cmd():
"""
Creates the distribution archive.
"""
qlib_dir = os.path.dirname(os.path.realpath(__file__))
qlib_parent = os.path.dirname(qlib_dir)
if qlib_dir.split(os.sep)[-1] != "qLib":
print "WARNING: Archived directory is not called 'qLib'."
try:
git_tag = None
git_tag = subprocess.check_output(["git", "describe", "--exact-match", "HEAD"],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print "ERROR: Retrieving git tag failed."
print e.output
except OSError as e:
print 'ERROR: Git not accessible.'
print '{0} ({1})'.format(e.strerror, e.errno)
if git_tag:
git_tag = git_tag[:-1]
else:
if args.force:
print "WARNING: Empty git tag, using 'NOTAG' instead."
git_tag = "NOTAG"
else:
sys.exit(1)
debug("qLib dir: {0} qLib parent dir: {1}, git tag: {2}".format(qlib_dir, qlib_parent, git_tag))
file_list = []
file_list.append(os.path.join(qlib_dir, "README"))
file_list.append(os.path.join(qlib_dir, "LICENCE"))
file_list.extend(glob.glob(os.path.join(qlib_dir, "examples", "*.hip")))
if args.backup:
tar_name = "qLib-backup-" + datetime.date.today().isoformat() + ".tbz2"
file_list.append(os.path.join(qlib_dir, "README.developer"))
file_list.extend(glob.glob(os.path.join(qlib_dir, "houdini11.0", "otls", "*", "*_OTL")))
else:
tar_name = "qLib-" + git_tag + ".tbz2"
file_list.extend(glob.glob(os.path.join(qlib_dir, "otls", "*", "*.otl")))
file_list = [(f, os.path.relpath(f, qlib_parent)) for f in file_list]
tar_file = tarfile.open(tar_name, "w:bz2")
for f in file_list:
print "Adding: {0}".format(f[1])
tar_file.add(f[0],f[1])
tar_file.close()
def list_cmd():
"""
Assembles a Markdown formatted list of assets from help files.
"""
section_order = {
"base": "0",
"spec": "1",
"future": "2",
"experimental": "3",
"graveyard": "4",
}
context_order = {
"Object": "0",
"Sop": "1",
"Pop": "2",
"Dop": "3",
"Driver": "4",
"Shop": "5",
"Vop": "6",
}
context_abbrev = {
"Object": "OBJ",
"Sop": "SOP",
"Pop": "POP",
"Dop": "DOP",
"Driver": "ROP",
"Shop": "SHOP",
"Vop": "VOP",
}
otls_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "otls")
args.file = (otls_dir,)
args.recursive = True
print args.file, args.recursive
assets = {}
for f in files():
if os.path.basename(f) == "Help":
asset_dir = os.path.basename(os.path.dirname(f))
section = os.path.basename(os.path.dirname(os.path.dirname(os.path.dirname(f))))
ns, name, version = tuple(re.split("_8_8", asset_dir))
name = re.split("_1",name)
context = name[0]
name = name[1] + "_" + name[0]
if name in assets:
assets[name]["version"].append(int(version))
else:
assets[name] = {
"version": [int(version)],
"section": section,
"context": context,
"file": f,
}
asset_list = list(assets.items())
asset_list.sort(key=lambda a:section_order[a[1]["section"]]+context_order[a[1]["context"]]+a[0])
with open(os.path.join(os.path.dirname(otls_dir), "assets.md"), "w") as alist:
alist.write("# Assets\n")
alist.write("This page lists all the assets and their brief descriptions extracted from their Help cards.\n\n")
sections = set()
for asset_name, asset in asset_list:
if asset["section"] not in sections:
alist.write("## " + asset["section"].capitalize() + " Section\n\n")
sections.add(asset["section"])
contexts = set()
if asset["context"] not in contexts:
if len(contexts) > 0:
alist.write("---\n\n")
contexts.add(asset["context"])
with open(asset["file"]) as f:
help_text = f.read()
name = re.search(r"\n\s*=(.*)=", help_text)
description = re.search(r"\n\s*\"\"\"(.*)\"\"\"", help_text)
if name is None or description is None:
alist.write("> **" + " ".join(asset_name.split("_")) +
"**'s help card doesn't define a proper name and/or description..\n\n")
else:
alist.write("#### " + name.group(1).strip() + " " + context_abbrev[asset["context"]] + "\n")
alist.write(re.sub(r"\[(.*?)\|.*?\]", lambda m: "**" + m.group(1) + "**", description.group(1).strip()) + "\n\n")
def files():
"""
Yield files based on command line arguments.
"""
for a in args.file:
if a[-1:] == "/":
a = a[0:-1]
debug("Yielding file: {0}".format(a))
yield a
if os.path.isdir(a):
for d in os.walk(a):
if not args.recursive and d[0] != a:
break
for f in d[1] + d[2]:
jf = os.path.join(d[0],f)
if jf[-1:] == "/":
jf = a[0:-1]
debug("Yielding file: {0}".format(jf))
yield jf
def debug(msg):
"""
Print debug messages
"""
if args.debug:
print "DEBUG:", msg
def parse_args():
"""
Parse command line arguments.
"""
script_path = os.path.dirname(os.path.abspath(__file__))
default_bin_path = os.path.join(script_path,"qLib","otls")
default_src_path = os.path.join(script_path,"src")
common_args = argparse.ArgumentParser(add_help=False)
common_args.add_argument('-D', '--debug', action='store_true',
help='Print debug messages')
otl_args = argparse.ArgumentParser(add_help=False)
otl_args.add_argument('file', type=str, nargs='*', default='.',
help='File and/or directory to process ["%(default)s"]')
otl_args.add_argument('-x','--extension', type=str, default='_OTL',
help='File extension of extracted OTL directories ["%(default)s"]')
otl_args.add_argument('-R', '--recursive', action='store_true',
help='Descend into sub-directories')
otl_args.add_argument("-s", "--source-dir", type=str, default=default_src_path,
help="Source directory path ['%(default)s']")
otl_args.add_argument("-b", "--binary-dir", type=str, default=default_bin_path,
help="Binary otl directory path ['%(default)s']")
main_parser = argparse.ArgumentParser(parents=[common_args],
description='Command line tool to manage qLib.')
subparsers = main_parser.add_subparsers(dest='command', help='Supported commands')
build_parser = subparsers.add_parser('build', parents=[common_args, otl_args],
help='Builds OTLs from extracted dir structures',
description='Builds OTLs from extracted dir structures.')
extract_parser = subparsers.add_parser('extract', parents=[common_args, otl_args],
help='Extracts OTLs into dir structures',
description='Extracts OTLs into dir structures.')
clean_parser = subparsers.add_parser('clean', parents=[common_args, otl_args],
help='Removes OTLs or OTL sources',
description='Removes OTLs or OTL sources.')
pack_parser = subparsers.add_parser('pack', parents=[common_args],
help='Creates tarballs for ditribution and backup purposes.')
list_parser = subparsers.add_parser('list', parents=[common_args],
help='Creates a list of assets from help files')
extract_parser.add_argument("-k", "--keep", action="store_true",
help="Keeps the source folder and extracts otl over it.")
clean_parser.add_argument('-S', '--source', action='store_true',
help='Deletes sources instead of built OTLs')
pack_parser.add_argument('-B', '--backup', action='store_true',
help='Creates a backup archive instead of distribution archive.')
pack_parser.add_argument('-f', '--force', action='store_true',
help='Force pack creation ignoring errors.')
global args
args = main_parser.parse_args()
debug(args)
# Main
parse_args()
{'build': build_cmd,
'extract': extract_cmd,
'clean': clean_cmd,
'pack': pack_cmd,
'list': list_cmd,
}[args.command]()