]> git.ipfire.org Git - pakfire.git/blame - pakfire/cli.py
Remove pakfire-repo command.
[pakfire.git] / pakfire / cli.py
CommitLineData
47a4cb89
MT
1#!/usr/bin/python
2
3import argparse
4import sys
5
60845a36 6import logger
47a4cb89 7import packages
fa6d335b 8import repository
677ff42a 9import server
e9c20259 10import util
47a4cb89 11
18edfe75 12import pakfire.api as pakfire
47a4cb89
MT
13from constants import *
14from i18n import _
15
60845a36
MT
16# Initialize a very simple logging that is removed when a Pakfire instance
17# is started.
18logger.setup_logging()
19
47a4cb89 20class Cli(object):
47a4cb89
MT
21 def __init__(self):
22 self.parser = argparse.ArgumentParser(
23 description = _("Pakfire command line interface."),
24 )
25
26 self.parse_common_arguments()
27
2ba449f0 28 self.parser.add_argument("--root", metavar="PATH",
d2e26956 29 default="/",
47a4cb89
MT
30 help=_("The path where pakfire should operate in."))
31
32 # Add sub-commands.
33 self.sub_commands = self.parser.add_subparsers()
34
35 self.parse_command_install()
5e87fa4f 36 self.parse_command_localinstall()
a39fd08b 37 self.parse_command_remove()
47a4cb89
MT
38 self.parse_command_info()
39 self.parse_command_search()
40 self.parse_command_update()
fa6d335b 41 self.parse_command_provides()
c1962d40 42 self.parse_command_grouplist()
ce2764c1 43 self.parse_command_groupinstall()
67bc4528 44 self.parse_command_repolist()
31267a64 45 self.parse_command_clean()
35d89fd7 46 self.parse_command_check()
47a4cb89
MT
47
48 # Finally parse all arguments from the command line and save them.
49 self.args = self.parser.parse_args()
50
47a4cb89 51 self.action2func = {
5e87fa4f
MT
52 "install" : self.handle_install,
53 "localinstall" : self.handle_localinstall,
a39fd08b 54 "remove" : self.handle_remove,
5e87fa4f
MT
55 "update" : self.handle_update,
56 "info" : self.handle_info,
57 "search" : self.handle_search,
fa6d335b 58 "provides" : self.handle_provides,
c1962d40 59 "grouplist" : self.handle_grouplist,
ce2764c1 60 "groupinstall" : self.handle_groupinstall,
67bc4528 61 "repolist" : self.handle_repolist,
31267a64 62 "clean_all" : self.handle_clean_all,
35d89fd7 63 "check" : self.handle_check,
47a4cb89
MT
64 }
65
7c8f2953
MT
66 @property
67 def pakfire_args(self):
6557ff4c
MT
68 ret = { "mode" : "normal" }
69
2ba449f0
MT
70 if hasattr(self.args, "root"):
71 ret["path"] = self.args.root
f9a012a8
MT
72
73 if hasattr(self.args, "disable_repo"):
74 ret["disable_repos"] = self.args.disable_repo
75
76 if hasattr(self.args, "enable_repo"):
77 ret["enable_repos"] = self.args.enable_repo
78
79 return ret
7c8f2953 80
47a4cb89
MT
81 def parse_common_arguments(self):
82 self.parser.add_argument("-v", "--verbose", action="store_true",
83 help=_("Enable verbose output."))
84
85 self.parser.add_argument("-c", "--config", nargs="?",
86 help=_("Path to a configuration file to load."))
87
f781b1ab
MT
88 self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO",
89 help=_("Disable a repository temporarily."))
90
f9a012a8
MT
91 self.parser.add_argument("--enabled-repo", nargs="*", metavar="REPO",
92 help=_("Enable a repository temporarily."))
93
47a4cb89
MT
94 def parse_command_install(self):
95 # Implement the "install" command.
96 sub_install = self.sub_commands.add_parser("install",
97 help=_("Install one or more packages to the system."))
98 sub_install.add_argument("package", nargs="+",
99 help=_("Give name of at least one package to install."))
100 sub_install.add_argument("action", action="store_const", const="install")
101
5e87fa4f
MT
102 def parse_command_localinstall(self):
103 # Implement the "localinstall" command.
104 sub_install = self.sub_commands.add_parser("localinstall",
105 help=_("Install one or more packages from the filesystem."))
106 sub_install.add_argument("package", nargs="+",
107 help=_("Give filename of at least one package."))
108 sub_install.add_argument("action", action="store_const", const="localinstall")
109
a39fd08b
MT
110 def parse_command_remove(self):
111 # Implement the "remove" command.
112 sub_remove = self.sub_commands.add_parser("remove",
113 help=_("Remove one or more packages from the system."))
114 sub_remove.add_argument("package", nargs="+",
115 help=_("Give name of at least one package to remove."))
116 sub_remove.add_argument("action", action="store_const", const="remove")
117
47a4cb89
MT
118 def parse_command_update(self):
119 # Implement the "update" command.
120 sub_update = self.sub_commands.add_parser("update",
121 help=_("Update the whole system or one specific package."))
122 sub_update.add_argument("package", nargs="*",
123 help=_("Give a name of a package to update or leave emtpy for all."))
124 sub_update.add_argument("action", action="store_const", const="update")
125
126 def parse_command_info(self):
127 # Implement the "info" command.
128 sub_info = self.sub_commands.add_parser("info",
129 help=_("Print some information about the given package(s)."))
130 sub_info.add_argument("package", nargs="+",
131 help=_("Give at least the name of one package."))
132 sub_info.add_argument("action", action="store_const", const="info")
133
134 def parse_command_search(self):
135 # Implement the "search" command.
136 sub_search = self.sub_commands.add_parser("search",
137 help=_("Search for a given pattern."))
138 sub_search.add_argument("pattern",
139 help=_("A pattern to search for."))
140 sub_search.add_argument("action", action="store_const", const="search")
141
fa6d335b
MT
142 def parse_command_provides(self):
143 # Implement the "provides" command
144 sub_provides = self.sub_commands.add_parser("provides",
145 help=_("Get a list of packages that provide a given file or feature."))
146 sub_provides.add_argument("pattern", nargs="+",
147 help=_("File or feature to search for."))
148 sub_provides.add_argument("action", action="store_const", const="provides")
149
c1962d40
MT
150 def parse_command_grouplist(self):
151 # Implement the "grouplist" command
152 sub_grouplist = self.sub_commands.add_parser("grouplist",
153 help=_("Get list of packages that belong to the given group."))
154 sub_grouplist.add_argument("group", nargs=1,
155 help=_("Group name to search for."))
156 sub_grouplist.add_argument("action", action="store_const", const="grouplist")
157
ce2764c1
MT
158 def parse_command_groupinstall(self):
159 # Implement the "grouplist" command
160 sub_groupinstall = self.sub_commands.add_parser("groupinstall",
161 help=_("Install all packages that belong to the given group."))
162 sub_groupinstall.add_argument("group", nargs=1,
163 help=_("Group name."))
164 sub_groupinstall.add_argument("action", action="store_const", const="groupinstall")
165
67bc4528
MT
166 def parse_command_repolist(self):
167 # Implement the "repolist" command
168 sub_repolist = self.sub_commands.add_parser("repolist",
169 help=_("List all currently enabled repositories."))
170 sub_repolist.add_argument("action", action="store_const", const="repolist")
ce2764c1 171
31267a64
MT
172 def parse_command_clean(self):
173 sub_clean = self.sub_commands.add_parser("clean", help=_("Cleanup commands."))
174
175 sub_clean_commands = sub_clean.add_subparsers()
176
177 self.parse_command_clean_all(sub_clean_commands)
178
179 def parse_command_clean_all(self, sub_commands):
180 sub_create = sub_commands.add_parser("all",
181 help=_("Cleanup all temporary files."))
182 sub_create.add_argument("action", action="store_const", const="clean_all")
183
35d89fd7
MT
184 def parse_command_check(self):
185 # Implement the "check" command
186 sub_check = self.sub_commands.add_parser("check",
187 help=_("Check the system for any errors."))
188 sub_check.add_argument("action", action="store_const", const="check")
189
47a4cb89
MT
190 def run(self):
191 action = self.args.action
192
193 if not self.action2func.has_key(action):
194 raise
195
196 try:
197 func = self.action2func[action]
198 except KeyError:
199 raise # XXX catch and return better error message
200
201 return func()
202
9afa5620 203 def handle_info(self, long=False):
7c8f2953 204 pkgs = pakfire.info(self.args.package, **self.pakfire_args)
47a4cb89 205
7c8f2953
MT
206 for pkg in pkgs:
207 print pkg.dump(long=long)
47a4cb89
MT
208
209 def handle_search(self):
7c8f2953 210 pkgs = pakfire.search(self.args.pattern, **self.pakfire_args)
47a4cb89
MT
211
212 for pkg in pkgs:
213 print pkg.dump(short=True)
214
215 def handle_update(self):
7c8f2953 216 pakfire.update(self.args.package, **self.pakfire_args)
47a4cb89 217
e0b99370
MT
218 def handle_install(self):
219 pakfire.install(self.args.package, **self.pakfire_args)
5e87fa4f
MT
220
221 def handle_localinstall(self):
e0b99370 222 pakfire.localinstall(self.args.package, **self.pakfire_args)
5e87fa4f 223
a39fd08b
MT
224 def handle_remove(self):
225 pakfire.remove(self.args.package, **self.pakfire_args)
226
fa6d335b 227 def handle_provides(self):
7c8f2953 228 pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args)
fa6d335b
MT
229
230 for pkg in pkgs:
231 print pkg.dump()
232
c1962d40 233 def handle_grouplist(self):
7c8f2953 234 pkgs = pakfire.grouplist(self.args.group[0], **self.pakfire_args)
c1962d40
MT
235
236 for pkg in pkgs:
237 print " * %s" % pkg
238
ce2764c1 239 def handle_groupinstall(self):
7c8f2953 240 pakfire.groupinstall(self.args.group[0], **self.pakfire_args)
ce2764c1 241
67bc4528 242 def handle_repolist(self):
7c8f2953 243 repos = pakfire.repo_list(**self.pakfire_args)
67bc4528 244
c605d735 245 FORMAT = " %-20s %8s %12s %12s "
67bc4528 246
c605d735 247 title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
67bc4528
MT
248 print title
249 print "=" * len(title) # spacing line
250
251 for repo in repos:
252 # Skip the installed repository.
253 if repo.name == "installed":
254 continue
255
c605d735 256 print FORMAT % (repo.name, repo.enabled, repo.priority, len(repo))
67bc4528 257
31267a64
MT
258 def handle_clean_all(self):
259 print _("Cleaning up everything...")
260
261 pakfire.clean_all(**self.pakfire_args)
262
35d89fd7
MT
263 def handle_check(self):
264 pakfire.check(**self.pakfire_args)
265
47a4cb89
MT
266
267class CliBuilder(Cli):
268 def __init__(self):
269 self.parser = argparse.ArgumentParser(
270 description = _("Pakfire builder command line interface."),
271 )
272
273 self.parse_common_arguments()
274
275 # Add sub-commands.
276 self.sub_commands = self.parser.add_subparsers()
277
278 self.parse_command_build()
279 self.parse_command_dist()
280 self.parse_command_info()
281 self.parse_command_search()
282 self.parse_command_shell()
283 self.parse_command_update()
4fbd4216 284 self.parse_command_provides()
2c84aceb 285 self.parse_command_grouplist()
67bc4528 286 self.parse_command_repolist()
31267a64 287 self.parse_command_clean()
47a4cb89
MT
288
289 # Finally parse all arguments from the command line and save them.
290 self.args = self.parser.parse_args()
291
47a4cb89 292 self.action2func = {
fa6d335b
MT
293 "build" : self.handle_build,
294 "dist" : self.handle_dist,
295 "update" : self.handle_update,
296 "info" : self.handle_info,
297 "search" : self.handle_search,
298 "shell" : self.handle_shell,
4fbd4216 299 "provides" : self.handle_provides,
2c84aceb 300 "grouplist" : self.handle_grouplist,
67bc4528 301 "repolist" : self.handle_repolist,
31267a64 302 "clean_all" : self.handle_clean_all,
47a4cb89
MT
303 }
304
7c8f2953
MT
305 @property
306 def pakfire_args(self):
6557ff4c 307 ret = { "mode" : "builder" }
f9a012a8
MT
308
309 if hasattr(self.args, "disable_repo"):
310 ret["disable_repos"] = self.args.disable_repo
311
312 if hasattr(self.args, "enable_repo"):
313 ret["enable_repos"] = self.args.enable_repo
314
315 return ret
7c8f2953 316
47a4cb89
MT
317 def parse_command_update(self):
318 # Implement the "update" command.
319 sub_update = self.sub_commands.add_parser("update",
320 help=_("Update the package indexes."))
321 sub_update.add_argument("action", action="store_const", const="update")
322
323 def parse_command_build(self):
324 # Implement the "build" command.
325 sub_build = self.sub_commands.add_parser("build",
326 help=_("Build one or more packages."))
327 sub_build.add_argument("package", nargs=1,
328 help=_("Give name of at least one package to build."))
329 sub_build.add_argument("action", action="store_const", const="build")
330
331 sub_build.add_argument("-a", "--arch",
332 help=_("Build the package for the given architecture."))
333 sub_build.add_argument("--resultdir", nargs="?",
334 help=_("Path were the output files should be copied to."))
f22069bb
MT
335 sub_build.add_argument("-m", "--mode", nargs="?", default="development",
336 help=_("Mode to run in. Is either 'release' or 'development' (default)."))
47a4cb89
MT
337
338 def parse_command_shell(self):
339 # Implement the "shell" command.
340 sub_shell = self.sub_commands.add_parser("shell",
341 help=_("Go into a shell."))
042266f3 342 sub_shell.add_argument("package", nargs="?",
47a4cb89
MT
343 help=_("Give name of a package."))
344 sub_shell.add_argument("action", action="store_const", const="shell")
345
346 sub_shell.add_argument("-a", "--arch",
347 help=_("Emulated architecture in the shell."))
6ee3d6b9
MT
348 sub_shell.add_argument("-m", "--mode", nargs="?", default="development",
349 help=_("Mode to run in. Is either 'release' or 'development' (default)."))
47a4cb89
MT
350
351 def parse_command_dist(self):
352 # Implement the "dist" command.
353 sub_dist = self.sub_commands.add_parser("dist",
354 help=_("Generate a source package."))
e412b8dc
MT
355 sub_dist.add_argument("package", nargs="+",
356 help=_("Give name(s) of a package(s)."))
47a4cb89
MT
357 sub_dist.add_argument("action", action="store_const", const="dist")
358
359 sub_dist.add_argument("--resultdir", nargs="?",
360 help=_("Path were the output files should be copied to."))
361
9afa5620
MT
362 def handle_info(self):
363 Cli.handle_info(self, long=True)
364
47a4cb89 365 def handle_build(self):
47a4cb89
MT
366 # Get the package descriptor from the command line options
367 pkg = self.args.package[0]
368
369 # Check, if we got a regular file
370 if os.path.exists(pkg):
371 pkg = os.path.abspath(pkg)
372
47a4cb89 373 else:
7c8f2953 374 raise FileNotFoundError, pkg
47a4cb89 375
7c8f2953
MT
376 # Create distribution configuration from command line.
377 distro_config = {
378 "arch" : self.args.arch,
379 }
380
f22069bb
MT
381 pakfire.build(pkg, builder_mode=self.args.mode, distro_config=distro_config,
382 resultdirs=[self.args.resultdir,], shell=True, **self.pakfire_args)
47a4cb89
MT
383
384 def handle_shell(self):
042266f3
MT
385 pkg = None
386
47a4cb89 387 # Get the package descriptor from the command line options
042266f3 388 if self.args.package:
ad1b844f 389 pkg = self.args.package
47a4cb89 390
7c8f2953
MT
391 # Check, if we got a regular file
392 if os.path.exists(pkg):
393 pkg = os.path.abspath(pkg)
47a4cb89 394
7c8f2953
MT
395 else:
396 raise FileNotFoundError, pkg
47a4cb89 397
7c8f2953
MT
398 # Create distribution configuration from command line.
399 distro_config = {
400 "arch" : self.args.arch,
401 }
47a4cb89 402
6ee3d6b9
MT
403 pakfire.shell(pkg, builder_mode=self.args.mode,
404 distro_config=distro_config, **self.pakfire_args)
47a4cb89
MT
405
406 def handle_dist(self):
e412b8dc
MT
407 # Get the packages from the command line options
408 pkgs = []
47a4cb89 409
e412b8dc
MT
410 for pkg in self.args.package:
411 # Check, if we got a regular file
412 if os.path.exists(pkg):
413 pkg = os.path.abspath(pkg)
7c8f2953 414 pkgs.append(pkg)
47a4cb89 415
e412b8dc 416 else:
7c8f2953
MT
417 raise FileNotFoundError, pkg
418
6519843a
MT
419 pakfire.dist(pkgs, resultdirs=[self.args.resultdir,],
420 **self.pakfire_args)
47a4cb89 421
c605d735
MT
422 def handle_provides(self):
423 pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args)
424
425 for pkg in pkgs:
426 print pkg.dump(long=True)
427
47a4cb89 428
3ad4bb5a 429class CliServer(Cli):
677ff42a
MT
430 def __init__(self):
431 self.parser = argparse.ArgumentParser(
3ad4bb5a 432 description = _("Pakfire server command line interface."),
677ff42a
MT
433 )
434
435 self.parse_common_arguments()
436
437 # Add sub-commands.
438 self.sub_commands = self.parser.add_subparsers()
439
a52f536c 440 self.parse_command_build()
677ff42a 441 self.parse_command_keepalive()
8276111d 442 self.parse_command_repoupdate()
df9c4f62 443 self.parse_command_repo()
677ff42a
MT
444
445 # Finally parse all arguments from the command line and save them.
446 self.args = self.parser.parse_args()
447
3ad4bb5a 448 self.server = server.Server()
677ff42a
MT
449
450 self.action2func = {
8276111d
MT
451 "build" : self.handle_build,
452 "keepalive" : self.handle_keepalive,
453 "repoupdate" : self.handle_repoupdate,
df9c4f62 454 "repo_create": self.handle_repo_create,
677ff42a
MT
455 }
456
6557ff4c
MT
457 @property
458 def pakfire_args(self):
459 ret = { "mode" : "server" }
460
461 return ret
462
a52f536c
MT
463 def parse_command_build(self):
464 # Implement the "build" command.
465 sub_keepalive = self.sub_commands.add_parser("build",
466 help=_("Request a build job from the server."))
467 sub_keepalive.add_argument("action", action="store_const", const="build")
468
677ff42a
MT
469 def parse_command_keepalive(self):
470 # Implement the "keepalive" command.
471 sub_keepalive = self.sub_commands.add_parser("keepalive",
472 help=_("Send a keepalive to the server."))
473 sub_keepalive.add_argument("action", action="store_const",
474 const="keepalive")
475
8276111d
MT
476 def parse_command_repoupdate(self):
477 # Implement the "repoupdate" command.
478 sub_repoupdate = self.sub_commands.add_parser("repoupdate",
479 help=_("Update all repositories."))
480 sub_repoupdate.add_argument("action", action="store_const",
481 const="repoupdate")
482
df9c4f62
MT
483 def parse_command_repo(self):
484 sub_repo = self.sub_commands.add_parser("repo",
485 help=_("Repository management commands."))
486
487 sub_repo_commands = sub_repo.add_subparsers()
488
489 self.parse_command_repo_create(sub_repo_commands)
490
491 def parse_command_repo_create(self, sub_commands):
492 sub_create = sub_commands.add_parser("create",
493 help=_("Create a new repository index."))
494 sub_create.add_argument("path", nargs=1, help=_("Path to the packages."))
495 sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages."))
496 sub_create.add_argument("action", action="store_const", const="repo_create")
497
677ff42a 498 def handle_keepalive(self):
3ad4bb5a 499 self.server.update_info()
9613a111 500
a52f536c 501 def handle_build(self):
3ad4bb5a 502 self.server.build_job()
8276111d
MT
503
504 def handle_repoupdate(self):
505 self.server.update_repositories()
df9c4f62
MT
506
507 def handle_repo_create(self):
508 path = self.args.path[0]
509
510 pakfire.repo_create(path, self.args.inputs, **self.pakfire_args)