-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_folder.py
executable file
·50 lines (36 loc) · 1.42 KB
/
watch_folder.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
#!/usr/bin/env python3
import argparse
from pathlib import Path
import time
POLL_INTERVAL_SEC = 60
MIN_AGE_SEC = 300
def watch_folder(indir, outfile, min_age, poll_interval):
items = set()
if Path(outfile).exists():
items = set(line.strip() for line in open(outfile))
with open(outfile, 'a') as outf:
while True:
for p in Path(indir).rglob('*-binary-*.h5'):
abspath = str(p.absolute())
if abspath in items:
continue
# NOTE: time.time() and st_mtime are both UNIX timestamps, so
# this subtraction is "valid" regardless of timezone.
if time.time() - p.stat().st_mtime < MIN_AGE_SEC:
continue
outf.write(f'{abspath}\n')
items.add(abspath)
print(f'Added {abspath}')
outf.flush()
print('Sleeping...')
time.sleep(POLL_INTERVAL_SEC)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--indir', required=True)
ap.add_argument('-o', '--outfile', required=True)
ap.add_argument('--min-age', type=int, help='seconds', default=MIN_AGE_SEC)
ap.add_argument('--poll-interval', type=int, help='seconds', default=POLL_INTERVAL_SEC)
args = ap.parse_args()
watch_folder(args.indir, args.outfile, args.min_age, args.poll_interval)
if __name__ == '__main__':
main()