Index: pydir/ifatts.py |
diff --git a/pydir/ifatts.py b/pydir/ifatts.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..317adc3de41358b9e666fad59015c0ae8130e4ae |
--- /dev/null |
+++ b/pydir/ifatts.py |
@@ -0,0 +1,61 @@ |
+#!/usr/bin/env python2 |
+ |
+import argparse |
+import os |
+import sys |
+ |
+from utils import shellcmd |
+ |
+def GetFileAttributes(Filename): |
+ """Returns the set of names contained in file named Filename. |
+ """ |
+ if not os.path.isfile(Filename): |
+ return [] |
jvoung (off chromium)
2014/10/15 21:57:05
Should this ever happen, or should it be considere
Karl
2014/10/16 16:51:48
Rethinking this, I agree this should be an error.
|
+ with open(Filename, 'r') as f: |
+ return f.read().split() |
+ |
+def hasFileAttributes(Filename, Attributes): |
jvoung (off chromium)
2014/10/15 21:57:05
HasFileAttributes
Karl
2014/10/16 16:51:48
Done.
|
+ """Returns true if the set of names in Attributes also appear |
+ in the set of names contained in file named Filename. |
+ """ |
jvoung (off chromium)
2014/10/15 21:57:05
I think the usual style is to line up the comments
Karl
2014/10/16 16:51:48
Done.
|
+ return set(Attributes) <= set(GetFileAttributes(Filename)) |
+ |
+def main(): |
+ """Check if the set attributes (i.e. names), contained in FILE, |
+ contains the attributes defined by --att=ATTRIBUTE arguments. If |
+ so, runs in a shell the command defined by the remaining |
+ arguments. Otherwise, quits (generating no output) and returns |
+ success. |
+ """ |
+ argparser = argparse.ArgumentParser( |
+ description=' ' + main.__doc__, |
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
+ argparser.add_argument('file', metavar='FILE', |
+ help='File defining attributes to check against.') |
+ argparser.add_argument('--att', required=False, default=[], |
+ action='append', metavar='ATTRIBUTE', |
+ help='Attribute to check. May be repeated.') |
+ argparser.add_argument('--echo-cmd', required=False, |
+ action='store_true', |
+ help='Trace the command before running.') |
+ argparser.add_argument('--command', nargs=argparse.REMAINDER, |
+ help='Command to run if attributes found.') |
+ |
+ args = argparser.parse_args() |
+ |
+ # Quit early if no command to run. |
+ if not args.command: |
+ return 0 |
jvoung (off chromium)
2014/10/15 21:57:05
Should this ever happen, or should it be considere
Jim Stichnoth
2014/10/15 22:23:54
All but the last return should probably be "return
Karl
2014/10/16 16:51:48
Actually, the last 2 should return 0! The conditio
Karl
2014/10/16 16:51:48
Now that I have simplified this python file, and -
|
+ |
+ # Quit early if the executable doesn't support expected build attributes. |
+ if not hasFileAttributes(args.file, args.att): |
+ return 0 |
+ |
+ stdout_result = shellcmd(args.command, echo=args.echo_cmd) |
+ if not args.echo_cmd: |
+ sys.stdout.write(stdout_result) |
+ |
+ return 0 |
+ |
+if __name__ == '__main__': |
+ main() |
Jim Stichnoth
2014/10/15 22:23:54
indent 2 spaces
Karl
2014/10/16 16:51:48
Done.
|