]> git.ipfire.org Git - pakfire.git/blame - pakfire/__init__.py
Improve repository handling.
[pakfire.git] / pakfire / __init__.py
CommitLineData
47a4cb89
MT
1#!/usr/bin/python
2
3import logging
4import os
5import random
6import string
7
8import builder
9import config
10import database
11import depsolve
12import distro
13import logger
14import packages
15import plugins
16import repository
17import transaction
18
19from constants import *
f781b1ab 20from errors import BuildError, PakfireError
47a4cb89
MT
21from i18n import _
22
23__version__ = 0.1
24
25
26class Pakfire(object):
f781b1ab
MT
27 def __init__(self, path="/tmp/pakfire", builder=False, configs=[],
28 disable_repos=None):
47a4cb89
MT
29 # The path where we are operating in
30 self.path = path
31
32 # Save if we are in the builder mode
33 self.builder = builder
34
35 if self.builder:
36 rnd = random.sample(string.lowercase + string.digits, 12)
37 self.path = os.path.join(BUILD_ROOT, "".join(rnd))
38
39 self.debug = False
40
41 # Read configuration file(s)
42 self.config = config.Config(pakfire=self)
43 for filename in configs:
44 self.config.read(filename)
45
46 # Setup the logger
47 logger.setup_logging(self.config)
48 self.config.dump()
49
50 # Load plugins
51 self.plugins = plugins.Plugins(pakfire=self)
52
53 # Get more information about the distribution we are running
54 # or building
55 self.distro = distro.Distribution(pakfire=self)
56
57 # Load all repositories
58 self.repos = repository.Repositories(pakfire=self)
59
60 # Run plugins that implement an initialization method.
61 self.plugins.run("init")
62
f781b1ab
MT
63 # Disable repositories if passed on command line
64 if disable_repos:
65 for repo in disable_repos:
66 self.repos.disable_repo(repo)
67
68 # Check if there is at least one enabled repository.
69 if len(self.repos) < 2:
70 raise PakfireError, "No repositories were configured."
71
72 # Update all indexes of the repositories (not force) so that we will
73 # always work with valid data.
74 self.repos.update_indexes()
47a4cb89
MT
75
76 def check_build_mode(self):
77 """
78 Check if we are running in build mode.
79 Otherwise, raise an exception.
80 """
81 if not self.builder:
82 raise BuildError, "Cannot build when not in build mode."
83
84 def check_host_arch(self, arch):
85 """
86 Check if we can build for arch.
87 """
88
89 # If no arch was given on the command line we build for our
90 # own arch which should always work.
91 if not arch:
92 return True
93
94 if not self.distro.host_supports_arch(arch):
95 raise BuildError, "Cannot build for the target architecture: %s" % arch
96
97 raise BuildError, arch
98
0ec833c6 99 def build(self, pkg, arch=None, resultdirs=None):
47a4cb89
MT
100 self.check_build_mode()
101 self.check_host_arch(arch)
102
0ec833c6
MT
103 if not resultdirs:
104 resultdirs = []
105
106 # Always include local repository
107 resultdirs.append(self.repos.local_build.path)
108
47a4cb89
MT
109 b = builder.Builder(pakfire=self, pkg=pkg)
110 b.extract()
111
47a4cb89
MT
112 try:
113 b.build()
0ec833c6
MT
114
115 # Copy-out all resultfiles
116 for resultdir in resultdirs:
117 if not resultdir:
118 continue
119
120 b.copy_result(resultdir)
47a4cb89
MT
121 finally:
122 b.cleanup()
123
124 def shell(self, pkg, arch=None):
125 self.check_build_mode()
126 self.check_host_arch(arch)
127
128 b = builder.Builder(pakfire=self, pkg=pkg)
129 b.extract(SHELL_PACKAGES)
130
131 try:
132 b.shell()
133 finally:
134 b.cleanup()
135
136 def dist(self, pkg, resultdir=None):
137 self.check_build_mode()
138
139 b = builder.Builder(pakfire=self, pkg=pkg)
140 b.extract(build_deps=False)
141
142 if not resultdir:
143 resultdir = self.config.get("resultdir")
144
145 try:
146 b.dist()
147 b.copy_result(resultdir)
148 finally:
149 b.cleanup()
150
151 def install(self, requires):
152 ds = depsolve.DependencySet(pakfire=self)
153
154 for req in requires:
5e87fa4f 155 if isinstance(req, packages.BinaryPackage):
47a4cb89
MT
156 ds.add_package(req)
157 else:
158 ds.add_requires(req)
159
160 ds.resolve()
161
162 ts = transaction.TransactionSet(self, ds)
163 ts.dump()
164
165 ret = cli.ask_user(_("Is this okay?"))
166 if not ret:
167 return
168
169 ts.run()
170
fa6d335b
MT
171 def provides(self, patterns):
172 pkgs = []
173
174 for pattern in patterns:
175 pkgs += self.repos.get_by_provides(pattern)
176
177 pkgs = packages.PackageListing(pkgs)
178 #pkgs.unique()
179
180 return pkgs
181
66af936c 182 def repo_create(self, path, input_paths):
fa6d335b
MT
183 if not os.path.exists(path) or not os.path.isdir(path):
184 raise PakfireError, "Given path is not existant or not a directory: %s" % path
185
66af936c 186 repo = repository.LocalRepository(
fa6d335b
MT
187 self,
188 name="new",
189 description="New repository.",
66af936c 190 path=path,
fa6d335b
MT
191 )
192
66af936c
MT
193 for input_path in input_paths:
194 repo._collect_packages(input_path)
fa6d335b 195