1#!/usr/bin/env python
2# Copyright 2014 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
6"""Upload DM output PNG files and JSON summary to Google Storage."""
7
8
9import datetime
10import os
11import shutil
12import sys
13import tempfile
14
15def main(dm_dir, build_number, builder_name):
16  """Upload DM output PNG files and JSON summary to Google Storage.
17
18    dm_dir:        path to PNG files and JSON summary    (str)
19    build_number:  nth build on this builder             (str or int)
20    builder_name:  name of this builder                  (str)
21  """
22  # import gs_utils
23  current_dir = os.path.dirname(os.path.abspath(__file__))
24  sys.path.insert(0, os.path.join(current_dir, "../../../common/py/utils"))
25  import gs_utils
26
27  # Private, but Google-readable.
28  ACL = gs_utils.GSUtils.PredefinedACL.PRIVATE
29  FINE_ACLS = [(
30    gs_utils.GSUtils.IdType.GROUP_BY_DOMAIN,
31    'google.com',
32    gs_utils.GSUtils.Permission.READ
33  )]
34
35  if not os.path.isfile(os.path.join(dm_dir, 'dm.json')):
36    sys.exit("no dm.json file found in output directory.")
37
38  # Move dm.json to its own directory to make uploading it easier.
39  tmp = tempfile.mkdtemp()
40  shutil.move(os.path.join(dm_dir, 'dm.json'),
41              os.path.join(tmp,    'dm.json'))
42
43  # Only images are left in dm_dir.  Upload any new ones.
44  gs = gs_utils.GSUtils()
45  gs.upload_dir_contents(dm_dir,
46                         'skia-android-dm',
47                         'dm-images-v1',
48                         upload_if = gs.UploadIf.IF_NEW,
49                         predefined_acl = ACL,
50                         fine_grained_acl_list = FINE_ACLS)
51
52
53  # /dm-json-v1/year/month/day/hour/build-number/builder/dm.json
54  now = datetime.datetime.utcnow()
55  summary_dest_dir = '/'.join(['dm-json-v1',
56                               str(now.year ).zfill(4),
57                               str(now.month).zfill(2),
58                               str(now.day  ).zfill(2),
59                               str(now.hour ).zfill(2),
60                               str(build_number),
61                               builder_name])
62
63  # Upload the JSON summary.
64  gs.upload_dir_contents(tmp,
65                         'skia-android-dm',
66                         summary_dest_dir,
67                         predefined_acl = ACL,
68                         fine_grained_acl_list = FINE_ACLS)
69
70
71  # Just for hygiene, put dm.json back.
72  shutil.move(os.path.join(tmp,    'dm.json'),
73              os.path.join(dm_dir, 'dm.json'))
74  os.rmdir(tmp)
75
76if '__main__' == __name__:
77  main(*sys.argv[1:])
78