]> git.ipfire.org Git - pakfire.git/blame - pakfire/cli.py
Add repolist command.
[pakfire.git] / pakfire / cli.py
CommitLineData
47a4cb89
MT
1#!/usr/bin/python
2
3import argparse
4import sys
5
6import packages
fa6d335b 7import repository
e9c20259 8import util
47a4cb89
MT
9
10from pakfire import Pakfire
11
12from constants import *
13from i18n import _
14
15def ask_user(question):
16 """
17 Ask the user the question, he or she can answer with yes or no.
18
19 This function returns True for "yes" and False for "no".
20
21 If the software is running in a non-inteactive shell, no question
22 is asked at all and the answer is always "yes".
23 """
e9c20259 24 if not util.cli_is_interactive():
47a4cb89
MT
25 return True
26
27 print _("%s [y/N]") % question,
28 ret = raw_input()
29
30 return ret in ("y", "Y")
31
32
33class Cli(object):
47a4cb89
MT
34 def __init__(self):
35 self.parser = argparse.ArgumentParser(
36 description = _("Pakfire command line interface."),
37 )
38
39 self.parse_common_arguments()
40
41 self.parser.add_argument("--instroot", metavar="PATH",
d2e26956 42 default="/",
47a4cb89
MT
43 help=_("The path where pakfire should operate in."))
44
45 # Add sub-commands.
46 self.sub_commands = self.parser.add_subparsers()
47
48 self.parse_command_install()
5e87fa4f 49 self.parse_command_localinstall()
47a4cb89
MT
50 self.parse_command_info()
51 self.parse_command_search()
52 self.parse_command_update()
fa6d335b 53 self.parse_command_provides()
c1962d40 54 self.parse_command_grouplist()
ce2764c1 55 self.parse_command_groupinstall()
67bc4528 56 self.parse_command_repolist()
47a4cb89
MT
57
58 # Finally parse all arguments from the command line and save them.
59 self.args = self.parser.parse_args()
60
61 # Create instance of the wonderful pakfire :)
62 self.pakfire = Pakfire(
63 self.args.instroot,
64 configs = [self.args.config],
f781b1ab 65 disable_repos = self.args.disable_repo,
47a4cb89
MT
66 )
67
68 self.action2func = {
5e87fa4f
MT
69 "install" : self.handle_install,
70 "localinstall" : self.handle_localinstall,
71 "update" : self.handle_update,
72 "info" : self.handle_info,
73 "search" : self.handle_search,
fa6d335b 74 "provides" : self.handle_provides,
c1962d40 75 "grouplist" : self.handle_grouplist,
ce2764c1 76 "groupinstall" : self.handle_groupinstall,
67bc4528 77 "repolist" : self.handle_repolist,
47a4cb89
MT
78 }
79
80 def parse_common_arguments(self):
81 self.parser.add_argument("-v", "--verbose", action="store_true",
82 help=_("Enable verbose output."))
83
84 self.parser.add_argument("-c", "--config", nargs="?",
85 help=_("Path to a configuration file to load."))
86
f781b1ab
MT
87 self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO",
88 help=_("Disable a repository temporarily."))
89
47a4cb89
MT
90 def parse_command_install(self):
91 # Implement the "install" command.
92 sub_install = self.sub_commands.add_parser("install",
93 help=_("Install one or more packages to the system."))
94 sub_install.add_argument("package", nargs="+",
95 help=_("Give name of at least one package to install."))
96 sub_install.add_argument("action", action="store_const", const="install")
97
5e87fa4f
MT
98 def parse_command_localinstall(self):
99 # Implement the "localinstall" command.
100 sub_install = self.sub_commands.add_parser("localinstall",
101 help=_("Install one or more packages from the filesystem."))
102 sub_install.add_argument("package", nargs="+",
103 help=_("Give filename of at least one package."))
104 sub_install.add_argument("action", action="store_const", const="localinstall")
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):
47a4cb89
MT
174 for pattern in self.args.package:
175 pkgs = self.pakfire.repos.get_by_glob(pattern)
176
177 pkgs = packages.PackageListing(pkgs)
178
179 for pkg in pkgs:
9afa5620 180 print pkg.dump(long=long)
47a4cb89
MT
181
182 def handle_search(self):
183 pkgs = self.pakfire.repos.search(self.args.pattern)
184
185 pkgs = packages.PackageListing(pkgs)
186
187 for pkg in pkgs:
188 print pkg.dump(short=True)
189
190 def handle_update(self):
a412c989 191 self.pakfire.update(self.args.package)
47a4cb89 192
5e87fa4f
MT
193 def handle_install(self, local=False):
194 if local:
195 repo = repository.FileSystemRepository(self.pakfire)
196
197 pkgs = []
198 for pkg in self.args.package:
199 if local and os.path.exists(pkg):
200 pkg = packages.BinaryPackage(self.pakfire, repo, pkg)
201
202 pkgs.append(pkg)
203
204 self.pakfire.install(pkgs)
205
206 def handle_localinstall(self):
207 return self.handle_install(local=True)
208
fa6d335b
MT
209 def handle_provides(self):
210 pkgs = self.pakfire.provides(self.args.pattern)
211
212 for pkg in pkgs:
213 print pkg.dump()
214
c1962d40
MT
215 def handle_grouplist(self):
216 pkgs = self.pakfire.grouplist(self.args.group[0])
217
218 for pkg in pkgs:
219 print " * %s" % pkg
220
ce2764c1
MT
221 def handle_groupinstall(self):
222 self.pakfire.groupinstall(self.args.group[0])
223
67bc4528
MT
224 def handle_repolist(self):
225 repos = self.pakfire.repolist()
226 repos.sort()
227
228 FORMAT = " %-20s %8s %12s "
229
230 title = FORMAT % (_("Repository"), _("Enabled"), _("Priority"))
231 print title
232 print "=" * len(title) # spacing line
233
234 for repo in repos:
235 # Skip the installed repository.
236 if repo.name == "installed":
237 continue
238
239 print FORMAT % (repo.name, repo.enabled, repo.priority)
240
47a4cb89
MT
241
242class CliBuilder(Cli):
243 def __init__(self):
244 self.parser = argparse.ArgumentParser(
245 description = _("Pakfire builder command line interface."),
246 )
247
248 self.parse_common_arguments()
249
250 # Add sub-commands.
251 self.sub_commands = self.parser.add_subparsers()
252
253 self.parse_command_build()
254 self.parse_command_dist()
255 self.parse_command_info()
256 self.parse_command_search()
257 self.parse_command_shell()
258 self.parse_command_update()
4fbd4216 259 self.parse_command_provides()
67bc4528 260 self.parse_command_repolist()
47a4cb89
MT
261
262 # Finally parse all arguments from the command line and save them.
263 self.args = self.parser.parse_args()
264
265 self.pakfire = Pakfire(
266 builder = True,
267 configs = [self.args.config],
f781b1ab 268 disable_repos = self.args.disable_repo,
47a4cb89
MT
269 )
270
271 self.action2func = {
fa6d335b
MT
272 "build" : self.handle_build,
273 "dist" : self.handle_dist,
274 "update" : self.handle_update,
275 "info" : self.handle_info,
276 "search" : self.handle_search,
277 "shell" : self.handle_shell,
4fbd4216 278 "provides" : self.handle_provides,
67bc4528 279 "repolist" : self.handle_repolist,
47a4cb89
MT
280 }
281
282 def parse_command_update(self):
283 # Implement the "update" command.
284 sub_update = self.sub_commands.add_parser("update",
285 help=_("Update the package indexes."))
286 sub_update.add_argument("action", action="store_const", const="update")
287
288 def parse_command_build(self):
289 # Implement the "build" command.
290 sub_build = self.sub_commands.add_parser("build",
291 help=_("Build one or more packages."))
292 sub_build.add_argument("package", nargs=1,
293 help=_("Give name of at least one package to build."))
294 sub_build.add_argument("action", action="store_const", const="build")
295
296 sub_build.add_argument("-a", "--arch",
297 help=_("Build the package for the given architecture."))
298 sub_build.add_argument("--resultdir", nargs="?",
299 help=_("Path were the output files should be copied to."))
300
301 def parse_command_shell(self):
302 # Implement the "shell" command.
303 sub_shell = self.sub_commands.add_parser("shell",
304 help=_("Go into a shell."))
042266f3 305 sub_shell.add_argument("package", nargs="?",
47a4cb89
MT
306 help=_("Give name of a package."))
307 sub_shell.add_argument("action", action="store_const", const="shell")
308
309 sub_shell.add_argument("-a", "--arch",
310 help=_("Emulated architecture in the shell."))
311
312 def parse_command_dist(self):
313 # Implement the "dist" command.
314 sub_dist = self.sub_commands.add_parser("dist",
315 help=_("Generate a source package."))
e412b8dc
MT
316 sub_dist.add_argument("package", nargs="+",
317 help=_("Give name(s) of a package(s)."))
47a4cb89
MT
318 sub_dist.add_argument("action", action="store_const", const="dist")
319
320 sub_dist.add_argument("--resultdir", nargs="?",
321 help=_("Path were the output files should be copied to."))
322
9afa5620
MT
323 def handle_info(self):
324 Cli.handle_info(self, long=True)
325
47a4cb89 326 def handle_build(self):
47a4cb89
MT
327 # Get the package descriptor from the command line options
328 pkg = self.args.package[0]
329
330 # Check, if we got a regular file
331 if os.path.exists(pkg):
332 pkg = os.path.abspath(pkg)
333
334 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 335 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
336
337 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
338 repo = repository.FileSystemRepository(self.pakfire)
339 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
340
341 else:
342 # XXX walk through the source tree and find a matching makefile
343 pass
344
0ec833c6 345 self.pakfire.build(pkg, arch=self.args.arch, resultdirs=[self.args.resultdir,])
47a4cb89
MT
346
347 def handle_shell(self):
042266f3
MT
348 pkg = None
349
47a4cb89 350 # Get the package descriptor from the command line options
042266f3 351 if self.args.package:
ad1b844f 352 pkg = self.args.package
47a4cb89
MT
353
354 # Check, if we got a regular file
042266f3 355 if pkg and os.path.exists(pkg):
47a4cb89
MT
356 pkg = os.path.abspath(pkg)
357
358 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 359 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
360
361 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
362 repo = repository.FileSystemRepository(self.pakfire)
363 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
364
365 else:
366 # XXX walk through the source tree and find a matching makefile
367 pass
368
369 self.pakfire.shell(pkg, arch=self.args.arch)
370
371 def handle_dist(self):
e412b8dc
MT
372 # Get the packages from the command line options
373 pkgs = []
47a4cb89 374
e412b8dc
MT
375 for pkg in self.args.package:
376 # Check, if we got a regular file
377 if os.path.exists(pkg):
378 pkg = os.path.abspath(pkg)
47a4cb89 379
e412b8dc
MT
380 if pkg.endswith(MAKEFILE_EXTENSION):
381 pkg = packages.Makefile(self.pakfire, pkg)
382 pkgs.append(pkg)
47a4cb89 383
e412b8dc
MT
384 else:
385 # XXX walk through the source tree and find a matching makefile
386 pass
47a4cb89 387
e412b8dc 388 self.pakfire.dist(pkgs, resultdirs=[self.args.resultdir,])
47a4cb89 389
92806f47
MT
390class CliServer(Cli):
391 def __init__(self):
392 self.parser = argparse.ArgumentParser(
393 description = _("Pakfire server command line interface."),
394 )
395
396 self.parse_common_arguments()
397
398 # Add sub-commands.
399 self.sub_commands = self.parser.add_subparsers()
400
401 self.parse_command_repo()
402
403 # Finally parse all arguments from the command line and save them.
404 self.args = self.parser.parse_args()
405
406 self.pakfire = Pakfire(
407 builder = True,
408 configs = [self.args.config],
409 disable_repos = self.args.disable_repo,
410 )
411
412 self.action2func = {
413 "repo_create" : self.handle_repo_create,
414 }
415
416 def parse_command_repo(self):
417 sub_repo = self.sub_commands.add_parser("repo",
418 help=_("Repository management commands."))
419
420 sub_repo_commands = sub_repo.add_subparsers()
421
422 self.parse_command_repo_create(sub_repo_commands)
423
424 def parse_command_repo_create(self, sub_commands):
425 sub_create = sub_commands.add_parser("create",
426 help=_("Create a new repository index."))
427 sub_create.add_argument("path", nargs=1, help=_("Path to the packages."))
428 sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages."))
429 sub_create.add_argument("action", action="store_const", const="repo_create")
430
fa6d335b
MT
431 def handle_repo_create(self):
432 path = self.args.path[0]
433
66af936c 434 self.pakfire.repo_create(path, self.args.inputs)