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 12 import argparse 13 import asset_utils 14 import os 15 import shutil 16 import subprocess 17 import sys 18 19 FILE_DIR = os.path.dirname(os.path.abspath(__file__)) 20 INFRA_BOTS_DIR = os.path.realpath(os.path.join(FILE_DIR, os.pardir)) 21 22 sys.path.insert(0, INFRA_BOTS_DIR) 23 import utils 24 25 26 def add(args): 27 """Add a new asset.""" 28 asset_utils.Asset.add(args.asset_name, 29 asset_utils.MultiStore(gsutil=args.gsutil)) 30 31 32 def remove(args): 33 """Remove an asset.""" 34 asset_utils.Asset(args.asset_name, 35 asset_utils.MultiStore(gsutil=args.gsutil)).remove() 36 37 38 def download(args): 39 """Download the current version of an asset.""" 40 asset = asset_utils.Asset(args.asset_name, 41 asset_utils.MultiStore(gsutil=args.gsutil)) 42 asset.download_current_version(args.target_dir) 43 44 45 def upload(args): 46 """Upload a new version of the asset.""" 47 asset = asset_utils.Asset(args.asset_name, 48 asset_utils.MultiStore(gsutil=args.gsutil)) 49 asset.upload_new_version(args.target_dir, commit=args.commit) 50 51 52 def main(argv): 53 parser = argparse.ArgumentParser(description='Tool for managing assets.') 54 subs = parser.add_subparsers(help='Commands:') 55 56 prs_add = subs.add_parser('add', help='Add a new asset.') 57 prs_add.set_defaults(func=add) 58 prs_add.add_argument('asset_name', help='Name of the asset.') 59 prs_add.add_argument('--gsutil') 60 61 prs_remove = subs.add_parser('remove', help='Remove an asset.') 62 prs_remove.set_defaults(func=remove) 63 prs_remove.add_argument('asset_name', help='Name of the asset.') 64 prs_remove.add_argument('--gsutil') 65 66 prs_download = subs.add_parser( 67 'download', help='Download the current version of an asset.') 68 prs_download.set_defaults(func=download) 69 prs_download.add_argument('asset_name', help='Name of the asset.') 70 prs_download.add_argument('--target_dir', '-t', required=True) 71 prs_download.add_argument('--gsutil') 72 73 prs_upload = subs.add_parser( 74 'upload', help='Upload a new version of an asset.') 75 prs_upload.set_defaults(func=upload) 76 prs_upload.add_argument('asset_name', help='Name of the asset.') 77 prs_upload.add_argument('--target_dir', '-t', required=True) 78 prs_upload.add_argument('--gsutil') 79 prs_upload.add_argument('--commit', action='store_true') 80 81 args = parser.parse_args(argv) 82 args.func(args) 83 84 85 if __name__ == '__main__': 86 main(sys.argv[1:]) 87