Revision 810c50b7 lib/cli.py

b/lib/cli.py
41 41
           "cli_option", "GenerateTable", "AskUser",
42 42
           "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
43 43
           "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT",
44
           "ListTags", "AddTags", "RemoveTags",
44
           "ListTags", "AddTags", "RemoveTags", "TAG_SRC_OPT",
45 45
           ]
46 46

  
47 47

  
......
66 66
  return retval
67 67

  
68 68

  
69
def _ExtendTags(opts, args):
70
  """Extend the args if a source file has been given.
71

  
72
  This function will extend the tags with the contents of the file
73
  passed in the 'tags_source' attribute of the opts parameter. A file
74
  named '-' will be replaced by stdin.
75

  
76
  """
77
  fname = opts.tags_source
78
  if fname is None:
79
    return
80
  if fname == "-":
81
    new_fh = sys.stdin
82
  else:
83
    new_fh = open(fname, "r")
84
  new_data = []
85
  try:
86
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
87
    # because of python bug 1633941
88
    while True:
89
      line = new_fh.readline()
90
      if not line:
91
        break
92
      new_data.append(line.strip())
93
  finally:
94
    new_fh.close()
95
  args.extend(new_data)
96

  
97

  
69 98
def ListTags(opts, args):
70 99
  """List the tags on a given object.
71 100

  
......
94 123

  
95 124
  """
96 125
  kind, name = _ExtractTagsObject(opts, args)
126
  _ExtendTags(opts, args)
97 127
  if not args:
98 128
    raise errors.OpPrereqError("No tags to be added")
99 129
  op = opcodes.OpAddTags(kind=kind, name=name, tags=args)
......
110 140

  
111 141
  """
112 142
  kind, name = _ExtractTagsObject(opts, args)
143
  _ExtendTags(opts, args)
113 144
  if not args:
114 145
    raise errors.OpPrereqError("No tags to be removed")
115 146
  op = opcodes.OpDelTags(kind=kind, name=name, tags=args)
......
143 174
_LOCK_OPT = make_option("--lock-retries", default=None,
144 175
                        type="int", help=SUPPRESS_HELP)
145 176

  
177
TAG_SRC_OPT = make_option("--from", dest="tags_source",
178
                          default=None, help="File with tag names")
179

  
146 180
def ARGS_FIXED(val):
147 181
  """Macro-like function denoting a fixed number of arguments"""
148 182
  return -val

Also available in: Unified diff