Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cbuildbot / prebuilts.py
1 # Copyright (c) 2014 The Chromium OS 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 """cbuildbot logic for uploading prebuilts and managing binhosts."""
6
7 from __future__ import print_function
8
9 from datetime import datetime
10 import glob
11 import os
12
13 from chromite.cbuildbot import cbuildbot_config
14 from chromite.cbuildbot import commands
15 from chromite.cbuildbot import constants
16 from chromite.lib import cros_build_lib
17 from chromite.lib import portage_util
18
19 _PREFLIGHT_BINHOST = 'PREFLIGHT_BINHOST'
20 _CHROME_BINHOST = 'CHROME_BINHOST'
21 _FULL_BINHOST = 'FULL_BINHOST'
22 _BINHOST_PACKAGE_FILE = ('/usr/share/dev-install/portage/make.profile/'
23                          'package.installable')
24 _PRIVATE_BINHOST_CONF_DIR = ('src/private-overlays/chromeos-partner-overlay/'
25                              'chromeos/binhost')
26 _PUBLIC_BINHOST_CONF_DIR = 'src/third_party/chromiumos-overlay/chromeos/binhost'
27
28
29 def _AddPackagesForPrebuilt(filename):
30   """Add list of packages for upload.
31
32   Process a file that lists all the packages that can be uploaded to the
33   package prebuilt bucket and generates the command line args for
34   upload_prebuilts.
35
36   Args:
37     filename: file with the package full name (category/name-version), one
38               package per line.
39
40   Returns:
41     A list of parameters for upload_prebuilts. For example:
42     ['--packages=net-misc/dhcp', '--packages=app-admin/eselect-python']
43   """
44   try:
45     cmd = []
46     with open(filename) as f:
47       # Get only the package name and category as that is what upload_prebuilts
48       # matches on.
49       for line in f:
50         atom = line.split('#', 1)[0].strip()
51         try:
52           cpv = portage_util.SplitCPV(atom)
53         except ValueError:
54           cros_build_lib.Warning('Could not split atom %r (line: %r)',
55                                  atom, line)
56           continue
57         if cpv:
58           cmd.extend(['--packages=%s/%s' % (cpv.category, cpv.package)])
59     return cmd
60   except IOError as e:
61     cros_build_lib.Warning('Problem with package file %s' % filename)
62     cros_build_lib.Warning('Skipping uploading of prebuilts.')
63     cros_build_lib.Warning('ERROR(%d): %s' % (e.errno, e.strerror))
64     return None
65
66
67 def _GenerateSdkVersion():
68   """Generate a version string for sdk builds
69
70   This needs to be global for test overrides.  It also needs to be done here
71   rather than in upload_prebuilts because we want to put toolchain tarballs
72   in a specific subdir and that requires keeping the version string in one
73   place.  Otherwise we'd have to have the various scripts re-interpret the
74   string and try and sync dates across.
75   """
76   return datetime.now().strftime('%Y.%m.%d.%H%M%S')
77
78
79 def UploadPrebuilts(category, chrome_rev, private_bucket, buildroot, **kwargs):
80   """Upload Prebuilts for non-dev-installer use cases.
81
82   Args:
83     category: Build type. Can be [binary|full|chrome|chroot|paladin].
84     chrome_rev: Chrome_rev of type constants.VALID_CHROME_REVISIONS.
85     private_bucket: True if we are uploading to a private bucket.
86     buildroot: The root directory where the build occurs.
87     board: Board type that was built on this machine.
88     extra_args: Extra args to pass to prebuilts script.
89   """
90   extra_args = ['--prepend-version', category]
91   extra_args.extend(['--upload', 'gs://chromeos-prebuilt'])
92   if private_bucket:
93     extra_args.extend(['--private', '--binhost-conf-dir',
94                        _PRIVATE_BINHOST_CONF_DIR])
95   else:
96     extra_args.extend(['--binhost-conf-dir', _PUBLIC_BINHOST_CONF_DIR])
97
98   if category == constants.CHROOT_BUILDER_TYPE:
99     extra_args.extend(['--sync-host',
100                        '--upload-board-tarball'])
101     tarball_location = os.path.join(buildroot, 'built-sdk.tar.xz')
102     extra_args.extend(['--prepackaged-tarball', tarball_location])
103
104     # See _GenerateSdkVersion comments for more details.
105     version = _GenerateSdkVersion()
106     extra_args.extend(['--set-version', version])
107
108     # The local tarballs will be simply "<tuple>.tar.xz".  We need
109     # them to be "<tuple>-<version>.tar.xz" to avoid collisions.
110     for tarball in glob.glob(os.path.join(
111         buildroot, constants.DEFAULT_CHROOT_DIR,
112         constants.SDK_TOOLCHAINS_OUTPUT, '*.tar.*')):
113       tarball_components = os.path.basename(tarball).split('.', 1)
114
115       # Only add the path arg when processing the first tarball.  We do
116       # this to get access to the tarball suffix dynamically (so it can
117       # change and this code will still work).
118       if '--toolchain-upload-path' not in extra_args:
119         # Stick the toolchain tarballs into <year>/<month>/ subdirs so
120         # we don't start dumping even more stuff into the top level.
121         subdir = ('/'.join(version.split('.')[0:2]) + '/' +
122                   '%%(target)s-%(version)s.' + tarball_components[1])
123         extra_args.extend(['--toolchain-upload-path', subdir])
124
125       arg = '%s:%s' % (tarball_components[0], tarball)
126       extra_args.extend(['--toolchain-tarball', arg])
127
128   if category == constants.CHROME_PFQ_TYPE:
129     assert chrome_rev
130     key = '%s_%s' % (chrome_rev, _CHROME_BINHOST)
131     extra_args.extend(['--key', key.upper()])
132   elif cbuildbot_config.IsPFQType(category):
133     extra_args.extend(['--key', _PREFLIGHT_BINHOST])
134   else:
135     assert category in (constants.BUILD_FROM_SOURCE_TYPE,
136                         constants.CHROOT_BUILDER_TYPE)
137     extra_args.extend(['--key', _FULL_BINHOST])
138
139   if category == constants.CHROME_PFQ_TYPE:
140     extra_args += ['--packages=%s' % x
141                    for x in [constants.CHROME_PN] +
142                             constants.OTHER_CHROME_PACKAGES]
143
144   kwargs.setdefault('extra_args', []).extend(extra_args)
145   return _UploadPrebuilts(buildroot=buildroot, **kwargs)
146
147
148 class PackageFileMissing(Exception):
149   """Raised when the dev installer package file is missing."""
150   pass
151
152
153 def UploadDevInstallerPrebuilts(binhost_bucket, binhost_key, binhost_base_url,
154                                 buildroot, board, **kwargs):
155   """Upload Prebuilts for dev-installer use case.
156
157   Args:
158     binhost_bucket: bucket for uploading prebuilt packages. If it equals None
159                     then the default bucket is used.
160     binhost_key: key parameter to pass onto upload_prebuilts. If it equals
161                  None, then chrome_rev is used to select a default key.
162     binhost_base_url: base url for upload_prebuilts. If None the parameter
163                       --binhost-base-url is absent.
164     buildroot: The root directory where the build occurs.
165     board: Board type that was built on this machine.
166     extra_args: Extra args to pass to prebuilts script.
167   """
168   extra_args = ['--prepend-version', constants.CANARY_TYPE]
169   extra_args.extend(['--binhost-base-url', binhost_base_url])
170   extra_args.extend(['--upload', binhost_bucket])
171   extra_args.extend(['--key', binhost_key])
172
173   filename = os.path.join(buildroot, 'chroot', 'build', board,
174                           _BINHOST_PACKAGE_FILE.lstrip('/'))
175   cmd_packages = _AddPackagesForPrebuilt(filename)
176   if cmd_packages:
177     extra_args.extend(cmd_packages)
178   else:
179     raise PackageFileMissing()
180
181   kwargs.setdefault('extra_args', []).extend(extra_args)
182   return _UploadPrebuilts(buildroot=buildroot, board=board, **kwargs)
183
184
185 def _UploadPrebuilts(buildroot, board, extra_args):
186   """Upload prebuilts.
187
188   Args:
189     buildroot: The root directory where the build occurs.
190     board: Board type that was built on this machine.
191     extra_args: Extra args to pass to prebuilts script.
192   """
193   cwd = constants.CHROMITE_BIN_DIR
194   cmd = ['./upload_prebuilts',
195          '--build-path', buildroot]
196
197   if board:
198     cmd.extend(['--board', board])
199
200   cmd.extend(extra_args)
201   commands.RunBuildScript(buildroot, cmd, cwd=cwd)