|
1 |
| -# Licensed to the Apache Software Foundation (ASF) under one |
2 |
| -# or more contributor license agreements. See the NOTICE file |
3 |
| -# distributed with this work for additional information |
4 |
| -# regarding copyright ownership. The ASF licenses this file |
5 |
| -# to you under the Apache License, Version 2.0 (the |
6 |
| -# "License"); you may not use this file except in compliance |
7 |
| -# with the License. You may obtain a copy of the License at |
8 |
| -# |
9 |
| -# http://www.apache.org/licenses/LICENSE-2.0 |
10 |
| -# |
11 |
| -# Unless required by applicable law or agreed to in writing, |
12 |
| -# software distributed under the License is distributed on an |
13 |
| -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 |
| -# KIND, either express or implied. See the License for the |
15 |
| -# specific language governing permissions and limitations |
16 |
| -# under the License. |
17 |
| - |
18 | 1 | SKIPDIR = ["virtualenv", "node_modules", "__pycache__"]
|
19 | 2 |
|
20 |
| -import os, sys, signal, os.path |
21 |
| -from subprocess import Popen |
22 |
| -from watchdog.observers import Observer |
23 |
| -from watchdog.events import FileSystemEventHandler |
| 3 | +import watchfiles |
| 4 | +import asyncio |
| 5 | +import os.path |
| 6 | +import signal |
24 | 7 |
|
25 | 8 | from .deploy import deploy
|
26 | 9 | from .client import serve
|
27 | 10 |
|
28 |
| -class ChangeHandler(FileSystemEventHandler): |
29 |
| - """Logs all the events captured.""" |
30 |
| - |
31 |
| - last_modified = {} |
32 |
| - |
33 |
| - def on_any_event(self, event): |
34 |
| - ## filter what is needed |
35 |
| - # only modified |
36 |
| - if event.event_type != "modified": return |
37 |
| - # no directories |
38 |
| - if event.is_directory: return |
39 |
| - src = event.src_path |
40 |
| - # no missing files |
41 |
| - if not os.path.exists(src): return |
42 |
| - # no generated directories |
43 |
| - for dir in src.split("/")[:-1]: |
44 |
| - if dir in SKIPDIR: return |
45 |
| - # no generated files |
46 |
| - if src.endswith(".zip"): return |
47 |
| - |
48 |
| - # cache last modified to do only once |
49 |
| - cur = os.path.getmtime(src) |
50 |
| - if self.last_modified.get(src, 0) == cur: |
51 |
| - return |
52 |
| - self.last_modified[src] = cur |
53 |
| - deploy(src) |
54 |
| - |
55 |
| -def watch(): |
56 |
| - observer = Observer() |
57 |
| - event_handler = ChangeHandler() |
58 |
| - observer.schedule(event_handler, "packages", recursive=True) |
59 |
| - #observer.schedule(event_handler, "web", recursive=True) |
60 |
| - observer.start() |
| 11 | +def check_and_deploy(change): |
| 12 | + cur_dir_len = len(os.getcwd())+1 |
| 13 | + change_type, path = change |
| 14 | + src = path[cur_dir_len:] |
| 15 | + print(f"{change_type}: {src}") |
| 16 | + # only modified |
| 17 | + if change_type != watchfiles.Change.modified: return |
| 18 | + # no directories |
| 19 | + if os.path.isdir(src): return |
| 20 | + # no missing files |
| 21 | + if not os.path.exists(src): return |
| 22 | + # no generated directories |
| 23 | + for dir in src.split("/")[:-1]: |
| 24 | + if dir in SKIPDIR: return |
| 25 | + # no generated files |
| 26 | + if src.endswith(".zip"): return |
| 27 | + # now you can deploy |
| 28 | + deploy(src) |
| 29 | + |
| 30 | +async def redeploy(): |
| 31 | + print("redeploy") |
| 32 | + iterator = watchfiles.awatch("packages", recursive=True) |
61 | 33 | try:
|
62 |
| - serve() |
| 34 | + async for changes in iterator: |
| 35 | + for change in changes: |
| 36 | + check_and_deploy(change) |
63 | 37 | except KeyboardInterrupt:
|
64 |
| - observer.stop() |
65 |
| - observer.join() |
| 38 | + print("Keyboard Interrupt") |
| 39 | + except: |
| 40 | + print("Exception") |
| 41 | + |
| 42 | +def watch(): |
| 43 | + loop = asyncio.get_event_loop() |
| 44 | + #task = loop.create_task(redeploy()) |
| 45 | + task = asyncio.ensure_future(redeploy()) |
| 46 | + def end_loop(): |
| 47 | + print("Ending task.") |
| 48 | + task.cancel() |
| 49 | + loop.add_signal_handler(signal.SIGTERM, end_loop) |
66 | 50 |
|
| 51 | + try: |
| 52 | + loop.run_until_complete(task) |
| 53 | + except asyncio.CancelledError: |
| 54 | + pass |
| 55 | + finally: |
| 56 | + loop.stop() |
| 57 | + loop.close() |
0 commit comments