branch.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env vpython3
  2. # Copyright 2020 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. """Script for updating the project settings for a chromium branch.
  6. To initialize a new chromium branch, run the following from the root of
  7. the repo (where MM is the milestone number and BBBB is the branch
  8. number):
  9. ```
  10. infra/config/scripts/branch.py initialize --milestone MM --branch BBBB
  11. infra/config/main.star
  12. infra/config/dev.star
  13. ```
  14. Usage:
  15. branch.py initialize --milestone XX --branch YYYY
  16. """
  17. import argparse
  18. import json
  19. import os
  20. INFRA_CONFIG_DIR = os.path.abspath(os.path.join(__file__, '..', '..'))
  21. def parse_args(args=None, *, parser_type=None):
  22. parser_type = parser_type or argparse.ArgumentParser
  23. parser = parser_type(
  24. description='Update the project settings for a chromium branch')
  25. parser.set_defaults(func=None)
  26. parser.add_argument('--settings-json',
  27. help='Path to the settings.json file',
  28. default=os.path.join(INFRA_CONFIG_DIR, 'settings.json'))
  29. subparsers = parser.add_subparsers()
  30. init_parser = subparsers.add_parser(
  31. 'initialize', help='Initialize the settings for a branch')
  32. init_parser.set_defaults(func=initialize_cmd)
  33. init_parser.add_argument(
  34. '--milestone',
  35. required=True,
  36. help=('The milestone identifier '
  37. '(e.g. the milestone number for standard release channel)'))
  38. init_parser.add_argument(
  39. '--branch',
  40. required=True,
  41. help='The branch name, must correspond to a ref in refs/branch-heads')
  42. set_type_parser = subparsers.add_parser(
  43. 'set-type', help='Change the branch type of the project')
  44. set_type_parser.set_defaults(func=set_type_cmd)
  45. set_type_parser.add_argument(
  46. '--type',
  47. required=True,
  48. choices=BRANCH_TYPES,
  49. action='append',
  50. help='The type of the branch to change the project config to')
  51. args = parser.parse_args(args)
  52. if args.func is None:
  53. parser.error('no sub-command specified')
  54. return args
  55. def initial_settings(milestone, branch):
  56. settings = dict(
  57. project=f'chromium-m{milestone}',
  58. project_title=f'Chromium M{milestone}',
  59. ref=f'refs/branch-heads/{branch}',
  60. chrome_project=f'chrome-m{milestone}',
  61. branch_types=['standard'],
  62. )
  63. return json.dumps(settings, indent=4) + '\n'
  64. def initialize_cmd(args):
  65. settings = initial_settings(args.milestone, args.branch)
  66. with open(args.settings_json, 'w') as f:
  67. f.write(settings)
  68. BRANCH_TYPES = (
  69. 'standard',
  70. 'desktop-extended-stable',
  71. 'cros-lts',
  72. 'fuchsia-lts',
  73. )
  74. def set_type(settings_json, branch_types):
  75. for t in branch_types:
  76. assert t in BRANCH_TYPES, 'Unknown branch_type {!r}'.format(t)
  77. settings = json.loads(settings_json)
  78. settings.update(branch_types=branch_types)
  79. return json.dumps(settings, indent=4) + '\n'
  80. def set_type_cmd(args):
  81. with open(args.settings_json) as f:
  82. settings = f.read()
  83. settings = set_type(settings, args.type)
  84. with open(args.settings_json, 'w') as f:
  85. f.write(settings)
  86. def main():
  87. args = parse_args()
  88. args.func(args)
  89. if __name__ == '__main__':
  90. main()