]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire-builder: Move CLI into an extra script
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 14 Jun 2021 15:48:36 +0000 (15:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 14 Jun 2021 15:48:36 +0000 (15:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
src/pakfire/cli.py
src/scripts/pakfire-builder.in [new file with mode: 0644]

index 94e969911a04d2411c23154870366602daccac29..7bf8a17f6eb16557e74c7d245dbb5545b602b426 100644 (file)
@@ -8,6 +8,7 @@
 /missing
 /contrib/pakfire.nm
 /src/pakfire/__version__.py
+/src/scripts/pakfire-builder
 /src/scripts/pakfire-daemon
 /src/systemd/*.service
 /tests/.root
index a586f985a4dcc95ce0293b91e42e4efb383bfa52..c801d54ed129963ac5cbb6e7236566e6d849cdec 100644 (file)
@@ -106,18 +106,20 @@ dist_bin_SCRIPTS = \
        src/scripts/pakfire
        
 bin_SCRIPTS = \
+       src/scripts/pakfire-builder \
        src/scripts/pakfire-daemon
 
 EXTRA_DIST += \
+       src/scripts/pakfire-builder.in \
        src/scripts/pakfire-daemon.in
 
 CLEANFILES += \
+       src/scripts/pakfire-builder \
        src/scripts/pakfire-daemon
 
 install-exec-local:
        $(MKDIR_P) $(DESTDIR)/$(bindir)
        cd $(DESTDIR)/$(bindir) && \
-               $(LN_S) -vf pakfire pakfire-builder && \
                $(LN_S) -vf pakfire pakfire-client && \
                $(LN_S) -vf pakfire pakfire-key
 
index 8cfd3fecd63864e9242e602ace0ebd3d82220653..ba19b2fda9dfb7eb9b6083651bf2f144215cbae2 100644 (file)
@@ -378,164 +378,6 @@ class Cli(object):
                        p.extract(ns.package, target=ns.target)
 
 
-class CliBuilder(Cli):
-       default_path = None
-
-       def parse_cli(self):
-               parser = argparse.ArgumentParser(
-                       description = _("Pakfire builder command line interface"),
-               )
-               subparsers = parser.add_subparsers()
-
-               # Add common arguments
-               self._add_common_arguments(parser)
-
-               # Add additional arguments
-               parser.add_argument("--arch", "-a", nargs="?",
-                       help=_("Run pakfire for the given architecture"))
-               parser.add_argument("--distro", nargs="?", default="ipfire3", # XXX for now
-                       help=_("Choose the distribution configuration to use for build"))
-               parser.add_argument("--disable-snapshot", action="store_true",
-                       help=_("Disable using snapshots"))
-
-               # build
-               build = subparsers.add_parser("build", help=_("Build one or more packages"))
-               build.add_argument("package", nargs=1,
-                       help=_("Give name of at least one package to build"))
-               build.set_defaults(func=self.handle_build)
-
-               build.add_argument("--resultdir", nargs="?",
-                       help=_("Path were the output files should be copied to"))
-               build.add_argument("-m", "--mode", nargs="?", default="development",
-                       help=_("Mode to run in. Is either 'release' or 'development' (default)"))
-               build.add_argument("--after-shell", action="store_true",
-                       help=_("Run a shell after a successful build"))
-               build.add_argument("--skip-install-test", action="store_true",
-                       help=_("Do not perform the install test"))
-               build.add_argument("--private-network", action="store_true",
-                       help=_("Disable network in container"))
-
-               # clean
-               clean = subparsers.add_parser("clean", help=_("Cleanup all temporary files"))
-               clean.set_defaults(func=self.handle_clean)
-
-               # dist
-               dist = subparsers.add_parser("dist", help=_("Generate a source package"))
-               dist.add_argument("package", nargs="+", help=_("Give name(s) of a package(s)"))
-               dist.set_defaults(func=self.handle_dist)
-
-               dist.add_argument("--resultdir", nargs="?",
-                       help=_("Path were the output files should be copied to"))
-
-               # extract
-               extract = subparsers.add_parser("extract", help=_("Extract a package to a directory"))
-               extract.add_argument("package", nargs="+",
-                       help=_("Give name of the file to extract"))
-               extract.add_argument("--target", nargs="?",
-                       help=_("Target directory where to extract to"))
-               extract.set_defaults(func=self.handle_extract)
-
-               # info
-               info = subparsers.add_parser("info",
-                       help=_("Print some information about the given package(s)"))
-               info.add_argument("--filelist", action="store_true",
-                       help=_("Show filelist"))
-               info.add_argument("package", nargs="+",
-                       help=_("Give at least the name of one package."))
-               info.set_defaults(func=self.handle_info)
-
-               # provides
-               provides = subparsers.add_parser("provides",
-                       help=_("Get a list of packages that provide a given file or feature"))
-               provides.add_argument("pattern", nargs="+",
-                       help=_("File or feature to search for"))
-               provides.set_defaults(func=self.handle_provides)
-
-               # repolist
-               repolist = subparsers.add_parser("repolist",
-                       help=_("List all currently enabled repositories"))
-               repolist.set_defaults(func=self.handle_repolist)
-
-               # search
-               search = subparsers.add_parser("search", help=_("Search for a given pattern"))
-               search.add_argument("pattern", help=_("A pattern to search for"))
-               search.set_defaults(func=self.handle_search)
-
-               # shell
-               shell = subparsers.add_parser("shell", help=_("Go into a build shell"))
-               shell.add_argument("package", nargs="*", help=_("Give name of a package"))
-               shell.add_argument("--install", nargs="*",
-                       help=_("Packages that should be installed in the shell"))
-               shell.set_defaults(func=self.handle_shell)
-
-               shell.add_argument("-m", "--mode", nargs="?", default="development",
-                       help=_("Mode to run in. Is either 'release' or 'development' (default)."))
-               shell.add_argument("--private-network", action="store_true",
-                       help=_("Disable network in container"))
-
-               return parser.parse_args()
-
-       def builder(self, ns):
-               # Find distro configuration file
-               conf = os.path.join(CONFIG_DISTRO_DIR, "%s.conf" % ns.distro)
-
-               return builder.Builder(
-                       conf=conf,
-                       arch=ns.arch,
-                       enable_snapshot=not ns.disable_snapshot
-               )
-
-       def handle_build(self, ns):
-               package, = ns.package
-
-               # Initialise a builder instance and build this package
-               with self.builder(ns) as b:
-                       b.build(package)
-
-       def handle_info(self, ns):
-               with self.builder(ns) as b:
-                       with b.pakfire as p:
-                               for pkg in p.info(ns.package):
-                                       s = pkg.dump(long=True, filelist=ns.filelist)
-                                       print(s)
-
-       def handle_repolist(self, ns):
-               with self.builder(ns) as b:
-                       with b.pakfire as p:
-                               FORMAT = " %-20s %8s %12s %12s "
-                               title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
-                               print(title)
-                               print("=" * len(title)) # spacing line
-
-                               for repo in p.repos:
-                                       print(FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)))
-
-       def handle_shell(self, ns):
-               with self.builder(ns) as b:
-                       b.shell(packages=ns.package, install=ns.install)
-
-       def handle_dist(self, ns):
-               # Get the packages from the command line options
-               pkgs = []
-
-               for pkg in ns.package:
-                       # Check, if we got a regular file
-                       if os.path.exists(pkg):
-                               pkg = os.path.abspath(pkg)
-                               pkgs.append(pkg)
-
-                       else:
-                               raise FileNotFoundError(pkg)
-
-               # Put packages to where the user said or our
-               # current working directory.
-               resultdir = ns.resultdir or os.getcwd()
-
-               p = self.pakfire(ns)
-               for pkg in pkgs:
-                       p.dist(pkg, resultdir)
-
-
 class CliClient(Cli):
        def __init__(self):
                # Create connection to pakfire hub
diff --git a/src/scripts/pakfire-builder.in b/src/scripts/pakfire-builder.in
new file mode 100644 (file)
index 0000000..6814edb
--- /dev/null
@@ -0,0 +1,226 @@
+#!/usr/bin/python3
+##############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import argparse
+import os.path
+import sys
+
+import pakfire
+from pakfire.constants import CONFIG_DISTRO_DIR
+from pakfire.i18n import _
+
+class Cli(object):
+       def parse_cli(self):
+               parser = argparse.ArgumentParser(
+                       description = _("Pakfire builder command line interface"),
+               )
+               subparsers = parser.add_subparsers()
+
+               # Add arguments
+               parser.add_argument("--arch", "-a", nargs="?",
+                       help=_("Run pakfire for the given architecture"))
+               parser.add_argument("--distro", nargs="?", default="ipfire3", # XXX for now
+                       help=_("Choose the distribution configuration to use for build"))
+               parser.add_argument("--disable-snapshot", action="store_true",
+                       help=_("Disable using snapshots"))
+
+               # build
+               build = subparsers.add_parser("build", help=_("Build one or more packages"))
+               build.add_argument("package", nargs=1,
+                       help=_("Give name of at least one package to build"))
+               build.set_defaults(func=self._build)
+
+               build.add_argument("--resultdir", nargs="?",
+                       help=_("Path were the output files should be copied to"))
+               build.add_argument("-m", "--mode", nargs="?", default="development",
+                       help=_("Mode to run in. Is either 'release' or 'development' (default)"))
+               build.add_argument("--after-shell", action="store_true",
+                       help=_("Run a shell after a successful build"))
+               build.add_argument("--skip-install-test", action="store_true",
+                       help=_("Do not perform the install test"))
+               build.add_argument("--private-network", action="store_true",
+                       help=_("Disable network in container"))
+
+               # clean
+               clean = subparsers.add_parser("clean", help=_("Cleanup all temporary files"))
+               clean.set_defaults(func=self._clean)
+
+               # dist
+               dist = subparsers.add_parser("dist", help=_("Generate a source package"))
+               dist.add_argument("package", nargs="+", help=_("Give name(s) of a package(s)"))
+               dist.set_defaults(func=self._dist)
+
+               dist.add_argument("--resultdir", nargs="?",
+                       help=_("Path were the output files should be copied to"))
+
+               # info
+               info = subparsers.add_parser("info",
+                       help=_("Print some information about the given package(s)"))
+               info.add_argument("--filelist", action="store_true",
+                       help=_("Show filelist"))
+               info.add_argument("package", nargs="+",
+                       help=_("Give at least the name of one package."))
+               info.set_defaults(func=self._info)
+
+               # provides
+               provides = subparsers.add_parser("provides",
+                       help=_("Get a list of packages that provide a given file or feature"))
+               provides.add_argument("pattern", nargs="+",
+                       help=_("File or feature to search for"))
+               #provides.set_defaults(func=self._provides)
+
+               # repolist
+               repolist = subparsers.add_parser("repolist",
+                       help=_("List all currently enabled repositories"))
+               repolist.set_defaults(func=self._repolist)
+
+               # search
+               search = subparsers.add_parser("search", help=_("Search for a given pattern"))
+               search.add_argument("pattern", help=_("A pattern to search for"))
+               #search.set_defaults(func=self._search)
+
+               # shell
+               shell = subparsers.add_parser("shell", help=_("Go into a build shell"))
+               shell.add_argument("package", nargs="*", help=_("Give name of a package"))
+               shell.add_argument("--install", nargs="*",
+                       help=_("Packages that should be installed in the shell"))
+               shell.set_defaults(func=self._shell)
+
+               shell.add_argument("-m", "--mode", nargs="?", default="development",
+                       help=_("Mode to run in. Is either 'release' or 'development' (default)."))
+               shell.add_argument("--private-network", action="store_true",
+                       help=_("Disable network in container"))
+
+               args = parser.parse_args()
+
+               # Print usage if no action was given
+               if not "func" in args:
+                       parser.print_usage()
+                       sys.exit(2)
+
+               return args
+
+       def pakfire(self, ns, build=True):
+               # Find distro configuration file
+               conf = os.path.join(CONFIG_DISTRO_DIR, "%s.conf" % ns.distro)
+
+               return pakfire.Pakfire(
+                       conf=conf,
+                       arch=ns.arch,
+
+                       # Enable build mode
+                       build=build,
+                       enable_snapshot=not ns.disable_snapshot
+               )
+
+       def __call__(self):
+               # Parse command line arguments
+               args = self.parse_cli()
+
+               # Call function
+               try:
+                       ret = args.func(args)
+
+               # Catch invalid inputs
+               except ValueError as e:
+                       sys.stderr.write("%s\n" % e)
+                       ret = 2
+
+               # Return with exit code
+               sys.exit(ret or 0)
+
+       def _clean(self, ns):
+               """
+                       Cleans up
+               """
+               self.pakfire(ns).clean()
+
+       def _build(self, ns):
+               """
+                       Builds a package
+               """
+               package, = ns.package
+
+               # Initialise a builder instance and build this package
+               p = self.pakfire(ns)
+               p.build(package)
+
+       def _dist(self, ns):
+               # Get the packages from the command line options
+               pkgs = []
+
+               for pkg in ns.package:
+                       # Check, if we got a regular file
+                       if os.path.exists(pkg):
+                               pkg = os.path.abspath(pkg)
+                               pkgs.append(pkg)
+
+                       else:
+                               raise FileNotFoundError(pkg)
+
+               # Put packages to where the user said or our
+               # current working directory.
+               resultdir = ns.resultdir or os.getcwd()
+
+               p = self.pakfire(ns)
+
+               for pkg in pkgs:
+                       p.dist(pkg, resultdir)
+
+       def _info(self, ns):
+               """
+                       Shows information about a certain package
+               """
+               p = self.pakfire(ns)
+
+               for pkg in p.info(ns.package):
+                       s = pkg.dump(long=True, filelist=ns.filelist)
+                       print(s)
+
+       def _repolist(self, ns):
+               """
+                       List all repositories
+               """
+               p = self.pakfire(ns)
+
+               FORMAT = " %-20s %8s %12s %12s "
+               title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
+               print(title)
+               print("=" * len(title)) # spacing line
+
+               for repo in p.repos:
+                       print(FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)))
+
+       def _shell(self, ns):
+               """
+                       Open a shell in a build environment
+               """
+               p = self.pakfire(ns)
+
+               # XXX Handle packages to install
+               # XXX Handle private network and other arguments
+
+               p.shell()
+
+
+if __name__ == "__main__":
+       c = Cli()
+       c()