]> git.ipfire.org Git - pakfire.git/blame - pakfire/cli.py
Initial import.
[pakfire.git] / pakfire / cli.py
CommitLineData
47a4cb89
MT
1#!/usr/bin/python
2
3import argparse
4import sys
5
6import packages
7
8from pakfire import Pakfire
9
10from constants import *
11from i18n import _
12
13def ask_user(question):
14 """
15 Ask the user the question, he or she can answer with yes or no.
16
17 This function returns True for "yes" and False for "no".
18
19 If the software is running in a non-inteactive shell, no question
20 is asked at all and the answer is always "yes".
21 """
22 if not sys.stdin.isatty() or not sys.stdout.isatty() or not sys.stderr.isatty():
23 return True
24
25 print _("%s [y/N]") % question,
26 ret = raw_input()
27
28 return ret in ("y", "Y")
29
30
31class Cli(object):
32 # XXX check if we are running as the root user
33
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",
42 default="/tmp/pakfire",
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()
49 self.parse_command_info()
50 self.parse_command_search()
51 self.parse_command_update()
52
53 # Finally parse all arguments from the command line and save them.
54 self.args = self.parser.parse_args()
55
56 # Create instance of the wonderful pakfire :)
57 self.pakfire = Pakfire(
58 self.args.instroot,
59 configs = [self.args.config],
60 )
61
62 self.action2func = {
63 "install" : self.handle_install,
64 "update" : self.handle_update,
65 "info" : self.handle_info,
66 "search" : self.handle_search,
67 }
68
69 def parse_common_arguments(self):
70 self.parser.add_argument("-v", "--verbose", action="store_true",
71 help=_("Enable verbose output."))
72
73 self.parser.add_argument("-c", "--config", nargs="?",
74 help=_("Path to a configuration file to load."))
75
76 def parse_command_install(self):
77 # Implement the "install" command.
78 sub_install = self.sub_commands.add_parser("install",
79 help=_("Install one or more packages to the system."))
80 sub_install.add_argument("package", nargs="+",
81 help=_("Give name of at least one package to install."))
82 sub_install.add_argument("action", action="store_const", const="install")
83
84 def parse_command_update(self):
85 # Implement the "update" command.
86 sub_update = self.sub_commands.add_parser("update",
87 help=_("Update the whole system or one specific package."))
88 sub_update.add_argument("package", nargs="*",
89 help=_("Give a name of a package to update or leave emtpy for all."))
90 sub_update.add_argument("action", action="store_const", const="update")
91
92 def parse_command_info(self):
93 # Implement the "info" command.
94 sub_info = self.sub_commands.add_parser("info",
95 help=_("Print some information about the given package(s)."))
96 sub_info.add_argument("package", nargs="+",
97 help=_("Give at least the name of one package."))
98 sub_info.add_argument("action", action="store_const", const="info")
99
100 def parse_command_search(self):
101 # Implement the "search" command.
102 sub_search = self.sub_commands.add_parser("search",
103 help=_("Search for a given pattern."))
104 sub_search.add_argument("pattern",
105 help=_("A pattern to search for."))
106 sub_search.add_argument("action", action="store_const", const="search")
107
108 def run(self):
109 action = self.args.action
110
111 if not self.action2func.has_key(action):
112 raise
113
114 try:
115 func = self.action2func[action]
116 except KeyError:
117 raise # XXX catch and return better error message
118
119 return func()
120
121 def handle_info(self):
122 for pattern in self.args.package:
123 pkgs = self.pakfire.repos.get_by_glob(pattern)
124
125 pkgs = packages.PackageListing(pkgs)
126
127 for pkg in pkgs:
128 print pkg.dump()
129
130 def handle_search(self):
131 pkgs = self.pakfire.repos.search(self.args.pattern)
132
133 pkgs = packages.PackageListing(pkgs)
134
135 for pkg in pkgs:
136 print pkg.dump(short=True)
137
138 def handle_update(self):
139 pass
140
141
142class CliBuilder(Cli):
143 def __init__(self):
144 self.parser = argparse.ArgumentParser(
145 description = _("Pakfire builder command line interface."),
146 )
147
148 self.parse_common_arguments()
149
150 # Add sub-commands.
151 self.sub_commands = self.parser.add_subparsers()
152
153 self.parse_command_build()
154 self.parse_command_dist()
155 self.parse_command_info()
156 self.parse_command_search()
157 self.parse_command_shell()
158 self.parse_command_update()
159
160 # Finally parse all arguments from the command line and save them.
161 self.args = self.parser.parse_args()
162
163 self.pakfire = Pakfire(
164 builder = True,
165 configs = [self.args.config],
166 )
167
168 self.action2func = {
169 "build" : self.handle_build,
170 "dist" : self.handle_dist,
171 "update" : self.handle_update,
172 "info" : self.handle_info,
173 "search" : self.handle_search,
174 "shell" : self.handle_shell,
175 }
176
177 def parse_command_update(self):
178 # Implement the "update" command.
179 sub_update = self.sub_commands.add_parser("update",
180 help=_("Update the package indexes."))
181 sub_update.add_argument("action", action="store_const", const="update")
182
183 def parse_command_build(self):
184 # Implement the "build" command.
185 sub_build = self.sub_commands.add_parser("build",
186 help=_("Build one or more packages."))
187 sub_build.add_argument("package", nargs=1,
188 help=_("Give name of at least one package to build."))
189 sub_build.add_argument("action", action="store_const", const="build")
190
191 sub_build.add_argument("-a", "--arch",
192 help=_("Build the package for the given architecture."))
193 sub_build.add_argument("--resultdir", nargs="?",
194 help=_("Path were the output files should be copied to."))
195
196 def parse_command_shell(self):
197 # Implement the "shell" command.
198 sub_shell = self.sub_commands.add_parser("shell",
199 help=_("Go into a shell."))
200 sub_shell.add_argument("package", nargs=1,
201 help=_("Give name of a package."))
202 sub_shell.add_argument("action", action="store_const", const="shell")
203
204 sub_shell.add_argument("-a", "--arch",
205 help=_("Emulated architecture in the shell."))
206
207 def parse_command_dist(self):
208 # Implement the "dist" command.
209 sub_dist = self.sub_commands.add_parser("dist",
210 help=_("Generate a source package."))
211 sub_dist.add_argument("package", nargs=1,
212 help=_("Give name of a package."))
213 sub_dist.add_argument("action", action="store_const", const="dist")
214
215 sub_dist.add_argument("--resultdir", nargs="?",
216 help=_("Path were the output files should be copied to."))
217
218 def handle_build(self):
219 print self.args
220 # Get the package descriptor from the command line options
221 pkg = self.args.package[0]
222
223 # Check, if we got a regular file
224 if os.path.exists(pkg):
225 pkg = os.path.abspath(pkg)
226
227 if pkg.endswith(MAKEFILE_EXTENSION):
228 pkg = packages.Makefile(pkg)
229
230 elif pkg.endswith(PACKAGE_EXTENSION):
231 pkg = packages.SourcePackage(pkg)
232
233 else:
234 # XXX walk through the source tree and find a matching makefile
235 pass
236
237 self.pakfire.build(pkg, arch=self.args.arch, resultdir=self.args.resultdir)
238
239 def handle_shell(self):
240 print self.args
241 # Get the package descriptor from the command line options
242 pkg = self.args.package[0]
243
244 # Check, if we got a regular file
245 if os.path.exists(pkg):
246 pkg = os.path.abspath(pkg)
247
248 if pkg.endswith(MAKEFILE_EXTENSION):
249 pkg = packages.Makefile(pkg)
250
251 elif pkg.endswith(PACKAGE_EXTENSION):
252 pkg = packages.SourcePackage(pkg)
253
254 else:
255 # XXX walk through the source tree and find a matching makefile
256 pass
257
258 self.pakfire.shell(pkg, arch=self.args.arch)
259
260 def handle_dist(self):
261 print self.args
262 # Get the package descriptor from the command line options
263 pkg = self.args.package[0]
264
265 # Check, if we got a regular file
266 if os.path.exists(pkg):
267 pkg = os.path.abspath(pkg)
268
269 if pkg.endswith(MAKEFILE_EXTENSION):
270 pkg = packages.Makefile(pkg)
271
272 else:
273 # XXX walk through the source tree and find a matching makefile
274 pass
275
276 self.pakfire.dist(pkg, self.args.resultdir)
277