aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/tradefed/upload_dm_results.py
blob: a8ca01e64c601463c6421b36f1cec504f469840d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Upload DM output PNG files and JSON summary to Google Storage."""


import datetime
import os
import shutil
import sys
import tempfile

def main(dm_dir, build_number, builder_name):
  """Upload DM output PNG files and JSON summary to Google Storage.

    dm_dir:        path to PNG files and JSON summary    (str)
    build_number:  nth build on this builder             (str or int)
    builder_name:  name of this builder                  (str)
  """
  # import gs_utils
  current_dir = os.path.dirname(os.path.abspath(__file__))
  sys.path.insert(0, os.path.join(current_dir, "../../../common/py/utils"))
  import gs_utils

  # Private, but Google-readable.
  ACL = gs_utils.GSUtils.PredefinedACL.PRIVATE
  FINE_ACLS = [(
    gs_utils.GSUtils.IdType.GROUP_BY_DOMAIN,
    'google.com',
    gs_utils.GSUtils.Permission.READ
  )]

  if not os.path.isfile(os.path.join(dm_dir, 'dm.json')):
    sys.exit("no dm.json file found in output directory.")

  # Move dm.json to its own directory to make uploading it easier.
  tmp = tempfile.mkdtemp()
  shutil.move(os.path.join(dm_dir, 'dm.json'),
              os.path.join(tmp,    'dm.json'))

  # Only images are left in dm_dir.  Upload any new ones.
  gs = gs_utils.GSUtils()
  gs.upload_dir_contents(dm_dir,
                         'skia-android-dm',
                         'dm-images-v1',
                         upload_if = gs.UploadIf.IF_NEW,
                         predefined_acl = ACL,
                         fine_grained_acl_list = FINE_ACLS)


  # /dm-json-v1/year/month/day/hour/build-number/builder/dm.json
  now = datetime.datetime.utcnow()
  summary_dest_dir = '/'.join(['dm-json-v1',
                               str(now.year ).zfill(4),
                               str(now.month).zfill(2),
                               str(now.day  ).zfill(2),
                               str(now.hour ).zfill(2),
                               str(build_number),
                               builder_name])

  # Upload the JSON summary.
  gs.upload_dir_contents(tmp,
                         'skia-android-dm',
                         summary_dest_dir,
                         predefined_acl = ACL,
                         fine_grained_acl_list = FINE_ACLS)


  # Just for hygiene, put dm.json back.
  shutil.move(os.path.join(tmp,    'dm.json'),
              os.path.join(dm_dir, 'dm.json'))
  os.rmdir(tmp)

if '__main__' == __name__:
  main(*sys.argv[1:])