]> git.ipfire.org Git - people/stevee/pakfire.git/blame - pakfire/cli.py
Remove requires switch which is currently not supported.
[people/stevee/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()
c1962d40 37 self.parse_command_grouplist()
ce2764c1 38 self.parse_command_groupinstall()
67bc4528 39 self.parse_command_repolist()
47a4cb89
MT
40
41 # Finally parse all arguments from the command line and save them.
42 self.args = self.parser.parse_args()
43
47a4cb89 44 self.action2func = {
5e87fa4f
MT
45 "install" : self.handle_install,
46 "localinstall" : self.handle_localinstall,
a39fd08b 47 "remove" : self.handle_remove,
5e87fa4f
MT
48 "update" : self.handle_update,
49 "info" : self.handle_info,
50 "search" : self.handle_search,
fa6d335b 51 "provides" : self.handle_provides,
c1962d40 52 "grouplist" : self.handle_grouplist,
ce2764c1 53 "groupinstall" : self.handle_groupinstall,
67bc4528 54 "repolist" : self.handle_repolist,
47a4cb89
MT
55 }
56
7c8f2953
MT
57 @property
58 def pakfire_args(self):
715d7009
MT
59 return {
60 "path" : self.args.instroot,
61 }
7c8f2953 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
c1962d40
MT
129 def parse_command_grouplist(self):
130 # Implement the "grouplist" command
131 sub_grouplist = self.sub_commands.add_parser("grouplist",
132 help=_("Get list of packages that belong to the given group."))
133 sub_grouplist.add_argument("group", nargs=1,
134 help=_("Group name to search for."))
135 sub_grouplist.add_argument("action", action="store_const", const="grouplist")
136
ce2764c1
MT
137 def parse_command_groupinstall(self):
138 # Implement the "grouplist" command
139 sub_groupinstall = self.sub_commands.add_parser("groupinstall",
140 help=_("Install all packages that belong to the given group."))
141 sub_groupinstall.add_argument("group", nargs=1,
142 help=_("Group name."))
143 sub_groupinstall.add_argument("action", action="store_const", const="groupinstall")
144
67bc4528
MT
145 def parse_command_repolist(self):
146 # Implement the "repolist" command
147 sub_repolist = self.sub_commands.add_parser("repolist",
148 help=_("List all currently enabled repositories."))
149 sub_repolist.add_argument("action", action="store_const", const="repolist")
ce2764c1 150
47a4cb89
MT
151 def run(self):
152 action = self.args.action
153
154 if not self.action2func.has_key(action):
155 raise
156
157 try:
158 func = self.action2func[action]
159 except KeyError:
160 raise # XXX catch and return better error message
161
162 return func()
163
9afa5620 164 def handle_info(self, long=False):
7c8f2953 165 pkgs = pakfire.info(self.args.package, **self.pakfire_args)
47a4cb89 166
7c8f2953
MT
167 for pkg in pkgs:
168 print pkg.dump(long=long)
47a4cb89
MT
169
170 def handle_search(self):
7c8f2953 171 pkgs = pakfire.search(self.args.pattern, **self.pakfire_args)
47a4cb89
MT
172
173 for pkg in pkgs:
174 print pkg.dump(short=True)
175
176 def handle_update(self):
7c8f2953 177 pakfire.update(self.args.package, **self.pakfire_args)
47a4cb89 178
e0b99370
MT
179 def handle_install(self):
180 pakfire.install(self.args.package, **self.pakfire_args)
5e87fa4f
MT
181
182 def handle_localinstall(self):
e0b99370 183 pakfire.localinstall(self.args.package, **self.pakfire_args)
5e87fa4f 184
a39fd08b
MT
185 def handle_remove(self):
186 pakfire.remove(self.args.package, **self.pakfire_args)
187
fa6d335b 188 def handle_provides(self):
7c8f2953 189 pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args)
fa6d335b
MT
190
191 for pkg in pkgs:
192 print pkg.dump()
193
c1962d40 194 def handle_grouplist(self):
7c8f2953 195 pkgs = pakfire.grouplist(self.args.group[0], **self.pakfire_args)
c1962d40
MT
196
197 for pkg in pkgs:
198 print " * %s" % pkg
199
ce2764c1 200 def handle_groupinstall(self):
7c8f2953 201 pakfire.groupinstall(self.args.group[0], **self.pakfire_args)
ce2764c1 202
67bc4528 203 def handle_repolist(self):
7c8f2953 204 repos = pakfire.repo_list(**self.pakfire_args)
67bc4528 205
c605d735 206 FORMAT = " %-20s %8s %12s %12s "
67bc4528 207
c605d735 208 title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages"))
67bc4528
MT
209 print title
210 print "=" * len(title) # spacing line
211
212 for repo in repos:
213 # Skip the installed repository.
214 if repo.name == "installed":
215 continue
216
c605d735 217 print FORMAT % (repo.name, repo.enabled, repo.priority, len(repo))
67bc4528 218
47a4cb89
MT
219
220class CliBuilder(Cli):
221 def __init__(self):
222 self.parser = argparse.ArgumentParser(
223 description = _("Pakfire builder command line interface."),
224 )
225
226 self.parse_common_arguments()
227
228 # Add sub-commands.
229 self.sub_commands = self.parser.add_subparsers()
230
231 self.parse_command_build()
232 self.parse_command_dist()
233 self.parse_command_info()
234 self.parse_command_search()
235 self.parse_command_shell()
236 self.parse_command_update()
4fbd4216 237 self.parse_command_provides()
2c84aceb 238 self.parse_command_grouplist()
67bc4528 239 self.parse_command_repolist()
47a4cb89
MT
240
241 # Finally parse all arguments from the command line and save them.
242 self.args = self.parser.parse_args()
243
47a4cb89 244 self.action2func = {
fa6d335b
MT
245 "build" : self.handle_build,
246 "dist" : self.handle_dist,
247 "update" : self.handle_update,
248 "info" : self.handle_info,
249 "search" : self.handle_search,
250 "shell" : self.handle_shell,
4fbd4216 251 "provides" : self.handle_provides,
2c84aceb 252 "grouplist" : self.handle_grouplist,
67bc4528 253 "repolist" : self.handle_repolist,
47a4cb89
MT
254 }
255
7c8f2953
MT
256 @property
257 def pakfire_args(self):
258 return { "builder" : 1 }
259
47a4cb89
MT
260 def parse_command_update(self):
261 # Implement the "update" command.
262 sub_update = self.sub_commands.add_parser("update",
263 help=_("Update the package indexes."))
264 sub_update.add_argument("action", action="store_const", const="update")
265
266 def parse_command_build(self):
267 # Implement the "build" command.
268 sub_build = self.sub_commands.add_parser("build",
269 help=_("Build one or more packages."))
270 sub_build.add_argument("package", nargs=1,
271 help=_("Give name of at least one package to build."))
272 sub_build.add_argument("action", action="store_const", const="build")
273
274 sub_build.add_argument("-a", "--arch",
275 help=_("Build the package for the given architecture."))
276 sub_build.add_argument("--resultdir", nargs="?",
277 help=_("Path were the output files should be copied to."))
278
279 def parse_command_shell(self):
280 # Implement the "shell" command.
281 sub_shell = self.sub_commands.add_parser("shell",
282 help=_("Go into a shell."))
042266f3 283 sub_shell.add_argument("package", nargs="?",
47a4cb89
MT
284 help=_("Give name of a package."))
285 sub_shell.add_argument("action", action="store_const", const="shell")
286
287 sub_shell.add_argument("-a", "--arch",
288 help=_("Emulated architecture in the shell."))
289
290 def parse_command_dist(self):
291 # Implement the "dist" command.
292 sub_dist = self.sub_commands.add_parser("dist",
293 help=_("Generate a source package."))
e412b8dc
MT
294 sub_dist.add_argument("package", nargs="+",
295 help=_("Give name(s) of a package(s)."))
47a4cb89
MT
296 sub_dist.add_argument("action", action="store_const", const="dist")
297
298 sub_dist.add_argument("--resultdir", nargs="?",
299 help=_("Path were the output files should be copied to."))
300
9afa5620
MT
301 def handle_info(self):
302 Cli.handle_info(self, long=True)
303
47a4cb89 304 def handle_build(self):
47a4cb89
MT
305 # Get the package descriptor from the command line options
306 pkg = self.args.package[0]
307
308 # Check, if we got a regular file
309 if os.path.exists(pkg):
310 pkg = os.path.abspath(pkg)
311
47a4cb89 312 else:
7c8f2953 313 raise FileNotFoundError, pkg
47a4cb89 314
7c8f2953
MT
315 # Create distribution configuration from command line.
316 distro_config = {
317 "arch" : self.args.arch,
318 }
319
18edfe75 320 pakfire.build(pkg, distro_config=distro_config, resultdirs=[self.args.resultdir,],
dacaa18b 321 shell=True, **self.pakfire_args)
47a4cb89
MT
322
323 def handle_shell(self):
042266f3
MT
324 pkg = None
325
47a4cb89 326 # Get the package descriptor from the command line options
042266f3 327 if self.args.package:
ad1b844f 328 pkg = self.args.package
47a4cb89 329
7c8f2953
MT
330 # Check, if we got a regular file
331 if os.path.exists(pkg):
332 pkg = os.path.abspath(pkg)
47a4cb89 333
7c8f2953
MT
334 else:
335 raise FileNotFoundError, pkg
47a4cb89 336
7c8f2953
MT
337 # Create distribution configuration from command line.
338 distro_config = {
339 "arch" : self.args.arch,
340 }
47a4cb89 341
983f1a7d 342 pakfire.shell(pkg, distro_config=distro_config, **self.pakfire_args)
47a4cb89
MT
343
344 def handle_dist(self):
e412b8dc
MT
345 # Get the packages from the command line options
346 pkgs = []
47a4cb89 347
e412b8dc
MT
348 for pkg in self.args.package:
349 # Check, if we got a regular file
350 if os.path.exists(pkg):
351 pkg = os.path.abspath(pkg)
7c8f2953 352 pkgs.append(pkg)
47a4cb89 353
e412b8dc 354 else:
7c8f2953
MT
355 raise FileNotFoundError, pkg
356
6519843a
MT
357 pakfire.dist(pkgs, resultdirs=[self.args.resultdir,],
358 **self.pakfire_args)
47a4cb89 359
c605d735
MT
360 def handle_provides(self):
361 pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args)
362
363 for pkg in pkgs:
364 print pkg.dump(long=True)
365
47a4cb89 366
9613a111 367class CliRepo(Cli):
92806f47
MT
368 def __init__(self):
369 self.parser = argparse.ArgumentParser(
9613a111 370 description = _("Pakfire repo command line interface."),
92806f47
MT
371 )
372
373 self.parse_common_arguments()
374
375 # Add sub-commands.
376 self.sub_commands = self.parser.add_subparsers()
377
378 self.parse_command_repo()
379
380 # Finally parse all arguments from the command line and save them.
381 self.args = self.parser.parse_args()
382
92806f47
MT
383 self.action2func = {
384 "repo_create" : self.handle_repo_create,
385 }
386
387 def parse_command_repo(self):
388 sub_repo = self.sub_commands.add_parser("repo",
389 help=_("Repository management commands."))
390
391 sub_repo_commands = sub_repo.add_subparsers()
392
393 self.parse_command_repo_create(sub_repo_commands)
394
395 def parse_command_repo_create(self, sub_commands):
396 sub_create = sub_commands.add_parser("create",
397 help=_("Create a new repository index."))
398 sub_create.add_argument("path", nargs=1, help=_("Path to the packages."))
399 sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages."))
400 sub_create.add_argument("action", action="store_const", const="repo_create")
401
fa6d335b
MT
402 def handle_repo_create(self):
403 path = self.args.path[0]
404
7c8f2953 405 pakfire.repo_create(path, self.args.inputs, **self.pakfire_args)
9613a111
MT
406
407
408class CliMaster(Cli):
677ff42a
MT
409 def __init__(self):
410 self.parser = argparse.ArgumentParser(
411 description = _("Pakfire master command line interface."),
412 )
413
414 self.parse_common_arguments()
415
416 # Add sub-commands.
417 self.sub_commands = self.parser.add_subparsers()
418
419 self.parse_command_update()
420
421 # Finally parse all arguments from the command line and save them.
422 self.args = self.parser.parse_args()
423
7c8f2953 424 self.master = server.master.Master()
677ff42a
MT
425
426 self.action2func = {
427 "update" : self.handle_update,
428 }
429
430 def parse_command_update(self):
431 # Implement the "update" command.
432 sub_update = self.sub_commands.add_parser("update",
433 help=_("Update the sources."))
434 sub_update.add_argument("action", action="store_const", const="update")
435
436 def handle_update(self):
437 self.master.update_sources()
9613a111
MT
438
439
3ad4bb5a 440class CliServer(Cli):
677ff42a
MT
441 def __init__(self):
442 self.parser = argparse.ArgumentParser(
3ad4bb5a 443 description = _("Pakfire server command line interface."),
677ff42a
MT
444 )
445
446 self.parse_common_arguments()
447
448 # Add sub-commands.
449 self.sub_commands = self.parser.add_subparsers()
450
a52f536c 451 self.parse_command_build()
677ff42a
MT
452 self.parse_command_keepalive()
453
454 # Finally parse all arguments from the command line and save them.
455 self.args = self.parser.parse_args()
456
3ad4bb5a 457 self.server = server.Server()
677ff42a
MT
458
459 self.action2func = {
a52f536c 460 "build" : self.handle_build,
677ff42a
MT
461 "keepalive" : self.handle_keepalive,
462 }
463
a52f536c
MT
464 def parse_command_build(self):
465 # Implement the "build" command.
466 sub_keepalive = self.sub_commands.add_parser("build",
467 help=_("Request a build job from the server."))
468 sub_keepalive.add_argument("action", action="store_const", const="build")
469
677ff42a
MT
470 def parse_command_keepalive(self):
471 # Implement the "keepalive" command.
472 sub_keepalive = self.sub_commands.add_parser("keepalive",
473 help=_("Send a keepalive to the server."))
474 sub_keepalive.add_argument("action", action="store_const",
475 const="keepalive")
476
477 def handle_keepalive(self):
3ad4bb5a 478 self.server.update_info()
9613a111 479
a52f536c 480 def handle_build(self):
3ad4bb5a 481 self.server.build_job()