-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-folder.py
executable file
·81 lines (70 loc) · 2.34 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""A Python3 script to auto-deploy new calliope projects."""
from os import path, walk, chdir, stat, makedirs
import re
import sys
import subprocess
from time import sleep, time as t
import datetime as dt
from shutil import move
MINI_PROJECTS = []
PROJECTS_DIR = 'projects'
INTERVAL = 5
def _get_new_files(directory, seconds):
new_files = []
now = dt.datetime.now()
ago = now - dt.timedelta(seconds=seconds)
for root, dirs, files in walk(directory):
for fname in files:
pathstring = path.join(root, fname)
st = stat(pathstring)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
new_files.append(path.abspath(pathstring))
return new_files
def _handle_files(input_file, target_dir):
try:
makedirs(target_dir)
except FileExistsError:
pass # ignore
i = 1
now_filesave = dt.datetime.fromtimestamp(t()).strftime('%Y%m%d_%H%M%S')
tfiles = []
for ifile in input_file:
bn = re.sub('^mini-', '', path.basename(ifile))
bn = re.sub(' .*', '', bn)
bn = re.sub('\.hex$', '', bn)
tfile = path.join(target_dir, '{}-{}-{}.hex'.format(
bn, now_filesave, i))
print('[DL] {} [{}]\n[PROJECT] {}'.format(ifile, i, tfile))
move(ifile, tfile)
tfiles.append(tfile)
i += 1
return tfiles
def _deploy_file(file):
print('>>> {}'.format(file))
subprocess.Popen('./copy-to-calliope.py {}'.format(file), shell=True)
if __name__ == '__main__':
if len(sys.argv) <= 1:
print('Path to watch folder not provided.')
sys.exit(1)
if not path.exists(sys.argv[1]):
print('Path to watch folder does not exist.')
sys.exit(1)
wfolder = path.abspath(sys.argv[1])
chdir(path.dirname(path.abspath(__file__)))
target_dir = path.abspath('projects_new')
print('-- watching folder {}'.format(wfolder))
print('-- writing to {}'.format(target_dir))
try:
while 1:
newf = _get_new_files(wfolder, INTERVAL)
if len(newf) > 0:
tfiles = _handle_files(newf, target_dir)
if tfiles and len(tfiles) > 0:
_deploy_file(tfiles[0])
sleep(INTERVAL)
except KeyboardInterrupt:
print()
sys.exit(0)