elizavetai | 340cab2 | 2014-10-10 14:04:27 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | |
| 6 | import sys |
| 7 | import getopt |
| 8 | import os |
| 9 | |
| 10 | here = os.path.realpath(__file__) |
| 11 | src_path = (os.path.normpath(os.path.join(here, '..', '..', '..'))) |
| 12 | sys.path.append(os.path.normpath(os.path.join(src_path, '..', 'depot_tools'))) |
| 13 | |
| 14 | USAGE = 'The utility uploads .png files to ' \ |
| 15 | 'chrome-os-oobe-ui-screenshot-testing Google Storage bucket.\n' \ |
| 16 | '-i:\n\tdirectory with .png files which have to be uploaded\n' \ |
| 17 | '-o (optional):\n\tdirectory to store generated .sha1 files. ' \ |
| 18 | 'Is set to chrome/browser/chromeos/login/screenshot_testing' \ |
| 19 | '/golden_screenshots by default\n--help:\n\thelp' |
| 20 | |
| 21 | |
| 22 | import upload_to_google_storage |
| 23 | import download_from_google_storage |
| 24 | |
| 25 | def upload(png_path): |
| 26 | |
| 27 | # Creating a list of files which need to be uploaded to Google Storage: |
| 28 | # all .png files from the directory containing golden screenshots. |
| 29 | target = [] |
| 30 | for file in os.listdir(png_path): |
| 31 | if file.endswith('.png'): |
| 32 | target.append(os.path.join(png_path, file)) |
| 33 | |
| 34 | # Creating a standard gsutil object, assuming there are depot_tools |
| 35 | # and everything related is set up already. |
| 36 | gsutil_path = os.path.abspath(os.path.join(src_path, '..', 'depot_tools', |
| 37 | 'third_party', 'gsutil', |
| 38 | 'gsutil')) |
| 39 | gsutil = download_from_google_storage.Gsutil(gsutil_path, |
| 40 | boto_path=None, |
| 41 | bypass_prodaccess=True) |
| 42 | |
| 43 | # URL of the bucket used for storing screenshots. |
| 44 | bucket_url = 'gs://chrome-os-oobe-ui-screenshot-testing' |
| 45 | |
| 46 | # Uploading using the most simple way, |
| 47 | # see depot_tools/upload_to_google_storage.py to have better understanding |
| 48 | # of this False and 1 arguments. |
| 49 | upload_to_google_storage.upload_to_google_storage(target, bucket_url, gsutil, |
| 50 | False, False, 1, False) |
| 51 | |
| 52 | print 'All images are uploaded to Google Storage.' |
| 53 | |
| 54 | def move_sha1(from_path, to_path): |
| 55 | from shutil import move |
| 56 | for file in os.listdir(from_path): |
| 57 | if (file.endswith('.sha1')): |
| 58 | old_place = os.path.join(from_path, file) |
| 59 | new_place = os.path.join(to_path, file) |
| 60 | if not os.path.exists(os.path.dirname(new_place)): |
| 61 | os.makedirs(os.path.dirname(new_place)) |
| 62 | move(old_place, new_place) |
| 63 | |
| 64 | def main(argv): |
| 65 | png_path = '' |
| 66 | sha1_path = os.path.join(src_path, |
| 67 | 'chrome', 'browser', 'chromeos', 'login', |
| 68 | 'screenshot_testing', 'golden_screenshots') |
| 69 | try: |
| 70 | opts, args = getopt.getopt(argv,'i:o:', ['--help']) |
| 71 | except getopt.GetoptError: |
| 72 | print USAGE |
| 73 | sys.exit(1) |
| 74 | for opt, arg in opts: |
| 75 | if opt == '--help': |
| 76 | print USAGE |
| 77 | sys.exit() |
| 78 | elif opt == '-i': |
| 79 | png_path = arg |
| 80 | elif opt =='-o': |
| 81 | sha1_path = arg |
| 82 | |
| 83 | if png_path == '': |
| 84 | print USAGE |
| 85 | sys.exit(1) |
| 86 | |
| 87 | png_path = os.path.abspath(png_path) |
| 88 | sha1_path = os.path.abspath(sha1_path) |
| 89 | |
| 90 | upload(png_path) |
| 91 | move_sha1(png_path, sha1_path) |
| 92 | |
| 93 | # TODO(elizavetai): Can this git stuff be done automatically? |
| 94 | print 'Please add new .sha1 files from ' \ |
| 95 | + str(sha1_path) + \ |
| 96 | ' to git manually.' |
| 97 | |
| 98 | if __name__ == "__main__": |
| 99 | main(sys.argv[1:]) |