]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/__init__.py
Cleanup database initialisation code
[people/jschlag/pbs.git] / src / buildservice / __init__.py
1 #!/usr/bin/python
2
3 from __future__ import absolute_import
4
5 import ConfigParser
6 import logging
7 import os
8 import pakfire
9
10 from . import arches
11 from . import bugtracker
12 from . import builders
13 from . import builds
14 from . import cache
15 from . import database
16 from . import distribution
17 from . import geoip
18 from . import keys
19 from . import logs
20 from . import messages
21 from . import mirrors
22 from . import packages
23 from . import repository
24 from . import settings
25 from . import sessions
26 from . import sources
27 from . import updates
28 from . import uploads
29 from . import users
30
31 log = logging.getLogger("backend")
32 log.propagate = 1
33
34 # Import version
35 from .__version__ import VERSION as __version__
36
37 from .decorators import *
38 from .constants import *
39
40 class Backend(object):
41 def __init__(self, config_file=None):
42 # Read configuration file.
43 self.config = self.read_config(config_file)
44
45 # Global pakfire settings (from database).
46 self.settings = settings.Settings(self)
47
48 self.arches = arches.Arches(self)
49 self.builds = builds.Builds(self)
50 self.cache = cache.Cache(self)
51 self.geoip = geoip.GeoIP(self)
52 self.jobs = builds.Jobs(self)
53 self.builders = builders.Builders(self)
54 self.distros = distribution.Distributions(self)
55 self.keys = keys.Keys(self)
56 self.messages = messages.Messages(self)
57 self.mirrors = mirrors.Mirrors(self)
58 self.packages = packages.Packages(self)
59 self.repos = repository.Repositories(self)
60 self.sessions = sessions.Sessions(self)
61 self.sources = sources.Sources(self)
62 self.updates = updates.Updates(self)
63 self.uploads = uploads.Uploads(self)
64 self.users = users.Users(self)
65
66 # Open a connection to bugzilla.
67 self.bugzilla = bugtracker.Bugzilla(self)
68
69 # A pool to store strings (for comparison).
70 self.pool = pakfire.satsolver.Pool("dummy")
71
72 def read_config(self, path):
73 c = ConfigParser.SafeConfigParser()
74
75 # Load default configuration file first
76 paths = [
77 os.path.join(CONFIGSDIR, "pbs.conf"),
78 ]
79
80 if path:
81 paths.append(path)
82
83 # Load all configuration files
84 for path in paths:
85 log.debug("Loading configuration from %s" % path)
86 c.read(path)
87
88 return c
89
90 @lazy_property
91 def db(self):
92 name = self.config.get("database", "name")
93 hostname = self.config.get("database", "hostname")
94 user = self.config.get("database", "user")
95 password = self.config.get("database", "password")
96
97 log.debug("Connecting to database %s @ %s" % (name, hostname))
98
99 return database.Connection(hostname, name, user=user, password=password)
100
101 def cleanup_files(self):
102 query = self.db.query("SELECT * FROM queue_delete")
103
104 for row in query:
105 if not row.path:
106 continue
107
108 path = os.path.join(PACKAGES_DIR, row.path)
109
110 try:
111 logging.debug("Removing %s..." % path)
112 os.unlink(path)
113 except OSError, e:
114 logging.error("Could not remove %s: %s" % (path, e))
115
116 while True:
117 path = os.path.dirname(path)
118
119 # Stop if we are running outside of the tree.
120 if not path.startswith(PACKAGES_DIR):
121 break
122
123 # If the directory is not empty, we cannot remove it.
124 if os.path.exists(path) and os.listdir(path):
125 break
126
127 try:
128 logging.debug("Removing %s..." % path)
129 os.rmdir(path)
130 except OSError, e:
131 logging.error("Could not remove %s: %s" % (path, e))
132 break
133
134 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)