forked from Tinkerforge/generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_all.py
executable file
·302 lines (222 loc) · 9.95 KB
/
copy_all.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.hexversion < 0x3040000:
print('Python >= 3.4 required')
sys.exit(1)
import os
import shutil
import filecmp
import socket
import zipfile
import tempfile
import glob
import importlib.util
import importlib.machinery
generators_dir = os.path.dirname(os.path.realpath(__file__))
def create_generators_module():
if sys.hexversion < 0x3050000:
generators_module = importlib.machinery.SourceFileLoader('generators', os.path.join(generators_dir, '__init__.py')).load_module()
else:
generators_spec = importlib.util.spec_from_file_location('generators', os.path.join(generators_dir, '__init__.py'))
generators_module = importlib.util.module_from_spec(generators_spec)
generators_spec.loader.exec_module(generators_module)
sys.modules['generators'] = generators_module
if 'generators' not in sys.modules:
create_generators_module()
from generators import common
doc_git = 'doc'
def text_files_are_not_the_same(src_file, dest_path):
dest_file = os.path.join(dest_path, src_file.split('/')[-1])
try:
with open(src_file, 'r') as f:
lines1 = f.readlines()
with open(dest_file, 'r') as f:
lines2 = f.readlines()
except:
return True
if len(lines1) != len(lines2):
return True
t = 'This file was automatically generated on'
for l1, l2 in zip(lines1, lines2):
if l1 != l2:
if t in l1 and t in l2:
continue
return True
return False
def files_are_not_the_same(src_file, dest_path):
if src_file.endswith('.vi') or src_file.endswith('.vi.png'):
dest_file = os.path.join(dest_path, src_file.split('/')[-1])
try:
with open(src_file, 'rb') as f:
data1 = f.read()
with open(dest_file, 'rb') as f:
data2 = f.read()
except:
return True
return data1 != data2
else:
return text_files_are_not_the_same(src_file, dest_path)
def main():
path = generators_dir
start_path = os.path.realpath(os.path.join(generators_dir, '..'))
brickv_path_bindings = os.path.join(start_path, 'brickv/src/brickv/bindings')
flash_test_path_bindings = os.path.join(start_path, 'flash-test/src/flash-test/plugin_system/tinkerforge')
bindings = []
for binding in os.listdir(generators_dir):
if not os.path.isdir(binding) or os.path.exists(os.path.join(generators_dir, binding, 'skip_copy_all')):
continue
if binding not in ['.git', '.m2', '.vscode', '__pycache__', 'configs', 'docker']:
bindings.append(binding)
bindings = sorted(bindings)
if socket.gethostname() != 'tinkerforge.com':
for tool_name, tool_path in [('brickv', brickv_path_bindings),
('flash-test', flash_test_path_bindings)]:
print('')
print('Copying ip_connection to {0}:'.format(tool_name))
src_file = os.path.join(path, 'python', 'ip_connection.py')
if files_are_not_the_same(src_file, tool_path):
shutil.copy(src_file, tool_path)
print(' * ip_connection.py')
print('')
print('Copying Python bindings to {0}:'.format(tool_name))
path_binding = os.path.join(path, 'python')
src_file_path = os.path.join(path_binding, 'bindings')
files = [f for f in os.listdir(src_file_path) if f.endswith('.py')]
files.remove('device_factory.py')
if tool_name != 'flash-test':
files.remove('device_factory_all.py')
for f in files:
src_file = os.path.join(src_file_path, f)
if files_are_not_the_same(src_file, tool_path):
shutil.copy(src_file, tool_path)
print(' * {0}'.format(f))
doc_copy = [('_Brick_', 'Bricks'),
('_Bricklet_', 'Bricklets'),
('IPConnection_', '.')]
to_delete = {'en': {}, 'de': {}}
doc_path = os.path.join(doc_git, '{0}/source/Software')
labview_image_path = os.path.join(doc_git, 'en/source/Images/Screenshots/LabVIEW')
for lang in ['en', 'de']:
print('')
print("Copying '{0}' documentation and examples:".format(lang))
for t in doc_copy:
dest_dir = os.path.join(start_path, doc_path.format(lang), t[1])
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
to_delete[lang][t[1]] = os.listdir(dest_dir)
for binding in bindings:
path_binding = os.path.join(path, binding)
src_file_path = os.path.join(path_binding, 'doc', lang)
for f in os.listdir(src_file_path):
if f.endswith('.swp'):
continue
for t in doc_copy:
if t[0] in f:
src_file = os.path.join(src_file_path, f)
if f.endswith('.vi.png'):
if lang != 'en':
continue
dest_path = os.path.join(start_path, labview_image_path)
else:
dest_path = os.path.join(start_path, doc_path.format(lang), t[1])
try:
to_delete[lang][t[1]].remove(f)
except:
pass
if files_are_not_the_same(src_file, dest_path):
shutil.copy(src_file, dest_path)
print(' * {0}'.format(f))
if socket.gethostname() != 'tinkerforge.com':
for lang in ['en', 'de']:
print('')
print('Copying Tinkerforge.js to {0}:'.format(os.path.join(doc_git, lang)))
src_file = os.path.join(path, 'javascript', 'Tinkerforge.js')
dest_dir = os.path.join(start_path, doc_path.format(lang), t[1])
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
if files_are_not_the_same(src_file, dest_dir):
shutil.copy(src_file, dest_dir)
print(' * Tinkerforge.js')
else:
tmp_dir = tempfile.mkdtemp()
zf = zipfile.ZipFile('/srv/web/com.tinkerforge.download/downloads/bindings/javascript/tinkerforge_javascript_bindings_latest.zip')
zf.extract('browser/source/Tinkerforge.js', tmp_dir)
zf.close()
for lang in ['en', 'de']:
print('')
print('Copying Tinkerforge.js to {0}:'.format(os.path.join(doc_git, lang)))
src_file = os.path.join(tmp_dir, 'browser', 'source', 'Tinkerforge.js')
dest_dir = os.path.join(start_path, doc_path.format(lang), t[1])
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
if files_are_not_the_same(src_file, dest_dir):
shutil.copy(src_file, dest_dir)
print(' * Tinkerforge.js')
shutil.rmtree(tmp_dir)
if socket.gethostname() == 'tinkerforge.com':
print('')
print('Linking 3D models:')
for git in os.listdir(os.path.join(path, '..')):
hardware_dir = os.path.normpath(os.path.join(path, '..', git, 'hardware'))
if not os.path.exists(hardware_dir):
continue
models = []
for suffix in ['*.step', '*.FCStd']:
for match in glob.glob(os.path.join(hardware_dir, suffix)):
models.append(os.path.relpath(match, hardware_dir))
for root, dirs, _ in os.walk(hardware_dir):
for name in dirs:
for suffix in ['*.step', '*.FCStd']:
for match in glob.glob(os.path.join(root, name, suffix)):
models.append(os.path.relpath(match, hardware_dir))
if len(models) == 0:
continue
if git.endswith('-brick'):
category = 'bricks'
device = '_'.join(git.split('-')[:-1])
elif git.endswith('-bricklet'):
category = 'bricklets'
device = '_'.join(git.split('-')[:-1])
elif git.endswith('-extension'):
category = 'extensions'
device = '_'.join(git.split('-')[:-1])
elif git.endswith('-powersupply'):
category = 'power_supplies'
device = '_'.join(git.split('-')[:-1])
else:
category = 'accessories'
device = git.replace('-', '_')
target_dir = os.path.join('/srv/web/com.tinkerforge.download/downloads/3d', category, device)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
for model in models:
source = os.path.join(hardware_dir, model)
base, name = os.path.split(model)
target_base = os.path.join(target_dir, base)
if not os.path.exists(target_base):
os.makedirs(target_base)
target = os.path.join(target_dir, model)
if not os.path.exists(target):
os.symlink(source, target)
print(' * {0}/{1}/{2}'.format(category, device, model))
print('')
print("Removing stale files:")
for lang in ['en', 'de']:
for t in doc_copy:
if t[1] == '.':
continue
for x in to_delete[lang][t[1]]:
if x.endswith('_openHAB.rst') or x.endswith('.rules'):
continue
if x.endswith('.table'):
continue
p = os.path.join(doc_git, lang, "source", "Software", t[1], x)
os.remove(os.path.join(start_path, p))
print(' * {0}'.format(p))
print('')
print('\033[01;35m>>> done\033[0m')
return 0
if __name__ == '__main__':
common.dockerize('', __file__)
sys.exit(main())