]>
git.ipfire.org Git - people/stevee/pakfire.git/blob - python/pakfire/cli.py
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
29 import pakfire
.api
as pakfire
39 from system
import system
40 from constants
import *
43 # Initialize a very simple logging that is removed when a Pakfire instance
45 logger
.setup_logging()
49 self
.parser
= argparse
.ArgumentParser(
50 description
= _("Pakfire command line interface."),
53 self
.parse_common_arguments()
55 self
.parser
.add_argument("--root", metavar
="PATH",
57 help=_("The path where pakfire should operate in."))
60 self
.sub_commands
= self
.parser
.add_subparsers()
62 self
.parse_command_install()
63 self
.parse_command_reinstall()
64 self
.parse_command_remove()
65 self
.parse_command_info()
66 self
.parse_command_search()
67 self
.parse_command_check_update()
68 self
.parse_command_update()
69 self
.parse_command_downgrade()
70 self
.parse_command_provides()
71 self
.parse_command_grouplist()
72 self
.parse_command_groupinstall()
73 self
.parse_command_repolist()
74 self
.parse_command_clean()
75 self
.parse_command_check()
76 self
.parse_command_resolvdep()
78 # Finally parse all arguments from the command line and save them.
79 self
.args
= self
.parser
.parse_args()
82 "install" : self
.handle_install
,
83 "reinstall" : self
.handle_reinstall
,
84 "remove" : self
.handle_remove
,
85 "check_update" : self
.handle_check_update
,
86 "update" : self
.handle_update
,
87 "downgrade" : self
.handle_downgrade
,
88 "info" : self
.handle_info
,
89 "search" : self
.handle_search
,
90 "provides" : self
.handle_provides
,
91 "grouplist" : self
.handle_grouplist
,
92 "groupinstall" : self
.handle_groupinstall
,
93 "repolist" : self
.handle_repolist
,
94 "clean_all" : self
.handle_clean_all
,
95 "check" : self
.handle_check
,
96 "resolvdep" : self
.handle_resolvdep
,
100 def pakfire_args(self
):
101 ret
= { "mode" : "normal" }
103 if hasattr(self
.args
, "root"):
104 ret
["path"] = self
.args
.root
106 if hasattr(self
.args
, "disable_repo"):
107 ret
["disable_repos"] = self
.args
.disable_repo
109 if hasattr(self
.args
, "enable_repo"):
110 ret
["enable_repos"] = self
.args
.enable_repo
112 if hasattr(self
.args
, "offline") and self
.args
.offline
:
113 ret
["downloader"] = {
114 "offline" : self
.args
.offline
,
117 if hasattr(self
.args
, "config"):
118 ret
["configs"] = self
.args
.config
120 ret
["configs"] = None
124 def parse_common_arguments(self
, repo_manage_switches
=True, offline_switch
=True):
125 self
.parser
.add_argument("--version", action
="version",
126 version
="%(prog)s " + PAKFIRE_VERSION
)
128 self
.parser
.add_argument("-v", "--verbose", action
="store_true",
129 help=_("Enable verbose output."))
131 self
.parser
.add_argument("-c", "--config", nargs
="?",
132 help=_("Path to a configuration file to load."))
134 if repo_manage_switches
:
135 self
.parser
.add_argument("--disable-repo", nargs
="*", metavar
="REPO",
136 help=_("Disable a repository temporarily."))
138 self
.parser
.add_argument("--enabled-repo", nargs
="*", metavar
="REPO",
139 help=_("Enable a repository temporarily."))
142 self
.parser
.add_argument("--offline", action
="store_true",
143 help=_("Run pakfire in offline mode."))
145 def parse_command_install(self
):
146 # Implement the "install" command.
147 sub_install
= self
.sub_commands
.add_parser("install",
148 help=_("Install one or more packages to the system."))
149 sub_install
.add_argument("package", nargs
="+",
150 help=_("Give name of at least one package to install."))
151 sub_install
.add_argument("--without-recommends", action
="store_true",
152 help=_("Don't install recommended packages."))
153 sub_install
.add_argument("action", action
="store_const", const
="install")
155 def parse_command_reinstall(self
):
156 # Implement the "reinstall" command.
157 sub_install
= self
.sub_commands
.add_parser("reinstall",
158 help=_("Reinstall one or more packages."))
159 sub_install
.add_argument("package", nargs
="+",
160 help=_("Give name of at least one package to reinstall."))
161 sub_install
.add_argument("action", action
="store_const", const
="reinstall")
163 def parse_command_remove(self
):
164 # Implement the "remove" command.
165 sub_remove
= self
.sub_commands
.add_parser("remove",
166 help=_("Remove one or more packages from the system."))
167 sub_remove
.add_argument("package", nargs
="+",
168 help=_("Give name of at least one package to remove."))
169 sub_remove
.add_argument("action", action
="store_const", const
="remove")
172 def _parse_command_update(parser
):
173 parser
.add_argument("package", nargs
="*",
174 help=_("Give a name of a package to update or leave emtpy for all."))
175 parser
.add_argument("--exclude", "-x", nargs
="+",
176 help=_("Exclude package from update."))
177 parser
.add_argument("--allow-vendorchange", action
="store_true",
178 help=_("Allow changing the vendor of packages."))
179 parser
.add_argument("--allow-archchange", action
="store_true",
180 help=_("Allow changing the architecture of packages."))
182 def parse_command_update(self
):
183 # Implement the "update" command.
184 sub_update
= self
.sub_commands
.add_parser("update",
185 help=_("Update the whole system or one specific package."))
186 sub_update
.add_argument("action", action
="store_const", const
="update")
187 self
._parse
_command
_update
(sub_update
)
189 def parse_command_check_update(self
):
190 # Implement the "check-update" command.
191 sub_check_update
= self
.sub_commands
.add_parser("check-update",
192 help=_("Check, if there are any updates available."))
193 sub_check_update
.add_argument("action", action
="store_const", const
="check_update")
194 self
._parse
_command
_update
(sub_check_update
)
196 def parse_command_downgrade(self
):
197 # Implement the "downgrade" command.
198 sub_downgrade
= self
.sub_commands
.add_parser("downgrade",
199 help=_("Downgrade one or more packages."))
200 sub_downgrade
.add_argument("package", nargs
="*",
201 help=_("Give a name of a package to downgrade."))
202 sub_downgrade
.add_argument("--allow-vendorchange", action
="store_true",
203 help=_("Allow changing the vendor of packages."))
204 sub_downgrade
.add_argument("--allow-archchange", action
="store_true",
205 help=_("Allow changing the architecture of packages."))
206 sub_downgrade
.add_argument("action", action
="store_const", const
="downgrade")
208 def parse_command_info(self
):
209 # Implement the "info" command.
210 sub_info
= self
.sub_commands
.add_parser("info",
211 help=_("Print some information about the given package(s)."))
212 sub_info
.add_argument("package", nargs
="+",
213 help=_("Give at least the name of one package."))
214 sub_info
.add_argument("action", action
="store_const", const
="info")
216 def parse_command_search(self
):
217 # Implement the "search" command.
218 sub_search
= self
.sub_commands
.add_parser("search",
219 help=_("Search for a given pattern."))
220 sub_search
.add_argument("pattern",
221 help=_("A pattern to search for."))
222 sub_search
.add_argument("action", action
="store_const", const
="search")
224 def parse_command_provides(self
):
225 # Implement the "provides" command
226 sub_provides
= self
.sub_commands
.add_parser("provides",
227 help=_("Get a list of packages that provide a given file or feature."))
228 sub_provides
.add_argument("pattern", nargs
="+",
229 help=_("File or feature to search for."))
230 sub_provides
.add_argument("action", action
="store_const", const
="provides")
232 def parse_command_grouplist(self
):
233 # Implement the "grouplist" command
234 sub_grouplist
= self
.sub_commands
.add_parser("grouplist",
235 help=_("Get list of packages that belong to the given group."))
236 sub_grouplist
.add_argument("group", nargs
=1,
237 help=_("Group name to search for."))
238 sub_grouplist
.add_argument("action", action
="store_const", const
="grouplist")
240 def parse_command_groupinstall(self
):
241 # Implement the "grouplist" command
242 sub_groupinstall
= self
.sub_commands
.add_parser("groupinstall",
243 help=_("Install all packages that belong to the given group."))
244 sub_groupinstall
.add_argument("group", nargs
=1,
245 help=_("Group name."))
246 sub_groupinstall
.add_argument("action", action
="store_const", const
="groupinstall")
248 def parse_command_repolist(self
):
249 # Implement the "repolist" command
250 sub_repolist
= self
.sub_commands
.add_parser("repolist",
251 help=_("List all currently enabled repositories."))
252 sub_repolist
.add_argument("action", action
="store_const", const
="repolist")
254 def parse_command_clean(self
):
255 sub_clean
= self
.sub_commands
.add_parser("clean", help=_("Cleanup commands."))
257 sub_clean_commands
= sub_clean
.add_subparsers()
259 self
.parse_command_clean_all(sub_clean_commands
)
261 def parse_command_clean_all(self
, sub_commands
):
262 sub_create
= sub_commands
.add_parser("all",
263 help=_("Cleanup all temporary files."))
264 sub_create
.add_argument("action", action
="store_const", const
="clean_all")
266 def parse_command_check(self
):
267 # Implement the "check" command
268 sub_check
= self
.sub_commands
.add_parser("check",
269 help=_("Check the system for any errors."))
270 sub_check
.add_argument("action", action
="store_const", const
="check")
272 def parse_command_resolvdep(self
):
273 # Implement the "resolvdep" command.
274 sub_resolvdep
= self
.sub_commands
.add_parser("resolvdep",
275 help=_("Check the dependencies for a particular package."))
276 sub_resolvdep
.add_argument("package", nargs
=1,
277 help=_("Give name of at least one package to check."))
278 sub_resolvdep
.add_argument("action", action
="store_const", const
="resolvdep")
281 action
= self
.args
.action
284 func
= self
.action2func
[action
]
286 raise Exception, "Unhandled action: %s" % action
290 def handle_info(self
, long=False):
291 pkgs
= pakfire
.info(self
.args
.package
, **self
.pakfire_args
)
294 print pkg
.dump(long=long)
296 def handle_search(self
):
297 pkgs
= pakfire
.search(self
.args
.pattern
, **self
.pakfire_args
)
300 print pkg
.dump(short
=True)
302 def handle_update(self
, **args
):
303 args
.update(self
.pakfire_args
)
305 pakfire
.update(self
.args
.package
, excludes
=self
.args
.exclude
,
306 allow_vendorchange
=self
.args
.allow_vendorchange
,
307 allow_archchange
=self
.args
.allow_archchange
,
310 def handle_check_update(self
):
311 self
.handle_update(check
=True)
313 def handle_downgrade(self
, **args
):
314 args
.update(self
.pakfire_args
)
316 pakfire
.downgrade(self
.args
.package
,
317 allow_vendorchange
=self
.args
.allow_vendorchange
,
318 allow_archchange
=self
.args
.allow_archchange
,
321 def handle_install(self
):
322 pakfire
.install(self
.args
.package
,
323 ignore_recommended
=self
.args
.without_recommends
,
326 def handle_reinstall(self
):
327 pakfire
.reinstall(self
.args
.package
, **self
.pakfire_args
)
329 def handle_remove(self
):
330 pakfire
.remove(self
.args
.package
, **self
.pakfire_args
)
332 def handle_provides(self
):
333 pkgs
= pakfire
.provides(self
.args
.pattern
, **self
.pakfire_args
)
338 def handle_grouplist(self
):
339 pkgs
= pakfire
.grouplist(self
.args
.group
[0], **self
.pakfire_args
)
344 def handle_groupinstall(self
):
345 pakfire
.groupinstall(self
.args
.group
[0], **self
.pakfire_args
)
347 def handle_repolist(self
):
348 repos
= pakfire
.repo_list(**self
.pakfire_args
)
350 FORMAT
= " %-20s %8s %12s %12s "
352 title
= FORMAT
% (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
354 print "=" * len(title
) # spacing line
357 # Skip the installed repository.
358 if repo
.name
== "installed":
361 print FORMAT
% (repo
.name
, repo
.enabled
, repo
.priority
, len(repo
))
363 def handle_clean_all(self
):
364 print _("Cleaning up everything...")
366 pakfire
.clean_all(**self
.pakfire_args
)
368 def handle_check(self
):
369 pakfire
.check(**self
.pakfire_args
)
371 def handle_resolvdep(self
):
372 (pkg
,) = self
.args
.package
374 solver
= pakfire
.resolvdep(pkg
, **self
.pakfire_args
)
377 solver
.transaction
.dump()
380 class CliBuilder(Cli
):
382 # Check if we are already running in a pakfire container. In that
383 # case, we cannot start another pakfire-builder.
384 if os
.environ
.get("container", None) == "pakfire-builder":
385 raise PakfireContainerError
, _("You cannot run pakfire-builder in a pakfire chroot.")
387 self
.parser
= argparse
.ArgumentParser(
388 description
= _("Pakfire builder command line interface."),
391 self
.parse_common_arguments()
394 self
.sub_commands
= self
.parser
.add_subparsers()
396 self
.parse_command_build()
397 self
.parse_command_dist()
398 self
.parse_command_info()
399 self
.parse_command_search()
400 self
.parse_command_shell()
401 self
.parse_command_update()
402 self
.parse_command_provides()
403 self
.parse_command_grouplist()
404 self
.parse_command_repolist()
405 self
.parse_command_clean()
406 self
.parse_command_resolvdep()
408 # Finally parse all arguments from the command line and save them.
409 self
.args
= self
.parser
.parse_args()
412 "build" : self
.handle_build
,
413 "dist" : self
.handle_dist
,
414 "update" : self
.handle_update
,
415 "info" : self
.handle_info
,
416 "search" : self
.handle_search
,
417 "shell" : self
.handle_shell
,
418 "provides" : self
.handle_provides
,
419 "grouplist" : self
.handle_grouplist
,
420 "repolist" : self
.handle_repolist
,
421 "clean_all" : self
.handle_clean_all
,
422 "resolvdep" : self
.handle_resolvdep
,
426 def pakfire_args(self
):
427 ret
= { "mode" : "builder" }
429 if hasattr(self
.args
, "disable_repo"):
430 ret
["disable_repos"] = self
.args
.disable_repo
432 if hasattr(self
.args
, "enable_repo"):
433 ret
["enable_repos"] = self
.args
.enable_repo
435 if hasattr(self
.args
, "offline") and self
.args
.offline
:
436 ret
["downloader"] = {
437 "offline" : self
.args
.offline
,
442 def parse_command_update(self
):
443 # Implement the "update" command.
444 sub_update
= self
.sub_commands
.add_parser("update",
445 help=_("Update the package indexes."))
446 sub_update
.add_argument("action", action
="store_const", const
="update")
448 def parse_command_build(self
):
449 # Implement the "build" command.
450 sub_build
= self
.sub_commands
.add_parser("build",
451 help=_("Build one or more packages."))
452 sub_build
.add_argument("package", nargs
=1,
453 help=_("Give name of at least one package to build."))
454 sub_build
.add_argument("action", action
="store_const", const
="build")
456 sub_build
.add_argument("-a", "--arch",
457 help=_("Build the package for the given architecture."))
458 sub_build
.add_argument("--resultdir", nargs
="?",
459 help=_("Path were the output files should be copied to."))
460 sub_build
.add_argument("-m", "--mode", nargs
="?", default
="development",
461 help=_("Mode to run in. Is either 'release' or 'development' (default)."))
462 sub_build
.add_argument("--after-shell", action
="store_true",
463 help=_("Run a shell after a successful build."))
464 sub_build
.add_argument("--no-install-test", action
="store_true",
465 help=_("Do not perform the install test."))
467 def parse_command_shell(self
):
468 # Implement the "shell" command.
469 sub_shell
= self
.sub_commands
.add_parser("shell",
470 help=_("Go into a shell."))
471 sub_shell
.add_argument("package", nargs
="?",
472 help=_("Give name of a package."))
473 sub_shell
.add_argument("action", action
="store_const", const
="shell")
475 sub_shell
.add_argument("-a", "--arch",
476 help=_("Emulated architecture in the shell."))
477 sub_shell
.add_argument("-m", "--mode", nargs
="?", default
="development",
478 help=_("Mode to run in. Is either 'release' or 'development' (default)."))
480 def parse_command_dist(self
):
481 # Implement the "dist" command.
482 sub_dist
= self
.sub_commands
.add_parser("dist",
483 help=_("Generate a source package."))
484 sub_dist
.add_argument("package", nargs
="+",
485 help=_("Give name(s) of a package(s)."))
486 sub_dist
.add_argument("action", action
="store_const", const
="dist")
488 sub_dist
.add_argument("--resultdir", nargs
="?",
489 help=_("Path were the output files should be copied to."))
491 def handle_info(self
):
492 Cli
.handle_info(self
, long=True)
494 def handle_build(self
):
495 # Get the package descriptor from the command line options
496 pkg
= self
.args
.package
[0]
498 # Check, if we got a regular file
499 if os
.path
.exists(pkg
):
500 pkg
= os
.path
.abspath(pkg
)
503 raise FileNotFoundError
, pkg
505 # Check whether to enable the install test.
506 install_test
= not self
.args
.no_install_test
508 pakfire
.build(pkg
, builder_mode
=self
.args
.mode
, install_test
=install_test
,
509 arch
=self
.args
.arch
, resultdirs
=[self
.args
.resultdir
,],
510 shell
=True, after_shell
=self
.args
.after_shell
, **self
.pakfire_args
)
512 def handle_shell(self
):
515 # Get the package descriptor from the command line options
516 if self
.args
.package
:
517 pkg
= self
.args
.package
519 # Check, if we got a regular file
520 if os
.path
.exists(pkg
):
521 pkg
= os
.path
.abspath(pkg
)
524 raise FileNotFoundError
, pkg
526 pakfire
.shell(pkg
, builder_mode
=self
.args
.mode
,
527 arch
=self
.args
.arch
, **self
.pakfire_args
)
529 def handle_dist(self
):
530 # Get the packages from the command line options
533 for pkg
in self
.args
.package
:
534 # Check, if we got a regular file
535 if os
.path
.exists(pkg
):
536 pkg
= os
.path
.abspath(pkg
)
540 raise FileNotFoundError
, pkg
542 # Put packages to where the user said or our
543 # current working directory.
544 resultdir
= self
.args
.resultdir
or os
.getcwd()
546 # Change the default pakfire configuration, because
547 # packaging source packages can be done in server mode.
548 pakfire_args
= self
.pakfire_args
549 pakfire_args
["mode"] = "server"
552 pakfire
.dist(pkg
, resultdir
=resultdir
, **pakfire_args
)
554 def handle_provides(self
):
555 pkgs
= pakfire
.provides(self
.args
.pattern
, **self
.pakfire_args
)
558 print pkg
.dump(long=True)
561 class CliServer(Cli
):
563 self
.parser
= argparse
.ArgumentParser(
564 description
= _("Pakfire server command line interface."),
567 self
.parse_common_arguments()
570 self
.sub_commands
= self
.parser
.add_subparsers()
572 self
.parse_command_build()
573 self
.parse_command_keepalive()
574 self
.parse_command_repoupdate()
575 self
.parse_command_repo()
576 self
.parse_command_info()
578 # Finally parse all arguments from the command line and save them.
579 self
.args
= self
.parser
.parse_args()
581 self
.server
= server
.Server(**self
.pakfire_args
)
584 "build" : self
.handle_build
,
585 "info" : self
.handle_info
,
586 "keepalive" : self
.handle_keepalive
,
587 "repoupdate" : self
.handle_repoupdate
,
588 "repo_create": self
.handle_repo_create
,
592 def pakfire_args(self
):
593 ret
= { "mode" : "server" }
595 if hasattr(self
.args
, "offline") and self
.args
.offline
:
596 ret
["downloader"] = {
597 "offline" : self
.args
.offline
,
602 def parse_command_build(self
):
603 # Implement the "build" command.
604 sub_build
= self
.sub_commands
.add_parser("build",
605 help=_("Send a scrach build job to the server."))
606 sub_build
.add_argument("package", nargs
=1,
607 help=_("Give name of at least one package to build."))
608 sub_build
.add_argument("--arch", "-a",
609 help=_("Limit build to only these architecture(s)."))
610 sub_build
.add_argument("action", action
="store_const", const
="build")
612 def parse_command_keepalive(self
):
613 # Implement the "keepalive" command.
614 sub_keepalive
= self
.sub_commands
.add_parser("keepalive",
615 help=_("Send a keepalive to the server."))
616 sub_keepalive
.add_argument("action", action
="store_const",
619 def parse_command_repoupdate(self
):
620 # Implement the "repoupdate" command.
621 sub_repoupdate
= self
.sub_commands
.add_parser("repoupdate",
622 help=_("Update all repositories."))
623 sub_repoupdate
.add_argument("action", action
="store_const",
626 def parse_command_repo(self
):
627 sub_repo
= self
.sub_commands
.add_parser("repo",
628 help=_("Repository management commands."))
630 sub_repo_commands
= sub_repo
.add_subparsers()
632 self
.parse_command_repo_create(sub_repo_commands
)
634 def parse_command_repo_create(self
, sub_commands
):
635 sub_create
= sub_commands
.add_parser("create",
636 help=_("Create a new repository index."))
637 sub_create
.add_argument("path", nargs
=1,
638 help=_("Path to the packages."))
639 sub_create
.add_argument("inputs", nargs
="+",
640 help=_("Path to input packages."))
641 sub_create
.add_argument("--key", "-k", nargs
="?",
642 help=_("Key to sign the repository with."))
643 sub_create
.add_argument("action", action
="store_const", const
="repo_create")
645 def parse_command_info(self
):
646 sub_info
= self
.sub_commands
.add_parser("info",
647 help=_("Dump some information about this machine."))
648 sub_info
.add_argument("action", action
="store_const", const
="info")
650 def handle_keepalive(self
):
651 self
.server
.update_info()
653 def handle_build(self
):
656 arches
= self
.args
.arch
.split()
658 (package
,) = self
.args
.package
660 self
.server
.create_scratch_build({})
663 # Temporary folter for source package.
664 tmpdir
= "/tmp/pakfire-%s" % util
.random_string()
669 pakfire
.dist(package
, resultdir
=[tmpdir
,])
671 for file in os
.listdir(tmpdir
):
672 file = os
.path
.join(tmpdir
, file)
677 if os
.path
.exists(tmpdir
):
680 def handle_repoupdate(self
):
681 self
.server
.update_repositories()
683 def handle_repo_create(self
):
684 path
= self
.args
.path
[0]
686 pakfire
.repo_create(path
, self
.args
.inputs
, key_id
=self
.args
.key
,
689 def handle_info(self
):
690 info
= self
.server
.info()
692 print "\n".join(info
)
695 class CliBuilderIntern(Cli
):
697 self
.parser
= argparse
.ArgumentParser(
698 description
= _("Pakfire builder command line interface."),
701 self
.parse_common_arguments()
704 self
.sub_commands
= self
.parser
.add_subparsers()
706 self
.parse_command_build()
708 # Finally parse all arguments from the command line and save them.
709 self
.args
= self
.parser
.parse_args()
712 "build" : self
.handle_build
,
715 def parse_command_build(self
):
716 # Implement the "build" command.
717 sub_build
= self
.sub_commands
.add_parser("build",
718 help=_("Build one or more packages."))
719 sub_build
.add_argument("package", nargs
=1,
720 help=_("Give name of at least one package to build."))
721 sub_build
.add_argument("action", action
="store_const", const
="build")
723 sub_build
.add_argument("-a", "--arch",
724 help=_("Build the package for the given architecture."))
725 sub_build
.add_argument("--resultdir", nargs
="?",
726 help=_("Path were the output files should be copied to."))
727 sub_build
.add_argument("-m", "--mode", nargs
="?", default
="development",
728 help=_("Mode to run in. Is either 'release' or 'development' (default)."))
729 sub_build
.add_argument("--nodeps", action
="store_true",
730 help=_("Do not verify build dependencies."))
731 sub_build
.add_argument("--prepare", action
="store_true",
732 help=_("Only run the prepare stage."))
734 def handle_build(self
):
735 # Get the package descriptor from the command line options
736 pkg
= self
.args
.package
[0]
738 # Check, if we got a regular file
739 if os
.path
.exists(pkg
):
740 pkg
= os
.path
.abspath(pkg
)
742 raise FileNotFoundError
, pkg
744 conf
= config
.ConfigBuilder()
747 disable_repos
= ["*"]
752 "arch" : self
.args
.arch
,
753 "builder_mode" : self
.args
.mode
,
755 "disable_repos" : disable_repos
,
756 "prepare" : self
.args
.prepare
,
757 "resultdir" : self
.args
.resultdir
,
760 pakfire
._build
(pkg
, **kwargs
)
763 class CliClient(Cli
):
765 self
.parser
= argparse
.ArgumentParser(
766 description
= _("Pakfire client command line interface."),
769 self
.parse_common_arguments(repo_manage_switches
=True, offline_switch
=True)
772 self
.sub_commands
= self
.parser
.add_subparsers()
774 self
.parse_command_build()
775 self
.parse_command_connection_check()
776 self
.parse_command_info()
777 self
.parse_command_jobs()
778 self
.parse_command_builds()
779 self
.parse_command_test()
781 # Finally parse all arguments from the command line and save them.
782 self
.args
= self
.parser
.parse_args()
785 "build" : self
.handle_build
,
786 "conn-check" : self
.handle_connection_check
,
787 "info" : self
.handle_info
,
788 "jobs_show" : self
.handle_jobs_show
,
789 "jobs_active" : self
.handle_jobs_active
,
790 "jobs_latest" : self
.handle_jobs_latest
,
791 "builds_show" : self
.handle_builds_show
,
792 "test" : self
.handle_test
,
795 # Read configuration for the pakfire client.
796 self
.conf
= conf
= config
.ConfigClient()
798 # Create connection to pakfire hub.
799 self
.client
= client
.PakfireUserClient(
800 conf
.get("client", "server"),
801 conf
.get("client", "username"),
802 conf
.get("client", "password"),
805 def parse_command_build(self
):
806 # Parse "build" command.
807 sub_build
= self
.sub_commands
.add_parser("build",
808 help=_("Build a package remotely."))
809 sub_build
.add_argument("package", nargs
=1,
810 help=_("Give name of a package to build."))
811 sub_build
.add_argument("action", action
="store_const", const
="build")
813 sub_build
.add_argument("-a", "--arch",
814 help=_("Build the package for the given architecture."))
816 def parse_command_info(self
):
817 # Implement the "info" command.
818 sub_info
= self
.sub_commands
.add_parser("info",
819 help=_("Print some information about this host."))
820 sub_info
.add_argument("action", action
="store_const", const
="info")
822 def parse_command_connection_check(self
):
823 # Implement the "conn-check" command.
824 sub_conn_check
= self
.sub_commands
.add_parser("conn-check",
825 help=_("Check the connection to the hub."))
826 sub_conn_check
.add_argument("action", action
="store_const", const
="conn-check")
828 def parse_command_jobs(self
):
829 sub_jobs
= self
.sub_commands
.add_parser("jobs",
830 help=_("Show information about build jobs."))
832 sub_jobs_commands
= sub_jobs
.add_subparsers()
834 self
.parse_command_jobs_active(sub_jobs_commands
)
835 self
.parse_command_jobs_latest(sub_jobs_commands
)
836 self
.parse_command_jobs_show(sub_jobs_commands
)
838 def parse_command_jobs_active(self
, sub_commands
):
839 sub_active
= sub_commands
.add_parser("active",
840 help=_("Show a list of all active jobs."))
841 sub_active
.add_argument("action", action
="store_const", const
="jobs_active")
843 def parse_command_jobs_latest(self
, sub_commands
):
844 sub_latest
= sub_commands
.add_parser("latest",
845 help=_("Show a list of all recently finished of failed build jobs."))
846 sub_latest
.add_argument("action", action
="store_const", const
="jobs_latest")
848 def parse_command_jobs_show(self
, sub_commands
):
849 sub_show
= sub_commands
.add_parser("show",
850 help=_("Show details about given build job."))
851 sub_show
.add_argument("job_id", nargs
=1, help=_("The ID of the build job."))
852 sub_show
.add_argument("action", action
="store_const", const
="jobs_show")
854 def parse_command_builds(self
):
855 sub_builds
= self
.sub_commands
.add_parser("builds",
856 help=_("Show information about builds."))
858 sub_builds_commands
= sub_builds
.add_subparsers()
860 self
.parse_command_builds_show(sub_builds_commands
)
862 def parse_command_builds_show(self
, sub_commands
):
863 sub_show
= sub_commands
.add_parser("show",
864 help=_("Show details about the given build."))
865 sub_show
.add_argument("build_id", nargs
=1, help=_("The ID of the build."))
866 sub_show
.add_argument("action", action
="store_const", const
="builds_show")
868 def parse_command_test(self
):
869 sub_test
= self
.sub_commands
.add_parser("test",
870 help=_("Test the connection to the hub."))
871 sub_test
.add_argument("error_code", nargs
=1, help=_("Error code to test."))
872 sub_test
.add_argument("action", action
="store_const", const
="test")
874 def handle_build(self
):
875 (package
,) = self
.args
.package
877 # XXX just for now, we do only upload source pfm files.
878 assert os
.path
.exists(package
)
880 # Create a temporary directory.
881 temp_dir
= tempfile
.mkdtemp()
884 if package
.endswith(".%s" % MAKEFILE_EXTENSION
):
885 pakfire_args
= { "mode" : "server" }
887 # Create a source package from the makefile.
888 package
= pakfire
.dist(package
, temp_dir
, **pakfire_args
)
890 elif package
.endswith(".%s" % PACKAGE_EXTENSION
):
894 raise Exception, "Unknown filetype: %s" % package
898 arches
= self
.args
.arch
.replace(",", " ")
902 # Create a new build on the server.
903 build
= self
.client
.build_create(package
, arches
=arches
)
905 # XXX Print the resulting build.
909 # Cleanup the temporary directory and all files.
910 if os
.path
.exists(temp_dir
):
911 shutil
.rmtree(temp_dir
, ignore_errors
=True)
913 def handle_info(self
):
917 ret
.append(" PAKFIRE %s" % PAKFIRE_VERSION
)
919 ret
.append(" %-20s: %s" % (_("Hostname"), system
.hostname
))
920 ret
.append(" %-20s: %s" % (_("Pakfire hub"), self
.conf
.get("client", "server")))
921 if self
.conf
.get("client", "username") and self
.conf
.get("client", "password"):
922 ret
.append(" %-20s: %s" % \
923 (_("Username"), self
.conf
.get("client", "username")))
926 # Hardware information
927 ret
.append(" %s:" % _("Hardware information"))
928 ret
.append(" %-16s: %s" % (_("CPU model"), system
.cpu_model
))
929 ret
.append(" %-16s: %s" % (_("Memory"), util
.format_size(system
.memory
)))
930 ret
.append(" %-16s: %s" % (_("Parallelism"), system
.parallelism
))
932 ret
.append(" %-16s: %s" % (_("Native arch"), system
.native_arch
))
933 if not system
.arch
== system
.native_arch
:
934 ret
.append(" %-16s: %s" % (_("Default arch"), system
.arch
))
936 header
= _("Supported arches")
937 for arch
in system
.supported_arches
:
938 ret
.append(" %-16s: %s" % (header
, arch
))
945 def handle_connection_check(self
):
948 address
= self
.client
.get_my_address()
949 ret
.append(" %-20s: %s" % (_("Your IP address"), address
))
952 authenticated
= self
.client
.check_auth()
954 ret
.append(" %s" % _("You are authenticated to the build service:"))
956 user
= self
.client
.get_user_profile()
957 assert user
, "Could not fetch user infomation"
960 ("name", _("User name")),
961 ("realname", _("Real name")),
962 ("email", _("Email address")),
963 ("registered", _("Registered")),
966 for key
, desc
in keys
:
967 ret
.append(" %-18s: %s" % (desc
, user
.get(key
)))
970 ret
.append(_("You could not be authenticated to the build service."))
975 def _print_jobs(self
, jobs
, heading
=None):
977 print "%s:" % heading
981 line
= " [%(type)8s] %(name)-30s: %(state)s"
985 print # Empty line at the end.
987 def handle_jobs_active(self
):
988 jobs
= self
.client
.get_active_jobs()
991 print _("No ongoing jobs found.")
994 self
._print
_jobs
(jobs
, _("Active build jobs"))
996 def handle_jobs_latest(self
):
997 jobs
= self
.client
.get_latest_jobs()
1000 print _("No jobs found.")
1003 self
._print
_jobs
(jobs
, _("Recently processed build jobs"))
1005 def handle_builds_show(self
):
1006 (build_id
,) = self
.args
.build_id
1008 build
= self
.client
.get_build(build_id
)
1010 print _("A build with ID %s could not be found.") % build_id
1013 print _("Build: %(name)s") % build
1017 fmt
% (_("State"), build
["state"]),
1018 fmt
% (_("Priority"), build
["priority"]),
1021 lines
.append("%s:" % _("Jobs"))
1022 for job
in build
["jobs"]:
1023 lines
.append(" * [%(uuid)s] %(name)-30s: %(state)s" % job
)
1029 def handle_jobs_show(self
):
1030 (job_id
,) = self
.args
.job_id
1032 job
= self
.client
.get_job(job_id
)
1034 print _("A job with ID %s could not be found.") % job_id
1038 if job
["builder_id"]:
1039 builder
= self
.client
.get_builder(job
["builder_id"])
1041 print _("Job: %(name)s") % job
1045 fmt
% (_("State"), job
["state"]),
1046 fmt
% (_("Arch"), job
["arch"]),
1051 fmt
% (_("Build host"), builder
["name"]),
1056 fmt
% (_("Time created"), job
["time_created"]),
1057 fmt
% (_("Time started"), job
["time_started"]),
1058 fmt
% (_("Time finished"), job
["time_finished"]),
1059 fmt
% (_("Duration"), job
["duration"]),
1063 lines
+= ["", "%s:" % _("Packages")]
1065 for pkg
in job
["packages"]:
1067 "* %(friendly_name)s" % pkg
,
1072 lines
+= [" %s" % line
for line
in pkg_lines
]
1078 def handle_test(self
):
1079 error_code
= self
.args
.error_code
[0]
1082 error_code
= int(error_code
)
1086 if error_code
< 100 or error_code
> 999:
1087 raise Error
, _("Invalid error code given.")
1089 res
= self
.client
.test_code(error_code
)
1090 print _("Reponse from the server: %s") % res
1093 class CliDaemon(Cli
):
1095 self
.parser
= argparse
.ArgumentParser(
1096 description
= _("Pakfire daemon command line interface."),
1099 self
.parse_common_arguments(repo_manage_switches
=True, offline_switch
=True)
1101 # Finally parse all arguments from the command line and save them.
1102 self
.args
= self
.parser
.parse_args()
1106 Runs the pakfire daemon with provided settings.
1108 # Read the configuration file for the daemon.
1109 conf
= config
.ConfigDaemon()
1111 # Create daemon instance.
1112 d
= pakfire
.client
.PakfireDaemon()
1116 # We cannot just kill the daemon, it needs a smooth shutdown.
1117 except (SystemExit, KeyboardInterrupt):
1123 self
.parser
= argparse
.ArgumentParser(
1124 description
= _("Pakfire key command line interface."),
1127 self
.parse_common_arguments(repo_manage_switches
=False,
1128 offline_switch
=True)
1131 self
.sub_commands
= self
.parser
.add_subparsers()
1133 self
.parse_command_generate()
1134 self
.parse_command_import()
1135 self
.parse_command_export()
1136 self
.parse_command_delete()
1137 self
.parse_command_list()
1138 self
.parse_command_sign()
1139 self
.parse_command_verify()
1141 # Finally parse all arguments from the command line and save them.
1142 self
.args
= self
.parser
.parse_args()
1144 # Create a pakfire instance.
1145 self
.pakfire
= pakfire
.Pakfire(**self
.pakfire_args
)
1147 self
.action2func
= {
1148 "generate" : self
.handle_generate
,
1149 "import" : self
.handle_import
,
1150 "export" : self
.handle_export
,
1151 "delete" : self
.handle_delete
,
1152 "list" : self
.handle_list
,
1153 "sign" : self
.handle_sign
,
1154 "verify" : self
.handle_verify
,
1158 def pakfire_args(self
):
1165 def parse_command_generate(self
):
1166 # Parse "generate" command.
1167 sub_gen
= self
.sub_commands
.add_parser("generate",
1168 help=_("Import a key from file."))
1169 sub_gen
.add_argument("--realname", nargs
=1,
1170 help=_("The real name of the owner of this key."))
1171 sub_gen
.add_argument("--email", nargs
=1,
1172 help=_("The email address of the owner of this key."))
1173 sub_gen
.add_argument("action", action
="store_const", const
="generate")
1175 def parse_command_import(self
):
1176 # Parse "import" command.
1177 sub_import
= self
.sub_commands
.add_parser("import",
1178 help=_("Import a key from file."))
1179 sub_import
.add_argument("filename", nargs
=1,
1180 help=_("Filename of that key to import."))
1181 sub_import
.add_argument("action", action
="store_const", const
="import")
1183 def parse_command_export(self
):
1184 # Parse "export" command.
1185 sub_export
= self
.sub_commands
.add_parser("export",
1186 help=_("Export a key to a file."))
1187 sub_export
.add_argument("keyid", nargs
=1,
1188 help=_("The ID of the key to export."))
1189 sub_export
.add_argument("filename", nargs
=1,
1190 help=_("Write the key to this file."))
1191 sub_export
.add_argument("--secret", action
="store_true",
1192 help=_("Export the secret key, too."))
1193 sub_export
.add_argument("action", action
="store_const", const
="export")
1195 def parse_command_delete(self
):
1196 # Parse "delete" command.
1197 sub_del
= self
.sub_commands
.add_parser("delete",
1198 help=_("Delete a key from the local keyring."))
1199 sub_del
.add_argument("keyid", nargs
=1,
1200 help=_("The ID of the key to delete."))
1201 sub_del
.add_argument("action", action
="store_const", const
="delete")
1203 def parse_command_list(self
):
1204 # Parse "list" command.
1205 sub_list
= self
.sub_commands
.add_parser("list",
1206 help=_("List all imported keys."))
1207 sub_list
.add_argument("action", action
="store_const", const
="list")
1209 def parse_command_sign(self
):
1210 # Implement the "sign" command.
1211 sub_sign
= self
.sub_commands
.add_parser("sign",
1212 help=_("Sign one or more packages."))
1213 sub_sign
.add_argument("--key", "-k", nargs
=1,
1214 help=_("Key that is used sign the package(s)."))
1215 sub_sign
.add_argument("package", nargs
="+",
1216 help=_("Package(s) to sign."))
1217 sub_sign
.add_argument("action", action
="store_const", const
="sign")
1219 def parse_command_verify(self
):
1220 # Implement the "verify" command.
1221 sub_verify
= self
.sub_commands
.add_parser("verify",
1222 help=_("Verify one or more packages."))
1223 #sub_verify.add_argument("--key", "-k", nargs=1,
1224 # help=_("Key that is used verify the package(s)."))
1225 sub_verify
.add_argument("package", nargs
="+",
1226 help=_("Package(s) to verify."))
1227 sub_verify
.add_argument("action", action
="store_const", const
="verify")
1229 def handle_generate(self
):
1230 realname
= self
.args
.realname
[0]
1231 email
= self
.args
.email
[0]
1233 print _("Generating the key may take a moment...")
1237 pakfire
.key_generate(realname
, email
, **self
.pakfire_args
)
1239 def handle_import(self
):
1240 filename
= self
.args
.filename
[0]
1242 # Simply import the file.
1243 pakfire
.key_import(filename
, **self
.pakfire_args
)
1245 def handle_export(self
):
1246 keyid
= self
.args
.keyid
[0]
1247 filename
= self
.args
.filename
[0]
1248 secret
= self
.args
.secret
1250 pakfire
.key_export(keyid
, filename
, secret
=secret
, **self
.pakfire_args
)
1252 def handle_delete(self
):
1253 keyid
= self
.args
.keyid
[0]
1255 pakfire
.key_delete(keyid
, **self
.pakfire_args
)
1257 def handle_list(self
):
1258 lines
= pakfire
.key_list(**self
.pakfire_args
)
1263 def handle_sign(self
):
1264 # Get the files from the command line options
1267 for file in self
.args
.package
:
1268 # Check, if we got a regular file
1269 if os
.path
.exists(file):
1270 file = os
.path
.abspath(file)
1274 raise FileNotFoundError
, file
1276 key
= self
.args
.key
[0]
1280 pkg
= packages
.open(self
.pakfire
, None, file)
1282 print _("Signing %s...") % pkg
.friendly_name
1285 def handle_verify(self
):
1286 # Get the files from the command line options
1289 for file in self
.args
.package
:
1290 # Check, if we got a regular file
1291 if os
.path
.exists(file) and not os
.path
.isdir(file):
1292 file = os
.path
.abspath(file)
1297 pkg
= packages
.open(self
.pakfire
, None, file)
1299 print _("Verifying %s...") % pkg
.friendly_name
1303 key
= self
.pakfire
.keyring
.get_key(sig
.fpr
)
1305 subkey
= key
.subkeys
[0]
1307 print " %s %s" % (subkey
.fpr
[-16:], key
.uids
[0].uid
)
1309 print " %s" % _("This signature is valid.")
1312 print " %s <%s>" % (sig
.fpr
, _("Unknown key"))
1313 print " %s" % _("Could not check if this signature is valid.")
1315 created
= datetime
.datetime
.fromtimestamp(sig
.timestamp
)
1316 print " %s" % _("Created: %s") % created
1318 if sig
.exp_timestamp
:
1319 expires
= datetime
.datetime
.fromtimestamp(sig
.exp_timestamp
)
1320 print " %s" % _("Expires: %s") % expires