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