]> git.ipfire.org Git - pakfire.git/blob - pakfire/__init__.py
Initial import.
[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
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 # The path where we are operating in
29 self.path = path
30
31 # Save if we are in the builder mode
32 self.builder = builder
33
34 if self.builder:
35 rnd = random.sample(string.lowercase + string.digits, 12)
36 self.path = os.path.join(BUILD_ROOT, "".join(rnd))
37
38 self.debug = False
39
40 # Read configuration file(s)
41 self.config = config.Config(pakfire=self)
42 for filename in configs:
43 self.config.read(filename)
44
45 # Setup the logger
46 logger.setup_logging(self.config)
47 self.config.dump()
48
49 # Load plugins
50 self.plugins = plugins.Plugins(pakfire=self)
51
52 # Get more information about the distribution we are running
53 # or building
54 self.distro = distro.Distribution(pakfire=self)
55
56 # Load all repositories
57 self.repos = repository.Repositories(pakfire=self)
58
59 # Run plugins that implement an initialization method.
60 self.plugins.run("init")
61
62 # XXX disable repositories if passed on command line
63
64 def check_build_mode(self):
65 """
66 Check if we are running in build mode.
67 Otherwise, raise an exception.
68 """
69 if not self.builder:
70 raise BuildError, "Cannot build when not in build mode."
71
72 def check_host_arch(self, arch):
73 """
74 Check if we can build for arch.
75 """
76
77 # If no arch was given on the command line we build for our
78 # own arch which should always work.
79 if not arch:
80 return True
81
82 if not self.distro.host_supports_arch(arch):
83 raise BuildError, "Cannot build for the target architecture: %s" % arch
84
85 raise BuildError, arch
86
87 def build(self, pkg, arch=None, resultdir=None):
88 self.check_build_mode()
89 self.check_host_arch(arch)
90
91 b = builder.Builder(pakfire=self, pkg=pkg)
92 b.extract()
93
94 if not resultdir:
95 resultdir = self.config.get("resultdir")
96
97 try:
98 b.build()
99 b.copy_result(resultdir)
100 finally:
101 b.cleanup()
102
103 def shell(self, pkg, arch=None):
104 self.check_build_mode()
105 self.check_host_arch(arch)
106
107 b = builder.Builder(pakfire=self, pkg=pkg)
108 b.extract(SHELL_PACKAGES)
109
110 try:
111 b.shell()
112 finally:
113 b.cleanup()
114
115 def dist(self, pkg, resultdir=None):
116 self.check_build_mode()
117
118 b = builder.Builder(pakfire=self, pkg=pkg)
119 b.extract(build_deps=False)
120
121 if not resultdir:
122 resultdir = self.config.get("resultdir")
123
124 try:
125 b.dist()
126 b.copy_result(resultdir)
127 finally:
128 b.cleanup()
129
130 def install(self, requires):
131 ds = depsolve.DependencySet(pakfire=self)
132
133 for req in requires:
134 if isinstance(BinaryPackage, req):
135 ds.add_package(req)
136 else:
137 ds.add_requires(req)
138
139 ds.resolve()
140
141 ts = transaction.TransactionSet(self, ds)
142 ts.dump()
143
144 ret = cli.ask_user(_("Is this okay?"))
145 if not ret:
146 return
147
148 ts.run()
149