6 # Implements "Virtual Working Directory"
7 # Provides a way of emulating a shared writable directory in Keep based
8 # on a "check out, edit, check in, merge" model.
9 # At the moment, this only permits adding new files, applications
10 # cannot modify or delete existing files.
12 # Create a symlink tree rooted at target_dir mirroring arv-mounted
13 # source_collection. target_dir must be empty, and will be created if it
15 def checkout(source_collection, target_dir, keepmount=None):
18 keepmount = os.environ['TASK_KEEPMOUNT']
20 if not os.path.exists(target_dir):
21 os.makedirs(target_dir)
23 l = os.listdir(target_dir)
25 raise Exception("target_dir must be empty before checkout, contains %s" % l)
27 stem = os.path.join(keepmount, source_collection)
28 for root, dirs, files in os.walk(os.path.join(keepmount, source_collection), topdown=True):
29 rel = root[len(stem)+1:]
31 os.mkdir(os.path.join(target_dir, rel, d))
33 os.symlink(os.path.join(root, f), os.path.join(target_dir, rel, f))
35 # Delete all symlinks and check in any remaining normal files.
36 # If merge == True, merge the manifest with source_collection and return a
37 # CollectionReader for the combined collection.
38 def checkin(source_collection, target_dir, merge=True):
39 # delete symlinks, commit directory, merge manifests and return combined
41 for root, dirs, files in os.walk(target_dir):
43 s = os.lstat(os.path.join(root, f))
44 if stat.S_ISLNK(s.st_mode):
45 os.unlink(os.path.join(root, f))
47 uuid = robust_put.upload(target_dir)
49 cr1 = arvados.CollectionReader(source_collection)
50 cr2 = arvados.CollectionReader(uuid)
51 combined = arvados.CollectionReader(cr1.manifest_text() + cr2.manifest_text())
54 return arvados.CollectionReader(uuid)