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