2 # Copyright 2018 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 """This script is used to update local profiles (AFDO, PGO or orderfiles)
7 This uses profiles of Chrome, or orderfiles for compiling or linking. Though the
8 profiles are available externally, the bucket they sit in is otherwise
9 unreadable by non-Googlers. Gsutil usage with this bucket is therefore quite
10 awkward: you can't do anything but `cp` certain files with an external account,
11 and you can't even do that if you're not yet authenticated.
13 No authentication is necessary if you pull these profiles directly over https.
16 from __future__ import print_function
25 GS_HTTP_URL = 'https://storage.googleapis.com'
28 def ReadUpToDateProfileName(newest_profile_name_path):
29 with open(newest_profile_name_path) as f:
30 return f.read().strip()
33 def ReadLocalProfileName(local_profile_name_path):
35 with open(local_profile_name_path) as f:
36 return f.read().strip()
38 # Assume it either didn't exist, or we couldn't read it. In either case, we
39 # should probably grab a new profile (and, in doing so, make this file sane
44 def WriteLocalProfileName(name, local_profile_name_path):
45 with open(local_profile_name_path, 'w') as f:
49 def CheckCallOrExit(cmd):
50 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
51 stdout, stderr = proc.communicate()
52 exit_code = proc.wait()
57 '## %s failed with exit code %d' % (cmd[0], exit_code),
58 '## Full command: %s' % cmd,
59 '## Stdout:\n' + stdout,
60 '## Stderr:\n' + stderr,
62 print('\n'.join(complaint_lines), file=sys.stderr)
66 def RetrieveProfile(desired_profile_name, out_path, gs_url_base):
67 # vpython is > python 2.7.9, so we can expect urllib to validate HTTPS certs
69 ext = os.path.splitext(desired_profile_name)[1]
70 if ext in ['.bz2', '.xz']:
71 # For extension that requires explicit decompression, decompression will
72 # change the eventual file names by dropping the extension, and that's why
73 # an extra extension is appended here to make sure that the decompressed
74 # file path matches the |out_path| passed in as parameter.
78 if not desired_profile_name.startswith(gs_prefix):
79 gs_url = '/'.join([GS_HTTP_URL, gs_url_base, desired_profile_name])
81 gs_url = '/'.join([GS_HTTP_URL, desired_profile_name[len(gs_prefix):]])
83 with contextlib.closing(urllib2.urlopen(gs_url)) as u:
84 with open(out_path, 'wb') as f:
92 # NOTE: we can't use Python's bzip module, since it doesn't support
93 # multi-stream bzip files. It will silently succeed and give us a garbage
95 # bzip2 removes the compressed file on success.
96 CheckCallOrExit(['bzip2', '-d', out_path])
98 # ...And we can't use the `lzma` module, since it was introduced in python3.
99 # xz removes the compressed file on success.
100 CheckCallOrExit(['xz', '-d', out_path])
104 parser = argparse.ArgumentParser(
105 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
109 help='Path to the file with name of the newest profile. '
110 'We use this file to track the name of the newest profile '
115 help='Path of the file storing name of the local profile. '
116 'We use this file to track the most recent profile we\'ve '
117 'successfully pulled.')
121 help='The base GS URL to search for the profile.')
125 help='Output name of the downloaded and uncompressed profile.')
130 help='Fetch a profile even if the local one is current.')
131 args = parser.parse_args()
133 up_to_date_profile = ReadUpToDateProfileName(args.newest_state)
135 local_profile_name = ReadLocalProfileName(args.local_state)
136 # In a perfect world, the local profile should always exist if we
137 # successfully read local_profile_name. If it's gone, though, the user
138 # probably removed it as a way to get us to download it again.
139 if local_profile_name == up_to_date_profile \
140 and os.path.exists(args.output_name):
143 new_tmpfile = args.output_name + '.new'
144 RetrieveProfile(up_to_date_profile, new_tmpfile, args.gs_url_base)
145 os.rename(new_tmpfile, args.output_name)
146 WriteLocalProfileName(up_to_date_profile, args.local_state)
149 if __name__ == '__main__':