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