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