1#!/usr/bin/env python 2# 3# Copyright 2016 Google Inc. 4# 5# Use of this source code is governed by a BSD-style license that can be 6# found in the LICENSE file. 7 8 9"""Tool for managing assets.""" 10 11 12import argparse 13import asset_utils 14import os 15import shutil 16import subprocess 17import sys 18 19FILE_DIR = os.path.dirname(os.path.abspath(__file__)) 20INFRA_BOTS_DIR = os.path.realpath(os.path.join(FILE_DIR, os.pardir)) 21 22sys.path.insert(0, INFRA_BOTS_DIR) 23import utils 24 25 26def _common_args(prs): 27 """Add common args to the given argparse.ArgumentParser.""" 28 prs.add_argument('asset_name', help='Name of the asset.') 29 prs.add_argument('--gsutil') 30 prs.add_argument('--service_account_json') 31 32 33def _store(args): 34 """Return asset_utils.MultiStore based on args.""" 35 return asset_utils.MultiStore(gsutil=args.gsutil, 36 service_account_json=args.service_account_json) 37 38 39def _asset(args): 40 """Return asset_utils.Asset based on args.""" 41 return asset_utils.Asset(args.asset_name, _store(args)) 42 43 44def add(args): 45 """Add a new asset.""" 46 asset_utils.Asset.add(args.asset_name, _store(args)) 47 48 49def remove(args): 50 """Remove an asset.""" 51 _asset(args).remove() 52 53 54def download(args): 55 """Download the current version of an asset.""" 56 _asset(args).download_current_version(args.target_dir) 57 58 59def upload(args): 60 """Upload a new version of the asset.""" 61 _asset(args).upload_new_version(args.target_dir, commit=args.commit, 62 extra_tags=args.extra_tags) 63 64 65def main(argv): 66 parser = argparse.ArgumentParser(description='Tool for managing assets.') 67 subs = parser.add_subparsers(help='Commands:') 68 69 prs_add = subs.add_parser('add', help='Add a new asset.') 70 prs_add.set_defaults(func=add) 71 _common_args(prs_add) 72 73 prs_remove = subs.add_parser('remove', help='Remove an asset.') 74 prs_remove.set_defaults(func=remove) 75 _common_args(prs_remove) 76 77 prs_download = subs.add_parser( 78 'download', help='Download the current version of an asset.') 79 prs_download.set_defaults(func=download) 80 _common_args(prs_download) 81 prs_download.add_argument('--target_dir', '-t', required=True) 82 83 prs_upload = subs.add_parser( 84 'upload', help='Upload a new version of an asset.') 85 prs_upload.set_defaults(func=upload) 86 _common_args(prs_upload) 87 prs_upload.add_argument('--target_dir', '-t', required=True) 88 prs_upload.add_argument('--commit', action='store_true') 89 prs_upload.add_argument( 90 '--extra_tags', nargs='+', 91 help='Additional tags for the CIPD package, "key:value"') 92 93 args = parser.parse_args(argv) 94 args.func(args) 95 96 97if __name__ == '__main__': 98 main(sys.argv[1:]) 99