]>
git.ipfire.org Git - pakfire.git/blob - pakfire/cli.py
10 from pakfire
import Pakfire
12 from constants
import *
15 def ask_user(question
):
17 Ask the user the question, he or she can answer with yes or no.
19 This function returns True for "yes" and False for "no".
21 If the software is running in a non-inteactive shell, no question
22 is asked at all and the answer is always "yes".
24 if not util
.cli_is_interactive():
27 print _("%s [y/N]") % question
,
30 return ret
in ("y", "Y")
35 self
.parser
= argparse
.ArgumentParser(
36 description
= _("Pakfire command line interface."),
39 self
.parse_common_arguments()
41 self
.parser
.add_argument("--instroot", metavar
="PATH",
42 default
="/tmp/pakfire",
43 help=_("The path where pakfire should operate in."))
46 self
.sub_commands
= self
.parser
.add_subparsers()
48 self
.parse_command_install()
49 self
.parse_command_localinstall()
50 self
.parse_command_info()
51 self
.parse_command_search()
52 self
.parse_command_update()
53 self
.parse_command_provides()
55 # Finally parse all arguments from the command line and save them.
56 self
.args
= self
.parser
.parse_args()
58 # Create instance of the wonderful pakfire :)
59 self
.pakfire
= Pakfire(
61 configs
= [self
.args
.config
],
62 disable_repos
= self
.args
.disable_repo
,
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
,
71 "provides" : self
.handle_provides
,
74 def parse_common_arguments(self
):
75 self
.parser
.add_argument("-v", "--verbose", action
="store_true",
76 help=_("Enable verbose output."))
78 self
.parser
.add_argument("-c", "--config", nargs
="?",
79 help=_("Path to a configuration file to load."))
81 self
.parser
.add_argument("--disable-repo", nargs
="*", metavar
="REPO",
82 help=_("Disable a repository temporarily."))
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")
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")
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")
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")
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")
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")
133 action
= self
.args
.action
135 if not self
.action2func
.has_key(action
):
139 func
= self
.action2func
[action
]
141 raise # XXX catch and return better error message
145 def handle_info(self
, long=False):
146 for pattern
in self
.args
.package
:
147 pkgs
= self
.pakfire
.repos
.get_by_glob(pattern
)
149 pkgs
= packages
.PackageListing(pkgs
)
152 print pkg
.dump(long=long)
154 def handle_search(self
):
155 pkgs
= self
.pakfire
.repos
.search(self
.args
.pattern
)
157 pkgs
= packages
.PackageListing(pkgs
)
160 print pkg
.dump(short
=True)
162 def handle_update(self
):
165 def handle_install(self
, local
=False):
167 repo
= repository
.FileSystemRepository(self
.pakfire
)
170 for pkg
in self
.args
.package
:
171 if local
and os
.path
.exists(pkg
):
172 pkg
= packages
.BinaryPackage(self
.pakfire
, repo
, pkg
)
176 self
.pakfire
.install(pkgs
)
178 def handle_localinstall(self
):
179 return self
.handle_install(local
=True)
181 def handle_provides(self
):
182 pkgs
= self
.pakfire
.provides(self
.args
.pattern
)
188 class CliBuilder(Cli
):
190 self
.parser
= argparse
.ArgumentParser(
191 description
= _("Pakfire builder command line interface."),
194 self
.parse_common_arguments()
197 self
.sub_commands
= self
.parser
.add_subparsers()
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()
205 self
.parse_command_provides()
207 # Finally parse all arguments from the command line and save them.
208 self
.args
= self
.parser
.parse_args()
210 self
.pakfire
= Pakfire(
212 configs
= [self
.args
.config
],
213 disable_repos
= self
.args
.disable_repo
,
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
,
223 "provides" : self
.handle_provides
,
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")
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")
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."))
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."))
249 sub_shell
.add_argument("package", nargs
=1,
250 help=_("Give name of a package."))
251 sub_shell
.add_argument("action", action
="store_const", const
="shell")
253 sub_shell
.add_argument("-a", "--arch",
254 help=_("Emulated architecture in the shell."))
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."))
260 sub_dist
.add_argument("package", nargs
=1,
261 help=_("Give name of a package."))
262 sub_dist
.add_argument("action", action
="store_const", const
="dist")
264 sub_dist
.add_argument("--resultdir", nargs
="?",
265 help=_("Path were the output files should be copied to."))
267 def handle_info(self
):
268 Cli
.handle_info(self
, long=True)
270 def handle_build(self
):
272 # Get the package descriptor from the command line options
273 pkg
= self
.args
.package
[0]
275 # Check, if we got a regular file
276 if os
.path
.exists(pkg
):
277 pkg
= os
.path
.abspath(pkg
)
279 if pkg
.endswith(MAKEFILE_EXTENSION
):
280 pkg
= packages
.Makefile(self
.pakfire
, pkg
)
282 elif pkg
.endswith(PACKAGE_EXTENSION
):
283 repo
= repository
.FileSystemRepository(self
.pakfire
)
284 pkg
= packages
.SourcePackage(self
.pakfire
, repo
, pkg
)
287 # XXX walk through the source tree and find a matching makefile
290 self
.pakfire
.build(pkg
, arch
=self
.args
.arch
, resultdirs
=[self
.args
.resultdir
,])
292 def handle_shell(self
):
294 # Get the package descriptor from the command line options
295 pkg
= self
.args
.package
[0]
297 # Check, if we got a regular file
298 if os
.path
.exists(pkg
):
299 pkg
= os
.path
.abspath(pkg
)
301 if pkg
.endswith(MAKEFILE_EXTENSION
):
302 pkg
= packages
.Makefile(self
.pakfire
, pkg
)
304 elif pkg
.endswith(PACKAGE_EXTENSION
):
305 repo
= repository
.FileSystemRepository(self
.pakfire
)
306 pkg
= packages
.SourcePackage(self
.pakfire
, repo
, pkg
)
309 # XXX walk through the source tree and find a matching makefile
312 self
.pakfire
.shell(pkg
, arch
=self
.args
.arch
)
314 def handle_dist(self
):
316 # Get the package descriptor from the command line options
317 pkg
= self
.args
.package
[0]
319 # Check, if we got a regular file
320 if os
.path
.exists(pkg
):
321 pkg
= os
.path
.abspath(pkg
)
323 if pkg
.endswith(MAKEFILE_EXTENSION
):
324 pkg
= packages
.Makefile(self
.pakfire
, pkg
)
327 # XXX walk through the source tree and find a matching makefile
330 self
.pakfire
.dist(pkg
, resultdirs
=[self
.args
.resultdir
,])
332 class CliServer(Cli
):
334 self
.parser
= argparse
.ArgumentParser(
335 description
= _("Pakfire server command line interface."),
338 self
.parse_common_arguments()
341 self
.sub_commands
= self
.parser
.add_subparsers()
343 self
.parse_command_repo()
345 # Finally parse all arguments from the command line and save them.
346 self
.args
= self
.parser
.parse_args()
348 self
.pakfire
= Pakfire(
350 configs
= [self
.args
.config
],
351 disable_repos
= self
.args
.disable_repo
,
355 "repo_create" : self
.handle_repo_create
,
358 def parse_command_repo(self
):
359 sub_repo
= self
.sub_commands
.add_parser("repo",
360 help=_("Repository management commands."))
362 sub_repo_commands
= sub_repo
.add_subparsers()
364 self
.parse_command_repo_create(sub_repo_commands
)
366 def parse_command_repo_create(self
, sub_commands
):
367 sub_create
= sub_commands
.add_parser("create",
368 help=_("Create a new repository index."))
369 sub_create
.add_argument("path", nargs
=1, help=_("Path to the packages."))
370 sub_create
.add_argument("inputs", nargs
="+", help=_("Path to input packages."))
371 sub_create
.add_argument("action", action
="store_const", const
="repo_create")
373 def handle_repo_create(self
):
374 path
= self
.args
.path
[0]
376 self
.pakfire
.repo_create(path
, self
.args
.inputs
)