Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / tools / download_optimization_profile.py
1 #!/usr/bin/env python
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)
6
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.
12
13 No authentication is necessary if you pull these profiles directly over https.
14 """
15
16 from __future__ import print_function
17
18 import argparse
19 import contextlib
20 import os
21 import subprocess
22 import sys
23
24 if sys.version_info.major == 2:
25   from urllib2 import urlopen
26 else:
27   from urllib.request import urlopen
28
29 GS_HTTP_URL = 'https://storage.googleapis.com'
30
31
32 def ReadUpToDateProfileName(newest_profile_name_path):
33   with open(newest_profile_name_path) as f:
34     return f.read().strip()
35
36
37 def ReadLocalProfileName(local_profile_name_path):
38   try:
39     with open(local_profile_name_path) as f:
40       return f.read().strip()
41   except IOError:
42     # Assume it either didn't exist, or we couldn't read it. In either case, we
43     # should probably grab a new profile (and, in doing so, make this file sane
44     # again)
45     return None
46
47
48 def WriteLocalProfileName(name, local_profile_name_path):
49   with open(local_profile_name_path, 'w') as f:
50     f.write(name)
51
52
53 def CheckCallOrExit(cmd):
54   proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55   stdout, stderr = proc.communicate()
56   exit_code = proc.wait()
57   if not exit_code:
58     return
59
60   complaint_lines = [
61       '## %s failed with exit code %d' % (cmd[0], exit_code),
62       '## Full command: %s' % cmd,
63       '## Stdout:\n' + stdout,
64       '## Stderr:\n' + stderr,
65   ]
66   print('\n'.join(complaint_lines), file=sys.stderr)
67   sys.exit(1)
68
69
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
72   # properly.
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.
79     out_path += ext
80
81   gs_prefix = 'gs://'
82   if not desired_profile_name.startswith(gs_prefix):
83     gs_url = '/'.join([GS_HTTP_URL, gs_url_base, desired_profile_name])
84   else:
85     gs_url = '/'.join([GS_HTTP_URL, desired_profile_name[len(gs_prefix):]])
86
87   with contextlib.closing(urlopen(gs_url)) as u:
88     with open(out_path, 'wb') as f:
89       while True:
90         buf = u.read(4096)
91         if not buf:
92           break
93         f.write(buf)
94
95   if ext == '.bz2':
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
98     # profile.
99     # bzip2 removes the compressed file on success.
100     CheckCallOrExit(['bzip2', '-d', out_path])
101   elif ext == '.xz':
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])
105
106
107 def main():
108   parser = argparse.ArgumentParser(
109       description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
110   parser.add_argument(
111       '--newest_state',
112       required=True,
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 '
115       'we should pull.')
116   parser.add_argument(
117       '--local_state',
118       required=True,
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.')
122   parser.add_argument(
123       '--gs_url_base',
124       required=True,
125       help='The base GS URL to search for the profile.')
126   parser.add_argument(
127       '--output_name',
128       required=True,
129       help='Output name of the downloaded and uncompressed profile.')
130   parser.add_argument(
131       '-f',
132       '--force',
133       action='store_true',
134       help='Fetch a profile even if the local one is current.')
135   args = parser.parse_args()
136
137   up_to_date_profile = ReadUpToDateProfileName(args.newest_state)
138   if not args.force:
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):
145       return 0
146
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)
151
152
153 if __name__ == '__main__':
154   sys.exit(main())