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