]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Use new Pakfire context mechanism
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 3 Dec 2016 16:14:34 +0000 (17:14 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 3 Dec 2016 16:14:34 +0000 (17:14 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/cli.py

index c1e083dcc05453b727d7a813a382ad9df6e35df4..0e0d3a16fb66955039d02de5885010f7f3c35f7f 100644 (file)
@@ -221,16 +221,16 @@ class Cli(object):
                return args.func(args)
 
        def handle_info(self, ns):
-               p = self.create_pakfire(ns)
+               with self.pakfire(ns) as p:
+                       for pkg in p.info(ns.package):
+                               s = pkg.dump(long=ns.verbose)
+                               print(s)
 
-               for pkg in p.info(ns.package):
-                       print(pkg.dump(long=ns.verbose))
-
-       def handle_search(self):
-               p = self.create_pakfire()
-
-               for pkg in p.search(self.args.pattern):
-                       print(pkg.dump(short=True))
+       def handle_search(self, ns):
+               with self.pakfire(ns) as p:
+                       for pkg in p.search(ns.pattern):
+                               s = pkg.dump(short=True)
+                               print(s)
 
        def handle_update(self, **args):
                p = self.create_pakfire()
@@ -245,118 +245,112 @@ class Cli(object):
 
                p.update(packages, **args)
 
-       def handle_distro_sync(self):
-               self.handle_update(sync=True)
-
-       def handle_check_update(self):
-               self.handle_update(check=True)
-
-       def handle_downgrade(self, **args):
-               p = self.create_pakfire()
-               p.downgrade(
-                       self.args.package,
-                       allow_vendorchange=self.args.allow_vendorchange,
-                       allow_archchange=not self.args.disallow_archchange,
-                       **args
-               )
-
-       def handle_install(self):
-               p = self.create_pakfire()
-               p.install(self.args.package, ignore_recommended=self.args.without_recommends)
-
-       def handle_reinstall(self):
-               p = self.create_pakfire()
-               p.reinstall(self.args.package)
-
-       def handle_remove(self):
-               p = self.create_pakfire()
-               p.remove(self.args.package)
-
-       def handle_provides(self, long=False):
-               p = self.create_pakfire()
-
-               for pkg in p.provides(self.args.pattern):
-                       print(pkg.dump(int=int))
-
-       def handle_grouplist(self):
-               p = self.create_pakfire()
-
-               for pkg in p.grouplist(self.args.group[0]):
-                       print(" * %s" % pkg)
-
-       def handle_groupinstall(self):
-               p = self.create_pakfire()
-               p.groupinstall(self.args.group[0])
-
-       def handle_repolist(self):
-               p = self.create_pakfire()
-
-               # Get a list of all repositories.
-               repos = p.repo_list()
-
-               FORMAT = " %-20s %8s %12s %12s "
-               title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
-               print(title)
-               print("=" * len(title)) # spacing line
-
-               for repo in repos:
-                       print(FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)))
-
-       def handle_clean_all(self):
+       def handle_distro_sync(self, ns):
+               self.handle_update(ns, sync=True)
+
+       def handle_check_update(self, ns):
+               self.handle_update(ns, check=True)
+
+       def handle_downgrade(self, ns, **args):
+               with self.pakfire(ns) as p:
+                       p.downgrade(
+                               self.args.package,
+                               allow_vendorchange=self.args.allow_vendorchange,
+                               allow_archchange=not self.args.disallow_archchange,
+                               **args
+                       )
+
+       def handle_install(self, ns):
+               with self.pakfire(ns) as p:
+                       p.install(ns.package, ignore_recommended=ns.without_recommends)
+
+       def handle_reinstall(self, ns):
+               with self.pakfire(ns) as p:
+                       p.reinstall(ns.package)
+
+       def handle_remove(self, ns):
+               with self.pakfire(ns) as p:
+                       p.remove(ns.package)
+
+       def handle_provides(self, ns, long=False):
+               with self.pakfire(ns) as p:
+                       for pkg in p.provides(ns.pattern):
+                               s = pkg.dump(long=long)
+                               print(s)
+
+       def handle_grouplist(self, ns):
+               with self.pakfire(ns) as p:
+                       for pkg in p.grouplist(ns.group[0]):
+                               print(" * %s" % pkg)
+
+       def handle_groupinstall(self, ns):
+               with self.pakfire(ns) as p:
+                       p.groupinstall(ns.group[0])
+
+       def handle_repolist(self, ns):
+               with self.pakfire(ns) as p:
+                       # Get a list of all repositories.
+                       repos = p.repo_list()
+
+                       FORMAT = " %-20s %8s %12s %12s "
+                       title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
+                       print(title)
+                       print("=" * len(title)) # spacing line
+
+                       for repo in repos:
+                               print(FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)))
+
+       def handle_clean(self, ns):
                print(_("Cleaning up everything..."))
 
-               p = self.create_pakfire()
-               p.clean_all()
+               with self.pakfire(ns) as p:
+                       p.clean_all()
 
-       def handle_check(self):
-               p = self.create_pakfire()
-               p.check()
+       def handle_check(self, ns):
+               with self.pakfire(ns) as p:
+                       p.check()
 
-       def handle_resolvdep(self):
-               p = self.create_pakfire()
+       def handle_resolvdep(self, ns):
+               with self.pakfire(ns) as p:
+                       solver = p.resolvdep(ns.package[0])
+                       assert solver.status
 
-               (pkg,) = self.args.package
+                       t = transaction.Transaction.from_solver(p, solver)
+                       t.dump()
 
-               solver = p.resolvdep(pkg)
-               assert solver.status
-
-               t = transaction.Transaction.from_solver(p, solver)
-               t.dump()
-
-       def handle_extract(self):
-               p = self.create_pakfire()
-
-               # Open all packages.
-               pkgs = []
-               for pkg in self.args.package:
-                       pkg = packages.open(self, None, pkg)
-                       pkgs.append(pkg)
+       def handle_extract(self, ns):
+               with self.pakfire(ns) as p:
+                       # Open all packages.
+                       pkgs = []
+                       for pkg in ns.package:
+                               pkg = packages.open(self, None, pkg)
+                               pkgs.append(pkg)
 
-               target_prefix = self.args.target
+                       target_prefix = ns.target
 
-               # Search for binary packages.
-               binary_packages = any([p.type == "binary" for p in pkgs])
-               source_packages = any([p.type == "source" for p in pkgs])
+                       # Search for binary packages.
+                       binary_packages = any([p.type == "binary" for p in pkgs])
+                       source_packages = any([p.type == "source" for p in pkgs])
 
-               if binary_packages and source_packages:
-                       raise Error(_("Cannot extract mixed package types"))
+                       if binary_packages and source_packages:
+                               raise Error(_("Cannot extract mixed package types"))
 
-               if binary_packages and not target_prefix:
-                       raise Error(_("You must provide an install directory with --target=..."))
+                       if binary_packages and not target_prefix:
+                               raise Error(_("You must provide an install directory with --target=..."))
 
-               elif source_packages and not target_prefix:
-                       target_prefix = "/usr/src/packages/"
+                       elif source_packages and not target_prefix:
+                               target_prefix = "/usr/src/packages/"
 
-               if target_prefix == "/":
-                       raise Error(_("Cannot extract to /."))
+                       if target_prefix == "/":
+                               raise Error(_("Cannot extract to /."))
 
-               for pkg in pkgs:
-                       if pkg.type == "binary":
-                               target_dir = target_prefix
-                       elif pkg.type == "source":
-                               target_dir = os.path.join(target_prefix, pkg.friendly_name)
+                       for pkg in pkgs:
+                               if pkg.type == "binary":
+                                       target_dir = target_prefix
+                               elif pkg.type == "source":
+                                       target_dir = os.path.join(target_prefix, pkg.friendly_name)
 
-                       pkg.extract(message=_("Extracting"), prefix=target_dir)
+                               pkg.extract(message=_("Extracting"), prefix=target_dir)
 
 
 class CliBuilder(Cli):
@@ -472,25 +466,6 @@ class CliBuilder(Cli):
 
                return parser.parse_args()
 
-       @property
-       def pakfire_args(self):
-               ret = {
-                       "arch" : self.args.arch,
-               }
-
-               if hasattr(self.args, "offline") and self.args.offline:
-                       ret["downloader"] = {
-                               "offline" : self.args.offline,
-                       }
-
-               if hasattr(self.args, "distro"):
-                       ret["distro_name"] = self.args.distro
-
-               return ret
-
-       def handle_info(self, ns):
-               Cli.handle_info(self, ns)
-
        def handle_build(self):
                # Get the package descriptor from the command line options
                pkg = self.args.package[0]