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 add(args):
27  """Add a new asset."""
28  asset_utils.Asset.add(args.asset_name,
29                        asset_utils.MultiStore(gsutil=args.gsutil))
30
31
32def remove(args):
33  """Remove an asset."""
34  asset_utils.Asset(args.asset_name,
35                    asset_utils.MultiStore(gsutil=args.gsutil)).remove()
36
37
38def 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
45def 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                           extra_tags=args.extra_tags)
51
52
53def main(argv):
54  parser = argparse.ArgumentParser(description='Tool for managing assets.')
55  subs = parser.add_subparsers(help='Commands:')
56
57  prs_add = subs.add_parser('add', help='Add a new asset.')
58  prs_add.set_defaults(func=add)
59  prs_add.add_argument('asset_name', help='Name of the asset.')
60  prs_add.add_argument('--gsutil')
61
62  prs_remove = subs.add_parser('remove', help='Remove an asset.')
63  prs_remove.set_defaults(func=remove)
64  prs_remove.add_argument('asset_name', help='Name of the asset.')
65  prs_remove.add_argument('--gsutil')
66
67  prs_download = subs.add_parser(
68      'download', help='Download the current version of an asset.')
69  prs_download.set_defaults(func=download)
70  prs_download.add_argument('asset_name', help='Name of the asset.')
71  prs_download.add_argument('--target_dir', '-t', required=True)
72  prs_download.add_argument('--gsutil')
73
74  prs_upload = subs.add_parser(
75      'upload', help='Upload a new version of an asset.')
76  prs_upload.set_defaults(func=upload)
77  prs_upload.add_argument('asset_name', help='Name of the asset.')
78  prs_upload.add_argument('--target_dir', '-t', required=True)
79  prs_upload.add_argument('--gsutil')
80  prs_upload.add_argument('--commit', action='store_true')
81  prs_upload.add_argument(
82    '--extra_tags', nargs='+',
83    help='Additional tags for the CIPD package, "key:value"')
84
85  args = parser.parse_args(argv)
86  args.func(args)
87
88
89if __name__ == '__main__':
90  main(sys.argv[1:])
91