blob: 0b67706ae51e36b63b217948de37d378ba83208d [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"""
6Creates a new infra_superpoject gclient checkout based on an existing
7infra or infra_internal checkout.
8
9 Usage:
10
11 (1) Commit any WIP changes in all infra repos:
12 `git commit -a -v -m "another commit" OR `git commit -a -v --amend`
13
14 (2) Run `git rebase-update` and `gclient sync` and ensure all conflicts
15 are resolved.
16
17 (3) In your gclient root directory (the one that contains a .gclient file),
18 run:
19 `python3 path/to/depot_tools/infra_to_superproject.py <destination>`
20 example:
21 `cd ~/cr` # gclient root dir
22 `python3 depot_tools/infra_to_superproject.py ~/cr2`
23
24 (4) `cd <destination>` and check that everything looks like your original
25 gclient checkout. The file structure should be the same, your branches
26 and commits in repos should be copied over.
27
28 (5) `sudo rm -rf <old_directory_name>`
29 example:
30 `sudo rm -rf cr`
31
32 (6) `mv <destination> <old_directory_name>`
33 example:
34 `mv cr2 cr
35
36"""
37
38import subprocess
39import os
Joanna Wangc3dd9242023-04-10 16:42:13 +000040import platform
Joanna Wang0a590f32023-04-07 17:51:12 +000041import sys
42import json
43from pathlib import Path
44
45
46def main(argv):
47 assert len(argv) == 1, 'One and only one arg expected.'
Joanna Wangc3dd9242023-04-10 16:42:13 +000048 assert platform.system() == 'Linux', 'Non-linux OSs not supported yet.'
Joanna Wang0a590f32023-04-07 17:51:12 +000049 destination = argv[0]
50
51 # In case there is '~' in the destination string
52 destination = os.path.expanduser(destination)
53
54 Path(destination).mkdir(parents=True, exist_ok=True)
55
Joanna Wangc3dd9242023-04-10 16:42:13 +000056 print(f'Copying {os.getcwd()} into {destination}')
57 cp = subprocess.Popen(
58 ['cp', '-a', os.path.join(os.getcwd(), '.'), destination],
59 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE)
Joanna Wang0a590f32023-04-07 17:51:12 +000061 cp.wait()
Joanna Wangc3dd9242023-04-10 16:42:13 +000062 print('Copying complete')
Joanna Wang0a590f32023-04-07 17:51:12 +000063
Joanna Wangc3dd9242023-04-10 16:42:13 +000064 print(f'Deleting old {destination}/.gclient file')
Joanna Wang0a590f32023-04-07 17:51:12 +000065 gclient_file = os.path.join(destination, '.gclient')
66 with open(gclient_file, 'r') as file:
67 data = file.read()
68 internal = "infra_internal" in data
69
70 os.remove(gclient_file)
71
Joanna Wangc3dd9242023-04-10 16:42:13 +000072 print('Migrating to infra/infra_superproject')
Joanna Wang0a590f32023-04-07 17:51:12 +000073 cmds = ['fetch', '--force']
74 if internal:
75 cmds.append('infra_internal')
Joanna Wangc3dd9242023-04-10 16:42:13 +000076 print('including internal code in checkout')
Joanna Wang0a590f32023-04-07 17:51:12 +000077 else:
78 cmds.append('infra')
79 fetch = subprocess.Popen(cmds, cwd=destination)
80 fetch.wait()
81
82
83if __name__ == '__main__':
84 sys.exit(main(sys.argv[1:]))