]>
Commit | Line | Data |
---|---|---|
9137135a MT |
1 | #!/usr/bin/python |
2 | ||
3 | import tornado.web | |
4 | ||
5 | from handlers_auth import * | |
6 | from handlers_base import * | |
f6e6ff79 | 7 | from handlers_builds import * |
9137135a | 8 | from handlers_builders import * |
f6e6ff79 MT |
9 | from handlers_distro import * |
10 | from handlers_jobs import * | |
11 | from handlers_keys import * | |
12 | from handlers_mirrors import * | |
13 | from handlers_packages import * | |
9137135a | 14 | from handlers_search import * |
f6e6ff79 | 15 | from handlers_updates import * |
9137135a MT |
16 | from handlers_users import * |
17 | ||
18 | class IndexHandler(BaseHandler): | |
19 | def get(self): | |
6e63ed49 | 20 | jobs = self.pakfire.jobs.get_active() |
163d9d8b | 21 | jobs += self.pakfire.jobs.get_next() |
6e63ed49 | 22 | jobs += self.pakfire.jobs.get_latest(age="24 HOUR", limit=5) |
9137135a | 23 | |
f6e6ff79 MT |
24 | # Updates |
25 | updates = [] | |
26 | active = True | |
27 | for type in ("stable", "unstable", "testing"): | |
28 | u = self.pakfire.updates.get_latest(type=type) | |
29 | if u: | |
30 | updates.append((type, u, active)) | |
31 | active = False | |
9137135a | 32 | |
6e63ed49 | 33 | self.render("index.html", jobs=jobs, updates=updates) |
9137135a | 34 | |
9137135a | 35 | |
f6e6ff79 | 36 | class Error404Handler(BaseHandler): |
9137135a | 37 | def get(self): |
f6e6ff79 | 38 | raise tornado.web.HTTPError(404) |
9137135a | 39 | |
9137135a | 40 | |
f6e6ff79 MT |
41 | class StatisticsMainHandler(BaseHandler): |
42 | def get(self): | |
43 | args = {} | |
9137135a | 44 | |
f6e6ff79 MT |
45 | # Build statistics. |
46 | args.update({ | |
47 | "builds_count" : self.pakfire.builds.count(), | |
48 | }) | |
9137135a | 49 | |
f6e6ff79 MT |
50 | # Job statistics. |
51 | args.update({ | |
52 | "jobs_count_all" : self.pakfire.jobs.count(), | |
53 | "jobs_avg_build_time" : self.pakfire.jobs.get_average_build_time(), | |
54 | }) | |
9137135a | 55 | |
b5690ee3 | 56 | self.render("statistics/index.html", **args) |
9137135a | 57 | |
9137135a | 58 | |
f6e6ff79 | 59 | class UploadsHandler(BaseHandler): |
9137135a | 60 | @tornado.web.authenticated |
9137135a | 61 | def get(self): |
f6e6ff79 MT |
62 | if not self.current_user.is_admin(): |
63 | raise tornado.web.HTTPError(403) | |
9137135a | 64 | |
f6e6ff79 | 65 | uploads = self.pakfire.uploads.get_all() |
9137135a | 66 | |
f6e6ff79 | 67 | self.render("uploads-list.html", uploads=uploads) |
9137135a MT |
68 | |
69 | ||
70 | class DocsIndexHandler(BaseHandler): | |
71 | def get(self): | |
72 | self.render("docs-index.html") | |
73 | ||
74 | ||
75 | class DocsBuildsHandler(BaseHandler): | |
76 | def get(self): | |
77 | self.render("docs-build.html") | |
78 | ||
79 | ||
80 | class DocsUsersHandler(BaseHandler): | |
81 | def get(self): | |
82 | self.render("docs-users.html") | |
83 | ||
84 | ||
f6e6ff79 | 85 | class DocsWhatsthisHandler(BaseHandler): |
9137135a | 86 | def get(self): |
f6e6ff79 | 87 | self.render("docs-whatsthis.html") |
9137135a MT |
88 | |
89 | ||
90 | class FileDetailHandler(BaseHandler): | |
91 | def get(self, uuid): | |
92 | pkg, file = self.pakfire.packages.get_with_file_by_uuid(uuid) | |
93 | ||
94 | if not file: | |
95 | raise tornado.web.HTTPError(404, "File not found") | |
96 | ||
97 | self.render("file-detail.html", pkg=pkg, file=file) | |
98 | ||
99 | ||
f6e6ff79 | 100 | class LogHandler(BaseHandler): |
9137135a | 101 | def get(self): |
f6e6ff79 | 102 | self.render("log.html", log=self.pakfire.log) |
9137135a MT |
103 | |
104 | ||
20d7f5eb MT |
105 | class SessionsHandler(BaseHandler): |
106 | def prepare(self): | |
107 | # This is only accessible for administrators. | |
108 | if not self.current_user.is_admin(): | |
109 | raise tornado.web.HTTPError(403) | |
110 | ||
111 | @tornado.web.authenticated | |
112 | def get(self): | |
113 | sessions = self.pakfire.sessions.get_all() | |
114 | ||
115 | # Sort the sessions by user. | |
116 | users = {} | |
117 | ||
118 | for s in sessions: | |
119 | try: | |
120 | users[s.user].append(s) | |
121 | except KeyError: | |
122 | users[s.user] = [s] | |
123 | ||
124 | sessions = sorted(users.items()) | |
125 | ||
126 | self.render("sessions/index.html", sessions=sessions) | |
127 | ||
128 | ||
f6e6ff79 MT |
129 | class RepositoryDetailHandler(BaseHandler): |
130 | def get(self, distro, repo): | |
131 | distro = self.pakfire.distros.get_by_name(distro) | |
9137135a | 132 | if not distro: |
f6e6ff79 | 133 | raise tornado.web.HTTPError(404) |
9137135a | 134 | |
f6e6ff79 MT |
135 | repo = distro.get_repo(repo) |
136 | if not repo: | |
137 | raise tornado.web.HTTPError(404) | |
9137135a | 138 | |
f6e6ff79 MT |
139 | limit = self.get_argument("limit", 50) |
140 | try: | |
141 | limit = int(limit) | |
142 | except ValueError: | |
143 | limit = None | |
9137135a | 144 | |
f6e6ff79 MT |
145 | offset = self.get_argument("offset", 0) |
146 | try: | |
147 | offset = int(offset) | |
148 | except ValueError: | |
149 | offset = None | |
9137135a | 150 | |
f6e6ff79 MT |
151 | builds = repo.get_builds(limit=limit, offset=offset) |
152 | unpushed_builds = repo.get_unpushed_builds() | |
153 | obsolete_builds = repo.get_obsolete_builds() | |
154 | ||
155 | # Get the build times of this repository. | |
156 | build_times = repo.get_build_times() | |
157 | ||
158 | self.render("repository-detail.html", distro=distro, repo=repo, | |
159 | builds=builds, unpushed_builds=unpushed_builds, | |
160 | obsolete_builds=obsolete_builds, build_times=build_times) | |
9137135a | 161 | |
9137135a | 162 | |
f6e6ff79 | 163 | class RepositoryEditHandler(BaseHandler): |
9137135a | 164 | @tornado.web.authenticated |
f6e6ff79 MT |
165 | def get(self, distro, repo): |
166 | distro = self.pakfire.distros.get_by_name(distro) | |
9137135a | 167 | if not distro: |
f6e6ff79 | 168 | raise tornado.web.HTTPError(404) |
9137135a | 169 | |
f6e6ff79 MT |
170 | repo = distro.get_repo(repo) |
171 | if not repo: | |
172 | raise tornado.web.HTTPError(404) | |
9137135a | 173 | |
f6e6ff79 | 174 | # XXX check if user has permissions to do this |
9137135a | 175 | |
f6e6ff79 | 176 | self.render("repository-edit.html", distro=distro, repo=repo) |
9137135a MT |
177 | |
178 | ||
f6e6ff79 MT |
179 | class RepositoryConfHandler(BaseHandler): |
180 | def get(self, distro, repo): | |
181 | distro = self.pakfire.distros.get_by_name(distro) | |
182 | if not distro: | |
183 | raise tornado.web.HTTPError(404) | |
9137135a | 184 | |
f6e6ff79 MT |
185 | repo = distro.get_repo(repo) |
186 | if not repo: | |
187 | raise tornado.web.HTTPError(404) | |
9137135a | 188 | |
f6e6ff79 MT |
189 | # This is a plaintext file. |
190 | self.set_header("Content-Type", "text/plain") | |
191 | ||
192 | # Write the header. | |
193 | self.write("# Downloaded from the pakfire build service on %s.\n\n" \ | |
194 | % datetime.datetime.utcnow()) | |
195 | self.write(repo.get_conf()) | |
196 | self.finish() | |
197 | ||
198 | ||
199 | class RepositoryMirrorlistHandler(BaseHandler): | |
9137135a MT |
200 | def get(self, distro, repo): |
201 | distro = self.pakfire.distros.get_by_name(distro) | |
202 | if not distro: | |
203 | raise tornado.web.HTTPError(404) | |
204 | ||
205 | repo = distro.get_repo(repo) | |
206 | if not repo: | |
207 | raise tornado.web.HTTPError(404) | |
208 | ||
f6e6ff79 MT |
209 | # This is a plaintext file. |
210 | self.set_header("Content-Type", "text/plain") | |
211 | ||
212 | arch = self.get_argument("arch", None) | |
213 | arch = self.pakfire.arches.get_by_name(arch) | |
214 | ||
215 | if not arch: | |
216 | raise tornado.web.HTTPError(400, "You must specify the architecture.") | |
217 | ||
218 | ret = { | |
219 | "type" : "mirrorlist", | |
220 | "version" : 1, | |
221 | } | |
222 | ||
223 | # A list with mirrors that are sent to the user. | |
224 | mirrors = [] | |
225 | ||
226 | # Only search for mirrors on repositories that are supposed to be found | |
227 | # on mirror servers. | |
228 | ||
229 | if repo.mirrored: | |
230 | # See how many mirrors we can max. find. | |
231 | num_mirrors = self.mirrors.count(status="enabled") | |
232 | assert num_mirrors > 0 | |
233 | ||
234 | # Create a list with all mirrors that is up to 50 mirrors long. | |
235 | # First add all preferred mirrors and then fill the rest up | |
236 | # with other mirrors. | |
237 | if num_mirrors >= 10: | |
238 | MAX_MIRRORS = 10 | |
239 | else: | |
240 | MAX_MIRRORS = num_mirrors | |
241 | ||
242 | ||
243 | for mirror in self.mirrors.get_for_location(self.remote_address): | |
244 | mirrors.append({ | |
245 | "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch.name)), | |
246 | "location" : mirror.country_code, | |
247 | "preferred" : 1, | |
248 | }) | |
249 | ||
250 | while MAX_MIRRORS - len(mirrors) > 0: | |
251 | mirror = self.mirrors.get_random(limit=1)[0] | |
252 | ||
253 | mirror = { | |
254 | "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch.name)), | |
255 | "location" : mirror.country_code, | |
256 | "preferred" : 0, | |
257 | } | |
258 | ||
259 | if not mirror in mirrors: | |
260 | mirrors.append(mirror) | |
261 | ||
262 | else: | |
263 | repo_baseurl = self.pakfire.settings.get("repository_baseurl") | |
264 | if repo_baseurl.endswith("/"): | |
265 | repo_baseurl = repo_baseurl[:-1] | |
266 | ||
267 | for mirror in self.mirrors.get_all(): | |
268 | print mirror.url, repo_baseurl | |
269 | if not mirror.url == repo_baseurl: | |
270 | continue | |
271 | ||
272 | mirror = { | |
273 | "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch.name)), | |
274 | "location" : mirror.country_code, | |
275 | "preferred" : 0, | |
276 | } | |
277 | ||
278 | mirrors.append(mirror) | |
279 | break | |
280 | ||
281 | ret["mirrors"] = mirrors | |
282 | self.write(ret) | |
283 | ||
9137135a MT |
284 | |
285 | ||
286 | class RepoActionHandler(BaseHandler): | |
287 | @tornado.web.authenticated | |
288 | def post(self, type): | |
289 | assert type in ("run", "remove") | |
290 | ||
291 | action_id = self.get_argument("id") | |
292 | ||
293 | action = self.pakfire.repos.get_action_by_id(action_id) | |
294 | if not action: | |
295 | raise tornado.web.HTTPError(400) | |
296 | ||
297 | if type == "run": | |
298 | action.run(self.current_user) | |
299 | ||
300 | elif type == "remove": | |
301 | action.delete(self.current_user) |