Oben Sonne avatar Oben Sonne committed 73ce773 Draft

Use more fine-grained return codes

Return codes are now defined by constants. Return codes may be combined
bitwise, which is especially useful when multiple items are processed by
one command (because different errors may occur).

Comments (0)

Files changed (1)

 
 VERSION="0.1"
 
+# bitwise or'able return codes
+RC_OK = 0
+RC_MOUNT_FAILED = 1
+RC_KEYRING_LOCKED = 2
+RC_UNKNOWN_ITEM = 4
+RC_INVALID_PATH = 8
+RC_MOUNT_POINT_IN_USE = 16
+
 # =============================================================================
 # helper
 # =============================================================================
 
     return opts
 
-def _exit(ec):
+def _exit(rc):
     """Exit with additional check if autostart file is still needed."""
 
-    if ec != 2:
+    if rc != RC_KEYRING_LOCKED: # getting items requires an unlocked keyring
         _autostart(_get_items(amount="y"))
-    sys.exit(ec)
+    sys.exit(rc)
 
 def _proceed(msg):
     print("Warning: %s" % msg)
 
     if not items and path:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in items:
         epath = item.attributes["encfs-path"]
         print("  mount point    : %s" % mpoint)
         print("  mount at login : %s" % (amount == "y" and "yes" or "no"))
 
-    return True
+    return RC_OK
 
 def add_item(epath, mpoint):
     """Add new EncFS item to keyring."""
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def edit_item(mpoint):
     """Edit EncFS item in keyring."""
 
     if not edits:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in edits:
 
         global _items_cached
         _items_cached = None
 
-    return True
+    return RC_OK
 
 def remove_item(mpoint):
     """Remove EncFS item from keyring."""
 
     if not items:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in items:
         gk.item_delete_sync(KEYRING, item.item_id)
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def mount_items(path, autostart):
     """Mount selected items.
     point equals `path`. If `autostart` is True, mount only those items where
     auto-mount is set to 'y'.
 
+    Return true if all items have been mounted successfully and false
+    otherwise.
+
     """
     items = _get_items(anypath=path, amount=(autostart and "y" or None))
 
     if not items and path:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
-    rc = True
+    rc = 0
 
     for item in items:
         epath = item.attributes["encfs-path"]
         msg = "Mounting %s at %s: " % (epath, mpoint)
         if _is_mounted(mpoint):
             msg += "mount point already in use"
+            rc |= RC_MOUNT_POINT_IN_USE
         elif not os.path.isdir(mpoint):
             msg += "mount point does not exist or is not a directory"
-            rc = False
+            rc |= RC_INVALID_PATH
         else:
             cmd = ["encfs", "-o", "nonempty", "-S", epath, mpoint]
             p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
             p.communicate(input="%s\n" % item.secret)
             msg += p.returncode and "FAILED" or "OK"
-            rc &= not p.returncode
+            rc |= 0 if p.returncode == os.EX_OK else RC_MOUNT_FAILED
 
         print(msg)
 
     opts = _options()
 
     if opts.add:
-        ok = add_item(opts.p1, opts.p2)
+        rc = add_item(opts.p1, opts.p2)
     elif opts.list:
-        ok = list_items(opts.p1)
+        rc = list_items(opts.p1)
     elif opts.mount:
-        ok = mount_items(opts.p1, opts.autostart)
+        rc = mount_items(opts.p1, opts.autostart)
     elif opts.edit:
-        ok = edit_item(opts.p1)
+        rc = edit_item(opts.p1)
     elif opts.remove:
-        ok = remove_item(opts.p1)
+        rc = remove_item(opts.p1)
     else:
         assert False
 
-    return ok
-    
+    return rc
+
 if __name__ == '__main__':
-
     try:
-        ret = int(not main())
+        rc = main()
     except gk.CancelledError:
-        ret = 2
-    _exit(ret)
+        rc = RC_KEYRING_LOCKED
+    _exit(rc)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.