import shutil
import signal
import sys
-import tempfile
-import time
from . import _pakfire
from . import client
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(
--- /dev/null
+#!/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 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()