]> git.ipfire.org Git - people/ms/pakfire.git/blame - pakfire/cli.py
Remove _io_.py from POTFILES.
[people/ms/pakfire.git] / pakfire / cli.py
CommitLineData
47a4cb89
MT
1#!/usr/bin/python
2
3import argparse
4import sys
5
6import packages
fa6d335b 7import repository
47a4cb89
MT
8
9from pakfire import Pakfire
10
11from constants import *
12from i18n import _
13
14def ask_user(question):
15 """
16 Ask the user the question, he or she can answer with yes or no.
17
18 This function returns True for "yes" and False for "no".
19
20 If the software is running in a non-inteactive shell, no question
21 is asked at all and the answer is always "yes".
22 """
23 if not sys.stdin.isatty() or not sys.stdout.isatty() or not sys.stderr.isatty():
24 return True
25
26 print _("%s [y/N]") % question,
27 ret = raw_input()
28
29 return ret in ("y", "Y")
30
31
32class Cli(object):
33 # XXX check if we are running as the root user
34
35 def __init__(self):
36 self.parser = argparse.ArgumentParser(
37 description = _("Pakfire command line interface."),
38 )
39
40 self.parse_common_arguments()
41
42 self.parser.add_argument("--instroot", metavar="PATH",
43 default="/tmp/pakfire",
44 help=_("The path where pakfire should operate in."))
45
46 # Add sub-commands.
47 self.sub_commands = self.parser.add_subparsers()
48
49 self.parse_command_install()
5e87fa4f 50 self.parse_command_localinstall()
47a4cb89
MT
51 self.parse_command_info()
52 self.parse_command_search()
53 self.parse_command_update()
fa6d335b 54 self.parse_command_provides()
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,
47a4cb89
MT
73 }
74
75 def parse_common_arguments(self):
76 self.parser.add_argument("-v", "--verbose", action="store_true",
77 help=_("Enable verbose output."))
78
79 self.parser.add_argument("-c", "--config", nargs="?",
80 help=_("Path to a configuration file to load."))
81
f781b1ab
MT
82 self.parser.add_argument("--disable-repo", nargs="*", metavar="REPO",
83 help=_("Disable a repository temporarily."))
84
47a4cb89
MT
85 def parse_command_install(self):
86 # Implement the "install" command.
87 sub_install = self.sub_commands.add_parser("install",
88 help=_("Install one or more packages to the system."))
89 sub_install.add_argument("package", nargs="+",
90 help=_("Give name of at least one package to install."))
91 sub_install.add_argument("action", action="store_const", const="install")
92
5e87fa4f
MT
93 def parse_command_localinstall(self):
94 # Implement the "localinstall" command.
95 sub_install = self.sub_commands.add_parser("localinstall",
96 help=_("Install one or more packages from the filesystem."))
97 sub_install.add_argument("package", nargs="+",
98 help=_("Give filename of at least one package."))
99 sub_install.add_argument("action", action="store_const", const="localinstall")
100
47a4cb89
MT
101 def parse_command_update(self):
102 # Implement the "update" command.
103 sub_update = self.sub_commands.add_parser("update",
104 help=_("Update the whole system or one specific package."))
105 sub_update.add_argument("package", nargs="*",
106 help=_("Give a name of a package to update or leave emtpy for all."))
107 sub_update.add_argument("action", action="store_const", const="update")
108
109 def parse_command_info(self):
110 # Implement the "info" command.
111 sub_info = self.sub_commands.add_parser("info",
112 help=_("Print some information about the given package(s)."))
113 sub_info.add_argument("package", nargs="+",
114 help=_("Give at least the name of one package."))
115 sub_info.add_argument("action", action="store_const", const="info")
116
117 def parse_command_search(self):
118 # Implement the "search" command.
119 sub_search = self.sub_commands.add_parser("search",
120 help=_("Search for a given pattern."))
121 sub_search.add_argument("pattern",
122 help=_("A pattern to search for."))
123 sub_search.add_argument("action", action="store_const", const="search")
124
fa6d335b
MT
125 def parse_command_provides(self):
126 # Implement the "provides" command
127 sub_provides = self.sub_commands.add_parser("provides",
128 help=_("Get a list of packages that provide a given file or feature."))
129 sub_provides.add_argument("pattern", nargs="+",
130 help=_("File or feature to search for."))
131 sub_provides.add_argument("action", action="store_const", const="provides")
132
47a4cb89
MT
133 def run(self):
134 action = self.args.action
135
136 if not self.action2func.has_key(action):
137 raise
138
139 try:
140 func = self.action2func[action]
141 except KeyError:
142 raise # XXX catch and return better error message
143
144 return func()
145
146 def handle_info(self):
147 for pattern in self.args.package:
148 pkgs = self.pakfire.repos.get_by_glob(pattern)
149
150 pkgs = packages.PackageListing(pkgs)
151
152 for pkg in pkgs:
153 print pkg.dump()
154
155 def handle_search(self):
156 pkgs = self.pakfire.repos.search(self.args.pattern)
157
158 pkgs = packages.PackageListing(pkgs)
159
160 for pkg in pkgs:
161 print pkg.dump(short=True)
162
163 def handle_update(self):
164 pass
165
5e87fa4f
MT
166 def handle_install(self, local=False):
167 if local:
168 repo = repository.FileSystemRepository(self.pakfire)
169
170 pkgs = []
171 for pkg in self.args.package:
172 if local and os.path.exists(pkg):
173 pkg = packages.BinaryPackage(self.pakfire, repo, pkg)
174
175 pkgs.append(pkg)
176
177 self.pakfire.install(pkgs)
178
179 def handle_localinstall(self):
180 return self.handle_install(local=True)
181
fa6d335b
MT
182 def handle_provides(self):
183 pkgs = self.pakfire.provides(self.args.pattern)
184
185 for pkg in pkgs:
186 print pkg.dump()
187
47a4cb89
MT
188
189class CliBuilder(Cli):
190 def __init__(self):
191 self.parser = argparse.ArgumentParser(
192 description = _("Pakfire builder command line interface."),
193 )
194
195 self.parse_common_arguments()
196
197 # Add sub-commands.
198 self.sub_commands = self.parser.add_subparsers()
199
200 self.parse_command_build()
201 self.parse_command_dist()
202 self.parse_command_info()
203 self.parse_command_search()
204 self.parse_command_shell()
205 self.parse_command_update()
fa6d335b 206 self.parse_command_repo()
47a4cb89
MT
207
208 # Finally parse all arguments from the command line and save them.
209 self.args = self.parser.parse_args()
210
211 self.pakfire = Pakfire(
212 builder = True,
213 configs = [self.args.config],
f781b1ab 214 disable_repos = self.args.disable_repo,
47a4cb89
MT
215 )
216
217 self.action2func = {
fa6d335b
MT
218 "build" : self.handle_build,
219 "dist" : self.handle_dist,
220 "update" : self.handle_update,
221 "info" : self.handle_info,
222 "search" : self.handle_search,
223 "shell" : self.handle_shell,
224 "repo_create" : self.handle_repo_create,
47a4cb89
MT
225 }
226
227 def parse_command_update(self):
228 # Implement the "update" command.
229 sub_update = self.sub_commands.add_parser("update",
230 help=_("Update the package indexes."))
231 sub_update.add_argument("action", action="store_const", const="update")
232
233 def parse_command_build(self):
234 # Implement the "build" command.
235 sub_build = self.sub_commands.add_parser("build",
236 help=_("Build one or more packages."))
237 sub_build.add_argument("package", nargs=1,
238 help=_("Give name of at least one package to build."))
239 sub_build.add_argument("action", action="store_const", const="build")
240
241 sub_build.add_argument("-a", "--arch",
242 help=_("Build the package for the given architecture."))
243 sub_build.add_argument("--resultdir", nargs="?",
244 help=_("Path were the output files should be copied to."))
245
246 def parse_command_shell(self):
247 # Implement the "shell" command.
248 sub_shell = self.sub_commands.add_parser("shell",
249 help=_("Go into a shell."))
250 sub_shell.add_argument("package", nargs=1,
251 help=_("Give name of a package."))
252 sub_shell.add_argument("action", action="store_const", const="shell")
253
254 sub_shell.add_argument("-a", "--arch",
255 help=_("Emulated architecture in the shell."))
256
257 def parse_command_dist(self):
258 # Implement the "dist" command.
259 sub_dist = self.sub_commands.add_parser("dist",
260 help=_("Generate a source package."))
261 sub_dist.add_argument("package", nargs=1,
262 help=_("Give name of a package."))
263 sub_dist.add_argument("action", action="store_const", const="dist")
264
265 sub_dist.add_argument("--resultdir", nargs="?",
266 help=_("Path were the output files should be copied to."))
267
fa6d335b
MT
268 def parse_command_repo(self):
269 sub_repo = self.sub_commands.add_parser("repo",
270 help=_("Repository management commands."))
271
272 sub_repo_commands = sub_repo.add_subparsers()
273
274 self.parse_command_repo_create(sub_repo_commands)
275
276 def parse_command_repo_create(self, sub_commands):
277 sub_create = sub_commands.add_parser("create",
278 help=_("Create a new repository index."))
279 sub_create.add_argument("path", nargs=1, help=_("Path to the packages."))
280 sub_create.add_argument("action", action="store_const", const="repo_create")
281
47a4cb89
MT
282 def handle_build(self):
283 print self.args
284 # Get the package descriptor from the command line options
285 pkg = self.args.package[0]
286
287 # Check, if we got a regular file
288 if os.path.exists(pkg):
289 pkg = os.path.abspath(pkg)
290
291 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 292 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
293
294 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
295 repo = repository.FileSystemRepository(self.pakfire)
296 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
297
298 else:
299 # XXX walk through the source tree and find a matching makefile
300 pass
301
302 self.pakfire.build(pkg, arch=self.args.arch, resultdir=self.args.resultdir)
303
304 def handle_shell(self):
305 print self.args
306 # Get the package descriptor from the command line options
307 pkg = self.args.package[0]
308
309 # Check, if we got a regular file
310 if os.path.exists(pkg):
311 pkg = os.path.abspath(pkg)
312
313 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 314 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
315
316 elif pkg.endswith(PACKAGE_EXTENSION):
3723913b
MT
317 repo = repository.FileSystemRepository(self.pakfire)
318 pkg = packages.SourcePackage(self.pakfire, repo, pkg)
47a4cb89
MT
319
320 else:
321 # XXX walk through the source tree and find a matching makefile
322 pass
323
324 self.pakfire.shell(pkg, arch=self.args.arch)
325
326 def handle_dist(self):
327 print self.args
328 # Get the package descriptor from the command line options
329 pkg = self.args.package[0]
330
331 # Check, if we got a regular file
332 if os.path.exists(pkg):
333 pkg = os.path.abspath(pkg)
334
335 if pkg.endswith(MAKEFILE_EXTENSION):
3723913b 336 pkg = packages.Makefile(self.pakfire, pkg)
47a4cb89
MT
337
338 else:
339 # XXX walk through the source tree and find a matching makefile
340 pass
341
342 self.pakfire.dist(pkg, self.args.resultdir)
343
fa6d335b
MT
344 def handle_repo_create(self):
345 path = self.args.path[0]
346
347 self.pakfire.repo_create(path)
348