Index: pydir/ifatts.py |
diff --git a/pydir/ifatts.py b/pydir/ifatts.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..1cb0c2b76ae5d5522075af00f4d6bc3e740e2dcb |
--- /dev/null |
+++ b/pydir/ifatts.py |
@@ -0,0 +1,58 @@ |
+#!/usr/bin/env python2 |
+ |
+import argparse |
+import os |
+import sys |
+ |
+from utils import shellcmd |
+ |
+def GetFileAttributes(Filename): |
Jim Stichnoth
2014/10/20 16:44:28
http://google-styleguide.googlecode.com/svn/trunk/
Karl
2014/10/20 21:00:42
Done.
|
+ """Returns the set of names contained in file named Filename. |
+ """ |
+ if not os.path.isfile(Filename): |
+ raise RuntimeError("Can't open: %s" % Filename) |
+ with open(Filename, 'r') as f: |
+ return f.read().split() |
+ |
+def HasFileAttributes(Filename, Attributes): |
+ """Returns true if the set of names in Attributes also appear |
Jim Stichnoth
2014/10/20 16:44:28
I thought Jan already mentioned this, but multi-li
Karl
2014/10/20 21:00:42
Done.
Jim Stichnoth
2014/10/21 21:58:41
Not done, please do...
Karl
2014/10/22 17:03:35
Done.
|
+ in the set of names contained in file named Filename. |
+ """ |
+ 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. |
+ """ |
jvoung (off chromium)
2014/10/20 17:36:00
could line this """ up too
Karl
2014/10/20 21:00:42
Done.
|
+ 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: |
+ raise RuntimeError("No command argument(s) specified for ifatts") |
+ |
+ if HasFileAttributes(args.file, args.att): |
+ 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() |
jvoung (off chromium)
2014/10/20 17:36:00
This should be "sys.exit(main())", to use main's r
Karl
2014/10/20 21:00:42
Ok. Chose the latter.
jvoung (off chromium)
2014/10/21 19:12:00
Can you upload the new copy?
Karl
2014/10/21 21:14:18
Sorry about that. Apparently, I modified it after
|