-
Notifications
You must be signed in to change notification settings - Fork 141
/
pyozw_setup.py
1140 lines (974 loc) · 43.2 KB
/
pyozw_setup.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This file is part of **python-openzwave** project https://github.com/OpenZWave/python-openzwave.
:platform: Unix, Windows, MacOS X
.. moduleauthor:: bibi21000 aka Sébastien GALLET <[email protected]>
License : GPL(v3)
**python-openzwave** is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
**python-openzwave** 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with python-openzwave. If not, see http://www.gnu.org/licenses.
Build process :
- ask user what to do (zmq way in pip)
- or parametrizes it
--dev : use local sources and cythonize way (for python-openzwave devs, ...)
--embed : use local sources and cpp file (for third parties packagers, ...)
--git : download openzwave from git (for geeks)
--shared : use pkgconfig and cython (for debian devs and common users)
--pybind : use pybind alternative (not tested)
--auto (default) : try static, shared and cython, fails if it can't
"""
import time
import os, sys
from os import name as os_name
import re
import shutil
import setuptools
from setuptools import setup, find_packages
from distutils.extension import Extension
from distutils.spawn import find_executable
from distutils import log
from setuptools.command.install import install as _install
from distutils.command.build import build as _build
from distutils.command.clean import clean as _clean
from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
from setuptools.command.develop import develop as _develop
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
except ImportError:
log.warn("ImportError in : from wheel.bdist_wheel import bdist_wheel as _bdist_wheel")
from platform import system as platform_system
import glob
from pyozw_version import pyozw_version
try:
PY3 = not unicode
except NameError:
PY3 = True
LOCAL_OPENZWAVE = os.getenv('LOCAL_OPENZWAVE', 'openzwave')
SETUP_DIR = os.path.dirname(os.path.abspath(__file__))
class Template(object):
def __init__(self, openzwave=None, cleanozw=False, sysargv=None, flavor="embed", backend="cython"):
self.openzwave = openzwave
self._ctx = None
self.cleanozw = cleanozw
self.flavor = flavor
self.backend = backend
self.sysargv = sysargv
"""Specifics options for os ie windows
"""
self.os_options = dict()
def get_default_exts (self):
exts = { "name": "libopenzwave",
"sources": [ ],
"include_dirs": [ ],
"define_macros": [ ( 'PY_LIB_VERSION', pyozw_version ) ],
"libraries": [ ],
"extra_objects": [ ],
"extra_compile_args": [ ],
"extra_link_args": [ ],
"language": "c++",
}
return exts
def cython_context(self):
try:
from Cython.Distutils import build_ext
except ImportError:
return None
exts = self.get_default_exts()
exts['define_macros'] += [('PY_SSIZE_T_CLEAN',1)]
exts['sources'] = ["src-lib/libopenzwave/libopenzwave.pyx"]
return exts
def cpp_context(self):
try:
from distutils.command.build_ext import build_ext
except ImportError:
return None
exts = self.get_default_exts()
exts['define_macros'] += [('PY_SSIZE_T_CLEAN',1)]
exts['sources'] = ["openzwave-embed/open-zwave-master/python-openzwave/src-lib/libopenzwave/libopenzwave.cpp"]
exts["include_dirs"] += [ "src-lib/libopenzwave/" ]
return exts
def pybind_context(self):
exts = self.get_default_exts()
exts["sources"] = [ "src-lib/libopenzwave/LibZWaveException.cpp",
"src-lib/libopenzwave/Driver.cpp",
"src-lib/libopenzwave/Group.cpp",
"src-lib/libopenzwave/Log.cpp",
"src-lib/libopenzwave/Options.cpp",
"src-lib/libopenzwave/Manager.cpp",
"src-lib/libopenzwave/Notification.cpp",
"src-lib/libopenzwave/Node.cpp",
"src-lib/libopenzwave/Values.cpp",
"src-lib/libopenzwave/libopenzwave.cpp"
]
exts["include_dirs"] = [ "pybind11/include" ]
exts['extra_compile_args'] += [ "-fvisibility=hidden" ]
return exts
def system_context(self, ctx, static=False):
#System specific section
#~ os.environ["CC"] = "gcc"
#~ os.environ["CXX"] = "g++"
#~ os.environ["PKG_CONFIG_PATH"] = "PKG_CONFIG_PATH:/usr/local/lib/x86_64-linux-gnu/pkgconfig/"
log.info("Found platform {0}".format(sys.platform))
if static:
ctx['include_dirs'] += [
"{0}/cpp/src".format(self.openzwave),
"{0}/cpp/src/value_classes".format(self.openzwave),
"{0}/cpp/src/platform".format(self.openzwave) ]
if sys.platform.startswith("win"):
from pyozw_win import get_system_context
get_system_context(ctx, self.os_options, openzwave=os.path.abspath(self.openzwave), static=static)
elif sys.platform.startswith("cygwin"):
if static:
ctx['libraries'] += [ "udev", "stdc++",'resolv' ]
ctx['extra_objects'] = [ "{0}/libopenzwave.a".format(self.openzwave) ]
ctx['include_dirs'] += [ "{0}/cpp/build/linux".format(self.openzwave) ]
else:
import pyozw_pkgconfig
ctx['libraries'] += [ "openzwave" ]
extra = pyozw_pkgconfig.cflags('libopenzwave')
if extra != '':
for ssubstitute in ['', 'value_classes', 'platform']:
ctx['extra_compile_args'] += [ os.path.normpath(os.path.join(extra, ssubstitute)) ]
elif sys.platform.startswith("darwin") :
ctx['extra_link_args'] += [ "-framework", "CoreFoundation", "-framework", "IOKit" ]
ctx['extra_compile_args'] += [ "-stdlib=libc++", "-mmacosx-version-min=10.7" ]
if static:
ctx['extra_objects'] = [ "{0}/libopenzwave.a".format(self.openzwave) ]
ctx['include_dirs'] += [ "{0}/cpp/build/mac".format(self.openzwave) ]
else:
import pyozw_pkgconfig
ctx['libraries'] += [ "openzwave" ]
extra = pyozw_pkgconfig.cflags('libopenzwave')
if extra != '':
for ssubstitute in ['', 'value_classes', 'platform']:
ctx['extra_compile_args'] += [ os.path.normpath(os.path.join(extra, ssubstitute)) ]
elif sys.platform.startswith("freebsd"):
os.environ["CPPFLAGS"] = "-Wno-unused-private-field"
if static:
ctx['libraries'] += [ "usb", "stdc++" ]
ctx['extra_objects'] = [ "{0}/libopenzwave.a".format(self.openzwave) ]
ctx['include_dirs'] += [ "{0}/cpp/build/linux".format(self.openzwave) ]
else:
import pyozw_pkgconfig
ctx['libraries'] += [ "openzwave" ]
extra = pyozw_pkgconfig.cflags('libopenzwave')
if extra != '':
for ssubstitute in ['', 'value_classes', 'platform']:
ctx['extra_compile_args'] += [ os.path.normpath(os.path.join(extra, ssubstitute)) ]
elif sys.platform.startswith("sunos"):
if static:
ctx['libraries'] += [ "usb-1.0", "stdc++",'resolv' ]
ctx['extra_objects'] = [ "{0}/libopenzwave.a".format(self.openzwave) ]
ctx['include_dirs'] += [ "{0}/cpp/build/linux".format(self.openzwave) ]
else:
import pyozw_pkgconfig
ctx['libraries'] += [ "openzwave" ]
extra = pyozw_pkgconfig.cflags('libopenzwave')
if extra != '':
for ssubstitute in ['', 'value_classes', 'platform']:
ctx['extra_compile_args'] += [ os.path.normpath(os.path.join(extra, ssubstitute)) ]
elif sys.platform.startswith("linux"):
if static:
ctx['libraries'] += [ "udev", "stdc++",'resolv' ]
ctx['extra_objects'] = [ "{0}/libopenzwave.a".format(self.openzwave) ]
ctx['include_dirs'] += [ "{0}/cpp/build/linux".format(self.openzwave) ]
else:
import pyozw_pkgconfig
ctx['libraries'] += [ "openzwave" ]
extra = pyozw_pkgconfig.cflags('libopenzwave')
if extra != '':
for ssubstitute in ['', 'value_classes', 'platform']:
ctx['extra_compile_args'] += [ os.path.normpath(os.path.join(extra, ssubstitute)) ]
else:
# Unknown systemm
raise RuntimeError("Can't detect plateform {0}".format(sys.platform))
return ctx
@property
def ctx(self):
if self._ctx is None:
if 'install' in sys.argv or 'develop' in sys.argv or 'bdist_egg' in sys.argv:
current_template.install_minimal_dependencies()
self._ctx = self.get_context()
self.finalize_context(self._ctx)
return self._ctx
@property
def build_ext(self):
if 'install' in sys.argv or 'develop' in sys.argv or 'bdist_egg' in sys.argv:
current_template.install_minimal_dependencies()
from Cython.Distutils import build_ext as _build_ext
return _build_ext
@property
def copy_openzwave_config(self):
return True
@property
def install_openzwave_so(self):
return False
def finalize_context(self, ctx):
self.clean_cython()
if self.flavor:
ctx['define_macros'] += [('PY_LIB_FLAVOR', self.flavor.replace('--flavor=',''))]
else:
ctx['define_macros'] += [('PY_LIB_FLAVOR', "embed")]
if self.backend:
ctx['define_macros'] += [('PY_LIB_BACKEND', self.backend)]
else:
ctx['define_macros'] += [('PY_LIB_BACKEND', "cython")]
return ctx
def install_requires(self):
if sys.platform.startswith("win"):
return ['Cython']
else:
return ['Cython==0.28.6']
def build_requires(self):
if sys.platform.startswith("win"):
return ['Cython']
else:
return ['Cython==0.28.6']
def build(self):
if len(self.ctx['extra_objects']) == 1 and os.path.isfile(self.ctx['extra_objects'][0]):
log.info("Use cached build of openzwave")
return True
from subprocess import Popen, PIPE
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
io_q = Queue()
def stream_watcher(identifier, stream):
# fixes subprocess output lag issue when using python 2.x
if PY3:
dummy_return = b''
else:
dummy_return = ''
for line in iter(stream.readline, dummy_return):
if line:
io_q.put((identifier, line))
if not stream.closed:
stream.close()
def printer():
while True:
try:
# Block for 1 second.
item = io_q.get(True, 1)
except Empty:
# No output in either streams for a second. Are we done?
if proc.poll() is not None:
break
else:
identifier, line = item
log.debug(identifier + ':', line)
if identifier == 'STDERR':
sys.stderr.write('{0}\n'.format(line))
log.error('{0}\n'.format(line))
elif sys.platform.startswith("win"):
progress_bar.write(line)
if sys.platform.startswith("win"):
progress_bar.close()
if sys.platform.startswith("win"):
from pyozw_progressbar import ProgressBar
from pyozw_win import get_clean_command, get_build_command
cwd = os.path.split(self.os_options['solution_path'])[0]
build_command = get_build_command(**self.os_options)
log.info("Build openzwave ... be patient ...")
progress_bar = ProgressBar()
proc = Popen(build_command, stdout=PIPE, stderr=PIPE, cwd=cwd)
elif sys.platform.startswith("cygwin"):
log.info("Build openzwave ... be patient ...")
proc = Popen('make', stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("darwin"):
log.info("Build openzwave ... be patient ...")
proc = Popen('make', stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("freebsd"):
log.info("Build openzwave ... be patient ...")
proc = Popen('gmake', stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("sunos"):
log.info("Build openzwave ... be patient ...")
# fixed command issues to Popen
proc = Popen(['make', 'PREFIX=/opt/local'], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("linux"):
log.info("Build openzwave ... be patient ...")
proc = Popen('make', stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
else:
# Unknown systemm
raise RuntimeError("Can't detect plateform {0}".format(sys.platform))
Thread(target=stream_watcher, name='stdout-watcher',
args=('STDOUT', proc.stdout)).start()
Thread(target=stream_watcher, name='stderr-watcher',
args=('STDERR', proc.stderr)).start()
tprinter = Thread(target=printer, name='printer')
tprinter.start()
while tprinter.is_alive():
time.sleep(1)
tprinter.join()
return True
def install_so(self):
log.info("Install openzwave so ... be patient ...")
from subprocess import Popen, PIPE
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
io_q = Queue()
def stream_watcher(identifier, stream):
for line in stream:
io_q.put((identifier, line))
if not stream.closed:
stream.close()
def printer():
while True:
try:
# Block for 1 second.
item = io_q.get(True, 1)
except Empty:
# No output in either streams for a second. Are we done?
if proc.poll() is not None:
break
else:
identifier, line = item
log.debug(identifier + ':', line)
if identifier == 'STDERR':
sys.stderr.write('{0}\n'.format(line))
log.error('{0}\n'.format(line))
if sys.platform.startswith("win"):
proc = Popen([ 'copy', 'OpenZWave.dll' , '%SYSTEM32%\\' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.os_options['vsproject_build']))
elif sys.platform.startswith("cygwin"):
proc = Popen([ 'make', 'install' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("darwin"):
proc = Popen([ 'make', 'install' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("freebsd"):
proc = Popen([ 'gmake', 'install' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("sunos"):
proc = Popen([ 'make', 'PREFIX=/opt/local', 'install' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("linux"):
proc = Popen([ 'make', 'install' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
else:
# Unknown systemm
raise RuntimeError("Can't detect plateform {0}".format(sys.platform))
Thread(target=stream_watcher, name='stdout-watcher',
args=('STDOUT', proc.stdout)).start()
Thread(target=stream_watcher, name='stderr-watcher',
args=('STDERR', proc.stderr)).start()
tprinter = Thread(target=printer, name='printer')
tprinter.start()
while tprinter.is_alive():
time.sleep(1)
tprinter.join()
if sys.platform.startswith("win"):
log.info("Register dll ... be patient ...")
proc = Popen([ 'regsvr32', 'OpenZWave.dll' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.os_options['vsproject_build']))
elif sys.platform.startswith("cygwin"):
import pyozw_pkgconfig
ldpath = pyozw_pkgconfig.libs_only_l('libopenzwave')[2:]
log.info("ldconfig openzwave in {0} so ... be patient ...".format(ldpath))
proc = Popen([ 'ldconfig', ldpath ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("darwin"):
import pyozw_pkgconfig
ldpath = pyozw_pkgconfig.libs_only_l('libopenzwave')[2:]
log.info("ldconfig openzwave in {0} so ... be patient ...".format(ldpath))
proc = Popen([ 'ldconfig', ldpath ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("freebsd"):
import pyozw_pkgconfig
ldpath = pyozw_pkgconfig.libs_only_l('libopenzwave')[2:]
log.info("ldconfig openzwave in {0} so ... be patient ...".format(ldpath))
proc = Popen([ 'ldconfig', ldpath ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("sunos"):
import pyozw_pkgconfig
ldpath = pyozw_pkgconfig.libs_only_l('libopenzwave')[2:]
log.info("ldconfig openzwave in {0} so ... be patient ...".format(ldpath))
proc = Popen([ 'ldconfig', ldpath ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("linux"):
import pyozw_pkgconfig
ldpath = pyozw_pkgconfig.libs_only_l('libopenzwave')[2:]
log.info("ldconfig openzwave in {0} so ... be patient ...".format(ldpath))
proc = Popen([ 'ldconfig', ldpath ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
else:
# Unknown systemm
raise RuntimeError("Can't detect plateform {0}".format(sys.platform))
Thread(target=stream_watcher, name='stdout-watcher',
args=('STDOUT', proc.stdout)).start()
Thread(target=stream_watcher, name='stderr-watcher',
args=('STDERR', proc.stderr)).start()
tprinter = Thread(target=printer, name='printer')
tprinter.start()
while tprinter.is_alive():
time.sleep(1)
tprinter.join()
time.sleep(2.5)
log.info("Openzwave so installed and loaded")
tprinter = None
return True
def clean(self):
#Build openzwave
try:
if not os.path.isdir(self.openzwave):
return True
except TypeError:
return True
log.info("Clean openzwave in %s ... be patient ..." % (self.openzwave) )
from subprocess import Popen, PIPE
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
io_q = Queue()
def stream_watcher(identifier, stream):
for line in stream:
io_q.put((identifier, line))
if not stream.closed:
stream.close()
def printer():
while True:
try:
# Block for 1 second.
item = io_q.get(True, 1)
except Empty:
# No output in either streams for a second. Are we done?
if proc.poll() is not None:
break
else:
identifier, line = item
log.debug(identifier + ':', line)
if identifier == 'STDERR':
sys.stderr.write('{0}\n'.format(line))
log.error('{0}\n'.format(line))
proc = None
if sys.platform.startswith("win"):
from pyozw_win import get_clean_command
clean_command = get_clean_command(**self.os_options)
log.info("Clean openzwave project ... be patient ...")
proc = Popen(
clean_command,
stdout=PIPE,
stderr=PIPE,
cwd=os.path.split(self.os_options['solution_path'])[0]
)
#~ proc.wait()
#~ proc = Popen([ 'regsvr32', '-u', 'OpenZWave.dll' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(os.path.abspath(self.openzwave)))
#~ proc = Popen([ 'del', '/F', '/Q', '/S', '%SYSTEM32%\OpenZWave.dll' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(os.path.abspath(self.openzwave)))
elif sys.platform.startswith("cygwin"):
proc = Popen([ 'make', 'clean' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("darwin"):
proc = Popen([ 'make', 'clean' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("freebsd"):
proc = Popen([ 'gmake', 'clean' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("sunos"):
proc = Popen([ 'make', 'PREFIX=/opt/local', 'clean' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
elif sys.platform.startswith("linux"):
proc = Popen([ 'make', 'clean' ], stdout=PIPE, stderr=PIPE, cwd='{0}'.format(self.openzwave))
else:
# Unknown systemm
raise RuntimeError("Can't detect plateform {0}".format(sys.platform))
if proc is not None:
Thread(target=stream_watcher, name='stdout-watcher',
args=('STDOUT', proc.stdout)).start()
Thread(target=stream_watcher, name='stderr-watcher',
args=('STDERR', proc.stderr)).start()
tprinter = Thread(target=printer, name='printer')
tprinter.start()
while tprinter.is_alive():
time.sleep(1)
tprinter.join()
return True
def clean_all(self):
log.info("Clean-all openzwave ... be patient ...")
try:
from pkg_resources import resource_filename
dirn = resource_filename('python_openzwave.ozw_config', '__init__.py')
dirn = os.path.dirname(dirn)
except ImportError:
dirn = None
if dirn is None or (dirn is not None and not os.path.isfile(os.path.join(dirn,'device_classes.xml'))):
#At first, check in /etc/openzwave
return self.clean()
import shutil
for f in os.listdir(dirn):
if f not in ['__init__.py', '__init__.pyc']:
if os.path.isfile(os.path.join(dirn, f)):
os.remove(os.path.join(dirn, f))
elif os.path.isdir(os.path.join(dirn, f)):
shutil.rmtree(os.path.join(dirn, f))
return self.clean()
def check_minimal_config(self):
if sys.platform.startswith("win"):
log.info("Found MSBuild.exe : {0}".format(self.os_options['msbuild_path']))
log.info("Found arch : {0}".format(self.os_options['arch']))
log.info("Found build configuration : {0}".format(self.os_options['build_type']))
log.info("Found Visual Studio project : {0}".format(self.os_options['solution_path']))
log.info("Found build path : {0}".format(self.os_options['build_path']))
log.info("Found cython : {0}".format(find_executable("cython")))
else:
log.info("Found g++ : {0}".format(find_executable("g++")))
log.info("Found gcc : {0}".format(find_executable("gcc")))
log.info("Found make : {0}".format(find_executable("make")))
log.info("Found gmake : {0}".format(find_executable("gmake")))
log.info("Found cython : {0}".format(find_executable("cython")))
exe = find_executable("pkg-config")
log.info("Found pkg-config : {0}".format(exe))
if exe is not None:
import pyozw_pkgconfig
for lib in self.ctx['libraries'] + ['yaml-0.1', 'libopenzwave', 'python', 'python2', 'python3']:
log.info("Found library {0} : {1}".format(lib, pyozw_pkgconfig.exists(lib)))
def install_minimal_dependencies(self):
if len(self.build_requires()) == 0:
return
import pip
try:
log.info("Get installed packages")
try:
packages = pip.utils.get_installed_distributions()
except Exception:
packages = []
for pyreq in self.build_requires():
if pyreq not in packages:
try:
log.info("Install minimal dependencies {0}".format(pyreq))
pip.main(['install', pyreq])
except Exception:
log.warn("Fail to install minimal dependencies {0}".format(pyreq))
else:
log.info("Minimal dependencies already installed {0}".format(pyreq))
except Exception:
log.warn("Can't get package list from pip.")
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/master'):
#Get openzwave
"""download an archive to a specific location"""
dest,tail = os.path.split(self.openzwave)
dest_file = os.path.join(dest, 'open-zwave.zip')
if os.path.exists(self.openzwave):
if not self.cleanozw:
#~ log.info("Already have directory %s. Use it. Use --cleanozw to clean it.", self.openzwave)
return self.openzwave
else:
#~ log.info("Already have directory %s but remove and clean it as asked", self.openzwave)
self.clean_all()
try:
os.remove(dest_file)
except Exception:
pass
log.info("fetching {0} into {1} for version {2}".format(url, dest_file, pyozw_version))
if not os.path.exists(dest):
os.makedirs(dest)
try:
# py2
from urllib2 import urlopen
except ImportError:
# py3
from urllib.request import urlopen
req = urlopen(url)
with open(dest_file, 'wb') as f:
f.write(req.read())
import zipfile
zip_ref = zipfile.ZipFile(dest_file, 'r')
zip_ref.extractall(dest)
zip_ref.close()
return self.openzwave
def clean_openzwave_so(self):
for path in ['/usr/local/etc/openzwave', '/usr/local/include/openzwave', '/usr/local/share/doc/openzwave']:
try:
log.info('Try to remove {0}'.format('/usr/local/etc/openzwave'))
shutil.rmtree(self.openzwave)
except Exception:
pass
return True
def clean_cython(self):
try:
os.remove('src-lib/libopenzwave/libopenzwave.cpp')
except Exception:
pass
class DevTemplate(Template):
def __init__(self, **args):
Template.__init__(self, **args)
def get_context(self):
opzw_dir = LOCAL_OPENZWAVE
if LOCAL_OPENZWAVE is None:
if sys.platform.startswith("win"):
from pyozw_win import get_openzwave
get_openzwave('openzwave')
else:
return None
if not os.path.isdir(opzw_dir):
if sys.platform.startswith("win"):
from pyozw_win import get_openzwave
get_openzwave(opzw_dir)
else:
return None
self.openzwave = opzw_dir
ctx = self.cython_context()
if ctx is None:
log.error("Can't find Cython")
return None
ctx = self.system_context(ctx, static=True)
return ctx
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/master'):
return True
class GitTemplate(Template):
def __init__(self, **args):
Template.__init__(self, openzwave=os.path.join("openzwave-git", 'open-zwave-master'), **args)
def get_context(self):
ctx = self.cython_context()
if ctx is None:
log.error("Can't find Cython")
return None
ctx = self.system_context(ctx, static=True)
return ctx
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/master'):
return Template.get_openzwave(self, url)
def clean_all(self):
ret = self.clean()
dest,tail = os.path.split(self.openzwave)
if tail == "openzwave-git":
try:
log.info('Try to remove {0}'.format(self.openzwave))
if os.path.isdir(self.openzwave):
shutil.rmtree(self.openzwave)
except Exception:
pass
elif tail == 'open-zwave-master':
try:
log.info('Try to remove {0}'.format(dest))
if os.path.isdir(dest):
shutil.rmtree(dest)
except Exception:
pass
return ret
class GitSharedTemplate(GitTemplate):
def get_context(self):
ctx = self.cython_context()
if ctx is None:
log.error("Can't find Cython")
return None
ctx = self.system_context(ctx, static=False)
while '' in ctx['extra_compile_args']:
ctx['extra_compile_args'].remove('')
extra = '-I/usr/local/include/openzwave//'
for ssubstitute in ['/', '/value_classes/', '/platform/']:
incl = extra.replace('//', ssubstitute)
if not incl in ctx['extra_compile_args']:
ctx['extra_compile_args'] += [ incl ]
return ctx
@property
def copy_openzwave_config(self):
return sys.platform.startswith("win")
@property
def install_openzwave_so(self):
return True
def clean(self):
self.clean_openzwave_so()
return GitTemplate.clean(self)
class OzwdevTemplate(GitTemplate):
def __init__(self, **args):
Template.__init__(self, openzwave=os.path.join("openzwave-git", 'open-zwave-Dev'), **args)
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/Dev'):
return Template.get_openzwave(self, url)
class OzwdevSharedTemplate(GitSharedTemplate):
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/Dev'):
return Template.get_openzwave(self, url)
class EmbedTemplate(Template):
def __init__(self, backend='cpp', **args):
Template.__init__(self, openzwave=os.path.join("openzwave-embed", 'open-zwave-master'), backend=backend, **args)
@property
def build_ext(self):
if 'install' in sys.argv or 'develop' in sys.argv:
current_template.check_minimal_config()
current_template.install_minimal_dependencies()
from distutils.command.build_ext import build_ext as _build_ext
return _build_ext
def get_context(self):
ctx = self.cpp_context()
ctx = self.system_context(ctx, static=True)
return ctx
def install_requires(self):
return []
def build_requires(self):
return []
def get_openzwave(self, url='https://raw.githubusercontent.com/OpenZWave/python-openzwave/master/archives/open-zwave-master-{0}.zip'.format(pyozw_version)):
ret = Template.get_openzwave(self, url)
shutil.copyfile(os.path.join(self.openzwave,'python-openzwave','openzwave.vers.cpp'), os.path.join(self.openzwave,'cpp','src','vers.cpp'))
return ret
def clean(self):
ret = Template.clean(self)
try:
log.info('Try to copy {0}'.format(os.path.join(self.openzwave,'python-openzwave','openzwave.vers.cpp')))
shutil.copyfile(os.path.join(self.openzwave,'python-openzwave','openzwave.vers.cpp'), os.path.join(self.openzwave,'cpp','src','vers.cpp'))
except Exception:
pass
return ret
def clean_all(self):
ret = self.clean()
dest,tail = os.path.split(self.openzwave)
if tail == "openzwave-embed":
try:
log.info('Try to remove {0}'.format(self.openzwave))
shutil.rmtree(self.openzwave)
except Exception:
pass
elif tail == 'open-zwave-master':
try:
log.info('Try to remove {0}'.format(dest))
shutil.rmtree(dest)
except Exception:
pass
return ret
class EmbedSharedTemplate(EmbedTemplate):
def get_context(self):
ctx = self.cpp_context()
ctx = self.system_context(ctx, static=False)
while '' in ctx['extra_compile_args']:
ctx['extra_compile_args'].remove('')
extra = '-I/usr/local/include/openzwave//'
for ssubstitute in ['/', '/value_classes/', '/platform/']:
incl = extra.replace('//', ssubstitute)
if not incl in ctx['extra_compile_args']:
ctx['extra_compile_args'] += [ incl ]
return ctx
def clean(self):
self.clean_openzwave_so()
return EmbedTemplate.clean(self)
@property
def copy_openzwave_config(self):
return False
@property
def install_openzwave_so(self):
return True
class SharedTemplate(Template):
def __init__(self, **args):
Template.__init__(self, **args)
def get_context(self):
ctx = self.cython_context()
if ctx is None:
log.error("Can't find Cython")
return None
ctx = self.system_context(ctx, static=False)
return ctx
def build(self):
return True
@property
def copy_openzwave_config(self):
return sys.platform.startswith("win")
def get_openzwave(self, url='https://codeload.github.com/OpenZWave/open-zwave/zip/master'):
return True
def parse_template(sysargv):
tmpl = None
flavor = None
if '--flavor=dev' in sysargv:
index = sysargv.index('--flavor=dev')
flavor = sysargv.pop(index)
tmpl = DevTemplate(sysargv=sysargv)
elif '--flavor=git' in sysargv:
index = sysargv.index('--flavor=git')
flavor = sysargv.pop(index)
tmpl = GitTemplate(sysargv=sysargv)
elif '--flavor=git_shared' in sysargv:
index = sysargv.index('--flavor=git_shared')
flavor = sysargv.pop(index)
tmpl = GitSharedTemplate(sysargv=sysargv)
elif '--flavor=ozwdev' in sysargv:
index = sysargv.index('--flavor=ozwdev')
flavor = sysargv.pop(index)
tmpl = OzwdevTemplate(sysargv=sysargv)
elif '--flavor=ozwdev_shared' in sysargv:
index = sysargv.index('--flavor=ozwdev_shared')
flavor = sysargv.pop(index)
tmpl = OzwdevSharedTemplate(sysargv=sysargv)
elif '--flavor=embed' in sysargv:
index = sysargv.index('--flavor=embed')
flavor = sysargv.pop(index)
tmpl = EmbedTemplate(sysargv=sysargv)
elif '--flavor=embed_shared' in sysargv:
index = sysargv.index('--flavor=embed_shared')
flavor = sysargv.pop(index)
tmpl = EmbedSharedTemplate(sysargv=sysargv)
elif '--flavor=shared' in sysargv:
index = sysargv.index('--flavor=shared')
flavor = sysargv.pop(index)
tmpl = SharedTemplate(sysargv=sysargv)
if tmpl is None:
flavor = 'embed'
try:
import pyozw_pkgconfig
if pyozw_pkgconfig.exists('libopenzwave'):
try:
from Cython.Distutils import build_ext
flavor = 'shared'
except ImportError:
log.info("Can't find cython")
except:
log.info("Can't find pkg-config")
#Default template
if flavor == 'embed':
log.info("Use embeded package of openzwave")
tmpl = EmbedTemplate(sysargv=sysargv)
elif flavor == 'shared':
log.info("Use precompiled library openzwave")
tmpl = SharedTemplate(sysargv=sysargv)
tmpl.flavor = flavor
if '--cleanozw' in sysargv:
index = sysargv.index('--cleanozw')
sysargv.pop(index)
tmpl.cleanozw = True
log.info('sysargv', sysargv)
print('sysargv', sysargv)
log.info("Found SETUP_DIR : {0}".format(SETUP_DIR))
print("Found SETUP_DIR : {0}".format(SETUP_DIR))
return tmpl
current_template = parse_template(sys.argv)
def install_requires():
pkgs = ['six', 'pyserial']
if (sys.version_info > (3, 0)):
pkgs.append('PyDispatcher>=2.0.5')
else:
pkgs.append('Louie>=1.1')
pkgs += current_template.install_requires()
return pkgs
def build_requires():
return current_template.build_requires()
def get_dirs(base):
return [x for x in glob.iglob(os.path.join( base, '*')) if os.path.isdir(x) ]
def data_files_config(target, source, pattern):
ret = list()
tup = list()
tup.append(target)
tup.append(glob.glob(os.path.join(source,pattern)))
ret.append(tup)
dirs = get_dirs(source)
if len(dirs):
for d in dirs:
rd = d.replace(source+os.sep, "", 1)
ret.extend(data_files_config(os.path.join(target,rd), \
os.path.join(source,rd), pattern))
return ret
class bdist_egg(_bdist_egg):
def run(self):
build_openzwave = self.distribution.get_command_obj('build_openzwave')
build_openzwave.develop = True
self.run_command('build_openzwave')
_bdist_egg.run(self)