Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39558
1
#!/usr/bin/env python
2
3
import json, os, sys
4
join = os.path.join
5
6
SNAPSHOTS = join(os.environ['HOME'], '.snapshots')
7
MNT = "/mnt/snapshots/"
8
9
project_id = json.loads(open(join(os.environ['SMC'], 'info.json')).read())['project_id']
10
11
def find_snapshots():
12
listing_file = join(MNT,'listing')
13
if not os.path.exists(listing_file):
14
return []
15
for x in open(listing_file).read().split('\n\n'):
16
v = x.split()
17
if len(v) > 1:
18
path = join(MNT, v[0][:-1])
19
recent = v[-1]
20
if os.path.exists(join(path, recent, project_id)):
21
return path, v[1:]
22
23
24
def update_snapshots():
25
z = find_snapshots()
26
if not z: # nothing
27
return
28
[path, snapshots] = z
29
if not os.path.exists(SNAPSHOTS):
30
os.makedirs(SNAPSHOTS)
31
valid = set(snapshots)
32
current = set(os.listdir(SNAPSHOTS))
33
for s in current:
34
if s not in valid:
35
try:
36
os.unlink(s)
37
except:
38
pass
39
40
n = len("2015-12-13-215220")
41
current_trunc = set([x[:n] for x in current])
42
for s in snapshots:
43
if s[:n] not in current_trunc:
44
current_trunc.add(s[:n])
45
target = join(MNT, path, s, project_id)
46
if os.path.exists(target):
47
os.symlink(target, join(SNAPSHOTS, s))
48
49
50