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