blob: c7753cb8cf2d319a751e081cf2e5c02762e0868b [file] [log] [blame]
Joanna Wang0a590f32023-04-07 17:51:12 +00001#!/usr/bin/env python3
2# Copyright (c) 2023 The Chromium 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"""
Joanna Wang4a224312023-04-13 21:04:36 +00006Migrates an infra or infra_internal gclient to an infra_superproject one.
Joanna Wang0a590f32023-04-07 17:51:12 +00007
Joanna Wang4a224312023-04-13 21:04:36 +00008Does an in-place migration of the cwd's infra or infra_internal gclient
9checkout to an infra_superproject checkout, preserving all repo branches
10and commits. Should be called in the gclient root directory (the one that
11contains the .gclient file).
Joanna Wang0a590f32023-04-07 17:51:12 +000012
Joanna Wang4a224312023-04-13 21:04:36 +000013By default creates a backup dir of the original gclient checkout in
14`<dir>_backup`. If something goes wrong during the migration, this can
15be used to restore your environment to its original state.
Joanna Wang0a590f32023-04-07 17:51:12 +000016"""
17
Joanna Wang4a224312023-04-13 21:04:36 +000018import argparse
Joanna Wang0a590f32023-04-07 17:51:12 +000019import subprocess
20import os
Joanna Wangc3dd9242023-04-10 16:42:13 +000021import platform
Joanna Wang0a590f32023-04-07 17:51:12 +000022import sys
Joanna Wang4a224312023-04-13 21:04:36 +000023import shutil
Joanna Wang0a590f32023-04-07 17:51:12 +000024import json
25from pathlib import Path
26
27
28def main(argv):
Joanna Wang4a224312023-04-13 21:04:36 +000029 source = os.getcwd()
Joanna Wang0a590f32023-04-07 17:51:12 +000030
Joanna Wang4a224312023-04-13 21:04:36 +000031 parser = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0],
32 epilog=' '.join(
33 __doc__.strip().splitlines()[1:]))
34 parser.add_argument('-n',
35 '--no-backup',
36 action='store_true',
37 help='NOT RECOMMENDED. Skips copying the current '
38 'checkout (which can take up to ~15 min) to '
39 'a backup before starting the migration.')
40 args = parser.parse_args(argv)
Joanna Wang0a590f32023-04-07 17:51:12 +000041
Joanna Wang4a224312023-04-13 21:04:36 +000042 if not args.no_backup:
43 backup = source + '_backup'
44 print(f'Creating backup in {backup}')
45 print('May take up to ~15 minutes...')
46 shutil.copytree(source, backup, symlinks=True, dirs_exist_ok=True)
47 print('backup complete')
Joanna Wang0a590f32023-04-07 17:51:12 +000048
Joanna Wang4a224312023-04-13 21:04:36 +000049 print(f'Deleting old {source}/.gclient file')
50 gclient_file = os.path.join(source, '.gclient')
Joanna Wang0a590f32023-04-07 17:51:12 +000051 with open(gclient_file, 'r') as file:
52 data = file.read()
53 internal = "infra_internal" in data
Joanna Wang0a590f32023-04-07 17:51:12 +000054 os.remove(gclient_file)
55
Joanna Wangc3dd9242023-04-10 16:42:13 +000056 print('Migrating to infra/infra_superproject')
Joanna Wang0a590f32023-04-07 17:51:12 +000057 cmds = ['fetch', '--force']
58 if internal:
59 cmds.append('infra_internal')
Joanna Wangc3dd9242023-04-10 16:42:13 +000060 print('including internal code in checkout')
Joanna Wang0a590f32023-04-07 17:51:12 +000061 else:
62 cmds.append('infra')
Joanna Wang4a224312023-04-13 21:04:36 +000063 fetch = subprocess.Popen(cmds, cwd=source, shell=True)
Joanna Wang0a590f32023-04-07 17:51:12 +000064 fetch.wait()
65
66
67if __name__ == '__main__':
68 sys.exit(main(sys.argv[1:]))