blob: aa50f67e8ad77f41c37e0f49a15d689295bd87cd [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Jamesef74b1c2012-11-12 07:47:47 -08002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Sync the Chrome source code used by Chrome OS to the specified directory."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Simran Basi7d7dbd82015-01-14 14:01:55 -080010import os
Mike Frysinger6165cdc2020-02-21 02:38:07 -050011import sys
Ryan Cui21aa0be2013-05-02 10:34:21 -070012
Aviv Keshetb7519e12016-10-04 00:50:00 -070013from chromite.lib import constants
David Jamesef74b1c2012-11-12 07:47:47 -080014from chromite.lib import commandline
15from chromite.lib import cros_build_lib
Don Garrettb5b33d02016-06-01 09:37:58 -070016from chromite.lib import cros_logging as logging
David Jamesef74b1c2012-11-12 07:47:47 -080017from chromite.lib import gclient
18from chromite.lib import osutils
19
20
Mike Frysinger6165cdc2020-02-21 02:38:07 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
David Jamesef74b1c2012-11-12 07:47:47 -080024def GetParser():
25 """Creates the argparse parser."""
26 parser = commandline.ArgumentParser(description=__doc__)
27
28 version = parser.add_mutually_exclusive_group()
29 version.add_argument('--tag', help='Sync to specified Chrome release',
30 dest='version')
Stefan Zagerd49d9ff2014-08-15 21:33:37 -070031 version.add_argument('--revision', help='Sync to specified git revision',
32 dest='version')
David Jamesef74b1c2012-11-12 07:47:47 -080033
34 parser.add_argument('--internal', help='Sync internal version of Chrome',
35 action='store_true', default=False)
David Jamesef74b1c2012-11-12 07:47:47 -080036 parser.add_argument('--reset', help='Revert local changes',
37 action='store_true', default=False)
38 parser.add_argument('--gclient', help=commandline.argparse.SUPPRESS,
Simran Basi7d7dbd82015-01-14 14:01:55 -080039 default=None)
Ben Chan2e19e4e2015-01-06 20:17:15 -080040 parser.add_argument('--gclient_template', help='Template gclient input file')
Don Garrettc4424eb2018-06-20 17:45:37 +000041 parser.add_argument('--skip_cache', help='Skip using git cache',
42 dest='use_cache', action='store_false')
Don Garrett85b258b2019-02-13 17:33:31 -080043 parser.add_argument('--git_cache_dir', type='path',
44 help='Define explicit git cache.')
Ningning Xia4ef57152018-02-20 15:05:49 -080045 parser.add_argument('--ignore_locks', help='Ignore git cache locks.',
46 action='store_true', default=False)
David Jamesef74b1c2012-11-12 07:47:47 -080047 parser.add_argument('chrome_root', help='Directory to sync chrome in')
48
49 return parser
50
51
Don Garrettb5b33d02016-06-01 09:37:58 -070052def SyncChrome(gclient_path, options):
53 """Sync new Chrome."""
54 gclient.WriteConfigFile(gclient_path, options.chrome_root,
55 options.internal, options.version,
Don Garrett85b258b2019-02-13 17:33:31 -080056 options.gclient_template, options.use_cache,
57 git_cache_dir=options.git_cache_dir)
Ningning Xia4ef57152018-02-20 15:05:49 -080058 gclient.Sync(gclient_path, options.chrome_root, reset=options.reset,
59 ignore_locks=options.ignore_locks)
Don Garrettb5b33d02016-06-01 09:37:58 -070060
61
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040062def main(argv):
David Jamesef74b1c2012-11-12 07:47:47 -080063 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040064 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080065
Mike Frysingera0313d02017-07-10 16:44:43 -040066 if options.gclient == '':
Simran Basi7d7dbd82015-01-14 14:01:55 -080067 parser.error('--gclient can not be an empty string!')
68 gclient_path = options.gclient or osutils.Which('gclient')
69 if not gclient_path:
70 gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, 'gclient')
71
Don Garrettb5b33d02016-06-01 09:37:58 -070072 try:
73 if options.reset:
74 # Revert any lingering local changes.
Simran Basi7d7dbd82015-01-14 14:01:55 -080075 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080076
Don Garrettb5b33d02016-06-01 09:37:58 -070077 SyncChrome(gclient_path, options)
78 except cros_build_lib.RunCommandError:
79 # If we have an error resetting, or syncing, we clobber, and fresh sync.
80 logging.warning('Chrome checkout appears corrupt. Clobbering.')
Don Garrettad54ff22016-11-04 12:38:13 -070081 osutils.RmDir(options.chrome_root, ignore_missing=True, sudo=True)
Prathmesh Prabhue149b952016-11-11 10:41:20 -080082 osutils.SafeMakedirsNonRoot(options.chrome_root)
Don Garrettb5b33d02016-06-01 09:37:58 -070083 SyncChrome(gclient_path, options)
Ryan Cui21aa0be2013-05-02 10:34:21 -070084
David Jamesef74b1c2012-11-12 07:47:47 -080085 return 0