1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""A module for the sync command."""
6
7import os.path
8
9import cr
10
11
12class SyncCommand(cr.Command):
13  """The implementation of the sync command.
14
15  This command is a very thin shim over the gclient sync, and should remain so.
16  The only significant thing it adds is that the environment is set up so that
17  the run-hooks will do their work in the selected output directory.
18  """
19
20  # The configuration loaded to support this command.
21  DEFAULT = cr.Config.From(
22      GCLIENT_BINARY=os.path.join('{DEPOT_TOOLS}', 'gclient'),
23  )
24
25  # A placeholder for the detected gclient environment
26  DETECTED = cr.Config('DETECTED')
27
28  def __init__(self):
29    super(SyncCommand, self).__init__()
30    self.help = 'Sync the source tree'
31    self.description = 'Run gclient sync with the right environment.'
32
33  def AddArguments(self, subparsers):
34    parser = super(SyncCommand, self).AddArguments(subparsers)
35    self.ConsumeArgs(parser, 'gclient')
36    # TODO(iancottrell): clean no-hooks support would be nice.
37    return parser
38
39  def Run(self):
40    self.Sync(cr.context.remains)
41
42  @staticmethod
43  def Sync(args):
44    cr.PrepareCommand.UpdateContext()
45    # TODO(iancottrell): we should probably run the python directly,
46    # rather than the shell wrapper
47    # TODO(iancottrell): try to help out when the local state is not a good
48    # one to do a sync in
49    cr.Host.Execute('{GCLIENT_BINARY}', 'sync', *args)
50
51  @classmethod
52  def ClassInit(cls):
53    # Attempt to detect gclient and it's parent repository.
54    gclient_binaries = cr.Host.SearchPath('gclient')
55    if gclient_binaries:
56      cls.DETECTED.Set(GCLIENT_BINARY=gclient_binaries[0])
57      cls.DETECTED.Set(DEPOT_TOOLS=os.path.dirname(gclient_binaries[0]))
58