]> git.arvados.org - arvados.git/blob - sdk/python/bin/arv-mount
Merge branch 'master' into origin-2035-arv-mount-tags-folders
[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 import argparse
7
8 if __name__ == '__main__':
9     # Handle command line parameters
10     parser = argparse.ArgumentParser(
11         description='''Mount Keep data under the local filesystem.  By default, if neither
12         --collection or --tags is specified, this mounts as a virtual directory
13         under which all Keep collections are available as subdirectories named
14         with the Keep locator; however directories will not be visible to 'ls'
15         until a program tries to access them.''',
16         epilog="""
17 Note: When using the --exec feature, you must either specify the
18 mountpoint before --exec, or mark the end of your --exec arguments
19 with "--".
20 """)
21     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
22     parser.add_argument('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
23     parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
24 collections on the server.""")
25     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
26     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
27                         dest="exec_args", metavar=('command', 'args', '...', '--'),
28                         help="""Mount, run a command, then unmount and exit""")
29
30     args = parser.parse_args()
31
32     # Create the request handler
33     operations = Operations(os.getuid(), os.getgid())
34
35     if args.tags:
36         api = arvados.api('v1')
37         e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
38     elif args.collection != None:
39         # Set up the request handler with the collection at the root
40         e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
41     else:
42         # Set up the request handler with the 'magic directory' at the root
43         operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
44
45     # FUSE options, see mount.fuse(8)
46     opts = []
47
48     # Enable FUSE debugging (logs each FUSE request)
49     if args.debug:
50         opts += ['debug']
51
52     # Initialize the fuse connection
53     llfuse.init(operations, args.mountpoint, opts)
54
55     if args.exec_args:
56         t = threading.Thread(None, lambda: llfuse.main())
57         t.start()
58
59         # wait until the driver is finished initializing
60         operations.initlock.wait()
61
62         rc = 255
63         try:
64             rc = subprocess.call(args.exec_args, shell=False)
65         except OSError as e:
66             sys.stderr.write('arv-mount: %s -- exec %s\n' % (str(e), args.exec_args))
67             rc = e.errno
68         except Exception as e:
69             sys.stderr.write('arv-mount: %s\n' % str(e))
70         finally:
71             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
72
73         exit(rc)
74     else:
75         llfuse.main()