]>
git.ipfire.org Git - ipfire.org.git/blob - build/builder.py
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
27 from pysqlite2
import dbapi2
as sqlite
31 from constants
import config
34 def __init__(self
, path
):
35 self
.db
= sqlite
.connect(os
.path
.join(path
, config
["db_name"]))
38 create table if not exists config(key, value, date);
39 create table if not exists durations(duration);
51 return self
.db
.cursor()
57 def __init__(self
, db
, key
):
66 c
.execute("SELECT value FROM %(table)s WHERE key = '%(key)s'" \
67 % { "table" : "config",
70 self
.data
= c
.fetchone()[0]
81 c
.execute("SELECT date FROM %(table)s WHERE key = '%(key)s'" \
82 % { "table" : "config",
85 self
.date
= float("%s" % c
.fetchone()[0])
89 return self
.date
or float(0)
95 sql
= "INSERT INTO %(table)s(key, value, date) VALUES('%(key)s', '%(value)s', '%(date)s')" \
96 % { "table" : "config",
99 "date" : time
.time(), }
102 sql
= "UPDATE %(table)s SET value='%(value)s', date='%(date)s' WHERE key='%(key)s'" \
103 % { "table" : "config",
106 "date" : time
.time(), }
112 class DurationsConfig
:
113 def __init__(self
, db
):
116 def get(self
, sort
=0):
118 c
.execute("SELECT duration FROM durations")
120 for value
in c
.fetchall():
121 value
= int("%s" % value
)
122 if value
< 900: # 15min
129 def set(self
, value
):
132 c
.execute("INSERT INTO %(table)s(duration) VALUES('%(value)s')" \
133 % { "table" : "durations",
140 durations
= self
.get()
141 if not len(durations
):
143 for value
in durations
:
145 avg
= sum / len(durations
)
148 def get_eta(self
, timestamp
):
152 eta
= int(timestamp
) + avg
153 return time
.ctime(eta
)
155 class DistccConfig(DatabaseConfig
):
156 def __init__(self
, db
, key
, hostname
, jobs
):
157 DatabaseConfig
.__init
__(self
, db
, key
)
158 self
.hostname
= hostname
162 if not self
.ping() or self
.get() == "0":
164 return "%s:%s/%s,lzo \t# %s" % \
165 (socket
.gethostbyname(self
.hostname
), self
.get(), self
.jobs
or "4", self
.hostname
)
168 if not self
.hostname
:
170 return not os
.system("ping -c1 -w1 %s &>/dev/null" % self
.hostname
)
173 return os
.popen("distcc --version").readlines()
176 def __init__(self
, path
, filetype
):
177 self
.filename
= os
.path
.join(path
, config
["path"][filetype
])
179 # Create the file if not existant
180 if not os
.access(self
.filename
, os
.R_OK
):
181 f
= open(self
.filename
, "w")
187 f
= open(self
.filename
)
192 return ret
or ["Log is empty."]
196 def set(self
, lines
):
197 f
= open(self
.filename
, "w")
198 for line
in base64
.b64decode(lines
).split("\n"):
199 f
.write("%s\n" % line
.rstrip("\n"))
203 def __init__(self
, config
, uuid
):
206 self
.path
= os
.path
.join(self
.config
['path']['db'], self
.uuid
)
208 if not os
.access(self
.path
, os
.R_OK
):
214 self
.db
= Database(self
.path
)
216 self
.hostname
= DatabaseConfig(self
.db
, "hostname")
217 self
.state
= DatabaseConfig(self
.db
, "state")
218 self
.package
= DatabaseConfig(self
.db
, "package")
220 self
.duration
= DurationsConfig(self
.db
)
221 self
.jobs
= DatabaseConfig(self
.db
, "jobs")
222 self
.distcc
= DistccConfig(self
.db
, "distcc", self
.hostname(), self
.jobs())
224 self
.log
= FileConfig(self
.path
, "log")
226 def set(self
, key
, value
):
227 eval("self.%s.set(\"%s\")" % (key
, value
,))
230 return eval("self.%s.get()" % (key
,))
232 def getAllBuilders(age
=0):
234 for uuid
in os
.listdir(config
["path"]["db"]):
235 if uuid
== "empty.txt": continue
236 builder
= Builder(config
, uuid
)
237 # If there was no activity since "age" days -> continue...
238 if age
and (time
.time() - builder
.state
.time()) > age
*24*60*60:
240 builders
.append(builder
)