]>
Commit | Line | Data |
---|---|---|
47a4cb89 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import argparse | |
4 | import sys | |
5 | ||
6 | import packages | |
fa6d335b | 7 | import repository |
677ff42a | 8 | import server |
e9c20259 | 9 | import util |
47a4cb89 | 10 | |
18edfe75 | 11 | import pakfire.api as pakfire |
47a4cb89 MT |
12 | from constants import * |
13 | from i18n import _ | |
14 | ||
47a4cb89 | 15 | class 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): | |
f9a012a8 MT |
59 | ret = { "path" : self.args.instroot } |
60 | ||
61 | if hasattr(self.args, "disable_repo"): | |
62 | ret["disable_repos"] = self.args.disable_repo | |
63 | ||
64 | if hasattr(self.args, "enable_repo"): | |
65 | ret["enable_repos"] = self.args.enable_repo | |
66 | ||
67 | return ret | |
7c8f2953 | 68 | |
47a4cb89 MT |
69 | def parse_common_arguments(self): |
70 | self.parser.add_argument("-v", "--verbose", action="store_true", | |
71 | help=_("Enable verbose output.")) | |
72 | ||
73 | self.parser.add_argument("-c", "--config", nargs="?", | |
74 | help=_("Path to a configuration file to load.")) | |
75 | ||
f781b1ab MT |
76 | self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO", |
77 | help=_("Disable a repository temporarily.")) | |
78 | ||
f9a012a8 MT |
79 | self.parser.add_argument("--enabled-repo", nargs="*", metavar="REPO", |
80 | help=_("Enable a repository temporarily.")) | |
81 | ||
47a4cb89 MT |
82 | def parse_command_install(self): |
83 | # Implement the "install" command. | |
84 | sub_install = self.sub_commands.add_parser("install", | |
85 | help=_("Install one or more packages to the system.")) | |
86 | sub_install.add_argument("package", nargs="+", | |
87 | help=_("Give name of at least one package to install.")) | |
88 | sub_install.add_argument("action", action="store_const", const="install") | |
89 | ||
5e87fa4f MT |
90 | def parse_command_localinstall(self): |
91 | # Implement the "localinstall" command. | |
92 | sub_install = self.sub_commands.add_parser("localinstall", | |
93 | help=_("Install one or more packages from the filesystem.")) | |
94 | sub_install.add_argument("package", nargs="+", | |
95 | help=_("Give filename of at least one package.")) | |
96 | sub_install.add_argument("action", action="store_const", const="localinstall") | |
97 | ||
a39fd08b MT |
98 | def parse_command_remove(self): |
99 | # Implement the "remove" command. | |
100 | sub_remove = self.sub_commands.add_parser("remove", | |
101 | help=_("Remove one or more packages from the system.")) | |
102 | sub_remove.add_argument("package", nargs="+", | |
103 | help=_("Give name of at least one package to remove.")) | |
104 | sub_remove.add_argument("action", action="store_const", const="remove") | |
105 | ||
47a4cb89 MT |
106 | def parse_command_update(self): |
107 | # Implement the "update" command. | |
108 | sub_update = self.sub_commands.add_parser("update", | |
109 | help=_("Update the whole system or one specific package.")) | |
110 | sub_update.add_argument("package", nargs="*", | |
111 | help=_("Give a name of a package to update or leave emtpy for all.")) | |
112 | sub_update.add_argument("action", action="store_const", const="update") | |
113 | ||
114 | def parse_command_info(self): | |
115 | # Implement the "info" command. | |
116 | sub_info = self.sub_commands.add_parser("info", | |
117 | help=_("Print some information about the given package(s).")) | |
118 | sub_info.add_argument("package", nargs="+", | |
119 | help=_("Give at least the name of one package.")) | |
120 | sub_info.add_argument("action", action="store_const", const="info") | |
121 | ||
122 | def parse_command_search(self): | |
123 | # Implement the "search" command. | |
124 | sub_search = self.sub_commands.add_parser("search", | |
125 | help=_("Search for a given pattern.")) | |
126 | sub_search.add_argument("pattern", | |
127 | help=_("A pattern to search for.")) | |
128 | sub_search.add_argument("action", action="store_const", const="search") | |
129 | ||
fa6d335b MT |
130 | def parse_command_provides(self): |
131 | # Implement the "provides" command | |
132 | sub_provides = self.sub_commands.add_parser("provides", | |
133 | help=_("Get a list of packages that provide a given file or feature.")) | |
134 | sub_provides.add_argument("pattern", nargs="+", | |
135 | help=_("File or feature to search for.")) | |
136 | sub_provides.add_argument("action", action="store_const", const="provides") | |
137 | ||
c1962d40 MT |
138 | def parse_command_grouplist(self): |
139 | # Implement the "grouplist" command | |
140 | sub_grouplist = self.sub_commands.add_parser("grouplist", | |
141 | help=_("Get list of packages that belong to the given group.")) | |
142 | sub_grouplist.add_argument("group", nargs=1, | |
143 | help=_("Group name to search for.")) | |
144 | sub_grouplist.add_argument("action", action="store_const", const="grouplist") | |
145 | ||
ce2764c1 MT |
146 | def parse_command_groupinstall(self): |
147 | # Implement the "grouplist" command | |
148 | sub_groupinstall = self.sub_commands.add_parser("groupinstall", | |
149 | help=_("Install all packages that belong to the given group.")) | |
150 | sub_groupinstall.add_argument("group", nargs=1, | |
151 | help=_("Group name.")) | |
152 | sub_groupinstall.add_argument("action", action="store_const", const="groupinstall") | |
153 | ||
67bc4528 MT |
154 | def parse_command_repolist(self): |
155 | # Implement the "repolist" command | |
156 | sub_repolist = self.sub_commands.add_parser("repolist", | |
157 | help=_("List all currently enabled repositories.")) | |
158 | sub_repolist.add_argument("action", action="store_const", const="repolist") | |
ce2764c1 | 159 | |
47a4cb89 MT |
160 | def run(self): |
161 | action = self.args.action | |
162 | ||
163 | if not self.action2func.has_key(action): | |
164 | raise | |
165 | ||
166 | try: | |
167 | func = self.action2func[action] | |
168 | except KeyError: | |
169 | raise # XXX catch and return better error message | |
170 | ||
171 | return func() | |
172 | ||
9afa5620 | 173 | def handle_info(self, long=False): |
7c8f2953 | 174 | pkgs = pakfire.info(self.args.package, **self.pakfire_args) |
47a4cb89 | 175 | |
7c8f2953 MT |
176 | for pkg in pkgs: |
177 | print pkg.dump(long=long) | |
47a4cb89 MT |
178 | |
179 | def handle_search(self): | |
7c8f2953 | 180 | pkgs = pakfire.search(self.args.pattern, **self.pakfire_args) |
47a4cb89 MT |
181 | |
182 | for pkg in pkgs: | |
183 | print pkg.dump(short=True) | |
184 | ||
185 | def handle_update(self): | |
7c8f2953 | 186 | pakfire.update(self.args.package, **self.pakfire_args) |
47a4cb89 | 187 | |
e0b99370 MT |
188 | def handle_install(self): |
189 | pakfire.install(self.args.package, **self.pakfire_args) | |
5e87fa4f MT |
190 | |
191 | def handle_localinstall(self): | |
e0b99370 | 192 | pakfire.localinstall(self.args.package, **self.pakfire_args) |
5e87fa4f | 193 | |
a39fd08b MT |
194 | def handle_remove(self): |
195 | pakfire.remove(self.args.package, **self.pakfire_args) | |
196 | ||
fa6d335b | 197 | def handle_provides(self): |
7c8f2953 | 198 | pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args) |
fa6d335b MT |
199 | |
200 | for pkg in pkgs: | |
201 | print pkg.dump() | |
202 | ||
c1962d40 | 203 | def handle_grouplist(self): |
7c8f2953 | 204 | pkgs = pakfire.grouplist(self.args.group[0], **self.pakfire_args) |
c1962d40 MT |
205 | |
206 | for pkg in pkgs: | |
207 | print " * %s" % pkg | |
208 | ||
ce2764c1 | 209 | def handle_groupinstall(self): |
7c8f2953 | 210 | pakfire.groupinstall(self.args.group[0], **self.pakfire_args) |
ce2764c1 | 211 | |
67bc4528 | 212 | def handle_repolist(self): |
7c8f2953 | 213 | repos = pakfire.repo_list(**self.pakfire_args) |
67bc4528 | 214 | |
c605d735 | 215 | FORMAT = " %-20s %8s %12s %12s " |
67bc4528 | 216 | |
c605d735 | 217 | title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages")) |
67bc4528 MT |
218 | print title |
219 | print "=" * len(title) # spacing line | |
220 | ||
221 | for repo in repos: | |
222 | # Skip the installed repository. | |
223 | if repo.name == "installed": | |
224 | continue | |
225 | ||
c605d735 | 226 | print FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)) |
67bc4528 | 227 | |
47a4cb89 MT |
228 | |
229 | class CliBuilder(Cli): | |
230 | def __init__(self): | |
231 | self.parser = argparse.ArgumentParser( | |
232 | description = _("Pakfire builder command line interface."), | |
233 | ) | |
234 | ||
235 | self.parse_common_arguments() | |
236 | ||
237 | # Add sub-commands. | |
238 | self.sub_commands = self.parser.add_subparsers() | |
239 | ||
240 | self.parse_command_build() | |
241 | self.parse_command_dist() | |
242 | self.parse_command_info() | |
243 | self.parse_command_search() | |
244 | self.parse_command_shell() | |
245 | self.parse_command_update() | |
4fbd4216 | 246 | self.parse_command_provides() |
2c84aceb | 247 | self.parse_command_grouplist() |
67bc4528 | 248 | self.parse_command_repolist() |
47a4cb89 MT |
249 | |
250 | # Finally parse all arguments from the command line and save them. | |
251 | self.args = self.parser.parse_args() | |
252 | ||
47a4cb89 | 253 | self.action2func = { |
fa6d335b MT |
254 | "build" : self.handle_build, |
255 | "dist" : self.handle_dist, | |
256 | "update" : self.handle_update, | |
257 | "info" : self.handle_info, | |
258 | "search" : self.handle_search, | |
259 | "shell" : self.handle_shell, | |
4fbd4216 | 260 | "provides" : self.handle_provides, |
2c84aceb | 261 | "grouplist" : self.handle_grouplist, |
67bc4528 | 262 | "repolist" : self.handle_repolist, |
47a4cb89 MT |
263 | } |
264 | ||
7c8f2953 MT |
265 | @property |
266 | def pakfire_args(self): | |
f9a012a8 MT |
267 | ret = { "builder" : 1 } |
268 | ||
269 | if hasattr(self.args, "disable_repo"): | |
270 | ret["disable_repos"] = self.args.disable_repo | |
271 | ||
272 | if hasattr(self.args, "enable_repo"): | |
273 | ret["enable_repos"] = self.args.enable_repo | |
274 | ||
275 | return ret | |
7c8f2953 | 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 | |
c605d735 MT |
377 | def handle_provides(self): |
378 | pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args) | |
379 | ||
380 | for pkg in pkgs: | |
381 | print pkg.dump(long=True) | |
382 | ||
47a4cb89 | 383 | |
9613a111 | 384 | class CliRepo(Cli): |
92806f47 MT |
385 | def __init__(self): |
386 | self.parser = argparse.ArgumentParser( | |
9613a111 | 387 | description = _("Pakfire repo command line interface."), |
92806f47 MT |
388 | ) |
389 | ||
390 | self.parse_common_arguments() | |
391 | ||
392 | # Add sub-commands. | |
393 | self.sub_commands = self.parser.add_subparsers() | |
394 | ||
395 | self.parse_command_repo() | |
396 | ||
397 | # Finally parse all arguments from the command line and save them. | |
398 | self.args = self.parser.parse_args() | |
399 | ||
92806f47 MT |
400 | self.action2func = { |
401 | "repo_create" : self.handle_repo_create, | |
402 | } | |
403 | ||
404 | def parse_command_repo(self): | |
405 | sub_repo = self.sub_commands.add_parser("repo", | |
406 | help=_("Repository management commands.")) | |
407 | ||
408 | sub_repo_commands = sub_repo.add_subparsers() | |
409 | ||
410 | self.parse_command_repo_create(sub_repo_commands) | |
411 | ||
412 | def parse_command_repo_create(self, sub_commands): | |
413 | sub_create = sub_commands.add_parser("create", | |
414 | help=_("Create a new repository index.")) | |
415 | sub_create.add_argument("path", nargs=1, help=_("Path to the packages.")) | |
416 | sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages.")) | |
417 | sub_create.add_argument("action", action="store_const", const="repo_create") | |
418 | ||
fa6d335b MT |
419 | def handle_repo_create(self): |
420 | path = self.args.path[0] | |
421 | ||
7c8f2953 | 422 | pakfire.repo_create(path, self.args.inputs, **self.pakfire_args) |
9613a111 MT |
423 | |
424 | ||
425 | class CliMaster(Cli): | |
677ff42a MT |
426 | def __init__(self): |
427 | self.parser = argparse.ArgumentParser( | |
428 | description = _("Pakfire master command line interface."), | |
429 | ) | |
430 | ||
431 | self.parse_common_arguments() | |
432 | ||
433 | # Add sub-commands. | |
434 | self.sub_commands = self.parser.add_subparsers() | |
435 | ||
436 | self.parse_command_update() | |
437 | ||
438 | # Finally parse all arguments from the command line and save them. | |
439 | self.args = self.parser.parse_args() | |
440 | ||
7c8f2953 | 441 | self.master = server.master.Master() |
677ff42a MT |
442 | |
443 | self.action2func = { | |
444 | "update" : self.handle_update, | |
445 | } | |
446 | ||
447 | def parse_command_update(self): | |
448 | # Implement the "update" command. | |
449 | sub_update = self.sub_commands.add_parser("update", | |
450 | help=_("Update the sources.")) | |
451 | sub_update.add_argument("action", action="store_const", const="update") | |
452 | ||
453 | def handle_update(self): | |
454 | self.master.update_sources() | |
9613a111 MT |
455 | |
456 | ||
3ad4bb5a | 457 | class CliServer(Cli): |
677ff42a MT |
458 | def __init__(self): |
459 | self.parser = argparse.ArgumentParser( | |
3ad4bb5a | 460 | description = _("Pakfire server command line interface."), |
677ff42a MT |
461 | ) |
462 | ||
463 | self.parse_common_arguments() | |
464 | ||
465 | # Add sub-commands. | |
466 | self.sub_commands = self.parser.add_subparsers() | |
467 | ||
a52f536c | 468 | self.parse_command_build() |
677ff42a MT |
469 | self.parse_command_keepalive() |
470 | ||
471 | # Finally parse all arguments from the command line and save them. | |
472 | self.args = self.parser.parse_args() | |
473 | ||
3ad4bb5a | 474 | self.server = server.Server() |
677ff42a MT |
475 | |
476 | self.action2func = { | |
a52f536c | 477 | "build" : self.handle_build, |
677ff42a MT |
478 | "keepalive" : self.handle_keepalive, |
479 | } | |
480 | ||
a52f536c MT |
481 | def parse_command_build(self): |
482 | # Implement the "build" command. | |
483 | sub_keepalive = self.sub_commands.add_parser("build", | |
484 | help=_("Request a build job from the server.")) | |
485 | sub_keepalive.add_argument("action", action="store_const", const="build") | |
486 | ||
677ff42a MT |
487 | def parse_command_keepalive(self): |
488 | # Implement the "keepalive" command. | |
489 | sub_keepalive = self.sub_commands.add_parser("keepalive", | |
490 | help=_("Send a keepalive to the server.")) | |
491 | sub_keepalive.add_argument("action", action="store_const", | |
492 | const="keepalive") | |
493 | ||
494 | def handle_keepalive(self): | |
3ad4bb5a | 495 | self.server.update_info() |
9613a111 | 496 | |
a52f536c | 497 | def handle_build(self): |
3ad4bb5a | 498 | self.server.build_job() |