]> git.ipfire.org Git - people/stevee/pakfire.git/blame - pakfire/cli.py
Add grouplist command.
[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
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()
47a4cb89
MT
55
56 # Finally parse all arguments from the command line and save them.
57 self.args = self.parser.parse_args()
58
59 # Create instance of the wonderful pakfire :)
60 self.pakfire = Pakfire(
61 self.args.instroot,
62 configs = [self.args.config],
f781b1ab 63 disable_repos = self.args.disable_repo,
47a4cb89
MT
64 )
65
66 self.action2func = {
5e87fa4f
MT
67 "install" : self.handle_install,
68 "localinstall" : self.handle_localinstall,
69 "update" : self.handle_update,
70 "info" : self.handle_info,
71 "search" : self.handle_search,
fa6d335b 72 "provides" : self.handle_provides,
c1962d40 73 "grouplist" : self.handle_grouplist,
47a4cb89
MT
74 }
75
76 def parse_common_arguments(self):
77 self.parser.add_argument("-v", "--verbose", action="store_true",
78 help=_("Enable verbose output."))
79
80 self.parser.add_argument("-c", "--config", nargs="?",
81 help=_("Path to a configuration file to load."))
82
f781b1ab
MT
83 self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO",
84 help=_("Disable a repository temporarily."))
85
47a4cb89
MT
86 def parse_command_install(self):
87 # Implement the "install" command.
88 sub_install = self.sub_commands.add_parser("install",
89 help=_("Install one or more packages to the system."))
90 sub_install.add_argument("package", nargs="+",
91 help=_("Give name of at least one package to install."))
92 sub_install.add_argument("action", action="store_const", const="install")
93
5e87fa4f
MT
94 def parse_command_localinstall(self):
95 # Implement the "localinstall" command.
96 sub_install = self.sub_commands.add_parser("localinstall",
97 help=_("Install one or more packages from the filesystem."))
98 sub_install.add_argument("package", nargs="+",
99 help=_("Give filename of at least one package."))
100 sub_install.add_argument("action", action="store_const", const="localinstall")
101
47a4cb89
MT
102 def parse_command_update(self):
103 # Implement the "update" command.
104 sub_update = self.sub_commands.add_parser("update",
105 help=_("Update the whole system or one specific package."))
106 sub_update.add_argument("package", nargs="*",
107 help=_("Give a name of a package to update or leave emtpy for all."))
108 sub_update.add_argument("action", action="store_const", const="update")
109
110 def parse_command_info(self):
111 # Implement the "info" command.
112 sub_info = self.sub_commands.add_parser("info",
113 help=_("Print some information about the given package(s)."))
114 sub_info.add_argument("package", nargs="+",
115 help=_("Give at least the name of one package."))
116 sub_info.add_argument("action", action="store_const", const="info")
117
118 def parse_command_search(self):
119 # Implement the "search" command.
120 sub_search = self.sub_commands.add_parser("search",
121 help=_("Search for a given pattern."))
122 sub_search.add_argument("pattern",
123 help=_("A pattern to search for."))
124 sub_search.add_argument("action", action="store_const", const="search")
125
fa6d335b
MT
126 def parse_command_provides(self):
127 # Implement the "provides" command
128 sub_provides = self.sub_commands.add_parser("provides",
129 help=_("Get a list of packages that provide a given file or feature."))
130 sub_provides.add_argument("pattern", nargs="+",
131 help=_("File or feature to search for."))
132 sub_provides.add_argument("action", action="store_const", const="provides")
133
c1962d40
MT
134 def parse_command_grouplist(self):
135 # Implement the "grouplist" command
136 sub_grouplist = self.sub_commands.add_parser("grouplist",
137 help=_("Get list of packages that belong to the given group."))
138 sub_grouplist.add_argument("group", nargs=1,
139 help=_("Group name to search for."))
140 sub_grouplist.add_argument("action", action="store_const", const="grouplist")
141
47a4cb89
MT
142 def run(self):
143 action = self.args.action
144
145 if not self.action2func.has_key(action):
146 raise
147
148 try:
149 func = self.action2func[action]
150 except KeyError:
151 raise # XXX catch and return better error message
152
153 return func()
154
9afa5620 155 def handle_info(self, long=False):
47a4cb89
MT
156 for pattern in self.args.package:
157 pkgs = self.pakfire.repos.get_by_glob(pattern)
158
159 pkgs = packages.PackageListing(pkgs)
160
161 for pkg in pkgs:
9afa5620 162 print pkg.dump(long=long)
47a4cb89
MT
163
164 def handle_search(self):
165 pkgs = self.pakfire.repos.search(self.args.pattern)
166
167 pkgs = packages.PackageListing(pkgs)
168
169 for pkg in pkgs:
170 print pkg.dump(short=True)
171
172 def handle_update(self):
a412c989 173 self.pakfire.update(self.args.package)
47a4cb89 174
5e87fa4f
MT
175 def handle_install(self, local=False):
176 if local:
177 repo = repository.FileSystemRepository(self.pakfire)
178
179 pkgs = []
180 for pkg in self.args.package:
181 if local and os.path.exists(pkg):
182 pkg = packages.BinaryPackage(self.pakfire, repo, pkg)
183
184 pkgs.append(pkg)
185
186 self.pakfire.install(pkgs)
187
188 def handle_localinstall(self):
189 return self.handle_install(local=True)
190
fa6d335b
MT
191 def handle_provides(self):
192 pkgs = self.pakfire.provides(self.args.pattern)
193
194 for pkg in pkgs:
195 print pkg.dump()
196
c1962d40
MT
197 def handle_grouplist(self):
198 pkgs = self.pakfire.grouplist(self.args.group[0])
199
200 for pkg in pkgs:
201 print " * %s" % pkg
202
47a4cb89
MT
203
204class CliBuilder(Cli):
205 def __init__(self):
206 self.parser = argparse.ArgumentParser(
207 description = _("Pakfire builder command line interface."),
208 )
209
210 self.parse_common_arguments()
211
212 # Add sub-commands.
213 self.sub_commands = self.parser.add_subparsers()
214
215 self.parse_command_build()
216 self.parse_command_dist()
217 self.parse_command_info()
218 self.parse_command_search()
219 self.parse_command_shell()
220 self.parse_command_update()
4fbd4216 221 self.parse_command_provides()
47a4cb89
MT
222
223 # Finally parse all arguments from the command line and save them.
224 self.args = self.parser.parse_args()
225
226 self.pakfire = Pakfire(
227 builder = True,
228 configs = [self.args.config],
f781b1ab 229 disable_repos = self.args.disable_repo,
47a4cb89
MT
230 )
231
232 self.action2func = {
fa6d335b
MT
233 "build" : self.handle_build,
234 "dist" : self.handle_dist,
235 "update" : self.handle_update,
236 "info" : self.handle_info,
237 "search" : self.handle_search,
238 "shell" : self.handle_shell,
4fbd4216 239 "provides" : self.handle_provides,
47a4cb89
MT
240 }
241
242 def parse_command_update(self):
243 # Implement the "update" command.
244 sub_update = self.sub_commands.add_parser("update",
245 help=_("Update the package indexes."))
246 sub_update.add_argument("action", action="store_const", const="update")
247
248 def parse_command_build(self):
249 # Implement the "build" command.
250 sub_build = self.sub_commands.add_parser("build",
251 help=_("Build one or more packages."))
252 sub_build.add_argument("package", nargs=1,
253 help=_("Give name of at least one package to build."))
254 sub_build.add_argument("action", action="store_const", const="build")
255
256 sub_build.add_argument("-a", "--arch",
257 help=_("Build the package for the given architecture."))
258 sub_build.add_argument("--resultdir", nargs="?",
259 help=_("Path were the output files should be copied to."))
260
261 def parse_command_shell(self):
262 # Implement the "shell" command.
263 sub_shell = self.sub_commands.add_parser("shell",
264 help=_("Go into a shell."))
042266f3 265 sub_shell.add_argument("package", nargs="?",
47a4cb89
MT
266 help=_("Give name of a package."))
267 sub_shell.add_argument("action", action="store_const", const="shell")
268
269 sub_shell.add_argument("-a", "--arch",
270 help=_("Emulated architecture in the shell."))
271
272 def parse_command_dist(self):
273 # Implement the "dist" command.
274 sub_dist = self.sub_commands.add_parser("dist",
275 help=_("Generate a source package."))
e412b8dc
MT
276 sub_dist.add_argument("package", nargs="+",
277 help=_("Give name(s) of a package(s)."))
47a4cb89
MT
278 sub_dist.add_argument("action", action="store_const", const="dist")
279
280 sub_dist.add_argument("--resultdir", nargs="?",
281 help=_("Path were the output files should be copied to."))
282
9afa5620
MT
283 def handle_info(self):
284 Cli.handle_info(self, long=True)
285
47a4cb89 286 def handle_build(self):
47a4cb89
MT
287 # Get the package descriptor from the command line options
288 pkg = self.args.package[0]
289
290 # Check, if we got a regular file
291 if os.path.exists(pkg):
292 pkg = os.path.abspath(pkg)
293
294 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 295 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
296
297 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
298 repo = repository.FileSystemRepository(self.pakfire)
299 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
300
301 else:
302 # XXX walk through the source tree and find a matching makefile
303 pass
304
0ec833c6 305 self.pakfire.build(pkg, arch=self.args.arch, resultdirs=[self.args.resultdir,])
47a4cb89
MT
306
307 def handle_shell(self):
042266f3
MT
308 pkg = None
309
47a4cb89 310 # Get the package descriptor from the command line options
042266f3 311 if self.args.package:
ad1b844f 312 pkg = self.args.package
47a4cb89
MT
313
314 # Check, if we got a regular file
042266f3 315 if pkg and os.path.exists(pkg):
47a4cb89
MT
316 pkg = os.path.abspath(pkg)
317
318 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 319 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
320
321 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
322 repo = repository.FileSystemRepository(self.pakfire)
323 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
324
325 else:
326 # XXX walk through the source tree and find a matching makefile
327 pass
328
329 self.pakfire.shell(pkg, arch=self.args.arch)
330
331 def handle_dist(self):
e412b8dc
MT
332 # Get the packages from the command line options
333 pkgs = []
47a4cb89 334
e412b8dc
MT
335 for pkg in self.args.package:
336 # Check, if we got a regular file
337 if os.path.exists(pkg):
338 pkg = os.path.abspath(pkg)
47a4cb89 339
e412b8dc
MT
340 if pkg.endswith(MAKEFILE_EXTENSION):
341 pkg = packages.Makefile(self.pakfire, pkg)
342 pkgs.append(pkg)
47a4cb89 343
e412b8dc
MT
344 else:
345 # XXX walk through the source tree and find a matching makefile
346 pass
47a4cb89 347
e412b8dc 348 self.pakfire.dist(pkgs, resultdirs=[self.args.resultdir,])
47a4cb89 349
92806f47
MT
350class CliServer(Cli):
351 def __init__(self):
352 self.parser = argparse.ArgumentParser(
353 description = _("Pakfire server command line interface."),
354 )
355
356 self.parse_common_arguments()
357
358 # Add sub-commands.
359 self.sub_commands = self.parser.add_subparsers()
360
361 self.parse_command_repo()
362
363 # Finally parse all arguments from the command line and save them.
364 self.args = self.parser.parse_args()
365
366 self.pakfire = Pakfire(
367 builder = True,
368 configs = [self.args.config],
369 disable_repos = self.args.disable_repo,
370 )
371
372 self.action2func = {
373 "repo_create" : self.handle_repo_create,
374 }
375
376 def parse_command_repo(self):
377 sub_repo = self.sub_commands.add_parser("repo",
378 help=_("Repository management commands."))
379
380 sub_repo_commands = sub_repo.add_subparsers()
381
382 self.parse_command_repo_create(sub_repo_commands)
383
384 def parse_command_repo_create(self, sub_commands):
385 sub_create = sub_commands.add_parser("create",
386 help=_("Create a new repository index."))
387 sub_create.add_argument("path", nargs=1, help=_("Path to the packages."))
388 sub_create.add_argument("inputs", nargs="+", help=_("Path to input packages."))
389 sub_create.add_argument("action", action="store_const", const="repo_create")
390
fa6d335b
MT
391 def handle_repo_create(self):
392 path = self.args.path[0]
393
66af936c 394 self.pakfire.repo_create(path, self.args.inputs)