def test_translation_wrappers(self): ''' Test Translation wrappers''' self.assertTrue(isinstance(_('text'), basestring)) self.assertEqual(_('notfoundxxx'), 'notfoundxxx') self.assertTrue(isinstance(P_('text', 'texts', 2), basestring)) self.assertEqual(P_('notfound01', 'notfound02', 1), 'notfound01') self.assertEqual(P_('notfound01', 'notfound02', 2), 'notfound02')
class SampleCommand(dnf.cli.Command): """ the util command there is extending the dnf command line """ # TODO: the tool command, use your own command aliases = ["sample"] # TODO: summary for util, shown in dnf help, add your own summary = _('One line description of the util') # TODO usage string for the util, shown by dnf help <command> usage = _('[PARAMETERS]') def configure(self, args): """ setup the client demands""" demands = self.cli.demands demands.sack_activation = True demands.available_repos = True def run(self, args): """ execute the util action here """ # Setup ArgumentParser to handle util # You must only add options not used by dnf already parser = dnfutils.ArgumentParser(self.aliases[0]) # TODO: example options/arg add your own parser.add_argument('cmd', nargs=1, help='the sub command') parser.add_argument('parms', nargs='*', help='the parameters to the sub command') parser.add_argument("--some-option", action='store_true', help='an optional option') # parse the options/args # list available options/args on errors & exit opts = parser.parse_args(args) # show util help & exit if opts.help_cmd: print(parser.format_help()) return # TODO: the main tool code, add your own print('Sample util is running with :') print(' cmd = : %s' % opts.cmd) print(' parms = : %s' % opts.parms) print(' some-option : %s' % opts.some_option)
def run(self, args): ''' execute the util action here ''' # Setup ArgumentParser to handle util # You must only add options not used by dnf already self.parser = ArgumentParser(self.aliases[0]) self.parser.add_argument("packages", nargs='*', help=_('packages to download')) self.parser.add_argument("--source", action='store_true', help=_('download the src.rpm instead')) self.parser.add_argument("--destdir", help=_('download path, ' 'default is current dir')) self.parser.add_argument("--resolve", action='store_true', help=_('resolve and download ' 'needed dependencies')) # parse the options/args # list available options/args on errors & exit self.opts = self.parser.parse_args(args) # show util help & exit if self.opts.help_cmd: print(self.parser.format_help()) return if self.opts.source: locations = self._download_source(self.opts.packages) else: locations = self._download_rpms(self.opts.packages) if self.opts.destdir: dest = self.opts.destdir else: dest = os.getcwd() move = functools.partial(self._move_package, dest) map(move, locations) return
def _get_packages_with_deps(self, pkg_specs): """ get packages matching pkg_specs and the deps """ pkgs = self._get_packages(pkg_specs) goal = hawkey.Goal(self.base.sack) for pkg in pkgs: goal.install(pkg) rc = goal.run() if rc: pkgs = goal.list_installs() return pkgs else: logger.debug(_("Error in resolve")) return []
def _enable_source_repos(self): """ enable source repositories for enabled binary repositories binary repositories will be disabled and the dnf sack will be reloaded """ repo_dict = {} # find the source repos for the enabled binary repos for repo in self.base.repos.iter_enabled(): source_repo = "{}-source".format(repo.id) if source_repo in self.base.repos: repo_dict[repo.id] = (repo, self.base.repos[source_repo]) else: repo_dict[repo.id] = (repo, None) # disable the binary & enable the source ones for id_ in repo_dict: repo, src_repo = repo_dict[id_] repo.disable() if src_repo: logger.info(_("enabled {} repository").format(src_repo.id)) src_repo.enable() # reload the sack self.base.fill_sack()
class DnlCommand(dnf.cli.Command): """ the util command there is extending the dnf command line """ aliases = ['dnl'] summary = _('download packages from repositories') usage = _('PACKAGE..') def configure(self, args): # setup sack and populate it with enabled repos demands = self.cli.demands demands.sack_activation = True demands.available_repos = True def run(self, args): ''' execute the util action here ''' # Setup ArgumentParser to handle util # You must only add options not used by dnf already self.parser = ArgumentParser(self.aliases[0]) self.parser.add_argument("packages", nargs='*', help=_('packages to download')) self.parser.add_argument("--source", action='store_true', help=_('download the src.rpm instead')) self.parser.add_argument("--destdir", help=_('download path, ' 'default is current dir')) self.parser.add_argument("--resolve", action='store_true', help=_('resolve and download ' 'needed dependencies')) # parse the options/args # list available options/args on errors & exit self.opts = self.parser.parse_args(args) # show util help & exit if self.opts.help_cmd: print(self.parser.format_help()) return if self.opts.source: locations = self._download_source(self.opts.packages) else: locations = self._download_rpms(self.opts.packages) if self.opts.destdir: dest = self.opts.destdir else: dest = os.getcwd() move = functools.partial(self._move_package, dest) map(move, locations) return def _download_rpms(self, pkg_specs): """ Download packages to dnf cache """ if self.opts.resolve: pkgs = self._get_packages_with_deps(pkg_specs) else: pkgs = self._get_packages(pkg_specs) self.base.download_packages(pkgs, self.base.output.progress) locations = sorted([pkg.localPkg() for pkg in pkgs]) return locations def _download_source(self, pkg_specs): """ Download source packages to dnf cache """ pkgs = self._get_packages(pkg_specs) source_pkgs = self._get_source_packages(pkgs) self._enable_source_repos() pkgs = self._get_packages(source_pkgs, source=True) self.base.download_packages(pkgs, self.base.output.progress) locations = sorted([pkg.localPkg() for pkg in pkgs]) return locations def _get_packages(self, pkg_specs, source=False): """ get packages matching pkg_specs """ if source: queries = map(self._get_query_source, pkg_specs) else: queries = map(self._get_query, pkg_specs) pkgs = list(itertools.chain(*queries)) return pkgs def _get_packages_with_deps(self, pkg_specs): """ get packages matching pkg_specs and the deps """ pkgs = self._get_packages(pkg_specs) goal = hawkey.Goal(self.base.sack) for pkg in pkgs: goal.install(pkg) rc = goal.run() if rc: pkgs = goal.list_installs() return pkgs else: logger.debug(_("Error in resolve")) return [] def _get_source_packages(self, pkgs): """ get list of source rpm names for a list of packages """ source_pkgs = set() for pkg in pkgs: source_pkgs.add(pkg.sourcerpm) logger.debug(" --> Package {0} Source : {1}".format( str(pkg), pkg.sourcerpm)) return list(source_pkgs) def _enable_source_repos(self): """ enable source repositories for enabled binary repositories binary repositories will be disabled and the dnf sack will be reloaded """ repo_dict = {} # find the source repos for the enabled binary repos for repo in self.base.repos.iter_enabled(): source_repo = "{}-source".format(repo.id) if source_repo in self.base.repos: repo_dict[repo.id] = (repo, self.base.repos[source_repo]) else: repo_dict[repo.id] = (repo, None) # disable the binary & enable the source ones for id_ in repo_dict: repo, src_repo = repo_dict[id_] repo.disable() if src_repo: logger.info(_("enabled {} repository").format(src_repo.id)) src_repo.enable() # reload the sack self.base.fill_sack() def _get_query(self, pkg_spec): """ return a query to match a pkg_spec""" subj = dnf.subject.Subject(pkg_spec) q = subj.get_best_query(self.base.sack) q = q.available() q = q.latest() return q def _get_query_source(self, pkg_spec): """" return a query to match a source rpm file name """ pkg_spec = pkg_spec[:-8] # skip the .src.rpm n, v, r = pkg_spec.split('-') q = self.base.sack.query() q = q.available() q = q.latest() q = q.filter(name=n, version=v, release=r, arch='src') return q def _move_package(self, target, location): """ move a package """ shutil.copy(location, target) os.unlink(location) return target
def run(self, args): """Execute the util action here.""" # Setup ArgumentParser to handle util parser = dnfutils.ArgumentParser(self.aliases[0]) parser.add_argument('key', nargs='?', help=_('the key to search for')) parser.add_argument('--all', action='store_true', help=_('query in all packages (Default)')) parser.add_argument('--installed', action='store_true', help=_('query in installed packages')) parser.add_argument('--latest', action='store_true', help=_('show only latest packages')) parser.add_argument('--qf', "--queryformat", dest='queryformat', help=_('format for displaying found packages')) parser.add_argument('--repoid', metavar='REPO', help=_('show only results from this REPO')) parser.add_argument('--arch', metavar='ARCH', help=_('show only results from this ARCH')) parser.add_argument('--whatprovides', metavar='REQ', help=_('show only results there provides REQ')) parser.add_argument('--whatrequires', metavar='REQ', help=_('show only results there requires REQ')) parser.add_argument('--showtags', action='store_true', help=_('show available tags to use with ' '--queryformat')) opts = parser.parse_args(args) if opts.help_cmd: print(parser.format_help()) return if opts.showtags: print(_('Available query-tags: use --queryformat ".. %{tag} .."')) print(QUERY_TAGS) return q = self.base.sack.query() if opts.all: q = q.available() elif opts.installed: q = q.installed() if opts.latest: q = q.latest() if opts.key: if set(opts.key) & set('*[?'): # is pattern ? fdict = {'name__glob': opts.key} else: # substring fdict = {'name__substr': opts.key} q = q.filter(hawkey.ICASE, **fdict) if opts.repoid: q = q.filter(reponame=opts.repoid) if opts.arch: q = q.filter(arch=opts.arch) if opts.whatprovides: q = self.by_provides(self.base.sack, [opts.whatprovides], q) if opts.whatrequires: q = self.by_requires(self.base.sack, opts.whatrequires, q) fmt = self.get_format(opts.queryformat) self.show_packages(q, fmt)
class QueryCommand(dnf.cli.Command): """The util command there is extending the dnf command line """ aliases = ['query'] # summary for util, shown in dnf help summary = _('search for packages matching keyword') # usage string for the util usage = _('[OPTIONS] [KEYWORDS]') def get_format(self, qf): """Convert a rpm like QUERYFMT to an python .format() string """ def fmt_repl(matchobj): fill = matchobj.groups()[0] key = matchobj.groups()[1] if fill: if fill[0] == '-': fill = '>' + fill[1:] else: fill = '<' + fill fill = ':' + fill return '{0.' + key.lower() + fill + "}" if not qf: qf = '%{name}-%{epoch}:%{version}-%{release}.%{arch} : %{reponame}' qf = qf.replace("\\n", "\n") qf = qf.replace("\\t", "\t") fmt = re.sub(QF_MATCH, fmt_repl, qf) return fmt def show_packages(self, query, fmt): """Print packages in a query, in a given format.""" for po in query.run(): try: pkg = PackageWrapper(po) print(fmt.format(pkg)) except AttributeError as e: # catch that the user has specified attributes # there don't exist on the dnf Package object. raise dnf.exceptions.Error(str(e)) def configure(self, args): demands = self.cli.demands demands.sack_activation = True demands.available_repos = True def run(self, args): """Execute the util action here.""" # Setup ArgumentParser to handle util parser = dnfutils.ArgumentParser(self.aliases[0]) parser.add_argument('key', nargs='?', help=_('the key to search for')) parser.add_argument('--all', action='store_true', help=_('query in all packages (Default)')) parser.add_argument('--installed', action='store_true', help=_('query in installed packages')) parser.add_argument('--latest', action='store_true', help=_('show only latest packages')) parser.add_argument('--qf', "--queryformat", dest='queryformat', help=_('format for displaying found packages')) parser.add_argument('--repoid', metavar='REPO', help=_('show only results from this REPO')) parser.add_argument('--arch', metavar='ARCH', help=_('show only results from this ARCH')) parser.add_argument('--whatprovides', metavar='REQ', help=_('show only results there provides REQ')) parser.add_argument('--whatrequires', metavar='REQ', help=_('show only results there requires REQ')) parser.add_argument('--showtags', action='store_true', help=_('show available tags to use with ' '--queryformat')) opts = parser.parse_args(args) if opts.help_cmd: print(parser.format_help()) return if opts.showtags: print(_('Available query-tags: use --queryformat ".. %{tag} .."')) print(QUERY_TAGS) return q = self.base.sack.query() if opts.all: q = q.available() elif opts.installed: q = q.installed() if opts.latest: q = q.latest() if opts.key: if set(opts.key) & set('*[?'): # is pattern ? fdict = {'name__glob': opts.key} else: # substring fdict = {'name__substr': opts.key} q = q.filter(hawkey.ICASE, **fdict) if opts.repoid: q = q.filter(reponame=opts.repoid) if opts.arch: q = q.filter(arch=opts.arch) if opts.whatprovides: q = self.by_provides(self.base.sack, [opts.whatprovides], q) if opts.whatrequires: q = self.by_requires(self.base.sack, opts.whatrequires, q) fmt = self.get_format(opts.queryformat) self.show_packages(q, fmt) def by_provides(self, sack, pattern, query): """Get a query for matching given provides.""" try: reldeps = list(map(functools.partial(hawkey.Reldep, sack), pattern)) except hawkey.ValueException: return query.filter(empty=True) return query.filter(provides=reldeps) def by_requires(self, sack, pattern, query): """Get a query for matching given requirements.""" try: reldep = hawkey.Reldep(sack, pattern) except hawkey.ValueException: return query.filter(empty=True) return query.filter(requires=reldep)