Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python2 | |
| 2 | |
| 3 import argparse | |
| 4 import os | |
| 5 import sys | |
| 6 | |
| 7 from utils import shellcmd | |
| 8 | |
| 9 def main(): | |
| 10 """Run the specified command only if conditions are met. | |
| 11 | |
| 12 Two conditions are checked. First, the CONDITION must be true. | |
| 13 Secondly, all NEED names must be in the set of HAVE names. | |
| 14 If both conditionas are met, the command defined by the remaining | |
| 15 arguments is run in a shell. | |
| 16 """ | |
| 17 argparser = argparse.ArgumentParser( | |
| 18 description=' ' + main.__doc__, | |
| 19 formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| 20 argparser.add_argument('--cond', choices={'true', 'false'} , required=False, | |
|
Jim Stichnoth
2014/10/29 21:29:37
A little bikeshedding. What about:
argparser.add
Karl
2014/10/29 21:55:27
We can't use the same desc field for two different
| |
| 21 default='true', metavar='CONDITION', | |
| 22 help='Condition to test.') | |
| 23 argparser.add_argument('--need', required=False, default=[], | |
| 24 action='append', metavar='NEED', | |
| 25 help='Needed name. May be repeated.') | |
| 26 argparser.add_argument('--have', required=False, default=[], | |
| 27 action='append', metavar='HAVE', | |
| 28 help='Name you have. May be repeated.') | |
| 29 argparser.add_argument('--echo-cmd', required=False, | |
| 30 action='store_true', | |
| 31 help='Trace the command before running.') | |
| 32 argparser.add_argument('--command', nargs=argparse.REMAINDER, | |
| 33 help='Command to run if attributes found.') | |
| 34 | |
| 35 args = argparser.parse_args() | |
| 36 | |
| 37 # Quit early if no command to run. | |
| 38 if not args.command: | |
| 39 raise RuntimeError("No command argument(s) specified for ifatts") | |
| 40 | |
| 41 if args.cond and set(args.need) <= set(args.have): | |
| 42 stdout_result = shellcmd(args.command, echo=args.echo_cmd) | |
| 43 if not args.echo_cmd: | |
| 44 sys.stdout.write(stdout_result) | |
| 45 | |
| 46 if __name__ == '__main__': | |
| 47 main() | |
| 48 sys.exit(0) | |
| OLD | NEW |