-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
80 lines (56 loc) · 1.99 KB
/
compile.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
import json
import subprocess
import sys
import os
header_text = """/*** Bridge4KT Z-Way HA module *******************************
Author: Ralph Wetzel <[email protected]>
Version: {version}
Description:
This module announces Z-Way HA devices to Apple HomeKit
Conceptually based on:
HomeKitGate Z-Way HA module
Author: Andreas Freud <[email protected]>
**************************************************************/
"""
cmd = "npx rapydscript compile ./bridge4kt/__init__.pyj --output ../index.js"
c = cmd.split(" ")
print('\nLoading module.json...')
with open("../module.json") as module_json:
module_data = json.load(module_json)
header_text = header_text.format(version = module_data['version'])
try:
print("Transpiling the Bridge4KT sources...")
# subprocess.run is shorter - yet check_output supported as well on py < 3.5
result = subprocess.check_output(c)
except subprocess.CalledProcessError as e:
print("\nError while executing the RapydScript transpiler.")
sys.exit()
if os.getenv('B4KT_DEBUG', None) == '1':
with open("../index.js", 'rb') as f:
result = f.read()
else:
cmd = "npx uglifyjs ../index.js --compress"
c = cmd.split(" ")
try:
print("Compressing...")
# subprocess.run is shorter - yet check_output supported as well on py < 3.5
result = subprocess.check_output(c)
except subprocess.CalledProcessError as e:
print("\nError while executing uglify.js.")
sys.exit()
print("Integrating file header...")
with open("../index.js", 'wb') as f:
f.write(header_text.encode())
f.write(result)
import time
import datetime
import json
t = time.time()
stamp = datetime.datetime.fromtimestamp(t).strftime('%Y%m%d|%H%M%S')
print("Updating serial information to '{}'".format(stamp))
module_data['serial'] = stamp
module_data['tick'] = int(t * 1000)
print('Writing module.json...')
with open("../module.json", 'w') as module_json:
json.dump(module_data, module_json, indent=2)
print("Done.")