From: Michael Tremer Date: Tue, 6 Jul 2021 15:37:38 +0000 (+0000) Subject: client: Move CLI to an own file X-Git-Tag: 0.9.28~1094 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19233dc0516c570ec96bad84a43d7b6d47c0980e;p=pakfire.git client: Move CLI to an own file Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 567cd0e15..6493cbfc3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /contrib/pakfire.nm /src/pakfire/__version__.py /src/scripts/pakfire-builder +/src/scripts/pakfire-client /src/scripts/pakfire-daemon /src/systemd/*.service /tests/.root diff --git a/Makefile.am b/Makefile.am index ac2c02b61..8cc0f01c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -106,20 +106,22 @@ dist_bin_SCRIPTS = \ bin_SCRIPTS = \ src/scripts/pakfire-builder \ + src/scripts/pakfire-client \ src/scripts/pakfire-daemon EXTRA_DIST += \ src/scripts/pakfire-builder.in \ + src/scripts/pakfire-client.in \ src/scripts/pakfire-daemon.in CLEANFILES += \ src/scripts/pakfire-builder \ + src/scripts/pakfire-client \ src/scripts/pakfire-daemon install-exec-local: $(MKDIR_P) $(DESTDIR)/$(bindir) cd $(DESTDIR)/$(bindir) && \ - $(LN_S) -vf pakfire pakfire-client && \ $(LN_S) -vf pakfire pakfire-key # ------------------------------------------------------------------------------ diff --git a/src/pakfire/cli.py b/src/pakfire/cli.py index 96605f3c3..98a9c224a 100644 --- a/src/pakfire/cli.py +++ b/src/pakfire/cli.py @@ -26,8 +26,6 @@ import os import shutil import signal import sys -import tempfile -import time from . import _pakfire from . import client @@ -323,129 +321,6 @@ class Cli(object): self.pakfire(ns).check() -class CliClient(Cli): - def __init__(self): - # Create connection to pakfire hub - self.client = client.Client() - - def parse_cli(self): - parser = argparse.ArgumentParser( - description = _("Pakfire client command line interface"), - ) - subparsers = parser.add_subparsers(help=_("sub-command help")) - - # Add common arguments - self._add_common_arguments(parser, offline_switch=False) - - # build - build = subparsers.add_parser("build", help=_("Build a package remote")) - build.add_argument("packages", nargs="+", help=_("Package(s) to build")) - build.set_defaults(func=self.handle_build) - - build.add_argument("-a", "--arch", - help=_("Build the package(s) for the given architecture only")) - - # check-connection - check_connection = subparsers.add_parser("check-connection", - help=_("Check the connection to the hub")) - check_connection.set_defaults(func=self.handle_check_connection) - - # upload - upload = subparsers.add_parser("upload", help=_("Upload a file to the build service")) - upload.add_argument("file", nargs=1, help=_("Filename")) - upload.set_defaults(func=self.handle_upload) - - # watch-build - watch_build = subparsers.add_parser("watch-build", help=_("Watch the status of a build")) - watch_build.add_argument("id", nargs=1, help=_("Build ID")) - watch_build.set_defaults(func=self.handle_watch_build) - - # watch-job - watch_job = subparsers.add_parser("watch-job", help=_("Watch the status of a job")) - watch_job.add_argument("id", nargs=1, help=_("Job ID")) - watch_job.set_defaults(func=self.handle_watch_job) - - return parser.parse_args() - - def handle_build(self, ns): - # Create a temporary directory. - temp_dir = tempfile.mkdtemp() - - # Format arches. - if ns.arch: - arches = self.args.arch.split(",") - else: - arches = None - - try: - # Package all packages first and save the actual filenames - packages = [] - - for package in ns.packages: - if package.endswith(".%s" % MAKEFILE_EXTENSION): - # Create a source package from the makefile. - p = self.pakfire() - package = p.dist(package, temp_dir) - packages.append(package) - - elif package.endswith(".%s" % PACKAGE_EXTENSION): - packages.append(package) - - else: - raise Exception("Unknown filetype: %s" % package) - - assert packages - - # Upload the packages to the build service - for package in packages: - build = self.client.create_build(package, type="scratch", arches=arches) - - # Show information about the uploaded build - summary = build.dump() - for line in summary.splitlines(): - print(" %s" % line) - - finally: - # Cleanup the temporary directory and all files. - if os.path.exists(temp_dir): - shutil.rmtree(temp_dir, ignore_errors=True) - - def handle_check_connection(self, ns): - success = self.client.check_connection() - - if success: - print("%s: %s" % (_("Connection OK"), success)) - - def handle_upload(self, ns): - for path in ns.file: - self.client.upload_file(path) - - def handle_watch_build(self, ns): - build = self.client.get_build(ns.id[0]) - - return self._watch_something(build) - - def handle_watch_job(self, ns): - job = self.client.get_job(ns.id[0]) - - return self._watch_something(job) - - def _watch_something(self, o): - while True: - s = o.dump() - print(s) - - # Break the loop if the build/job is not active any more - # (since we don't expect any changes) - if not o.is_active(): - break - - time.sleep(60) - - # Update data before the next loop is shown - o.refresh() - - class CliKey(Cli): def parse_cli(self): parser = argparse.ArgumentParser( diff --git a/src/scripts/pakfire-client.in b/src/scripts/pakfire-client.in new file mode 100644 index 000000000..13f1fef57 --- /dev/null +++ b/src/scripts/pakfire-client.in @@ -0,0 +1,182 @@ +#!/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 . # +# # +############################################################################### + +import argparse +import os.path +import sys +import tempfile +import time + +import pakfire.client +from pakfire.i18n import _ + +class Cli(object): + def parse_cli(self): + parser = argparse.ArgumentParser( + description = _("Pakfire Client command line interface"), + ) + subparsers = parser.add_subparsers() + + # build + build = subparsers.add_parser("build", + help=_("Build a package remote")) + build.add_argument("packages", nargs="+", + help=_("Package(s) to build")) + build.add_argument("-a", "--arch", + help=_("Build the package(s) for the given architecture only")) + build.set_defaults(func=self._build) + + # check-connection + check_connection = subparsers.add_parser("check-connection", + help=_("Check the connection to the hub")) + check_connection.set_defaults(func=self._check_connection) + + # upload + upload = subparsers.add_parser("upload", + help=_("Upload a file to the build service")) + upload.add_argument("file", + help=_("Filename")) + upload.set_defaults(func=self._upload) + + # watch-build + watch_build = subparsers.add_parser("watch-build", + help=_("Watch the status of a build")) + watch_build.add_argument("id", + help=_("Build ID")) + watch_build.set_defaults(func=self._watch_build) + + # watch-job + watch_job = subparsers.add_parser("watch-job", + help=_("Watch the status of a job")) + watch_job.add_argument("id", + help=_("Job ID")) + watch_job.set_defaults(func=self._watch_job) + + 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 __call__(self): + # Parse command line arguments + args = self.parse_cli() + + # Create the Client + client = pakfire.client.Client() + + # Call function + try: + ret = args.func(client, 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 _build(self, client, ns): + # Create a temporary directory. + temp_dir = tempfile.mkdtemp() + + # Format arches. + if ns.arch: + arches = self.args.arch.split(",") + else: + arches = None + + try: + # Package all packages first and save the actual filenames + packages = [] + + for package in ns.packages: + if package.endswith(".%s" % MAKEFILE_EXTENSION): + # Create a source package from the makefile. + p = self.pakfire() + package = p.dist(package, temp_dir) + packages.append(package) + + elif package.endswith(".%s" % PACKAGE_EXTENSION): + packages.append(package) + + else: + raise Exception("Unknown filetype: %s" % package) + + assert packages + + # Upload the packages to the build service + for package in packages: + build = client.create_build(package, type="scratch", arches=arches) + + # Show information about the uploaded build + summary = build.dump() + for line in summary.splitlines(): + print(" %s" % line) + + finally: + # Cleanup the temporary directory and all files. + if os.path.exists(temp_dir): + shutil.rmtree(temp_dir, ignore_errors=True) + + def _check_connection(self, client, ns): + success = client.check_connection() + + if success: + print("%s: %s" % (_("Connection OK"), success)) + + def _upload(self, client, ns): + for path in ns.file: + client.upload_file(path) + + def _watch_build(self, client, ns): + build = client.get_build(ns.id[0]) + + return self._watch_something(build) + + def _watch_job(self, client, ns): + job = client.get_job(ns.id[0]) + + return self._watch_something(job) + + def _watch_something(self, o): + while True: + s = o.dump() + print(s) + + # Break the loop if the build/job is not active any more + # (since we don't expect any changes) + if not o.is_active(): + break + + time.sleep(60) + + # Update data before the next loop is shown + o.refresh() + + +if __name__ == "__main__": + c = Cli() + c()