1#!/usr/bin/env vpython3 2# Copyright 2022 The Chromium Authors 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 ffx tool.""" 6 7import argparse 8import sys 9 10from typing import Iterable 11 12from common import make_clean_directory, read_package_paths, \ 13 register_common_args, run_ffx_command 14 15 16def publish_packages(packages: Iterable[str], 17 repo: str, 18 new_repo: bool = False) -> None: 19 """Publish packages to a repo directory, initializing it if necessary.""" 20 if new_repo: 21 run_ffx_command(cmd=['repository', 'create', repo], check=True) 22 23 args = ['repository', 'publish'] 24 for package in packages: 25 args += ['--package-archive', package] 26 args += [repo] 27 run_ffx_command(cmd=args, check=True) 28 29 30def register_package_args(parser: argparse.ArgumentParser, 31 allow_temp_repo: bool = False) -> None: 32 """Register common arguments for package publishing.""" 33 package_args = parser.add_argument_group( 34 'package', 'Arguments for package publishing.') 35 package_args.add_argument('--packages', 36 action='append', 37 help='Paths of the package archives to install') 38 package_args.add_argument('--repo', 39 help='Directory packages will be published to.') 40 package_args.add_argument('--purge-repo', 41 action='store_true', 42 help='If clear the content in the repo.') 43 if allow_temp_repo: 44 package_args.add_argument( 45 '--no-repo-init', 46 action='store_true', 47 default=False, 48 help='Do not initialize the package repository.') 49 50 51def main(): 52 """Stand-alone function for publishing packages.""" 53 parser = argparse.ArgumentParser() 54 register_package_args(parser) 55 register_common_args(parser) 56 args = parser.parse_args() 57 if not args.repo: 58 raise ValueError('Must specify directory to publish packages.') 59 if not args.packages: 60 raise ValueError('Must specify packages to publish.') 61 if args.out_dir: 62 package_paths = [] 63 for package in args.packages: 64 package_paths.extend(read_package_paths(args.out_dir, package)) 65 else: 66 package_paths = args.packages 67 if args.purge_repo: 68 make_clean_directory(args.repo) 69 publish_packages(package_paths, args.repo) 70 71 72if __name__ == '__main__': 73 sys.exit(main()) 74