forked from boltsparts/BOLTS_archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolts.py
444 lines (376 loc) · 14.2 KB
/
bolts.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python
# BOLTS - Open Library of Technical Specifications
# Copyright (C) 2013 Johannes Reinhardt <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import argparse
import os.path
from datetime import datetime
from os import getcwd
from os import listdir
from os import walk
from shutil import copyfile
from shutil import make_archive
from subprocess import call
from subprocess import Popen
from tempfile import mkstemp
from backends.license import LICENSES_SHORT
from bolttools.blt import Repository
from bolttools.drawings import DrawingsData
from bolttools.freecad import FreeCADData
from bolttools.openscad import OpenSCADData
from bolttools.pythonpackage import PythonPackageData
# from bolttools.solidworks import SolidWorksData
def export(args):
# load data
repo = Repository(args.repo)
dbs = {}
license = LICENSES_SHORT[args.license]
out_path = os.path.join(repo.path, "output", args.target)
if args.target == "openscad":
dbs["openscad"] = OpenSCADData(repo)
from backends.openscad import OpenSCADBackend
OpenSCADBackend(repo, dbs).write_output(
out_path, target_license=license, version="development", expand=args.debug
)
copyfile(
os.path.join(repo.path, "backends", "licenses", args.license.strip("+")),
os.path.join(out_path, "LICENSE")
)
elif args.target == "freecad":
dbs["freecad"] = FreeCADData(repo)
from backends.freecad import FreeCADBackend
FreeCADBackend(repo, dbs).write_output(
out_path, target_license=license, version="development"
)
copyfile(
os.path.join(repo.path, "backends", "licenses", args.license.strip("+")),
os.path.join(out_path, "BOLTS", "LICENSE")
)
elif args.target == "pythonpackage":
dbs["pythonpackage"] = PythonPackageData(repo)
from backends.pythonpackage import PythonPackageBackend
PythonPackageBackend(repo, dbs).write_output(
out_path, target_license=license, version="development"
)
# elif args.target == "solidworks":
# dbs["solidworks"] = SolidWorksData(repo)
# from backends.solidworks import SolidWorksBackend
# SolidWorksBackend(repo,dbs).write_output(out_path,"development")
elif args.target == "iges":
from backends.exchange import IGESBackend
IGESBackend(repo, dbs).write_output(out_path, "development")
elif args.target == "website":
dbs["drawings"] = DrawingsData(repo)
dbs["freecad"] = FreeCADData(repo)
dbs["openscad"] = OpenSCADData(repo)
from backends.webpage import WebsiteBackend
WebsiteBackend(repo, dbs).write_output(out_path)
elif args.target == "drawings":
dbs["drawings"] = DrawingsData(repo)
def test(args):
exec_dir = os.path.join(args.repo, "output", args.target)
if args.target == "freecad":
freecad_process = Popen(["freecad", "-M", "."], cwd=exec_dir)
freecad_process.wait()
elif args.target == "openscad":
freecad_process = Popen(["openscad"], cwd=exec_dir)
freecad_process.wait()
elif args.target == "website":
from backends.website import app
extra_files = []
blog_path = os.path.join(args.repo, "backends", "website", "blog", "posts")
for filename in listdir(blog_path):
if filename.startswith('.'):
continue
extra_files.append(os.path.join(blog_path, filename))
doc_path = os.path.join(args.repo, "website", "docs", "sources")
for dirpath, _, filenames in walk(doc_path):
for filename in filenames:
if filename.startswith('.'):
continue
extra_files.append(os.path.join(dirpath, filename))
trans_path = os.path.join(args.repo, "translations")
for dirpath, _, filenames in walk(trans_path):
for filename in filenames:
if filename.endswith('.mo'):
extra_files.append(os.path.join(dirpath, filename))
# templates
for dirpath, _, filenames in walk(args.repo):
if os.path.basename(dirpath) == "templates":
for filename in filenames:
extra_files.append(os.path.join(dirpath, filename))
app.run(debug=True, extra_files=extra_files)
def check(args):
repo = Repository(args.repo)
dbs = {}
dbs["openscad"] = OpenSCADData(repo)
dbs["freecad"] = FreeCADData(repo)
dbs["drawings"] = DrawingsData(repo)
# dbs["solidworks"] = SolidWorksData(repo)
from backends.checker import CheckerBackend
checker = CheckerBackend(repo, dbs)
for check in checker.checks.values():
print(check.print_table(),)
def tasks(args):
repo = Repository(args.repo)
dbs = {}
dbs["openscad"] = OpenSCADData(repo)
dbs["freecad"] = FreeCADData(repo)
dbs["drawings"] = DrawingsData(repo)
# dbs["solidworks"] = SolidWorksData(repo)
from backends.checker import CheckerBackend
checker = CheckerBackend(repo, dbs)
for task in checker.tasks.values():
print(task.print_table(),)
def connectors(args):
repo = Repository(args.repo)
dbs = {}
dbs["openscad"] = OpenSCADData(repo)
dbs["drawings"] = DrawingsData(repo)
out_path = os.path.join(repo.path, "output", "connectordrawings")
from backends.connectordrawings import ConnectorDrawingsBackend
ConnectorDrawingsBackend(repo, dbs).write_output(out_path)
def add_lang(args):
# from website
_, fname = mkstemp(suffix=".pot")
os.system("pybabel extract -F website/babel.cfg -o %s website/" % fname)
os.system("pybabel init -D messages -d translations/ -i %s -l %s" % (fname, args.lang))
os.remove(fname)
# from documentation
from website.utils import Documentation
fhandle, fname = mkstemp(suffix=".pot")
with os.fdopen(fhandle, "w") as fid:
docs = Documentation(os.path.join(args.repo, 'website', 'docs', 'sources'))
docs.extract_messages(fid)
os.system("pybabel init -D docs -d translations/ -i %s -l %s" % (fname, args.lang))
os.remove(fname)
# from parts data
repo = Repository(args.repo)
from backends.translations import TranslationBackend
fhandle, fname = mkstemp(suffix=".pot")
TranslationBackend(repo, []).write_output(fname)
os.system("pybabel init -D parts -d translations/ -i %s -l %s" % (fname, args.lang))
os.remove(fname)
print(
"Don't forget to edit website/templates/base.html "
"and add the language to the dropdown menu"
)
def translate(args):
if args.update_translation:
# from website
_, fname = mkstemp(suffix=".pot")
os.system("pybabel extract -F website/babel.cfg -o %s website/" % fname)
os.system("pybabel update -D messages -i %s -d translations/" % fname)
os.remove(fname)
# from documentation
from website.utils import Documentation
fhandle, fname = mkstemp(suffix=".pot")
with os.fdopen(fhandle, "w") as fid:
docs = Documentation(os.path.join(args.repo, 'website', 'docs', 'sources'))
docs.extract_messages(fid)
os.system("pybabel update -D docs -d translations -i %s" % fname)
os.remove(fname)
# from parts data
repo = Repository(args.repo)
from backends.translations import TranslationBackend
fhandle, fname = mkstemp(suffix=".pot")
TranslationBackend(repo, []).write_output(fname)
os.system("pybabel update -D parts -d translations -i %s" % fname)
os.remove(fname)
if args.compile_translation:
os.system("pybabel compile -D messages -d translations/")
os.system("pybabel compile -D parts -d translations/")
os.system("pybabel compile -D docs -d translations/")
def release(args):
# check that there are no uncommitted changes
if call(["git", "diff", "--exit-code", "--quiet"]) == 1:
print(
"There are uncommitted changes present in the git repository. "
"Please take care of them before releasing"
)
exit(1)
if args.kind == "stable" and args.version is None:
print("Please specify a version using -v when releasing a stable version")
exit(2)
if args.kind == "development" and args.version is not None:
print("No explicit version can be given for development releases")
exit(2)
repo = Repository(args.repo)
version = datetime.now().strftime("%Y%m%d%H%M")
stable = args.kind == "stable"
if stable:
version = args.version
targets = [args.target]
if targets[0] == "all":
targets = ["freecad", "openscad", "iges"]
from backends.openscad import OpenSCADBackend
from backends.freecad import FreeCADBackend
dbs = {}
dbs["openscad"] = OpenSCADData(repo)
dbs["freecad"] = FreeCADData(repo)
backend_names = {"freecad": "FreeCAD", "openscad": "OpenSCAD", "iges": "IGES"}
# OpenSCAD, FreeCAD export
for li_short in ["lgpl2.1+", "gpl3"]:
license = LICENSES_SHORT[li_short]
OpenSCADBackend(repo, dbs).write_output(
os.path.join(repo.path, "output", "openscad"),
target_license=license,
version=version,
expand=not stable
)
copyfile(
os.path.join(repo.path, "backends", "licenses", li_short.strip("+")),
os.path.join(repo.path, "output", "openscad", "LICENSE")
)
FreeCADBackend(repo, dbs).write_output(
os.path.join(repo.path, "output", "freecad"),
target_license=license,
version=version
)
copyfile(
os.path.join(repo.path, "backends", "licenses", li_short.strip("+")),
os.path.join(repo.path, "output", "freecad", "BOLTS", "LICENSE")
)
for backend in ["openscad", "freecad"]:
if backend not in targets:
continue
backend_name = backend_names[backend]
# construct filename from date
template = "BOLTS_%s_%s_%s" % (backend_name, version, li_short)
# create archives
root_dir = os.path.join(repo.path, "output", backend)
base_name = os.path.join(repo.path, "downloads", backend, template)
make_archive(base_name, "gztar", root_dir)
make_archive(base_name, "zip", root_dir)
# iges export
from backends.exchange import IGESBackend
if "iges" in targets:
try:
import tarfile
import lzma
except ImportError:
print("Could not find python-lzma, which is required for IGES export")
return
IGESBackend(repo, dbs).write_output(
os.path.join(repo.path, "output", "iges"), version=version
)
# write xz file, see http://stackoverflow.com/a/13131500
backend_name = backend_names["iges"]
xz_name = "BOLTS_%s_%s.tar.xz" % (backend_name, version)
xz_fid = lzma.LZMAFile(os.path.join(repo.path, "downloads", "iges", xz_name), mode="w")
with tarfile.open(mode="w", fileobj=xz_fid) as tar_xz_fid:
tar_xz_fid.add(os.path.join(repo.path, "output", "iges"), arcname="/")
xz_fid.close()
parser = argparse.ArgumentParser()
parser.add_argument(
"--repo",
type=str,
help="path of the BOLTS repository to work on",
default=getcwd()
)
subparsers = parser.add_subparsers()
parser_export = subparsers.add_parser(
"export"
)
parser_export.add_argument(
"target",
type=str,
choices=["openscad", "freecad", "iges", "website", "pythonpackage"],
help="the distribution to create"
)
parser_export.add_argument(
"-l",
"--license",
type=str,
choices=["lgpl2.1", "lgpl2.1+", "lgpl3", "lgpl3+", "gpl3", "gpl3+"],
default="lgpl2.1",
help="the license of the exported license"
)
parser_export.add_argument(
"--debug",
dest='debug',
action="store_true",
help="take special measures to ease debugging"
)
parser_export.set_defaults(func=export)
parser_test = subparsers.add_parser(
"test"
)
parser_test.add_argument(
"target",
type=str,
choices=["openscad", "freecad", "website"],
help="the backend to test"
)
parser_test.set_defaults(func=test)
parser_check = subparsers.add_parser("check")
parser_check.set_defaults(func=check)
parser_check = subparsers.add_parser("tasks")
parser_check.set_defaults(func=tasks)
parser_check = subparsers.add_parser("connectors")
parser_check.set_defaults(func=connectors)
parser_release = subparsers.add_parser(
"release"
)
parser_release.set_defaults(
func=release
)
parser_release.add_argument(
"kind",
type=str,
choices=["development", "stable"],
help="whether to create a development snapshot or an official release",
default="development"
)
parser_release.add_argument(
"target",
type=str,
choices=["all", "openscad", "freecad", "iges"],
default="all"
)
parser_release.add_argument(
"-v",
"--version",
type=str
)
parser_translate = subparsers.add_parser(
"translate"
)
parser_translate.add_argument(
"-u",
dest='update_translation',
action="store_true",
help="update the po message catalogs from sources and parts data"
)
parser_translate.add_argument(
"-c",
dest='compile_translation',
action="store_true",
help="compile the po message catalogs into mo catalogs"
)
parser_translate.set_defaults(func=translate)
parser_add_lang = subparsers.add_parser(
"add_lang"
)
parser_add_lang.add_argument(
'lang',
help="add the specified language"
)
parser_add_lang.set_defaults(
func=add_lang
)
args = parser.parse_args()
args.func(args)