Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 1 | #!/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 Wang | 4a22431 | 2023-04-13 21:04:36 +0000 | [diff] [blame] | 6 | Migrates an infra or infra_internal gclient to an infra_superproject one. |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 7 | |
Joanna Wang | 4a22431 | 2023-04-13 21:04:36 +0000 | [diff] [blame] | 8 | Does an in-place migration of the cwd's infra or infra_internal gclient |
| 9 | checkout to an infra_superproject checkout, preserving all repo branches |
| 10 | and commits. Should be called in the gclient root directory (the one that |
| 11 | contains the .gclient file). |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 12 | |
Joanna Wang | 4a22431 | 2023-04-13 21:04:36 +0000 | [diff] [blame] | 13 | By default creates a backup dir of the original gclient checkout in |
| 14 | `<dir>_backup`. If something goes wrong during the migration, this can |
| 15 | be used to restore your environment to its original state. |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 16 | """ |
| 17 | |
Joanna Wang | 4a22431 | 2023-04-13 21:04:36 +0000 | [diff] [blame] | 18 | import argparse |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 19 | import subprocess |
| 20 | import os |
| 21 | import sys |
Joanna Wang | 4a22431 | 2023-04-13 21:04:36 +0000 | [diff] [blame] | 22 | import shutil |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def main(argv): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 26 | source = os.getcwd() |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 27 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 28 | parser = argparse.ArgumentParser( |
| 29 | description=__doc__.strip().splitlines()[0], |
| 30 | epilog=' '.join(__doc__.strip().splitlines()[1:])) |
| 31 | parser.add_argument('-n', |
| 32 | '--no-backup', |
| 33 | action='store_true', |
| 34 | help='NOT RECOMMENDED. Skips copying the current ' |
| 35 | 'checkout (which can take up to ~15 min) to ' |
| 36 | 'a backup before starting the migration.') |
| 37 | args = parser.parse_args(argv) |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 38 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 39 | if not args.no_backup: |
| 40 | backup = source + '_backup' |
| 41 | print(f'Creating backup in {backup}') |
| 42 | print('May take up to ~15 minutes...') |
| 43 | shutil.copytree(source, backup, symlinks=True, dirs_exist_ok=True) |
| 44 | print('backup complete') |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 45 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 46 | print(f'Deleting old {source}/.gclient file') |
| 47 | gclient_file = os.path.join(source, '.gclient') |
| 48 | with open(gclient_file, 'r') as file: |
| 49 | data = file.read() |
| 50 | internal = "infra_internal" in data |
| 51 | os.remove(gclient_file) |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 52 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 53 | print('Migrating to infra/infra_superproject') |
| 54 | cmds = ['fetch', '--force'] |
| 55 | if internal: |
| 56 | cmds.append('infra_internal') |
| 57 | print('including internal code in checkout') |
| 58 | else: |
| 59 | cmds.append('infra') |
| 60 | shell = sys.platform == 'win32' |
| 61 | fetch = subprocess.Popen(cmds, cwd=source, shell=shell) |
| 62 | fetch.wait() |
Joanna Wang | 0a590f3 | 2023-04-07 17:51:12 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 66 | sys.exit(main(sys.argv[1:])) |