Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / generate_delta_sysroot.py
1 # Copyright (c) 2013 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 """Script to generate a sysroot tarball.
6
7 Script that generates a tarball containing changes that are needed to create a
8 complete sysroot from extracted prebuilt packages.
9 """
10
11 from __future__ import print_function
12
13 import os
14
15 from chromite.cbuildbot import constants
16 from chromite.lib import commandline
17 from chromite.lib import cros_build_lib
18 from chromite.lib import osutils
19 from chromite.lib import portage_util
20
21 _CREATE_BATCH_CMD = ('rsync',)
22 _CREATE_BATCH_EXCLUDE = ('--exclude=/tmp/', '--exclude=/var/cache/',
23                          '--exclude=/usr/local/autotest/packages**',
24                          '--exclude=/packages/', '--exclude=**.pyc',
25                          '--exclude=**.pyo')
26 # rsync is used in archive mode with no --times.
27 # --checksum is used to ensure 100% accuracy.
28 # --delete is used to account for files that may be deleted during emerge.
29 # Short version: rsync -rplgoDc --delete
30 _CREATE_BATCH_ARGS = ('--recursive', '--links', '--perms', '--group',
31                      '--owner', '--devices', '--specials', '--checksum',
32                      '--delete')
33
34 # We want to ensure that we use only binary packages. However,
35 # build_packages will try to rebuild any unbuilt packages. Ignore those through
36 # --norebuild.
37 _BUILD_PKGS_CMD = (os.path.join(constants.CROSUTILS_DIR, 'build_packages'),
38                    '--skip_chroot_upgrade', '--norebuild', '--usepkgonly')
39
40
41 def CreateBatchFile(build_dir, out_dir, batch_file):
42   """Creates a batch file using rsync between build_dir and out_dir.
43
44   This batch file can be applied to any directory identical to out_dir, to make
45   it identical to build_dir.
46
47   Args:
48     build_dir: Directory to rsync from.
49     out_dir: Directory to rsync to.
50     batch_file: Batch file to be created.
51   """
52   cmd = list(_CREATE_BATCH_CMD)
53   cmd.extend(list(_CREATE_BATCH_EXCLUDE))
54   cmd.extend(list(_CREATE_BATCH_ARGS))
55   cmd.extend(['--only-write-batch=' + batch_file, build_dir + '/', out_dir])
56   cros_build_lib.SudoRunCommand(cmd)
57
58
59 def _ParseCommandLine(argv):
60   """Parse args, and run environment-independent checks."""
61   parser = commandline.ArgumentParser(description=__doc__)
62   parser.add_argument('--board', required=True,
63                       help='The board to generate the sysroot for.')
64   parser.add_argument('--out-dir', type=osutils.ExpandPath, required=True,
65                       help='Directory to place the generated tarball.')
66   parser.add_argument('--out-batch', default=constants.DELTA_SYSROOT_BATCH,
67                       help='The name to give to the batch file. Defaults to %r.'
68                             % constants.DELTA_SYSROOT_BATCH)
69   parser.add_argument('--out-file', default=constants.DELTA_SYSROOT_TAR,
70                       help='The name to give to the tarball. Defaults to %r.'
71                             % constants.DELTA_SYSROOT_TAR)
72   parser.add_argument('--skip-tests', action='store_false', default=True,
73                       dest='build_tests',
74                       help='If we should not build the autotests packages.')
75   options = parser.parse_args(argv)
76
77   return options
78
79
80 def FinishParsing(options):
81   """Run environment dependent checks on parsed args."""
82   target = os.path.join(options.out_dir, options.out_file)
83   if os.path.exists(target):
84     cros_build_lib.Die('Output file %r already exists.' % target)
85
86   if not os.path.isdir(options.out_dir):
87     cros_build_lib.Die(
88         'Non-existent directory %r specified for --out-dir' % options.out_dir)
89
90
91 def GenerateSysroot(sysroot, board, build_tests, unpack_only=False):
92   """Create a sysroot using only binary packages from local binhost.
93
94   Args:
95     sysroot: Where we want to place the sysroot.
96     board: Board we want to build for.
97     build_tests: If we should include autotest packages.
98     unpack_only: If we only want to unpack the binary packages, and not build
99                  them.
100   """
101   osutils.SafeMakedirs(sysroot)
102   if not unpack_only:
103     cros_build_lib.RunCommand(
104         [os.path.join(constants.CROSUTILS_DIR, 'install_toolchain'),
105          '--noconfigure', '--board_root', sysroot, '--board', board])
106   cmd = list(_BUILD_PKGS_CMD)
107   cmd.extend(['--board_root', sysroot, '--board', board])
108   if unpack_only:
109     cmd.append('--unpackonly')
110   if not build_tests:
111     cmd.append('--nowithautotest')
112   env = {'USE': os.environ.get('USE', ''),
113          'PORTAGE_BINHOST': 'file://localhost%s' %
114          portage_util.GetBinaryPackageDir(board)}
115   cros_build_lib.RunCommand(cmd, extra_env=env)
116
117
118 def main(argv):
119   """Generate a tarball containing a sysroot that can be patched over
120   extracted prebuilt package contents to create a complete sysroot.
121
122   1. Unpack all packages for a board into an unpack_only sysroot directory.
123   2. Emerge all packages for a board into a build sysroot directory.
124   3. Create a batch file using:
125     rsync -rplgoDc --delete --write-batch=<batch> <build_sys> <unpackonly_sys>
126   4. Put the batch file inside a tarball.
127   """
128   options = _ParseCommandLine(argv)
129   FinishParsing(options)
130
131   cros_build_lib.AssertInsideChroot()
132
133   with osutils.TempDir(set_global=False, sudo_rm=True) as tmp_dir:
134     build_sysroot = os.path.join(tmp_dir, 'build-sys')
135     unpackonly_sysroot = os.path.join(tmp_dir, 'tmp-sys')
136     batch_filename = options.out_batch
137
138     GenerateSysroot(unpackonly_sysroot, options.board, options.build_tests,
139                     unpack_only=True)
140     GenerateSysroot(build_sysroot, options.board, options.build_tests,
141                     unpack_only=False)
142
143     # Finally create batch file.
144     CreateBatchFile(build_sysroot, unpackonly_sysroot,
145                     os.path.join(tmp_dir, batch_filename))
146
147     cros_build_lib.CreateTarball(
148         os.path.join(options.out_dir, options.out_file), tmp_dir, sudo=True,
149         inputs=[batch_filename])