annotate service/iot2/tasks.py @ 1754:92999dfbf321 default tip

add shelly support
author drewp@bigasterisk.com
date Tue, 04 Jun 2024 13:03:43 -0700
parents 810ca0863490
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
1 from invoke import task
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
2 from pathlib import Path
728
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
3 import os
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
4
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
5 PY_ENV = f'build/py'
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
6 NIM_ENV = f'build/nim'
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
7 NIM_BIN = f'{NIM_ENV}/bin'
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
8 PY_SITE_PACKAGES = f'{PY_ENV}/lib/python3.7/site-packages'
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
9 SOME_PY_DEP = Path(f'{PY_SITE_PACKAGES}/standardservice')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
10
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
11 @task
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
12 def nim_install(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
13 if Path(f'{NIM_BIN}/nim').exists():
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
14 return
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
15 ctx.run(f'curl https://nim-lang.org/download/nim-1.0.4-linux_x64.tar.xz | '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
16 f'xz -dc | '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
17 f'tar --extract --strip-components=1 --one-top-level={NIM_ENV}')
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
18
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
19 @task
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
20 def py_install(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
21 if Path(f'{PY_ENV}/bin/python').exists():
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
22 return
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
23 ctx.run(f'mkdir -p {PY_ENV}')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
24 ctx.run(f'virtualenv -p /usr/bin/python3.7 {PY_ENV}')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
25 # now .../wheel is newer than requirements.txt
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
26
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
27 @task(pre=[py_install])
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
28 def py_deps(ctx):
729
810ca0863490 build: place generated nim in its own dir
drewp@bigasterisk.com
parents: 728
diff changeset
29 pip_installed = Path(f'{PY_SITE_PACKAGES}/.pip_last_install')
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
30 requirements = Path('requirements.txt').stat().st_mtime
729
810ca0863490 build: place generated nim in its own dir
drewp@bigasterisk.com
parents: 728
diff changeset
31 if pip_installed.exists() and pip_installed.stat().st_mtime > requirements:
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
32 return
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
33 ctx.run(f'{PY_ENV}/bin/pip install '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
34 #f'--quiet '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
35 f'--index-url https://projects.bigasterisk.com/ '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
36 f'--extra-index-url https://pypi.org/simple '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
37 f'-r requirements.txt')
729
810ca0863490 build: place generated nim in its own dir
drewp@bigasterisk.com
parents: 728
diff changeset
38 ctx.run(f'touch {str(pip_installed)}')
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
39
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
40 @task(pre=[nim_install])
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
41 def nim_deps(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
42 pkgs = [('nimpy', 'nimpy-0.1.0'),
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
43 ('https://github.com/avsej/capnp.nim.git', 'capnp-0.0.3'),
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
44 ]
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
45 if all(Path(f'~/.nimble/pkgs/{pkg[1]}').expanduser().exists() for pkg in pkgs):
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
46 return
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
47 plain_names = ' '.join(p[0] for p in pkgs)
691
ae8ad2b758e2 iot2: WIP still. more messages schema
drewp@bigasterisk.com
parents: 689
diff changeset
48 print('todo: on initial install, this may need to be run a few times')
ae8ad2b758e2 iot2: WIP still. more messages schema
drewp@bigasterisk.com
parents: 689
diff changeset
49 ctx.run(f'{NIM_BIN}/nimble install -y {plain_names}',
ae8ad2b758e2 iot2: WIP still. more messages schema
drewp@bigasterisk.com
parents: 689
diff changeset
50 pty=True, env={'PATH': f'/usr/bin:{NIM_BIN}'})
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
51 ctx.run(f'ln -s ~/.nimble/bin/capnpc ~/.nimble/bin/capnpc-nim')
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
52
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
53
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
54 @task(pre=[nim_deps])
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
55 def nim_build_x86(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
56 ctx.run(f'{NIM_BIN}/nim compile '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
57 f'--out:build/iot2_linux_x86 '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
58 f'iot2_linux.nim',
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
59 pty=True)
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
60
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
61 @task
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
62 def arm_cross_compiler_install(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
63 if Path('/usr/share/crossbuild-essential-armhf/list').exists():
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
64 return
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
65 ctx.run(f'sudo apt install -y crossbuild-essential-armhf', pty=True)
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
66
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
67 @task(pre=[arm_cross_compiler_install, nim_install])
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
68 def nim_build_arm(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
69 ctx.run(f'{NIM_BIN}/nim compile '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
70 f'--cpu:arm '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
71 f'--out:build/iot2_linux_arm '
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
72 f'iot2_linux.nim',
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
73 pty=True)
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
74
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
75 @task(pre=[py_deps, nim_build_x86])
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
76 def local_run(ctx):
689
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
77 ctx.run(f'build/iot2_linux_x86')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
78
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
79 @task
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
80 def install_nim_capnp(ctx):
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
81 ctx.run(f'git clone git@github.com:drewp/capnp.nim.git build/capnp.nim')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
82 ctx.run(f'cd build/capnp.nim; ./build.sh')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
83 ctx.run(f'cd build/capnp.nim; bin/nim c capnp/capnpc.nim')
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
84
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
85 @task
e3eceee54937 introduce capnp, more build, some demos
drewp@bigasterisk.com
parents: 688
diff changeset
86 def messages_build_nim(ctx):
729
810ca0863490 build: place generated nim in its own dir
drewp@bigasterisk.com
parents: 728
diff changeset
87 Path('build/nim_capn').mkdir(exist_ok=True)
728
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
88 ctx.run(f'capnp compile '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
89 f'-o ./build/capnp.nim/capnp/capnpc '
729
810ca0863490 build: place generated nim in its own dir
drewp@bigasterisk.com
parents: 728
diff changeset
90 f'messages.capnp > build/nim_capn/messages.nim')
728
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
91
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
92 if 0:
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
93 # maybe esp32 should be done as extra c files in an esphome build,
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
94 # not ESP-IDF from scratch
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
95
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
96 @task
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
97 def setup_esp_build(ctx):
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
98 if not Path('build/esp-idf').exists():
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
99 ctx.run(f'git clone --recursive https://github.com/espressif/esp-idf build/esp-idf')
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
100 if not Path('build/esp/').exists():
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
101 ctx.run(f'cd build/esp-idf; IDF_TOOLS_PATH=../esp ./install.sh')
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
102
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
103 esp_env = {'IDF_TOOLS_PATH': '../build/esp',
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
104 'PATH': os.environ['PATH'] + ':' + str(Path('build/esp/tools/xtensa-esp32-elf/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/').absolute())}
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
105 esp_port = '/dev/ttyUSB0'
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
106
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
107 @task(pre=[setup_esp_build])
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
108 def nim_build_esp32(ctx):
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
109 ctx.run(f'{NIM_BIN}/nim compile '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
110 f'esp32_main/iot2_esp32.nim')
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
111 ctx.run(f'cd esp32_main; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
112 f'. ../build/esp-idf/export.sh; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
113 f'idf.py build', env=esp_env)
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
114
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
115 @task(pre=[nim_build_esp32])
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
116 def esp32_flash(ctx):
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
117 ctx.run(f'cd esp32_main; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
118 f'. ../build/esp-idf/export.sh; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
119 f'idf.py -p {esp_port} flash', env=esp_env)
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
120
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
121 @task(pre=[nim_build_esp32])
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
122 def esp32_monitor(ctx):
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
123 ctx.run(f'cd esp32_main; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
124 f'. ../build/esp-idf/export.sh; '
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
125 f'idf.py -p {esp_port} monitor', env=esp_env)
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
126
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
127
17ff9322328b an attempt as ESP-IDF build (doesn't work) calling a nim-generated main function (also doesn't work)
drewp@bigasterisk.com
parents: 691
diff changeset
128
688
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
129
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
130
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
131 # pack this into docker for pushing to Pi
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
132
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
133 # apt install -y sshfs
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
134 # sshfs drewp@10.2.0.110:/my/proj/homeauto/service/iot2 /mnt
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
135 # cd /mnt
7ac46dc29517 get some iot2 tests going
drewp@bigasterisk.com
parents:
diff changeset
136 # ./iot2_linux_arm