infra_to_superproject.py 2.2 KB

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