Merge branch 'master' into 2257-inequality-conditions
[arvados.git] / sdk / python / bin / arv-mount
index 4a8233a3ca58da947fb45123aa9067caabcff480..5e773dfbc6c0185f32b02b76ab3255692a8b4cbe 100755 (executable)
@@ -1,14 +1,25 @@
 #!/usr/bin/env python
 
 from arvados.fuse import * 
+import arvados
+import subprocess
+import argparse
 
 if __name__ == '__main__':
     # Handle command line parameters
     parser = argparse.ArgumentParser(
-        description='Mount Keep data under the local filesystem.')
+        description='Mount Keep data under the local filesystem.',
+        epilog="""
+Note: When using the --exec feature, you must either specify the
+mountpoint before --exec, or mark the end of your --exec arguments
+with "--".
+""")
     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
     parser.add_argument('--collection', type=str, help="""Collection locator""")
     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
+    parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
+                        dest="exec_args", metavar=('command', 'args', '...', '--'),
+                        help="""Mount, run a command, then unmount and exit""")
 
     args = parser.parse_args()
 
@@ -28,16 +39,29 @@ if __name__ == '__main__':
 
     # Enable FUSE debugging (logs each FUSE request)
     if args.debug:
-        opts += ['debug']
+        opts += ['debug']    
     
     # Initialize the fuse connection
     llfuse.init(operations, args.mountpoint, opts)
 
-    # Run the main loop
-    #try:
-    llfuse.main()
-    #except:
-    #    llfuse.close(unmount=True)
-    #    raise
-    #
-    #llfuse.close(unmount=True)
+    if args.exec_args:
+        t = threading.Thread(None, lambda: llfuse.main())
+        t.start()
+
+        # wait until the driver is finished initializing
+        operations.initlock.wait()
+
+        rc = 255
+        try:
+            rc = subprocess.call(args.exec_args, shell=False)
+        except OSError as e:
+            sys.stderr.write('arv-mount: %s -- exec %s\n' % (str(e), args.exec_args))
+            rc = e.errno
+        except Exception as e:
+            sys.stderr.write('arv-mount: %s\n' % str(e))
+        finally:
+            subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
+
+        exit(rc)
+    else:
+        llfuse.main()