File: push_proto.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rwxr-xr-x 2,881 bytes parent folder | download | duplicates (11)
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
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Build and push the {vers}/all/ssl_error_assistant.pb file to GCS so
# that the component update system will pick it up and push it to users.
# See README.md before running this.
#
# Requires ninja and gsutil to be in the user's path.

import optparse
import os
import shutil
import subprocess
import sys


DEST_BUCKET = 'gs://chrome-components-ssl-error-assistant'
RESOURCE_SUBDIR = 'components/resources/ssl/ssl_error_assistant'

# Subdirectory to be copied to Google Cloud Storage. Contains a copy of the
# generated proto under a versioned directory.
GS_COPY_DIR = "gs_copy"

# TODO(meacer): This is pretty much a duplicate of
#               chrome/browser/safe_browsing/push_file_type_proto.py. Consider
#               refactoring and reusing code.
def main():
  parser = optparse.OptionParser()
  parser.add_option('-d', '--dir',
                    help='An up-to-date GN/Ninja build directory, '
                    'such as ./out/Debug')

  (opts, _) = parser.parse_args()
  if opts.dir is None:
    parser.print_help()
    return 1

  # Clear out the target dir before we build so we can be sure we've got
  # the freshest version.
  target_dir = os.path.join(opts.dir, "gen", RESOURCE_SUBDIR)
  if os.path.isdir(target_dir):
    shutil.rmtree(target_dir)

  gn_command = ['ninja',
                '-C', opts.dir,
                RESOURCE_SUBDIR + ':make_ssl_error_assistant_protobuf']
  print("Running the following")
  print("   " + (' '.join(gn_command)))
  if subprocess.call(gn_command):
    print("Ninja failed.")
    return 1

  # Use the versioned files under the copy directory to push to the GCS bucket.
  copy_dir = os.path.join(target_dir, GS_COPY_DIR)
  os.chdir(copy_dir)

  # Sanity check that there is a versioned copy under the directory.
  dirs = os.listdir('.')
  assert len(dirs) == 1 and dirs[0].isdigit(), (
      "There must be a single versioned dir under " + copy_dir)

  # Push the files with their directories, in the form
  #   {vers}/{platform}/download_file_types.pb
  # Don't overwrite existing files, in case we forgot to increment the
  # version.
  version_dir = dirs[0]
  command = ['gsutil', 'cp', '-Rn', version_dir, DEST_BUCKET]

  print('\nGoing to run the following command')
  print('   ', ' '.join(command))
  print('\nIn directory')
  print('   ', copy_dir)
  print('\nWhich should push the following files')
  expected_files = [os.path.join(dp, f) for dp, _, fn in
                    os.walk(version_dir) for f in fn]
  for f in expected_files:
    print('   ', f)

  shall = raw_input('\nAre you sure (y/N) ').lower() == 'y'
  if not shall:
    print('aborting')
    return 1
  return subprocess.call(command)


if __name__ == '__main__':
  sys.exit(main())