]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/__init__.py
Rename main object from Pakfire to Backend
[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 keys
18 from . import logs
19 from . import messages
20 from . import mirrors
21 from . import packages
22 from . import repository
23 from . import settings
24 from . import sessions
25 from . import sources
26 from . import updates
27 from . import uploads
28 from . import users
29
30 from .constants import *
31
32 class Backend(object):
33 def __init__(self, config_file="pbs.conf"):
34 # Read configuration file.
35 self.config = self.read_config(config_file)
36
37 # Connect to databases.
38 self.db = self.connect_database()
39 self.geoip_db = self.connect_database("geoip-database")
40
41 # Global pakfire settings (from database).
42 self.settings = settings.Settings(self)
43
44 self.arches = arches.Arches(self)
45 self.builds = builds.Builds(self)
46 self.cache = cache.Cache(self)
47 self.geoip = mirrors.GeoIP(self)
48 self.jobs = builds.Jobs(self)
49 self.builders = builders.Builders(self)
50 self.distros = distribution.Distributions(self)
51 self.keys = keys.Keys(self)
52 self.messages = messages.Messages(self)
53 self.mirrors = mirrors.Mirrors(self)
54 self.packages = packages.Packages(self)
55 self.repos = repository.Repositories(self)
56 self.sessions = sessions.Sessions(self)
57 self.sources = sources.Sources(self)
58 self.updates = updates.Updates(self)
59 self.uploads = uploads.Uploads(self)
60 self.users = users.Users(self)
61
62 # Open a connection to bugzilla.
63 self.bugzilla = bugtracker.Bugzilla(self)
64
65 # A pool to store strings (for comparison).
66 self.pool = pakfire.satsolver.Pool("dummy")
67
68 def __del__(self):
69 if self.db:
70 self.db.close()
71 del self.db
72
73 def read_config(self, path):
74 c = ConfigParser.SafeConfigParser()
75 c.read(path)
76
77 return c
78
79 def connect_database(self, section="database"):
80 db = self.config.get(section, "db")
81 host = self.config.get(section, "host")
82 user = self.config.get(section, "user")
83
84 if self.config.has_option(section, "pass"):
85 pw = self.config.get(section, "pass")
86 else:
87 pw = None
88
89 return database.Connection(host, db, user=user, password=pw)
90
91 def cleanup_files(self):
92 query = self.db.query("SELECT * FROM queue_delete")
93
94 for row in query:
95 if not row.path:
96 continue
97
98 path = os.path.join(PACKAGES_DIR, row.path)
99
100 try:
101 logging.debug("Removing %s..." % path)
102 os.unlink(path)
103 except OSError, e:
104 logging.error("Could not remove %s: %s" % (path, e))
105
106 while True:
107 path = os.path.dirname(path)
108
109 # Stop if we are running outside of the tree.
110 if not path.startswith(PACKAGES_DIR):
111 break
112
113 # If the directory is not empty, we cannot remove it.
114 if os.path.exists(path) and os.listdir(path):
115 break
116
117 try:
118 logging.debug("Removing %s..." % path)
119 os.rmdir(path)
120 except OSError, e:
121 logging.error("Could not remove %s: %s" % (path, e))
122 break
123
124 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)