Example #1
0
def cp(args):
    # The aws s3 cli uses "-" to represent stdin/stdout
    with (nullcontext(sys.stdin.buffer) if args.src == '-' else open(
            args.src,
            'rb',
    )) as from_file, (nullcontext(sys.stdout.buffer)
                      if args.dest == '-' else open(
                          args.dest,
                          'wb',
                      )) as to_file:
        to_file.write(from_file.read())
Example #2
0
    def _download_repodata(
            self, repodata: 'Repodata', *,
            is_primary: bool) -> Tuple[bool, str, Optional[List[Rpm]]]:
        '''
          - Returns True only if we just downloaded & stored this Repodata.
          - Returns our new storage_id, or the previous one from the DB.
          - For the selected primary repodata, returns a list of RPMs.
            Returns None for all others.
        '''
        # We only need to download the repodata if is not already in the DB,
        # or if it is primary (so we can parse it for RPMs).
        with self._repo_db_ctx as repo_db:
            storage_id = repo_db.get_storage_id(self._repodata_table, repodata)
        if is_primary:
            rpms = []
        elif storage_id:
            return False, storage_id, None  # Nothing stored, not primary
        else:
            rpms = None

        with (
                # We will parse the selected primary file to discover the RPMs.
                get_rpm_parser(repodata) if is_primary else nullcontext()
        ) as rpm_parser, (
                # Read the primary from storage if we already have an ID --
                # downloading is more likely to fail due to repo updates.
                self._storage.reader(storage_id) if storage_id else
                self._download(repodata.location)) as input, (
                    # Only write to storage if it's not already there.
                    self._storage.writer()
                    if not storage_id else nullcontext()) as output:
            log.info(f'Fetching {repodata}')
            for chunk in _verify_chunk_stream(
                    _read_chunks(input),
                [repodata.checksum],
                    repodata.size,
                    repodata.location,
            ):  # May raise a ReportableError
                if output:
                    output.write(chunk)
                if rpm_parser:
                    try:
                        rpms.extend(rpm_parser.feed(chunk))
                    except Exception as ex:
                        raise RepodataParseError((repodata.location, ex))
            # Must commit from inside the output context to get a storage_id.
            if output:
                return True, output.commit(), rpms
        # The repodata was already stored, and we parsed it for RPMs.
        assert storage_id is not None
        return False, storage_id, rpms
Example #3
0
 def _main(self, argv):
     updb.main(
         argv,
         nullcontext(lambda _pkg, _tag, opts: opts if opts else {'x': 'z'}),
         how_to_generate='how',
         overview_doc='overview doc',
         options_doc='opts doc',
     )
Example #4
0
def _nspawn_setup(opts: _NspawnOpts, popen_args: PopenArgs) -> _NspawnSetup:
    with (_snapshot_subvol(opts.layer, opts.debug_only_opts.snapshot_into)
          if opts.snapshot else nullcontext(opts.layer)) as nspawn_subvol:
        nspawn_args, cmd_env = _extra_nspawn_args_and_env(opts)
        yield _NspawnSetup(
            subvol=nspawn_subvol,
            nspawn_cmd=(*_nspawn_cmd(nspawn_subvol), *nspawn_args),
            # This is a safeguard in case the `sudo` policy lets through any
            # unwanted environment variables.
            nspawn_env=nspawn_sanitize_env(),
            opts=opts,
            cmd_env=tuple(cmd_env),
            popen_args=popen_args,
        )
Example #5
0
def _set_up_run_cli(argv: Iterable[str]) -> _CliSetup:
    args = _parse_cli_args(argv, allow_debug_only_opts=True)
    init_logging(debug=args.opts.debug_only_opts.debug)
    with (open(args.append_boot_console, 'ab')
          # By default, we send `systemd` console to `stderr`.
          if args.boot and args.append_boot_console else
          nullcontext()) as boot_console:
        yield _CliSetup(
            boot=args.boot,
            boot_console=boot_console,
            opts=args.opts,
            popen_wrappers=[
                functools.partial(
                    inject_yum_dnf_versionlock,
                    args.snapshot_to_versionlock,
                ),
                functools.partial(
                    inject_repo_servers,
                    args.serve_rpm_snapshots,
                ),
            ] if args.serve_rpm_snapshots else [],
        )