From 18edfe75eda8cd8746198ef2f0509f0d8aefd45b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 9 Apr 2011 17:16:44 +0200 Subject: [PATCH] Step two of code cleanup. Created api.py which is supposed to be imported by other applications. --- pakfire/__init__.py | 221 +------------------------------------------- pakfire/api.py | 73 +++++++++++++++ pakfire/base.py | 196 +++++++++++++++++++++++++++++++++++++-- pakfire/cli.py | 4 +- po/pakfire.pot | 10 +- 5 files changed, 272 insertions(+), 232 deletions(-) create mode 100644 pakfire/api.py diff --git a/pakfire/__init__.py b/pakfire/__init__.py index 117ea065f..efb21e43a 100644 --- a/pakfire/__init__.py +++ b/pakfire/__init__.py @@ -1,222 +1,7 @@ #!/usr/bin/python -import base -import builder -import depsolve -import packages -import transaction +from base import Pakfire -from errors import * +from constants import * -Builder = builder.Builder -Pakfire = base.Pakfire - -def install(requires, **pakfire_args): - pakfire = Pakfire(**pakfire_args) - - ds = depsolve.DependencySet(pakfire=pakfire) - - for req in requires: - if isinstance(req, packages.BinaryPackage): - ds.add_package(req) - else: - ds.add_requires(req) - - ds.resolve() - ds.dump() - - ret = cli.ask_user(_("Is this okay?")) - if not ret: - return - - ts = transaction.Transaction(pakfire, ds) - ts.run() - -def remove(**pakfire_args): - pass - -def update(pkgs, **pakfire_args): - pakfire = Pakfire(**pakfire_args) - - ds = depsolve.DependencySet(pakfire=self) - - for pkg in ds.packages: - # Skip unwanted packages (passed on command line) - if pkgs and not pkg.name in pkgs: - continue - - updates = pakfire.repos.get_by_name(pkg.name) - updates = packages.PackageListing(updates) - - latest = updates.get_most_recent() - - # If the current package is already the latest - # we skip it. - if latest == pkg: - continue - - # Otherwise we want to update the package. - ds.add_package(latest) - - ds.resolve() - ds.dump() - - ret = cli.ask_user(_("Is this okay?")) - if not ret: - return - - ts = transaction.Transaction(pakfire, ds) - ts.run() - -def info(patterns, **pakfire_args): - # Create pakfire instance. - pakfire = Pakfire(**pakfire_args) - - pkgs = [] - - for pattern in patterns: - pkgs += pakfire.repos.get_by_glob(pattern) - - return packages.PackageListing(pkgs) - -def search(pattern, **pakfire_args): - # Create pakfire instance. - pakfire = Pakfire(**pakfire_args) - - # Do the search. - pkgs = pakfire.repos.search(pattern) - - # Return the output as a package listing. - return packages.PackageListing(pkgs) - -def groupinstall(group, **pakfire_args): - pakfire = Pakfire(**pakfire_args) - - pkgs = grouplist(group, **pakfire_args) - - install(pkgs, **pakfire_args) - -def grouplist(group, **pakfire_args): - pakfire = Pakfire(**pakfire_args) - - pkgs = pakfire.repos.get_by_group(group) - - pkgs = packages.PackageListing(pkgs) - pkgs.unique() - - return [p.name for p in pkgs] - -def build(pkg, distro_config=None, build_id=None, resultdirs=None, **pakfire_args): - if not resultdirs: - resultdirs = [] - - b = Builder(pkg, distro_config, build_id=build_id, **pakfire_args) - - # Make shortcut to pakfire instance. - p = b.pakfire - - # Always include local repository. - resultdirs.append(p.repos.local_build.path) - - try: - b.prepare() - b.extract() - b.build() - b.install_test() - - # Copy-out all resultfiles - for resultdir in resultdirs: - if not resultdir: - continue - - b.copy_result(resultdir) - - except BuildError: - b.shell() - - finally: - b.destroy() - -def shell(pkg, distro_config=None, **pakfire_args): - b = builder.Builder(pkg, distro_config, **pakfire_args) - - try: - b.prepare() - b.extract() - b.shell() - finally: - b.destroy() - -def dist(pkg, resultdirs=None, **pakfire_args): - b = builder.Builder(pkg, **pakfire_args) - p = b.pakfire - - if not resultdirs: - resultdirs = [] - - # Always include local repository - resultdirs.append(p.repos.local_build.path) - - try: - b.prepare() - b.extract(build_deps=False) - - # Run the actual dist. - b.dist() - - # Copy-out all resultfiles - for resultdir in resultdirs: - if not resultdir: - continue - - b.copy_result(resultdir) - finally: - b.destroy() - -def provides(patterns, **pakfire_args): - # Create pakfire instance. - pakfire = Pakfire(**pakfire_args) - - pkgs = [] - for pattern in patterns: - requires = depsolve.Requires(None, pattern) - pkgs += pakfire.repos.get_by_provides(requires) - - pkgs = packages.PackageListing(pkgs) - #pkgs.unique() - - return pkgs - -def requires(patterns, **pakfire_args): - # Create pakfire instance. - pakfire = Pakfire(**pakfire_args) - - pkgs = [] - for pattern in patterns: - requires = depsolve.Requires(None, pattern) - pkgs += pakfire.repos.get_by_requires(requires) - - pkgs = packages.PackageListing(pkgs) - #pkgs.unique() - - return pkgs - -def repo_create(path, input_paths, **pakfire_args): - pakfire = Pakfire(**pakfire_args) - - repo = repository.LocalBinaryRepository( - pakfire, - name="new", - description="New repository.", - path=path, - ) - - for input_path in input_paths: - repo._collect_packages(input_path) - - repo.save() - -def repo_list(**pakfire_args): - pakfire = Pakfire(**pakfire_args) - - return pakfire.repos.all +__version__ = PAKFIRE_VERSION diff --git a/pakfire/api.py b/pakfire/api.py new file mode 100644 index 000000000..ff6a070fa --- /dev/null +++ b/pakfire/api.py @@ -0,0 +1,73 @@ +#!/usr/bin/python + +import base + +from errors import * + +Pakfire = base.Pakfire + +def install(requires, **pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.install(requires) + +def remove(**pakfire_args): + pass + +def update(pkgs, **pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.update(pkgs) + +def info(patterns, **pakfire_args): + # Create pakfire instance. + pakfire = Pakfire(**pakfire_args) + + return pakfire.info(patterns) + +def search(pattern, **pakfire_args): + # Create pakfire instance. + pakfire = Pakfire(**pakfire_args) + + return pakfire.search(pattern) + +def groupinstall(group, **pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.groupinstall(group) + +def grouplist(group, **pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.grouplist(group) + +def build(pkg, **kwargs): + return Pakfire.build(pkg, **kwargs) + +def shell(pkg, **kwargs): + return Pakfire.shell(pkg, **kwargs) + +def dist(pkg, **kwargs): + return Pakfire.dist(pkg, **kwargs) + +def provides(patterns, **pakfire_args): + # Create pakfire instance. + pakfire = Pakfire(**pakfire_args) + + return pakfire.provides(patterns) + +def requires(patterns, **pakfire_args): + # Create pakfire instance. + pakfire = Pakfire(**pakfire_args) + + return pakfire.requires(requires) + +def repo_create(path, input_paths, **pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.repo_create(path, input_paths) + +def repo_list(**pakfire_args): + pakfire = Pakfire(**pakfire_args) + + return pakfire.repo_list() diff --git a/pakfire/base.py b/pakfire/base.py index a5bffe4d8..963334d79 100644 --- a/pakfire/base.py +++ b/pakfire/base.py @@ -5,6 +5,7 @@ import os import random import string +import builder import config import depsolve import distro @@ -17,8 +18,6 @@ import util from constants import * from i18n import _ -__version__ = PAKFIRE_VERSION - class Pakfire(object): def __init__(self, builder=False, configs=[], disable_repos=None, distro_config=None): @@ -49,10 +48,10 @@ class Pakfire(object): self.distro = distro.Distribution(self, distro_config) self.repos = repository.Repositories(self) - # XXX Disable repositories if passed on command line - #if disable_repos: - # for repo in disable_repos: - # self.repos.disable_repo(repo) + # Disable repositories if passed on command line + if disable_repos: + for repo in disable_repos: + self.repos.disable_repo(repo) # Update all indexes of the repositories (not force) so that we will # always work with valid data. @@ -82,7 +81,6 @@ class Pakfire(object): """ Check if we can build for arch. """ - # If no arch was given on the command line we build for our # own arch which should always work. if not arch: @@ -92,3 +90,187 @@ class Pakfire(object): raise BuildError, "Cannot build for the target architecture: %s" % arch raise BuildError, arch + + def install(self, requires): + ds = depsolve.DependencySet(pakfire=self) + + for req in requires: + if isinstance(req, packages.BinaryPackage): + ds.add_package(req) + else: + ds.add_requires(req) + + ds.resolve() + ds.dump() + + ret = cli.ask_user(_("Is this okay?")) + if not ret: + return + + ts = transaction.Transaction(self, ds) + ts.run() + + def update(self, pkgs): + ds = depsolve.DependencySet(pakfire=self) + + for pkg in ds.packages: + # Skip unwanted packages (passed on command line) + if pkgs and not pkg.name in pkgs: + continue + + updates = self.repos.get_by_name(pkg.name) + updates = packages.PackageListing(updates) + + latest = updates.get_most_recent() + + # If the current package is already the latest + # we skip it. + if latest == pkg: + continue + + # Otherwise we want to update the package. + ds.add_package(latest) + + ds.resolve() + ds.dump() + + ret = cli.ask_user(_("Is this okay?")) + if not ret: + return + + ts = transaction.Transaction(self, ds) + ts.run() + + def info(self): + pkgs = [] + + for pattern in patterns: + pkgs += self.repos.get_by_glob(pattern) + + return packages.PackageListing(pkgs) + + def search(self, pattern): + # Do the search. + pkgs = self.repos.search(pattern) + + # Return the output as a package listing. + return packages.PackageListing(pkgs) + + def groupinstall(self, group): + pkgs = self.grouplist(group) + + self.install(pkgs) + + def grouplist(self, group): + pkgs = self.repos.get_by_group(group) + + pkgs = packages.PackageListing(pkgs) + pkgs.unique() + + return [p.name for p in pkgs] + + @staticmethod + def build(pkg, resultdirs=None, **kwargs): + if not resultdirs: + resultdirs = [] + + b = builder.Builder(pkg, **kwargs) + p = b.pakfire + + # Always include local repository. + resultdirs.append(p.repos.local_build.path) + + try: + b.prepare() + b.extract() + b.build() + b.install_test() + + # Copy-out all resultfiles + for resultdir in resultdirs: + if not resultdir: + continue + + b.copy_result(resultdir) + + except BuildError: + b.shell() + + finally: + b.destroy() + + @staticmethod + def shell(pkg, **kwargs): + b = builder.Builder(pkg, **kwargs) + + try: + b.prepare() + b.extract() + b.shell() + finally: + b.destroy() + + @staticmethod + def dist(pkg, resultdirs=None, **pakfire_args): + b = builder.Builder(pkg, **pakfire_args) + p = b.pakfire + + if not resultdirs: + resultdirs = [] + + # Always include local repository + resultdirs.append(p.repos.local_build.path) + + try: + b.prepare() + b.extract(build_deps=False) + + # Run the actual dist. + b.dist() + + # Copy-out all resultfiles + for resultdir in resultdirs: + if not resultdir: + continue + + b.copy_result(resultdir) + finally: + b.destroy() + + def provides(self, patterns): + pkgs = [] + for pattern in patterns: + requires = depsolve.Requires(None, pattern) + pkgs += self.repos.get_by_provides(requires) + + pkgs = packages.PackageListing(pkgs) + #pkgs.unique() + + return pkgs + + def requires(self, patterns): + pkgs = [] + for pattern in patterns: + requires = depsolve.Requires(None, pattern) + pkgs += self.repos.get_by_requires(requires) + + pkgs = packages.PackageListing(pkgs) + #pkgs.unique() + + return pkgs + + def repo_create(self, path, input_paths): + repo = repository.LocalBinaryRepository( + self, + name="new", + description="New repository.", + path=path, + ) + + for input_path in input_paths: + repo._collect_packages(input_path) + + repo.save() + + def repo_list(self): + return self.repos.all diff --git a/pakfire/cli.py b/pakfire/cli.py index c4eab2160..35a1ef527 100644 --- a/pakfire/cli.py +++ b/pakfire/cli.py @@ -8,7 +8,7 @@ import repository import server import util -import pakfire +import pakfire.api as pakfire from constants import * from i18n import _ @@ -348,7 +348,7 @@ class CliBuilder(Cli): "arch" : self.args.arch, } - pakfire.build(pkg, distro_config, resultdirs=[self.args.resultdir,], + pakfire.build(pkg, distro_config=distro_config, resultdirs=[self.args.resultdir,], **self.pakfire_args) def handle_shell(self): diff --git a/po/pakfire.pot b/po/pakfire.pot index dd3dbde38..9c78be8d0 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-04-09 14:55+0200\n" +"POT-Creation-Date: 2011-04-09 17:16+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,6 +17,10 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" +#: ../pakfire/base.py:106 ../pakfire/base.py:137 +msgid "Is this okay?" +msgstr "" + #: ../pakfire/builder.py:188 #, python-format msgid "Extracting: %s (source)" @@ -281,10 +285,6 @@ msgstr "" msgid "Total download size: %s" msgstr "" -#: ../pakfire/__init__.py:28 ../pakfire/__init__.py:64 -msgid "Is this okay?" -msgstr "" - #: ../pakfire/packages/base.py:71 msgid "Name" msgstr "" -- 2.39.5