]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | import argparse | |
4 | import sys | |
5 | ||
6 | import packages | |
7 | import repository | |
8 | import server | |
9 | import util | |
10 | ||
11 | import pakfire.api as pakfire | |
12 | from constants import * | |
13 | from i18n import _ | |
14 | ||
15 | class Cli(object): | |
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", | |
24 | default="/", | |
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() | |
31 | self.parse_command_localinstall() | |
32 | self.parse_command_remove() | |
33 | self.parse_command_info() | |
34 | self.parse_command_search() | |
35 | self.parse_command_update() | |
36 | self.parse_command_provides() | |
37 | self.parse_command_requires() | |
38 | self.parse_command_grouplist() | |
39 | self.parse_command_groupinstall() | |
40 | self.parse_command_repolist() | |
41 | ||
42 | # Finally parse all arguments from the command line and save them. | |
43 | self.args = self.parser.parse_args() | |
44 | ||
45 | self.action2func = { | |
46 | "install" : self.handle_install, | |
47 | "localinstall" : self.handle_localinstall, | |
48 | "remove" : self.handle_remove, | |
49 | "update" : self.handle_update, | |
50 | "info" : self.handle_info, | |
51 | "search" : self.handle_search, | |
52 | "provides" : self.handle_provides, | |
53 | "requires" : self.handle_requires, | |
54 | "grouplist" : self.handle_grouplist, | |
55 | "groupinstall" : self.handle_groupinstall, | |
56 | "repolist" : self.handle_repolist, | |
57 | } | |
58 | ||
59 | @property | |
60 | def pakfire_args(self): | |
61 | return {} | |
62 | ||
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 | ||
70 | self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO", | |
71 | help=_("Disable a repository temporarily.")) | |
72 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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") | |
158 | ||
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 | ||
172 | def handle_info(self, long=False): | |
173 | pkgs = pakfire.info(self.args.package, **self.pakfire_args) | |
174 | ||
175 | for pkg in pkgs: | |
176 | print pkg.dump(long=long) | |
177 | ||
178 | def handle_search(self): | |
179 | pkgs = pakfire.search(self.args.pattern, **self.pakfire_args) | |
180 | ||
181 | for pkg in pkgs: | |
182 | print pkg.dump(short=True) | |
183 | ||
184 | def handle_update(self): | |
185 | pakfire.update(self.args.package, **self.pakfire_args) | |
186 | ||
187 | def handle_install(self): | |
188 | pakfire.install(self.args.package, **self.pakfire_args) | |
189 | ||
190 | def handle_localinstall(self): | |
191 | pakfire.localinstall(self.args.package, **self.pakfire_args) | |
192 | ||
193 | def handle_remove(self): | |
194 | pakfire.remove(self.args.package, **self.pakfire_args) | |
195 | ||
196 | def handle_provides(self): | |
197 | pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args) | |
198 | ||
199 | for pkg in pkgs: | |
200 | print pkg.dump() | |
201 | ||
202 | def handle_requires(self): | |
203 | pkgs = pakfire.requires(self.args.pattern, **self.pakfire_args) | |
204 | ||
205 | for pkg in pkgs: | |
206 | print pkg.dump() | |
207 | ||
208 | def handle_grouplist(self): | |
209 | pkgs = pakfire.grouplist(self.args.group[0], **self.pakfire_args) | |
210 | ||
211 | for pkg in pkgs: | |
212 | print " * %s" % pkg | |
213 | ||
214 | def handle_groupinstall(self): | |
215 | pakfire.groupinstall(self.args.group[0], **self.pakfire_args) | |
216 | ||
217 | def handle_repolist(self): | |
218 | repos = pakfire.repo_list(**self.pakfire_args) | |
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 | ||
234 | ||
235 | class 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() | |
252 | self.parse_command_provides() | |
253 | self.parse_command_requires() | |
254 | self.parse_command_grouplist() | |
255 | self.parse_command_repolist() | |
256 | ||
257 | # Finally parse all arguments from the command line and save them. | |
258 | self.args = self.parser.parse_args() | |
259 | ||
260 | self.action2func = { | |
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, | |
267 | "provides" : self.handle_provides, | |
268 | "requires" : self.handle_requires, | |
269 | "grouplist" : self.handle_grouplist, | |
270 | "repolist" : self.handle_repolist, | |
271 | } | |
272 | ||
273 | @property | |
274 | def pakfire_args(self): | |
275 | return { "builder" : 1 } | |
276 | ||
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.")) | |
300 | sub_shell.add_argument("package", nargs="?", | |
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.")) | |
311 | sub_dist.add_argument("package", nargs="+", | |
312 | help=_("Give name(s) of a package(s).")) | |
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 | ||
318 | def handle_info(self): | |
319 | Cli.handle_info(self, long=True) | |
320 | ||
321 | def handle_build(self): | |
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 | ||
329 | else: | |
330 | raise FileNotFoundError, pkg | |
331 | ||
332 | # Create distribution configuration from command line. | |
333 | distro_config = { | |
334 | "arch" : self.args.arch, | |
335 | } | |
336 | ||
337 | pakfire.build(pkg, distro_config=distro_config, resultdirs=[self.args.resultdir,], | |
338 | shell=True, **self.pakfire_args) | |
339 | ||
340 | def handle_shell(self): | |
341 | pkg = None | |
342 | ||
343 | # Get the package descriptor from the command line options | |
344 | if self.args.package: | |
345 | pkg = self.args.package | |
346 | ||
347 | # Check, if we got a regular file | |
348 | if os.path.exists(pkg): | |
349 | pkg = os.path.abspath(pkg) | |
350 | ||
351 | else: | |
352 | raise FileNotFoundError, pkg | |
353 | ||
354 | # Create distribution configuration from command line. | |
355 | distro_config = { | |
356 | "arch" : self.args.arch, | |
357 | } | |
358 | ||
359 | pakfire.shell(pkg, distro_config=distro_config, **self.pakfire_args) | |
360 | ||
361 | def handle_dist(self): | |
362 | # Get the packages from the command line options | |
363 | pkgs = [] | |
364 | ||
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) | |
369 | pkgs.append(pkg) | |
370 | ||
371 | else: | |
372 | raise FileNotFoundError, pkg | |
373 | ||
374 | pakfire.dist(pkgs, resultdirs=[self.args.resultdir,], | |
375 | **self.pakfire_args) | |
376 | ||
377 | ||
378 | class CliRepo(Cli): | |
379 | def __init__(self): | |
380 | self.parser = argparse.ArgumentParser( | |
381 | description = _("Pakfire repo command line interface."), | |
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 | ||
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 | ||
413 | def handle_repo_create(self): | |
414 | path = self.args.path[0] | |
415 | ||
416 | pakfire.repo_create(path, self.args.inputs, **self.pakfire_args) | |
417 | ||
418 | ||
419 | class CliMaster(Cli): | |
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 | ||
435 | self.master = server.master.Master() | |
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() | |
449 | ||
450 | ||
451 | class CliSlave(Cli): | |
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 | ||
462 | self.parse_command_build() | |
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 | ||
468 | self.slave = server.slave.Slave() | |
469 | ||
470 | self.action2func = { | |
471 | "build" : self.handle_build, | |
472 | "keepalive" : self.handle_keepalive, | |
473 | } | |
474 | ||
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 | ||
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() | |
490 | ||
491 | def handle_build(self): | |
492 | self.slave.build_job() |