Ram Chandrasekar | 28b578f | 2021-11-15 18:33:18 +0000 | [diff] [blame^] | 1 | # Copyright 2021 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 | """Strip packages and place them in <sysroot>/stripped-packages.""" |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | from typing import List |
| 10 | |
| 11 | from chromite.lib import build_target_lib |
| 12 | from chromite.lib import commandline |
| 13 | from chromite.lib import constants |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import osutils |
| 16 | |
| 17 | # The builder module lives in the devserver path. |
| 18 | sys.path.append('/usr/lib/devserver/') |
| 19 | import builder |
| 20 | |
| 21 | _DEFAULT_MASK = 'DEFAULT_INSTALL_MASK' |
| 22 | |
| 23 | def create_parser() -> commandline.ArgumentParser: |
| 24 | """Creates the cmdline argparser, populates the options and description.""" |
| 25 | parser = commandline.ArgumentParser(description=__doc__) |
| 26 | |
| 27 | group = parser.add_mutually_exclusive_group(required=True) |
| 28 | group.add_argument('--board', |
| 29 | default=cros_build_lib.GetDefaultBoard(), |
| 30 | help='The board that processed packages belong to.') |
| 31 | group.add_argument('--sysroot', |
| 32 | type='path', |
| 33 | help='Sysroot that processed packages belong to. ' |
| 34 | 'This is incompatible with --board.') |
| 35 | |
| 36 | parser.add_argument('--deep', |
| 37 | action='store_true', |
| 38 | default=False, |
| 39 | help='Also strip dependencies of packages.') |
| 40 | parser.add_argument('packages', |
| 41 | nargs='+', |
| 42 | metavar='package', |
| 43 | help='Packages to strip.') |
| 44 | return parser |
| 45 | |
| 46 | |
| 47 | def populate_install_mask() -> bool: |
| 48 | """Extract the default install mask and populate the local environment.""" |
| 49 | env_var_value = osutils.SourceEnvironment( |
| 50 | os.path.join(constants.CROSUTILS_DIR, 'common.sh'), |
| 51 | [_DEFAULT_MASK], |
| 52 | multiline=True) |
| 53 | |
| 54 | if _DEFAULT_MASK not in env_var_value: |
| 55 | return False |
| 56 | os.environ[_DEFAULT_MASK] = env_var_value[_DEFAULT_MASK] |
| 57 | return True |
| 58 | |
| 59 | |
| 60 | def main(argv: List[str]) -> int: |
| 61 | """Main function.""" |
| 62 | cros_build_lib.AssertInsideChroot() |
| 63 | parser = create_parser() |
| 64 | options = parser.parse_args(argv) |
| 65 | options.Freeze() |
| 66 | |
| 67 | if options.sysroot is not None: |
| 68 | sysroot = options.sysroot |
| 69 | else: |
| 70 | sysroot = build_target_lib.get_default_sysroot_path(options.board) |
| 71 | |
| 72 | if not populate_install_mask(): |
| 73 | return False |
| 74 | if not builder.UpdateGmergeBinhost(sysroot, options.packages, options.deep): |
| 75 | return 1 |
| 76 | return 0 |