config_util.py 1.56 KB
Newer Older
1 2 3 4
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

5
"""This module holds utilities which make writing configs easier."""
6 7 8 9

import json


10 11
class Config(object):
  """Base class for all configs.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

  Provides methods that are expected to be overridden by child classes. Also
  provides an command-line parsing method that converts the unified command-line
  interface used in depot_tools to the unified python interface defined here."""

  @staticmethod
  def fetch_spec(_props):
    """Returns instructions to check out the project, conditioned on |props|."""
    raise NotImplementedError

  @staticmethod
  def expected_root(_props):
    """Returns the directory into which the checkout will be performed."""
    raise NotImplementedError

  def handle_args(self, argv):
    """Passes the command-line arguments through to the appropriate method."""
    methods = {'fetch': self.fetch_spec,
               'root':  self.expected_root}
    if len(argv) <= 1 or argv[1] not in methods:
      print 'Must specify a a fetch/root action'
      return 1

    def looks_like_arg(arg):
      return arg.startswith('--') and arg.count('=') == 1

    bad_parms = [x for x in argv[2:] if not looks_like_arg(x)]
    if bad_parms:
      print 'Got bad arguments %s' % bad_parms
      return 1

    method = methods[argv[1]]
    props = dict(x.split('=', 1) for x in (y.lstrip('-') for y in argv[2:]))

    self.output(method(props))

  @staticmethod
  def output(data):
    print(json.dumps(data))