]>
Commit | Line | Data |
---|---|---|
47a4cb89 | 1 | #!/usr/bin/python |
b792d887 MT |
2 | ############################################################################### |
3 | # # | |
4 | # Pakfire - The IPFire package management system # | |
5 | # Copyright (C) 2011 Pakfire development team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
47a4cb89 MT |
21 | |
22 | import argparse | |
936f6b37 | 23 | import os |
47a4cb89 MT |
24 | import sys |
25 | ||
60845a36 | 26 | import logger |
47a4cb89 | 27 | import packages |
fa6d335b | 28 | import repository |
677ff42a | 29 | import server |
e9c20259 | 30 | import util |
47a4cb89 | 31 | |
18edfe75 | 32 | import pakfire.api as pakfire |
47a4cb89 MT |
33 | from constants import * |
34 | from i18n import _ | |
35 | ||
60845a36 MT |
36 | # Initialize a very simple logging that is removed when a Pakfire instance |
37 | # is started. | |
38 | logger.setup_logging() | |
39 | ||
47a4cb89 | 40 | class Cli(object): |
47a4cb89 MT |
41 | def __init__(self): |
42 | self.parser = argparse.ArgumentParser( | |
43 | description = _("Pakfire command line interface."), | |
44 | ) | |
45 | ||
46 | self.parse_common_arguments() | |
47 | ||
2ba449f0 | 48 | self.parser.add_argument("--root", metavar="PATH", |
d2e26956 | 49 | default="/", |
47a4cb89 MT |
50 | help=_("The path where pakfire should operate in.")) |
51 | ||
52 | # Add sub-commands. | |
53 | self.sub_commands = self.parser.add_subparsers() | |
54 | ||
55 | self.parse_command_install() | |
5e87fa4f | 56 | self.parse_command_localinstall() |
0ca71090 | 57 | self.parse_command_reinstall() |
a39fd08b | 58 | self.parse_command_remove() |
47a4cb89 MT |
59 | self.parse_command_info() |
60 | self.parse_command_search() | |
e38914be | 61 | self.parse_command_check_update() |
47a4cb89 | 62 | self.parse_command_update() |
67d1ddbd | 63 | self.parse_command_downgrade() |
fa6d335b | 64 | self.parse_command_provides() |
c1962d40 | 65 | self.parse_command_grouplist() |
ce2764c1 | 66 | self.parse_command_groupinstall() |
67bc4528 | 67 | self.parse_command_repolist() |
31267a64 | 68 | self.parse_command_clean() |
35d89fd7 | 69 | self.parse_command_check() |
b25a3d84 | 70 | self.parse_command_resolvdep() |
47a4cb89 MT |
71 | |
72 | # Finally parse all arguments from the command line and save them. | |
73 | self.args = self.parser.parse_args() | |
74 | ||
47a4cb89 | 75 | self.action2func = { |
5e87fa4f MT |
76 | "install" : self.handle_install, |
77 | "localinstall" : self.handle_localinstall, | |
0ca71090 | 78 | "reinstall" : self.handle_reinstall, |
a39fd08b | 79 | "remove" : self.handle_remove, |
e38914be | 80 | "check_update" : self.handle_check_update, |
5e87fa4f | 81 | "update" : self.handle_update, |
67d1ddbd | 82 | "downgrade" : self.handle_downgrade, |
5e87fa4f MT |
83 | "info" : self.handle_info, |
84 | "search" : self.handle_search, | |
fa6d335b | 85 | "provides" : self.handle_provides, |
c1962d40 | 86 | "grouplist" : self.handle_grouplist, |
ce2764c1 | 87 | "groupinstall" : self.handle_groupinstall, |
67bc4528 | 88 | "repolist" : self.handle_repolist, |
31267a64 | 89 | "clean_all" : self.handle_clean_all, |
35d89fd7 | 90 | "check" : self.handle_check, |
b25a3d84 | 91 | "resolvdep" : self.handle_resolvdep, |
47a4cb89 MT |
92 | } |
93 | ||
7c8f2953 MT |
94 | @property |
95 | def pakfire_args(self): | |
6557ff4c MT |
96 | ret = { "mode" : "normal" } |
97 | ||
2ba449f0 MT |
98 | if hasattr(self.args, "root"): |
99 | ret["path"] = self.args.root | |
f9a012a8 MT |
100 | |
101 | if hasattr(self.args, "disable_repo"): | |
102 | ret["disable_repos"] = self.args.disable_repo | |
103 | ||
104 | if hasattr(self.args, "enable_repo"): | |
105 | ret["enable_repos"] = self.args.enable_repo | |
106 | ||
6a509182 MT |
107 | if hasattr(self.args, "offline"): |
108 | ret["offline"] = self.args.offline | |
109 | ||
f9a012a8 | 110 | return ret |
7c8f2953 | 111 | |
47a4cb89 | 112 | def parse_common_arguments(self): |
50381f5c MT |
113 | self.parser.add_argument("--version", action="version", |
114 | version="%(prog)s " + PAKFIRE_VERSION) | |
115 | ||
47a4cb89 MT |
116 | self.parser.add_argument("-v", "--verbose", action="store_true", |
117 | help=_("Enable verbose output.")) | |
118 | ||
119 | self.parser.add_argument("-c", "--config", nargs="?", | |
120 | help=_("Path to a configuration file to load.")) | |
121 | ||
f781b1ab MT |
122 | self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO", |
123 | help=_("Disable a repository temporarily.")) | |
124 | ||
f9a012a8 MT |
125 | self.parser.add_argument("--enabled-repo", nargs="*", metavar="REPO", |
126 | help=_("Enable a repository temporarily.")) | |
127 | ||
6a509182 MT |
128 | self.parser.add_argument("--offline", action="store_true", |
129 | help=_("Run pakfire in offline mode.")) | |
130 | ||
47a4cb89 MT |
131 | def parse_command_install(self): |
132 | # Implement the "install" command. | |
133 | sub_install = self.sub_commands.add_parser("install", | |
134 | help=_("Install one or more packages to the system.")) | |
135 | sub_install.add_argument("package", nargs="+", | |
136 | help=_("Give name of at least one package to install.")) | |
137 | sub_install.add_argument("action", action="store_const", const="install") | |
138 | ||
5e87fa4f MT |
139 | def parse_command_localinstall(self): |
140 | # Implement the "localinstall" command. | |
141 | sub_install = self.sub_commands.add_parser("localinstall", | |
142 | help=_("Install one or more packages from the filesystem.")) | |
143 | sub_install.add_argument("package", nargs="+", | |
144 | help=_("Give filename of at least one package.")) | |
145 | sub_install.add_argument("action", action="store_const", const="localinstall") | |
146 | ||
0ca71090 MT |
147 | def parse_command_reinstall(self): |
148 | # Implement the "reinstall" command. | |
149 | sub_install = self.sub_commands.add_parser("reinstall", | |
150 | help=_("Reinstall one or more packages.")) | |
151 | sub_install.add_argument("package", nargs="+", | |
152 | help=_("Give name of at least one package to reinstall.")) | |
153 | sub_install.add_argument("action", action="store_const", const="reinstall") | |
154 | ||
a39fd08b MT |
155 | def parse_command_remove(self): |
156 | # Implement the "remove" command. | |
157 | sub_remove = self.sub_commands.add_parser("remove", | |
158 | help=_("Remove one or more packages from the system.")) | |
159 | sub_remove.add_argument("package", nargs="+", | |
160 | help=_("Give name of at least one package to remove.")) | |
161 | sub_remove.add_argument("action", action="store_const", const="remove") | |
162 | ||
05fb1da0 MT |
163 | @staticmethod |
164 | def _parse_command_update(parser): | |
165 | parser.add_argument("package", nargs="*", | |
166 | help=_("Give a name of a package to update or leave emtpy for all.")) | |
167 | parser.add_argument("--exclude", "-x", nargs="+", | |
168 | help=_("Exclude package from update.")) | |
169 | parser.add_argument("--allow-vendorchange", action="store_true", | |
170 | help=_("Allow changing the vendor of packages.")) | |
171 | parser.add_argument("--allow-archchange", action="store_true", | |
172 | help=_("Allow changing the architecture of packages.")) | |
173 | ||
47a4cb89 MT |
174 | def parse_command_update(self): |
175 | # Implement the "update" command. | |
176 | sub_update = self.sub_commands.add_parser("update", | |
177 | help=_("Update the whole system or one specific package.")) | |
47a4cb89 | 178 | sub_update.add_argument("action", action="store_const", const="update") |
05fb1da0 | 179 | self._parse_command_update(sub_update) |
47a4cb89 | 180 | |
e38914be MT |
181 | def parse_command_check_update(self): |
182 | # Implement the "check-update" command. | |
183 | sub_check_update = self.sub_commands.add_parser("check-update", | |
184 | help=_("Check, if there are any updates available.")) | |
e38914be | 185 | sub_check_update.add_argument("action", action="store_const", const="check_update") |
05fb1da0 | 186 | self._parse_command_update(sub_check_update) |
e38914be | 187 | |
67d1ddbd MT |
188 | def parse_command_downgrade(self): |
189 | # Implement the "downgrade" command. | |
190 | sub_downgrade = self.sub_commands.add_parser("downgrade", | |
191 | help=_("Downgrade one or more packages.")) | |
192 | sub_downgrade.add_argument("package", nargs="*", | |
193 | help=_("Give a name of a package to downgrade.")) | |
194 | sub_downgrade.add_argument("--allow-vendorchange", action="store_true", | |
195 | help=_("Allow changing the vendor of packages.")) | |
196 | sub_downgrade.add_argument("--allow-archchange", action="store_true", | |
197 | help=_("Allow changing the architecture of packages.")) | |
198 | sub_downgrade.add_argument("action", action="store_const", const="downgrade") | |
199 | ||
47a4cb89 MT |
200 | def parse_command_info(self): |
201 | # Implement the "info" command. | |
202 | sub_info = self.sub_commands.add_parser("info", | |
203 | help=_("Print some information about the given package(s).")) | |
204 | sub_info.add_argument("package", nargs="+", | |
205 | help=_("Give at least the name of one package.")) | |
206 | sub_info.add_argument("action", action="store_const", const="info") | |
207 | ||
208 | def parse_command_search(self): | |
209 | # Implement the "search" command. | |
210 | sub_search = self.sub_commands.add_parser("search", | |
211 | help=_("Search for a given pattern.")) | |
212 | sub_search.add_argument("pattern", | |
213 | help=_("A pattern to search for.")) | |
214 | sub_search.add_argument("action", action="store_const", const="search") | |
215 | ||
fa6d335b MT |
216 | def parse_command_provides(self): |
217 | # Implement the "provides" command | |
218 | sub_provides = self.sub_commands.add_parser("provides", | |
219 | help=_("Get a list of packages that provide a given file or feature.")) | |
220 | sub_provides.add_argument("pattern", nargs="+", | |
221 | help=_("File or feature to search for.")) | |
222 | sub_provides.add_argument("action", action="store_const", const="provides") | |
223 | ||
c1962d40 MT |
224 | def parse_command_grouplist(self): |
225 | # Implement the "grouplist" command | |
226 | sub_grouplist = self.sub_commands.add_parser("grouplist", | |
227 | help=_("Get list of packages that belong to the given group.")) | |
228 | sub_grouplist.add_argument("group", nargs=1, | |
229 | help=_("Group name to search for.")) | |
230 | sub_grouplist.add_argument("action", action="store_const", const="grouplist") | |
231 | ||
ce2764c1 MT |
232 | def parse_command_groupinstall(self): |
233 | # Implement the "grouplist" command | |
234 | sub_groupinstall = self.sub_commands.add_parser("groupinstall", | |
235 | help=_("Install all packages that belong to the given group.")) | |
236 | sub_groupinstall.add_argument("group", nargs=1, | |
237 | help=_("Group name.")) | |
238 | sub_groupinstall.add_argument("action", action="store_const", const="groupinstall") | |
239 | ||
67bc4528 MT |
240 | def parse_command_repolist(self): |
241 | # Implement the "repolist" command | |
242 | sub_repolist = self.sub_commands.add_parser("repolist", | |
243 | help=_("List all currently enabled repositories.")) | |
244 | sub_repolist.add_argument("action", action="store_const", const="repolist") | |
ce2764c1 | 245 | |
31267a64 MT |
246 | def parse_command_clean(self): |
247 | sub_clean = self.sub_commands.add_parser("clean", help=_("Cleanup commands.")) | |
248 | ||
249 | sub_clean_commands = sub_clean.add_subparsers() | |
250 | ||
251 | self.parse_command_clean_all(sub_clean_commands) | |
252 | ||
253 | def parse_command_clean_all(self, sub_commands): | |
254 | sub_create = sub_commands.add_parser("all", | |
255 | help=_("Cleanup all temporary files.")) | |
256 | sub_create.add_argument("action", action="store_const", const="clean_all") | |
257 | ||
35d89fd7 MT |
258 | def parse_command_check(self): |
259 | # Implement the "check" command | |
260 | sub_check = self.sub_commands.add_parser("check", | |
261 | help=_("Check the system for any errors.")) | |
262 | sub_check.add_argument("action", action="store_const", const="check") | |
263 | ||
b25a3d84 MT |
264 | def parse_command_resolvdep(self): |
265 | # Implement the "resolvdep" command. | |
266 | sub_resolvdep = self.sub_commands.add_parser("resolvdep", | |
267 | help=_("Check the dependencies for a particular package.")) | |
268 | sub_resolvdep.add_argument("package", nargs="+", | |
269 | help=_("Give name of at least one package to check.")) | |
270 | sub_resolvdep.add_argument("action", action="store_const", const="resolvdep") | |
271 | ||
47a4cb89 MT |
272 | def run(self): |
273 | action = self.args.action | |
274 | ||
275 | if not self.action2func.has_key(action): | |
276 | raise | |
277 | ||
278 | try: | |
279 | func = self.action2func[action] | |
280 | except KeyError: | |
281 | raise # XXX catch and return better error message | |
282 | ||
283 | return func() | |
284 | ||
9afa5620 | 285 | def handle_info(self, long=False): |
7c8f2953 | 286 | pkgs = pakfire.info(self.args.package, **self.pakfire_args) |
47a4cb89 | 287 | |
7c8f2953 MT |
288 | for pkg in pkgs: |
289 | print pkg.dump(long=long) | |
47a4cb89 MT |
290 | |
291 | def handle_search(self): | |
7c8f2953 | 292 | pkgs = pakfire.search(self.args.pattern, **self.pakfire_args) |
47a4cb89 MT |
293 | |
294 | for pkg in pkgs: | |
295 | print pkg.dump(short=True) | |
296 | ||
05fb1da0 MT |
297 | def handle_update(self, **args): |
298 | args.update(self.pakfire_args) | |
299 | ||
300 | pakfire.update(self.args.package, excludes=self.args.exclude, | |
301 | allow_vendorchange=self.args.allow_vendorchange, | |
302 | allow_archchange=self.args.allow_archchange, | |
303 | **args) | |
47a4cb89 | 304 | |
e38914be | 305 | def handle_check_update(self): |
05fb1da0 | 306 | self.handle_update(check=True) |
e38914be | 307 | |
67d1ddbd MT |
308 | def handle_downgrade(self, **args): |
309 | args.update(self.pakfire_args) | |
310 | ||
311 | pakfire.downgrade(self.args.package, | |
312 | allow_vendorchange=self.args.allow_vendorchange, | |
313 | allow_archchange=self.args.allow_archchange, | |
314 | **args) | |
315 | ||
e0b99370 MT |
316 | def handle_install(self): |
317 | pakfire.install(self.args.package, **self.pakfire_args) | |
5e87fa4f MT |
318 | |
319 | def handle_localinstall(self): | |
e0b99370 | 320 | pakfire.localinstall(self.args.package, **self.pakfire_args) |
5e87fa4f | 321 | |
0ca71090 MT |
322 | def handle_reinstall(self): |
323 | pakfire.reinstall(self.args.package, **self.pakfire_args) | |
324 | ||
a39fd08b MT |
325 | def handle_remove(self): |
326 | pakfire.remove(self.args.package, **self.pakfire_args) | |
327 | ||
fa6d335b | 328 | def handle_provides(self): |
7c8f2953 | 329 | pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args) |
fa6d335b MT |
330 | |
331 | for pkg in pkgs: | |
332 | print pkg.dump() | |
333 | ||
c1962d40 | 334 | def handle_grouplist(self): |
7c8f2953 | 335 | pkgs = pakfire.grouplist(self.args.group[0], **self.pakfire_args) |
c1962d40 MT |
336 | |
337 | for pkg in pkgs: | |
338 | print " * %s" % pkg | |
339 | ||
ce2764c1 | 340 | def handle_groupinstall(self): |
7c8f2953 | 341 | pakfire.groupinstall(self.args.group[0], **self.pakfire_args) |
ce2764c1 | 342 | |
67bc4528 | 343 | def handle_repolist(self): |
7c8f2953 | 344 | repos = pakfire.repo_list(**self.pakfire_args) |
67bc4528 | 345 | |
c605d735 | 346 | FORMAT = " %-20s %8s %12s %12s " |
67bc4528 | 347 | |
c605d735 | 348 | title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"), _("Packages")) |
67bc4528 MT |
349 | print title |
350 | print "=" * len(title) # spacing line | |
351 | ||
352 | for repo in repos: | |
353 | # Skip the installed repository. | |
354 | if repo.name == "installed": | |
355 | continue | |
356 | ||
c605d735 | 357 | print FORMAT % (repo.name, repo.enabled, repo.priority, len(repo)) |
67bc4528 | 358 | |
31267a64 MT |
359 | def handle_clean_all(self): |
360 | print _("Cleaning up everything...") | |
361 | ||
362 | pakfire.clean_all(**self.pakfire_args) | |
363 | ||
35d89fd7 MT |
364 | def handle_check(self): |
365 | pakfire.check(**self.pakfire_args) | |
366 | ||
b25a3d84 MT |
367 | def handle_resolvdep(self): |
368 | pakfire.resolvdep(self.args.package, **self.pakfire_args) | |
369 | ||
47a4cb89 MT |
370 | |
371 | class CliBuilder(Cli): | |
372 | def __init__(self): | |
936f6b37 MT |
373 | # Check if we are already running in a pakfire container. In that |
374 | # case, we cannot start another pakfire-builder. | |
375 | if os.environ.get("container", None) == "pakfire-builder": | |
376 | raise PakfireContainerError, _("You cannot run pakfire-builder in a pakfire chroot.") | |
377 | ||
47a4cb89 MT |
378 | self.parser = argparse.ArgumentParser( |
379 | description = _("Pakfire builder command line interface."), | |
380 | ) | |
381 | ||
382 | self.parse_common_arguments() | |
383 | ||
384 | # Add sub-commands. | |
385 | self.sub_commands = self.parser.add_subparsers() | |
386 | ||
387 | self.parse_command_build() | |
388 | self.parse_command_dist() | |
389 | self.parse_command_info() | |
390 | self.parse_command_search() | |
391 | self.parse_command_shell() | |
392 | self.parse_command_update() | |
4fbd4216 | 393 | self.parse_command_provides() |
2c84aceb | 394 | self.parse_command_grouplist() |
67bc4528 | 395 | self.parse_command_repolist() |
31267a64 | 396 | self.parse_command_clean() |
b25a3d84 | 397 | self.parse_command_resolvdep() |
3817ae8e | 398 | self.parse_command_cache() |
47a4cb89 MT |
399 | |
400 | # Finally parse all arguments from the command line and save them. | |
401 | self.args = self.parser.parse_args() | |
402 | ||
47a4cb89 | 403 | self.action2func = { |
fa6d335b MT |
404 | "build" : self.handle_build, |
405 | "dist" : self.handle_dist, | |
406 | "update" : self.handle_update, | |
407 | "info" : self.handle_info, | |
408 | "search" : self.handle_search, | |
409 | "shell" : self.handle_shell, | |
4fbd4216 | 410 | "provides" : self.handle_provides, |
2c84aceb | 411 | "grouplist" : self.handle_grouplist, |
67bc4528 | 412 | "repolist" : self.handle_repolist, |
31267a64 | 413 | "clean_all" : self.handle_clean_all, |
b25a3d84 | 414 | "resolvdep" : self.handle_resolvdep, |
3817ae8e MT |
415 | "cache_create": self.handle_cache_create, |
416 | "cache_cleanup": self.handle_cache_cleanup, | |
47a4cb89 MT |
417 | } |
418 | ||
7c8f2953 MT |
419 | @property |
420 | def pakfire_args(self): | |
6557ff4c | 421 | ret = { "mode" : "builder" } |
f9a012a8 MT |
422 | |
423 | if hasattr(self.args, "disable_repo"): | |
424 | ret["disable_repos"] = self.args.disable_repo | |
425 | ||
426 | if hasattr(self.args, "enable_repo"): | |
427 | ret["enable_repos"] = self.args.enable_repo | |
428 | ||
6a509182 MT |
429 | if hasattr(self.args, "offline"): |
430 | ret["offline"] = self.args.offline | |
431 | ||
f9a012a8 | 432 | return ret |
7c8f2953 | 433 | |
47a4cb89 MT |
434 | def parse_command_update(self): |
435 | # Implement the "update" command. | |
436 | sub_update = self.sub_commands.add_parser("update", | |
437 | help=_("Update the package indexes.")) | |
438 | sub_update.add_argument("action", action="store_const", const="update") | |
439 | ||
440 | def parse_command_build(self): | |
441 | # Implement the "build" command. | |
442 | sub_build = self.sub_commands.add_parser("build", | |
443 | help=_("Build one or more packages.")) | |
444 | sub_build.add_argument("package", nargs=1, | |
445 | help=_("Give name of at least one package to build.")) | |
446 | sub_build.add_argument("action", action="store_const", const="build") | |
447 | ||
448 | sub_build.add_argument("-a", "--arch", | |
449 | help=_("Build the package for the given architecture.")) | |
450 | sub_build.add_argument("--resultdir", nargs="?", | |
451 | help=_("Path were the output files should be copied to.")) | |
f22069bb MT |
452 | sub_build.add_argument("-m", "--mode", nargs="?", default="development", |
453 | help=_("Mode to run in. Is either 'release' or 'development' (default).")) | |
1710ccec MT |
454 | sub_build.add_argument("--after-shell", action="store_true", |
455 | help=_("Run a shell after a successful build.")) | |
47a4cb89 MT |
456 | |
457 | def parse_command_shell(self): | |
458 | # Implement the "shell" command. | |
459 | sub_shell = self.sub_commands.add_parser("shell", | |
460 | help=_("Go into a shell.")) | |
042266f3 | 461 | sub_shell.add_argument("package", nargs="?", |
47a4cb89 MT |
462 | help=_("Give name of a package.")) |
463 | sub_shell.add_argument("action", action="store_const", const="shell") | |
464 | ||
465 | sub_shell.add_argument("-a", "--arch", | |
466 | help=_("Emulated architecture in the shell.")) | |
6ee3d6b9 MT |
467 | sub_shell.add_argument("-m", "--mode", nargs="?", default="development", |
468 | help=_("Mode to run in. Is either 'release' or 'development' (default).")) | |
47a4cb89 MT |
469 | |
470 | def parse_command_dist(self): | |
471 | # Implement the "dist" command. | |
472 | sub_dist = self.sub_commands.add_parser("dist", | |
473 | help=_("Generate a source package.")) | |
e412b8dc MT |
474 | sub_dist.add_argument("package", nargs="+", |
475 | help=_("Give name(s) of a package(s).")) | |
47a4cb89 MT |
476 | sub_dist.add_argument("action", action="store_const", const="dist") |
477 | ||
478 | sub_dist.add_argument("--resultdir", nargs="?", | |
479 | help=_("Path were the output files should be copied to.")) | |
480 | ||
3817ae8e MT |
481 | def parse_command_cache(self): |
482 | # Implement the "cache" command. | |
483 | sub_cache = self.sub_commands.add_parser("cache", | |
484 | help=_("Create a build environment cache.")) | |
485 | ||
486 | # Implement subcommands. | |
487 | sub_cache_commands = sub_cache.add_subparsers() | |
488 | ||
489 | self.parse_command_cache_create(sub_cache_commands) | |
490 | self.parse_command_cache_cleanup(sub_cache_commands) | |
491 | ||
492 | def parse_command_cache_create(self, sub_commands): | |
493 | sub_create = sub_commands.add_parser("create", | |
494 | help=_("Create a new build environment cache.")) | |
495 | sub_create.add_argument("action", action="store_const", const="cache_create") | |
496 | ||
497 | def parse_command_cache_cleanup(self, sub_commands): | |
498 | sub_cleanup = sub_commands.add_parser("cleanup", | |
499 | help=_("Remove all cached build environments.")) | |
500 | sub_cleanup.add_argument("action", action="store_const", const="cache_cleanup") | |
501 | ||
9afa5620 MT |
502 | def handle_info(self): |
503 | Cli.handle_info(self, long=True) | |
504 | ||
47a4cb89 | 505 | def handle_build(self): |
47a4cb89 MT |
506 | # Get the package descriptor from the command line options |
507 | pkg = self.args.package[0] | |
508 | ||
509 | # Check, if we got a regular file | |
510 | if os.path.exists(pkg): | |
511 | pkg = os.path.abspath(pkg) | |
512 | ||
47a4cb89 | 513 | else: |
7c8f2953 | 514 | raise FileNotFoundError, pkg |
47a4cb89 | 515 | |
7c8f2953 MT |
516 | # Create distribution configuration from command line. |
517 | distro_config = { | |
518 | "arch" : self.args.arch, | |
519 | } | |
520 | ||
c07a3ca7 MT |
521 | pakfire.build(pkg, builder_mode=self.args.mode, |
522 | distro_config=distro_config, resultdirs=[self.args.resultdir,], | |
1710ccec | 523 | shell=True, after_shell=self.args.after_shell, **self.pakfire_args) |
47a4cb89 MT |
524 | |
525 | def handle_shell(self): | |
042266f3 MT |
526 | pkg = None |
527 | ||
47a4cb89 | 528 | # Get the package descriptor from the command line options |
042266f3 | 529 | if self.args.package: |
ad1b844f | 530 | pkg = self.args.package |
47a4cb89 | 531 | |
7c8f2953 MT |
532 | # Check, if we got a regular file |
533 | if os.path.exists(pkg): | |
534 | pkg = os.path.abspath(pkg) | |
47a4cb89 | 535 | |
7c8f2953 MT |
536 | else: |
537 | raise FileNotFoundError, pkg | |
47a4cb89 | 538 | |
7c8f2953 MT |
539 | # Create distribution configuration from command line. |
540 | distro_config = { | |
541 | "arch" : self.args.arch, | |
542 | } | |
47a4cb89 | 543 | |
6ee3d6b9 MT |
544 | pakfire.shell(pkg, builder_mode=self.args.mode, |
545 | distro_config=distro_config, **self.pakfire_args) | |
47a4cb89 MT |
546 | |
547 | def handle_dist(self): | |
e412b8dc MT |
548 | # Get the packages from the command line options |
549 | pkgs = [] | |
47a4cb89 | 550 | |
e412b8dc MT |
551 | for pkg in self.args.package: |
552 | # Check, if we got a regular file | |
553 | if os.path.exists(pkg): | |
554 | pkg = os.path.abspath(pkg) | |
7c8f2953 | 555 | pkgs.append(pkg) |
47a4cb89 | 556 | |
e412b8dc | 557 | else: |
7c8f2953 MT |
558 | raise FileNotFoundError, pkg |
559 | ||
6519843a MT |
560 | pakfire.dist(pkgs, resultdirs=[self.args.resultdir,], |
561 | **self.pakfire_args) | |
47a4cb89 | 562 | |
c605d735 MT |
563 | def handle_provides(self): |
564 | pkgs = pakfire.provides(self.args.pattern, **self.pakfire_args) | |
565 | ||
566 | for pkg in pkgs: | |
567 | print pkg.dump(long=True) | |
568 | ||
3817ae8e MT |
569 | def handle_cache_create(self): |
570 | pakfire.cache_create(**self.pakfire_args) | |
571 | ||
572 | def handle_cache_cleanup(self): | |
573 | for env in os.listdir(CACHE_ENVIRON_DIR): | |
574 | if not env.endswith(".cache"): | |
575 | continue | |
576 | ||
577 | print _("Removing environment cache file: %s..." % env) | |
578 | env = os.path.join(CACHE_ENVIRON_DIR, env) | |
579 | ||
580 | try: | |
581 | os.unlink(env) | |
582 | except OSError: | |
583 | print _("Could not remove file: %s") % env | |
584 | ||
47a4cb89 | 585 | |
3ad4bb5a | 586 | class CliServer(Cli): |
677ff42a MT |
587 | def __init__(self): |
588 | self.parser = argparse.ArgumentParser( | |
3ad4bb5a | 589 | description = _("Pakfire server command line interface."), |
677ff42a MT |
590 | ) |
591 | ||
592 | self.parse_common_arguments() | |
593 | ||
594 | # Add sub-commands. | |
595 | self.sub_commands = self.parser.add_subparsers() | |
596 | ||
a52f536c | 597 | self.parse_command_build() |
677ff42a | 598 | self.parse_command_keepalive() |
8276111d | 599 | self.parse_command_repoupdate() |
df9c4f62 | 600 | self.parse_command_repo() |
aad6f600 | 601 | self.parse_command_info() |
677ff42a MT |
602 | |
603 | # Finally parse all arguments from the command line and save them. | |
604 | self.args = self.parser.parse_args() | |
605 | ||
269c59f3 | 606 | self.server = server.Server(**self.pakfire_args) |
677ff42a MT |
607 | |
608 | self.action2func = { | |
8276111d | 609 | "build" : self.handle_build, |
aad6f600 | 610 | "info" : self.handle_info, |
8276111d MT |
611 | "keepalive" : self.handle_keepalive, |
612 | "repoupdate" : self.handle_repoupdate, | |
df9c4f62 | 613 | "repo_create": self.handle_repo_create, |
677ff42a MT |
614 | } |
615 | ||
6557ff4c MT |
616 | @property |
617 | def pakfire_args(self): | |
618 | ret = { "mode" : "server" } | |
619 | ||
6a509182 MT |
620 | if hasattr(self.args, "offline"): |
621 | ret["offline"] = self.args.offline | |
622 | ||
6557ff4c MT |
623 | return ret |
624 | ||
a52f536c MT |
625 | def parse_command_build(self): |
626 | # Implement the "build" command. | |
627 | sub_keepalive = self.sub_commands.add_parser("build", | |
628 | help=_("Request a build job from the server.")) | |
629 | sub_keepalive.add_argument("action", action="store_const", const="build") | |
630 | ||
677ff42a MT |
631 | def parse_command_keepalive(self): |
632 | # Implement the "keepalive" command. | |
633 | sub_keepalive = self.sub_commands.add_parser("keepalive", | |
634 | help=_("Send a keepalive to the server.")) | |
635 | sub_keepalive.add_argument("action", action="store_const", | |
636 | const="keepalive") | |
637 | ||
8276111d MT |
638 | def parse_command_repoupdate(self): |
639 | # Implement the "repoupdate" command. | |
640 | sub_repoupdate = self.sub_commands.add_parser("repoupdate", | |
641 | help=_("Update all repositories.")) | |
642 | sub_repoupdate.add_argument("action", action="store_const", | |
643 | const="repoupdate") | |
644 | ||
df9c4f62 MT |
645 | def parse_command_repo(self): |
646 | sub_repo = self.sub_commands.add_parser("repo", | |
647 | help=_("Repository management commands.")) | |
648 | ||
649 | sub_repo_commands = sub_repo.add_subparsers() | |
650 | ||
651 | self.parse_command_repo_create(sub_repo_commands) | |
652 | ||
653 | def parse_command_repo_create(self, sub_commands): | |
654 | sub_create = sub_commands.add_parser("create", | |
655 | help=_("Create a new repository index.")) | |
656 | sub_create.add_argument("path", nargs=1, help=_("Path to the packages.")) | |
657 | sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages.")) | |
658 | sub_create.add_argument("action", action="store_const", const="repo_create") | |
659 | ||
aad6f600 MT |
660 | def parse_command_info(self): |
661 | sub_info = self.sub_commands.add_parser("info", | |
662 | help=_("Dump some information about this machine.")) | |
663 | sub_info.add_argument("action", action="store_const", const="info") | |
664 | ||
677ff42a | 665 | def handle_keepalive(self): |
3ad4bb5a | 666 | self.server.update_info() |
9613a111 | 667 | |
a52f536c | 668 | def handle_build(self): |
3ad4bb5a | 669 | self.server.build_job() |
8276111d MT |
670 | |
671 | def handle_repoupdate(self): | |
672 | self.server.update_repositories() | |
df9c4f62 MT |
673 | |
674 | def handle_repo_create(self): | |
675 | path = self.args.path[0] | |
676 | ||
677 | pakfire.repo_create(path, self.args.inputs, **self.pakfire_args) | |
c07a3ca7 | 678 | |
aad6f600 MT |
679 | def handle_info(self): |
680 | info = self.server.info() | |
681 | ||
682 | print "\n".join(info) | |
683 | ||
c07a3ca7 | 684 | |
9b875540 | 685 | class CliBuilderIntern(Cli): |
c07a3ca7 MT |
686 | def __init__(self): |
687 | self.parser = argparse.ArgumentParser( | |
688 | description = _("Pakfire builder command line interface."), | |
689 | ) | |
690 | ||
691 | self.parse_common_arguments() | |
692 | ||
693 | # Add sub-commands. | |
694 | self.sub_commands = self.parser.add_subparsers() | |
695 | ||
696 | self.parse_command_build() | |
697 | ||
698 | # Finally parse all arguments from the command line and save them. | |
699 | self.args = self.parser.parse_args() | |
700 | ||
701 | self.action2func = { | |
702 | "build" : self.handle_build, | |
703 | } | |
704 | ||
705 | def parse_command_build(self): | |
706 | # Implement the "build" command. | |
707 | sub_build = self.sub_commands.add_parser("build", | |
708 | help=_("Build one or more packages.")) | |
709 | sub_build.add_argument("package", nargs=1, | |
710 | help=_("Give name of at least one package to build.")) | |
711 | sub_build.add_argument("action", action="store_const", const="build") | |
712 | ||
713 | sub_build.add_argument("-a", "--arch", | |
714 | help=_("Build the package for the given architecture.")) | |
715 | sub_build.add_argument("--resultdir", nargs="?", | |
716 | help=_("Path were the output files should be copied to.")) | |
717 | sub_build.add_argument("-m", "--mode", nargs="?", default="development", | |
718 | help=_("Mode to run in. Is either 'release' or 'development' (default).")) | |
719 | sub_build.add_argument("--nodeps", action="store_true", | |
720 | help=_("Do not verify build dependencies.")) | |
721 | ||
722 | def handle_build(self): | |
723 | # Get the package descriptor from the command line options | |
724 | pkg = self.args.package[0] | |
725 | ||
726 | # Check, if we got a regular file | |
727 | if os.path.exists(pkg): | |
728 | pkg = os.path.abspath(pkg) | |
729 | else: | |
730 | raise FileNotFoundError, pkg | |
731 | ||
732 | # Create distribution configuration from command line. | |
733 | distro_config = { | |
734 | "arch" : self.args.arch, | |
735 | } | |
736 | ||
737 | pakfire._build(pkg, builder_mode=self.args.mode, | |
738 | distro_config=distro_config, resultdir=self.args.resultdir, | |
739 | nodeps=self.args.nodeps, **self.pakfire_args) |