public
Description: Fabric: Simple pythonic deployment (repository mirror of nongnu.org)
Homepage: http://www.nongnu.org/fab/
Clone URL: git://github.com/karmazilla/fabric.git
Revamped the CLI parsing and cut down the line count.
karmazilla (author)
Tue Feb 12 15:55:56 -0800 2008
commit  1b72b2efbc441701ff3233aab54ac81b692a8175
tree    29be38df97e0752f6f4f44ff47902b44c4c9c64f
parent  53c0c211d7cec1ea1498b0552e9b4a05aa0fd0ce
...
832
833
834
 
 
835
836
837
...
905
906
907
908
909
910
911
912
913
914
 
 
 
 
915
916
917
918
 
 
919
920
921
922
923
924
925
926
927
928
929
 
 
930
931
 
 
 
 
 
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
 
 
 
 
 
 
 
949
950
951
952
953
954
955
956
 
 
 
957
958
959
...
832
833
834
835
836
837
838
839
...
907
908
909
 
 
 
 
 
 
 
910
911
912
913
914
 
 
 
915
916
917
918
 
 
 
 
 
 
 
 
 
919
920
921
922
923
924
925
926
927
928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
929
930
931
932
933
934
935
936
937
938
939
940
941
 
 
942
943
944
945
946
947
0
@@ -832,6 +832,8 @@ def _disconnect():
0
 
0
 def _lazy_format(string, env=ENV):
0
     "Do recursive string substitution of ENV vars - both lazy and earger."
0
+ if string is None:
0
+ return None
0
     def replacer_fn(match):
0
         var = match.group('var')
0
         if var in env:
0
@@ -905,55 +907,41 @@ def _pick_fabfile():
0
 def _parse_args(args):
0
     cmds = []
0
     for cmd in args:
0
- cmd_name = cmd
0
- cmd_args = None
0
- if cmd.find(':') != -1:
0
- cmd_name, cmd_args = cmd.split(':', 1)
0
- if cmd_args is not None:
0
- cmd_arg_kvs = {}
0
- for cmd_arg_kv in cmd_args.split(','):
0
+ cmd_args = {}
0
+ if ':' in cmd:
0
+ cmd, cmd_str_args = cmd.split(':', 1)
0
+ for cmd_arg_kv in cmd_str_args.split(','):
0
                 k, _, v = cmd_arg_kv.partition('=')
0
- cmd_arg_kvs[k] = (v % ENV)
0
- cmd_args = cmd_arg_kvs
0
- cmds.append((cmd_name, cmd_args))
0
+ cmd_args[k] = (v % ENV)
0
+ cmds.append((cmd, cmd_args))
0
     return cmds
0
 
0
-def _validate_commands(args):
0
- for cmd in args:
0
- if cmd.find(':') != -1:
0
- cmd = cmd.split(':', 1)[0]
0
- if not cmd in COMMANDS:
0
- print("No such command: %s" % cmd)
0
- _list_commands()
0
- exit(1)
0
- if not args:
0
+def _validate_commands(cmds):
0
+ if not cmds:
0
         print("No commands given.")
0
         _list_commands()
0
+ else:
0
+ for cmd in cmds:
0
+ if not cmd[0] in COMMANDS:
0
+ print("No such command: %s" % cmd[0])
0
+ exit(1)
0
 
0
-def _execute_commands(args):
0
- for cmd in args:
0
- cmd_name = cmd
0
- cmd_args = None
0
- if cmd.find(':') != -1:
0
- cmd_name, cmd_args = cmd.split(':', 1)
0
- ENV['fab_cur_command'] = cmd_name
0
- print("Running %s..." % cmd_name)
0
- if cmd_args is not None:
0
- cmd_arg_kvs = {}
0
- for cmd_arg_kv in cmd_args.split(','):
0
- k, _, v = cmd_arg_kv.partition('=')
0
- cmd_arg_kvs[k] = (v % ENV)
0
- COMMANDS[cmd_name](**cmd_arg_kvs)
0
- else:
0
- COMMANDS[cmd]()
0
+def _execute_commands(cmds):
0
+ for cmd, args in cmds:
0
+ ENV['fab_cur_command'] = cmd
0
+ print("Running %s..." % cmd)
0
+ if args is not None:
0
+ args = dict(zip(args.keys(), map(_lazy_format, args.values())))
0
+ COMMANDS[cmd](**(args and args or {}))
0
 
0
 def main(args):
0
     try:
0
         print(__greeter__ % ENV)
0
         fabfile = _pick_fabfile()
0
         load(fabfile)
0
- _validate_commands(args)
0
- _execute_commands(args)
0
+ commands = _parse_args(args)
0
+ _validate_commands(commands)
0
+ _execute_commands(commands)
0
     finally:
0
         _disconnect()
0
         print("Done.")
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@ def setUp(self):
0
     pass
0
 def test_cli_arg_parsing(self):
0
     tests = [
0
- ("abc", ("abc", None)),
0
+ ("abc", ("abc", {})),
0
         ("ab:c", ("ab", {'c':''})),
0
         ("a:b=c", ('a', {'b':'c'})),
0
         ("a:b=c,d=e", ('a', {'b':'c','d':'e'})),

Comments

    No one has commented yet.