publish_package.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env vpython3
  2. # Copyright 2022 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. """Implements commands for managing Fuchsia repos via the pm tool."""
  6. import argparse
  7. import os
  8. import subprocess
  9. import sys
  10. from typing import Iterable
  11. from common import SDK_TOOLS_DIR
  12. _pm_tool = os.path.join(SDK_TOOLS_DIR, 'pm')
  13. def publish_packages(packages: Iterable[str],
  14. repo: str,
  15. new_repo: bool = False) -> None:
  16. """Publish packages to a repo directory, initializing it if necessary."""
  17. if new_repo:
  18. subprocess.run([_pm_tool, 'newrepo', '-repo', repo], check=True)
  19. for package in packages:
  20. subprocess.run([_pm_tool, 'publish', '-a', '-r', repo, '-f', package],
  21. check=True)
  22. def register_package_args(parser: argparse.ArgumentParser,
  23. allow_temp_repo: bool = False) -> None:
  24. """Register common arguments for package publishing."""
  25. package_args = parser.add_argument_group(
  26. 'package', 'Arguments for package publishing.')
  27. package_args.add_argument('--packages',
  28. action='append',
  29. help='Paths of the package archives to install')
  30. package_args.add_argument('--repo',
  31. help='Directory packages will be published to.')
  32. if allow_temp_repo:
  33. package_args.add_argument(
  34. '--no-repo-init',
  35. action='store_true',
  36. default=False,
  37. help='Do not initialize the package repository.')
  38. def main():
  39. """Stand-alone function for publishing packages."""
  40. parser = argparse.ArgumentParser()
  41. register_package_args(parser)
  42. args = parser.parse_args()
  43. if not args.repo:
  44. raise ValueError('Must specify directory to publish packages.')
  45. if not args.packages:
  46. raise ValueError('Must specify packages to publish.')
  47. publish_packages(args.packages, args.repo)
  48. if __name__ == '__main__':
  49. sys.exit(main())