Merge branch 'master' of git.clinicalfuture.com:arvados
[arvados.git] / sdk / python / bin / arv-mount
1 #!/usr/bin/env python
2
3 from arvados.fuse import * 
4 import arvados
5 import subprocess
6
7 if __name__ == '__main__':
8     api = arvados.api()
9
10     # Handle command line parameters
11     parser = argparse.ArgumentParser(
12         description='Mount Keep data under the local filesystem.')
13     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
14     parser.add_argument('--collection', type=str, help="""Collection locator""")
15     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
16     parser.add_argument('--exec', type=str, help="""Mount, run a command, then unmount and exit""", dest="ex")
17
18     args = parser.parse_args()
19
20     # Create the request handler
21     operations = Operations(os.getuid(), os.getgid())
22
23     if args.collection != None:
24         # Set up the request handler with the collection at the root
25         e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE))
26         operations.inodes.load_collection(e, arvados.CollectionReader(arvados.Keep.get(args.collection)))
27     else:
28         # Set up the request handler with the 'magic directory' at the root
29         operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
30
31     # FUSE options, see mount.fuse(8)
32     opts = []
33
34     # Enable FUSE debugging (logs each FUSE request)
35     if args.debug:
36         opts += ['debug']    
37     
38     # Initialize the fuse connection
39     llfuse.init(operations, args.mountpoint, opts)
40
41     if args.ex:
42         t = threading.Thread(None, lambda: llfuse.main())
43         t.start()
44
45         # wait until the driver is finished initializing
46         operations.initlock.wait()
47
48         try:
49             rc = subprocess.call(args.ex, shell=True)
50         except:
51             rc = 255
52         finally:
53             subprocess.call(["fusermount", "-u", args.mountpoint])
54
55         exit(rc)
56     else:
57         llfuse.main()