1 #!/usr/bin/env vpython3
2 # Copyright 2018 The Chromium Authors
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
24 from urllib.request import urlopen
26 GS_HTTP_URL = 'https://storage.googleapis.com'
29 def ReadUpToDateProfileName(newest_profile_name_path):
30 with open(newest_profile_name_path) as f:
31 return f.read().strip()
34 def ReadLocalProfileName(local_profile_name_path):
36 with open(local_profile_name_path) as f:
37 return f.read().strip()
39 # Assume it either didn't exist, or we couldn't read it. In either case, we
40 # should probably grab a new profile (and, in doing so, make this file sane
45 def WriteLocalProfileName(name, local_profile_name_path):
46 with open(local_profile_name_path, 'w') as f:
50 def CheckCallOrExit(cmd):
51 proc = subprocess.Popen(cmd,
52 stdout=subprocess.PIPE,
53 stderr=subprocess.PIPE,
55 stdout, stderr = proc.communicate()
56 exit_code = proc.wait()
61 '## %s failed with exit code %d' % (cmd[0], exit_code),
62 '## Full command: %s' % cmd,
63 '## Stdout:\n' + stdout,
64 '## Stderr:\n' + stderr,
66 print('\n'.join(complaint_lines), file=sys.stderr)
70 def RetrieveProfile(desired_profile_name, out_path, gs_url_base):
71 # vpython is > python 2.7.9, so we can expect urllib to validate HTTPS certs
73 ext = os.path.splitext(desired_profile_name)[1]
74 if ext in ['.bz2', '.xz']:
75 # For extension that requires explicit decompression, decompression will
76 # change the eventual file names by dropping the extension, and that's why
77 # an extra extension is appended here to make sure that the decompressed
78 # file path matches the |out_path| passed in as parameter.
82 if not desired_profile_name.startswith(gs_prefix):
83 gs_url = '/'.join([GS_HTTP_URL, gs_url_base, desired_profile_name])
85 gs_url = '/'.join([GS_HTTP_URL, desired_profile_name[len(gs_prefix):]])
87 with contextlib.closing(urlopen(gs_url)) as u:
88 with open(out_path, 'wb') as f:
96 # NOTE: we can't use Python's bzip module, since it doesn't support
97 # multi-stream bzip files. It will silently succeed and give us a garbage
99 # bzip2 removes the compressed file on success.
100 CheckCallOrExit(['bzip2', '-d', out_path])
102 # ...And we can't use the `lzma` module, since it was introduced in python3.
103 # xz removes the compressed file on success.
104 CheckCallOrExit(['xz', '-d', out_path])
108 parser = argparse.ArgumentParser(
109 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
113 help='Path to the file with name of the newest profile. '
114 'We use this file to track the name of the newest profile '
119 help='Path of the file storing name of the local profile. '
120 'We use this file to track the most recent profile we\'ve '
121 'successfully pulled.')
125 help='The base GS URL to search for the profile.')
129 help='Output name of the downloaded and uncompressed profile.')
134 help='Fetch a profile even if the local one is current.')
135 args = parser.parse_args()
137 up_to_date_profile = ReadUpToDateProfileName(args.newest_state)
139 local_profile_name = ReadLocalProfileName(args.local_state)
140 # In a perfect world, the local profile should always exist if we
141 # successfully read local_profile_name. If it's gone, though, the user
142 # probably removed it as a way to get us to download it again.
143 if local_profile_name == up_to_date_profile \
144 and os.path.exists(args.output_name):
147 new_tmpfile = args.output_name + '.new'
148 RetrieveProfile(up_to_date_profile, new_tmpfile, args.gs_url_base)
149 os.rename(new_tmpfile, args.output_name)
150 WriteLocalProfileName(up_to_date_profile, args.local_state)
153 if __name__ == '__main__':