rblanche: add optional authentication
authorAlex Dehnert <adehnert@mit.edu>
Wed, 9 Oct 2013 16:06:54 +0000 (12:06 -0400)
committerAlex Dehnert <adehnert@mit.edu>
Wed, 9 Oct 2013 16:06:54 +0000 (12:06 -0400)
rblanche.py

index 10b0705d7b59b73a381522e6a7f53764c6cf1dee..d135dcb858d4a729ed35f770c5a15931d7a0b274 100755 (executable)
@@ -1,7 +1,9 @@
 #!/usr/bin/python
-import moira
+import optparse
 import sys
 
+import moira
+
 cache = {}
 
 def get_members_of_list(lst):
@@ -42,6 +44,31 @@ def rblanche(lst):
     result = expand(lst)
     print_tree([lst], result)
 
+def parse_args():
+    parser = optparse.OptionParser(usage='usage: %prog [--auth=yes|no|try] list')
+    parser.add_option('--auth', dest='auth', default=None, )
+    options, args = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error("incorrect number of arguments")
+
+    if options.auth:
+        if options.auth == 'yes': options.auth = True
+        elif options.auth == 'try': options.auth = None
+        elif options.auth == 'no': options.auth = False
+        else:
+            parser.error("--auth takes yes, no, and try (default; auth if possible)")
+
+    return options, args
+
 if __name__ == '__main__':
+    options, args = parse_args()
     moira.connect()
-    rblanche(sys.argv[1])
+    if options.auth:
+        moira.auth('rblnchpy')
+    elif options.auth is None:
+        try:
+            moira.auth('rblnchpy')
+        except moira.MoiraException:
+            print >>sys.stderr, "Warning: Failed to authenticate to moira"
+    rblanche(args[0])