def _ParseCommandLine(argv): parser = commandline.OptionParser() parser.add_option('-g', '--gs-path', type='gs_path', help=('GS path that contains the chrome to deploy.')) return parser.parse_args(argv)
def _GetParser(): """Returns the parser to use for this module.""" parser = commandline.OptionParser(usage=_USAGE) parser.add_option('-d', '--draft', default=False, action='store_true', help='Upload a draft to Gerrit rather than a change.') parser.add_option('-n', '--dry-run', default=False, action='store_true', dest='dryrun', help='Apply changes locally but do not upload them.') parser.add_option('-e', '--email', help='If specified, use this email instead of ' 'the email you would upload changes as. Must be set if ' 'nomirror is set.') parser.add_option('--nomirror', default=True, dest='mirror', action='store_false', help='Disable mirroring -- requires ' 'email to be set.') parser.add_option('--nowipe', default=True, dest='wipe', action='store_false', help='Do not wipe the work directory after finishing.') return parser
def _GetParser(): """Returns the parser to use for this module.""" parser = commandline.OptionParser(usage=_USAGE) parser.add_option('-d', '--draft', default=False, action='store_true', help='upload a draft to Gerrit rather than a change') parser.add_option('-n', '--dry-run', default=False, action='store_true', dest='dryrun', help='apply changes locally but do not upload them') parser.add_option('-e', '--email', help='if specified, use this email instead of ' 'the email you would upload changes as; must be set w/' '--nomirror') parser.add_option('--nomirror', default=True, dest='mirror', action='store_false', help='checkout git repo directly; ' 'requires --email') parser.add_option('--nowipe', default=True, dest='wipe', action='store_false', help='do not wipe the work directory after finishing') return parser
def main(argv): usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args] This script is used for manipulating local chroot environments; creating, deleting, downloading, etc. If given --enter (or no args), it defaults to an interactive bash shell within the chroot. If given args those are passed to the chroot environment, and executed.""" conf = cros_build_lib.LoadKeyValueFile(os.path.join( constants.SOURCE_ROOT, constants.SDK_VERSION_FILE), ignore_missing=True) sdk_latest_version = conf.get('COREOS_SDK_VERSION', '<unknown>') parser = commandline.OptionParser(usage=usage, caching=True) commands = parser.add_option_group("Commands") commands.add_option('--enter', action='store_true', default=False, help='Enter the SDK chroot. Implies --create.') commands.add_option( '--create', action='store_true', default=False, help='Create the chroot only if it does not already exist. ' 'Implies --download.') commands.add_option( '--bootstrap', action='store_true', default=False, help='Build everything from scratch, including the sdk. ' 'Use this only if you need to validate a change ' 'that affects SDK creation itself (toolchain and ' 'build are typically the only folk who need this). ' 'Note this will quite heavily slow down the build. ' 'This option implies --create --nousepkg.') commands.add_option( '-r', '--replace', action='store_true', default=False, help='Replace an existing SDK chroot. Basically an alias ' 'for --delete --create.') commands.add_option('--delete', action='store_true', default=False, help='Delete the current SDK chroot if it exists.') commands.add_option('--download', action='store_true', default=False, help='Download the sdk.') # Global options: default_chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) parser.add_option('--chroot', dest='chroot', default=default_chroot, type='path', help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR)) parser.add_option('--chrome_root', default=None, type='path', help='Mount this chrome root into the SDK chroot') parser.add_option('--chrome_root_mount', default=None, type='path', help='Mount chrome into this path inside SDK chroot') parser.add_option( '--nousepkg', action='store_true', default=False, help='Do not use binary packages when creating a chroot.') parser.add_option('--nogetbinpkg', action='store_true', default=False, help='Do not fetch remote binary packages.') parser.add_option('-u', '--url', dest='sdk_url', default=None, help=('''Use sdk tarball located at this url. Use file:// for local files.''')) parser.add_option('--sdk-version', default=sdk_latest_version, help='Use this sdk version. Current is %default.') options, chroot_command = parser.parse_args(argv) # Some sanity checks first, before we ask for sudo credentials. cros_build_lib.AssertOutsideChroot() host = os.uname()[4] if host != 'x86_64': parser.error( "cros_sdk is currently only supported on x86_64; you're running" " %s. Please find a x86_64 machine." % (host, )) missing = osutils.FindMissingBinaries(NEEDED_TOOLS) if missing: parser.error( ('The tool(s) %s were not found.\n' 'Please install the appropriate package in your host.\n' 'Example(ubuntu):\n' ' sudo apt-get install <packagename>' % (', '.join(missing)))) _ReExecuteIfNeeded([sys.argv[0]] + argv) # Expand out the aliases... if options.replace: options.delete = options.create = True if options.bootstrap: options.create = True # If a command is not given, default to enter. options.enter |= not any( getattr(options, x.dest) for x in commands.option_list) options.enter |= bool(chroot_command) if options.enter and options.delete and not options.create: parser.error("Trying to enter the chroot when --delete " "was specified makes no sense.") # Finally, discern if we need to create the chroot. chroot_exists = os.path.exists(options.chroot) if options.create or options.enter: # Only create if it's being wiped, or if it doesn't exist. if not options.delete and chroot_exists: options.create = False else: options.download = True # Finally, flip create if necessary. if options.enter: options.create |= not chroot_exists # Based on selections, fetch the tarball. if options.sdk_url: urls = [options.sdk_url] else: urls = GetArchStageTarballs(options.sdk_version) lock_path = os.path.dirname(options.chroot) lock_path = os.path.join(lock_path, '.%s_lock' % os.path.basename(options.chroot)) with cgroups.SimpleContainChildren('cros_sdk'): with locking.FileLock(lock_path, 'chroot lock') as lock: if options.delete and os.path.exists(options.chroot): lock.write_lock() DeleteChroot(options.chroot) sdk_cache = os.path.join(options.cache_dir, 'sdks') distfiles_cache = os.path.join(options.cache_dir, 'distfiles') osutils.SafeMakedirs(options.cache_dir) for target in (sdk_cache, distfiles_cache): src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target)) if not os.path.exists(src): osutils.SafeMakedirs(target) continue lock.write_lock( "Upgrade to %r needed but chroot is locked; please exit " "all instances so this upgrade can finish." % src) if not os.path.exists(src): # Note that while waiting for the write lock, src may've vanished; # it's a rare race during the upgrade process that's a byproduct # of us avoiding taking a write lock to do the src check. If we # took a write lock for that check, it would effectively limit # all cros_sdk for a chroot to a single instance. osutils.SafeMakedirs(target) elif not os.path.exists(target): # Upgrade occurred, but a reversion, or something whacky # occurred writing to the old location. Wipe and continue. os.rename(src, target) else: # Upgrade occurred once already, but either a reversion or # some before/after separate cros_sdk usage is at play. # Wipe and continue. osutils.RmDir(src) if options.download: lock.write_lock() sdk_tarball = FetchRemoteTarballs(sdk_cache, urls) if options.create: lock.write_lock() CreateChroot(options.chroot, sdk_tarball, options.cache_dir, nousepkg=(options.bootstrap or options.nousepkg), nogetbinpkg=options.nogetbinpkg) if options.enter: lock.read_lock() EnterChroot(options.chroot, options.cache_dir, options.chrome_root, options.chrome_root_mount, chroot_command)
def _CreateParser(): """Create our custom parser.""" parser = commandline.OptionParser(usage=_USAGE, option_class=CustomOption, caching=True) # TODO(rcui): Have this use the UI-V2 format of having source and target # device be specified as positional arguments. parser.add_option('--force', action='store_true', default=False, help='Skip all prompts (i.e., for disabling of rootfs ' 'verification). This may result in the target ' 'machine being rebooted.') sdk_board_env = os.environ.get(cros_chrome_sdk.SDKFetcher.SDK_BOARD_ENV) parser.add_option( '--board', default=sdk_board_env, help="The board the Chrome build is targeted for. When in " "a 'cros chrome-sdk' shell, defaults to the SDK " "board.") parser.add_option( '--build-dir', type='path', help='The directory with Chrome build artifacts to deploy ' 'from. Typically of format <chrome_root>/out/Debug. ' 'When this option is used, the GYP_DEFINES ' 'environment variable must be set.') parser.add_option('--target-dir', type='path', help='Target directory on device to deploy Chrome into.', default=_CHROME_DIR) parser.add_option('-g', '--gs-path', type='gs_path', help='GS path that contains the chrome to deploy.') parser.add_option('--nostartui', action='store_false', dest='startui', default=True, help="Don't restart the ui daemon after deployment.") parser.add_option('--nostrip', action='store_false', dest='dostrip', default=True, help="Don't strip binaries during deployment. Warning: " "the resulting binaries will be very large!") parser.add_option('-p', '--port', type=int, default=remote.DEFAULT_SSH_PORT, help='Port of the target device to connect to.') parser.add_option('-t', '--to', help='The IP address of the CrOS device to deploy to.') parser.add_option('-v', '--verbose', action='store_true', default=False, help='Show more debug output.') group = optparse.OptionGroup(parser, 'Advanced Options') group.add_option('-l', '--local-pkg-path', type='path', help='Path to local chrome prebuilt package to deploy.') group.add_option('--sloppy', action='store_true', default=False, help='Ignore when mandatory artifacts are missing.') group.add_option( '--staging-flags', default=None, type='gyp_defines', help='Extra flags to control staging. Valid flags are - %s' % ', '.join(chrome_util.STAGING_FLAGS)) group.add_option( '--strict', action='store_true', default=False, help='Stage artifacts based on the GYP_DEFINES environment ' 'variable and --staging-flags, if set. Enforce that ' 'all optional artifacts are deployed.') group.add_option('--strip-flags', default=None, help="Flags to call the 'strip' binutil tool with. " "Overrides the default arguments.") parser.add_option_group(group) # GYP_DEFINES that Chrome was built with. Influences which files are staged # when --build-dir is set. Defaults to reading from the GYP_DEFINES # enviroment variable. parser.add_option('--gyp-defines', default=None, type='gyp_defines', help=optparse.SUPPRESS_HELP) # Path of an empty directory to stage chrome artifacts to. Defaults to a # temporary directory that is removed when the script finishes. If the path # is specified, then it will not be removed. parser.add_option('--staging-dir', type='path', default=None, help=optparse.SUPPRESS_HELP) # Only prepare the staging directory, and skip deploying to the device. parser.add_option('--staging-only', action='store_true', default=False, help=optparse.SUPPRESS_HELP) # Path to a binutil 'strip' tool to strip binaries with. The passed-in path # is used as-is, and not normalized. Used by the Chrome ebuild to skip # fetching the SDK toolchain. parser.add_option('--strip-bin', default=None, help=optparse.SUPPRESS_HELP) return parser
def main(argv): # Locate actions that are exposed to the user. All functions that start # with "UserAct" are fair game. act_pfx = 'UserAct' actions = [x for x in globals() if x.startswith(act_pfx)] usage = """%prog [options] <action> [action args] There is no support for doing line-by-line code review via the command line. This helps you manage various bits and CL status. Example: $ gerrit todo # List all the CLs that await your review. $ gerrit mine # List all of your open CLs. $ gerrit inspect 28123 # Inspect CL 28123 on the public gerrit. $ gerrit inspect *28123 # Inspect CL 28123 on the internal gerrit. $ gerrit verify 28123 1 # Mark CL 28123 as verified (+1). Actions:""" indent = max([len(x) - len(act_pfx) for x in actions]) for a in sorted(actions): usage += '\n %-*s: %s' % (indent, a[len(act_pfx):].lower(), globals()[a].__doc__) parser = commandline.OptionParser(usage=usage) parser.add_option('-i', '--internal', default=None, action='store_true', help='Query gerrit-int') parser.add_option('-e', '--external', dest='internal', action='store_false', help='Query gerrit (default)') parser.add_option('--sort', default='number', help='Key to sort on ' '(number, project)') parser.add_option('-v', '--verbose', default=False, action='store_true', help='Be more verbose in output') opts, args = parser.parse_args(argv) if not args: parser.error('missing action') # pylint: disable=W0603 global COLOR COLOR = terminal.Color(enabled=opts.color) # TODO: This sucks. We assume that all actions which take an argument are # a CL #. Or at least, there's no other reason for it to start with a *. # We do this to automatically select internal vs external gerrit as this # convention is encoded in much of our system. However, the rest of this # script doesn't expect (or want) the leading *. if len(args) > 1: if args[1][0] == '*': if opts.internal is None: opts.internal = True args[1] = args[1][1:] opts.gerrit = gerrit.GetGerritHelper( constants.INTERNAL_REMOTE if opts.internal else constants.EXTERNAL_REMOTE, print_cmd=opts.debug) # Now look up the requested user action and run it. cmd = args[0].lower() args = args[1:] functor = globals().get(act_pfx + cmd.capitalize()) if functor: argspec = inspect.getargspec(functor) if argspec.varargs: if len(args) < len(argspec.args): parser.error('incorrect number of args: %s expects at least %s' % (cmd, len(argspec.args))) elif len(argspec.args) - 1 != len(args): parser.error('incorrect number of args: %s expects %s' % (cmd, len(argspec.args) - 1)) try: functor(opts, *args) except (cros_build_lib.RunCommandError, gerrit.GerritException) as e: cros_build_lib.Die(e.message) else: parser.error('unknown action: %s' % (cmd,))
def main(argv=None): if argv is None: argv = sys.argv[1:] usage = 'usage: %prog' parser = commandline.OptionParser(usage=usage) parser.add_option('-r', '--testroot', action='store', type='path', help=('Directory where test checkout is stored.')) parser.add_option('-f', '--force', default=True, action='store_false', dest='dryrun', help='If enabled, allow committing.') parser.add_option( '--internal-manifest-url', default='%s/%s' % (constants.GERRIT_INT_SSH_URL, _INTERNAL_MANIFEST_PROJECT), help='Url to fetch the internal manifest from.') parser.add_option('--external-manifest-url', default='%s/%s' % (constants.GERRIT_SSH_URL, _EXTERNAL_MANIFEST_PROJECT), help='Url to fetch the external manifest from.') parser.add_option('-v', '--verbose', default=False, action='store_true', help='Run with debug output.') parser.add_option('--reference', default=None, help="Repository to reference") (options, _inputs) = parser.parse_args(argv) if not options.testroot: parser.error('Please specify a test root!') # Set cros_build_lib debug level to hide RunCommand spew. logging.getLogger().setLevel( logging.DEBUG if options.verbose else logging.WARN) if options.reference is None: options.reference = git.FindRepoCheckoutRoot(os.getcwd()) def process_target(internal, manifest_url, reference): subdir = 'internal' if internal else 'external' root = os.path.join(options.testroot, subdir) repo_dir = os.path.join(root, 'repo') osutils.SafeMakedirs(repo_dir) manifest_dir = os.path.join(root, 'manifest') if os.path.exists(manifest_dir): git.CleanAndCheckoutUpstream(manifest_dir) git.RunGit(manifest_dir, ['checkout', '-B', 'master', '-t', 'origin/master']) else: repository.CloneGitRepo(manifest_dir, manifest_url) m = Manifest(repo_dir, manifest_dir, internal, reference=reference, dryrun=options.dryrun) m.PerformUpdate() if options.dryrun: print "%s manifest is now:" % subdir print osutils.ReadFile(os.path.join(manifest_dir, MANIFEST_FILE)) # Process internal first so that any references to the passed in repo # are utilized in full, w/ external then slaving from the new internal. process_target(True, options.internal_manifest_url, options.reference) process_target(False, options.external_manifest_url, os.path.join(options.testroot, 'internal', 'repo'))
def main(argv): usage = """usage: %prog [options] The script installs and updates the toolchains in your chroot.""" parser = commandline.OptionParser(usage) parser.add_option('-u', '--nousepkg', action='store_false', dest='usepkg', default=True, help='Use prebuilt packages if possible') parser.add_option('-d', '--deleteold', action='store_true', dest='deleteold', default=False, help='Unmerge deprecated packages') parser.add_option('-t', '--targets', dest='targets', default='sdk', help='Comma separated list of tuples. ' 'Special keyword \'host\' is allowed. Default: sdk') parser.add_option('--include-boards', dest='include_boards', default='', help='Comma separated list of boards whose toolchains we' ' will always include. Default: none') parser.add_option('--hostonly', dest='hostonly', default=False, action='store_true', help='Only setup the host toolchain. ' 'Useful for bootstrapping chroot') parser.add_option('--show-board-cfg', dest='board_cfg', default=None, help='Board to list toolchain tuples for') parser.add_option('--create-packages', action='store_true', default=False, help='Build redistributable packages') parser.add_option('--output-dir', default=os.getcwd(), type='path', help='Output directory') parser.add_option('--reconfig', default=False, action='store_true', help='Reload crossdev config and reselect toolchains') (options, remaining_arguments) = parser.parse_args(argv) if len(remaining_arguments): parser.error('script does not take arguments: %s' % remaining_arguments) # Figure out what we're supposed to do and reject conflicting options. if options.board_cfg and options.create_packages: parser.error('conflicting options: create-packages & show-board-cfg') targets = set(options.targets.split(',')) boards = set(options.include_boards.split(',')) if options.include_boards \ else set() if options.board_cfg: ShowBoardConfig(options.board_cfg) elif options.create_packages: cros_build_lib.AssertInsideChroot() Crossdev.Load(False) CreatePackages(targets, options.output_dir) else: cros_build_lib.AssertInsideChroot() # This has to be always run as root. if os.geteuid() != 0: cros_build_lib.Die('this script must be run as root') Crossdev.Load(options.reconfig) UpdateToolchains(options.usepkg, options.deleteold, options.hostonly, options.reconfig, targets, boards) Crossdev.Save() return 0
def ParseCmdline(argv): """Parse all command line options. Args: argv: Arguments to parse. Returns: Tuple containing: options: Command line options from optpase args: List of command line arguments """ parser = commandline.OptionParser(__doc__) parser.add_option('-a', '--cbfargs', action='append', help='Pass extra arguments to cros_bundle_firmware') parser.add_option('-b', '--board', type='string', default=default_board, help='Select board to build (daisy/peach_pit/link)') parser.add_option('-B', '--build', action='store_false', default=True, help="Don't build U-Boot, just configure device tree") parser.add_option('-C', '--console', action='store_false', default=True, help='Permit console output') parser.add_option('-d', '--dt', default='seaboard', help='Select name of device tree file to use') parser.add_option( '-D', '--nodefaults', dest='use_defaults', action='store_false', default=True, help="Don't select default filenames for those not given") parser.add_option('-F', '--flash', action='store_true', default=False, help='Create magic flasher for SPI flash') parser.add_option('-M', '--mmc', action='store_true', default=False, help='Create magic flasher for eMMC') parser.add_option('-i', '--incremental', action='store_true', default=False, help="Don't reconfigure and clean") parser.add_option('-k', '--kernel', action='store_true', default=False, help='Send kernel to board also') parser.add_option('-O', '--objdump', action='store_true', default=False, help='Write disassembly output') parser.add_option('-r', '--run', action='store_false', default=True, help='Run the boot command') parser.add_option('--ro', action='store_true', default=False, help='Create Chrome OS read-only image') parser.add_option('--rw', action='store_true', default=False, help='Create Chrome OS read-write image') parser.add_option('-s', '--separate', action='store_false', default=True, help='Link device tree into U-Boot, instead of separate') parser.add_option('-S', '--secure', action='store_true', default=False, help='Use vboot_twostop secure boot') parser.add_option('--small', action='store_true', default=False, help='Create Chrome OS small image') parser.add_option('-t', '--trace', action='store_true', default=False, help='Enable trace support') parser.add_option('-v', '--verbose', type='int', default=0, help='Make cros_bundle_firmware verbose') parser.add_option('-V', '--verified', action='store_true', default=False, help='Include Chrome OS verified boot components') parser.add_option('-w', '--write', action='store_false', default=True, help="Don't write image to board using usb/em100") parser.add_option('-x', '--sdcard', action='store_true', default=False, help='Write to SD card instead of USB/em100') parser.add_option('-z', '--size', action='store_true', default=False, help='Display U-Boot image size') return parser.parse_args(argv)