-
Notifications
You must be signed in to change notification settings - Fork 1
/
filetimelapse.py
77 lines (63 loc) · 2.19 KB
/
filetimelapse.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
'''
Copy your file every few minutes while you work on it, so that you can have
snapshots of its history.
Not a replacement for real version control but could be applicable in very
simple situations or in cases where e.g. git is not.
'''
import argparse
import hashlib
import os
import shutil
import sys
import time
from voussoirkit import bytestring
from voussoirkit import pathclass
from voussoirkit import winglob
def hash_file(filepath, hasher):
bytestream = read_filebytes(filepath)
for chunk in bytestream:
hasher.update(chunk)
return hasher.hexdigest()
def hash_file_md5(filepath):
return hash_file(filepath, hasher=hashlib.md5())
def read_filebytes(filepath, chunk_size=bytestring.MEBIBYTE):
filepath = pathclass.Path(filepath)
if not filepath.is_file:
raise FileNotFoundError(filepath)
f = filepath.open('rb')
with f:
while True:
chunk = f.read(chunk_size)
if len(chunk) == 0:
break
yield chunk
def filetimelapse(filepath, rate):
(noext, extension) = os.path.splitext(filepath)
last_hash = None
existing_timelapses = winglob.glob(f'{noext}-*.filetimelapse{extension}')
if existing_timelapses:
last_hash = hash_file_md5(existing_timelapses[-1])
print(f'Starting with previous {existing_timelapses[-1]} {last_hash}')
while True:
new_hash = hash_file_md5(filepath)
if new_hash != last_hash:
timestamp = time.strftime('%Y%m%d%H%M%S')
copy_name = f'{noext}-{timestamp}.filetimelapse{extension}'
shutil.copy(filepath, copy_name)
last_hash = new_hash
print(copy_name, new_hash)
time.sleep(rate)
def filetimelapse_argparse(args):
try:
return filetimelapse(args.filepath, args.rate)
except KeyboardInterrupt:
pass
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('filepath')
parser.add_argument('--rate', default=None, required=True, type=int)
parser.set_defaults(func=filetimelapse_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))