1 # Copyright (C) 2009-2011 Gianluca Montecchi <gian@grys.it>
2 # Mathieu Clabaut <mathieu.clabaut@gmail.com>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Bugs Everywhere.
7 # Bugs Everywhere is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 2 of the License, or (at your
10 # option) any later version.
12 # Bugs Everywhere is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Bugs Everywhere. If not, see <http://www.gnu.org/licenses/>.
27 import xml.sax.saxutils
29 from jinja2 import Environment, FileSystemLoader, DictLoader, ChoiceLoader
33 import libbe.command.util
35 import libbe.util.encoding
37 import libbe.command.depend
40 class HTML (libbe.command.Command):
41 """Generate a static HTML dump of the current repository status
44 >>> import libbe.bugdir
45 >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
46 >>> io = libbe.command.StringInputOutput()
47 >>> io.stdout = sys.stdout
48 >>> ui = libbe.command.UserInterface(io=io)
49 >>> ui.storage_callbacks.set_storage(bd.storage)
52 >>> ret = ui.run(cmd, {'output':os.path.join(bd.storage.repo, 'html_export')})
53 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export'))
55 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export', 'index.html'))
57 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export', 'index_inactive.html'))
59 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export', 'bugs'))
61 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export', 'bugs', 'a', 'index.html'))
63 >>> os.path.exists(os.path.join(bd.storage.repo, 'html_export', 'bugs', 'b', 'index.html'))
70 def __init__(self, *args, **kwargs):
71 libbe.command.Command.__init__(self, *args, **kwargs)
73 libbe.command.Option(name='output', short_name='o',
74 help='Set the output path (%default)',
75 arg=libbe.command.Argument(
76 name='output', metavar='DIR', default='./html_export',
77 completion_callback=libbe.command.util.complete_path)),
78 libbe.command.Option(name='template-dir', short_name='t',
79 help='Use a different template. Defaults to internal templates',
80 arg=libbe.command.Argument(
81 name='template-dir', metavar='DIR',
82 completion_callback=libbe.command.util.complete_path)),
83 libbe.command.Option(name='title',
84 help='Set the bug repository title (%default)',
85 arg=libbe.command.Argument(
86 name='title', metavar='STRING',
87 default='Bugs Everywhere Issue Tracker')),
88 libbe.command.Option(name='index-header',
89 help='Set the index page headers (%default)',
90 arg=libbe.command.Argument(
91 name='index-header', metavar='STRING',
92 default='Bugs Everywhere Bug List')),
93 libbe.command.Option(name='export-template', short_name='e',
94 help='Export the default template and exit.'),
95 libbe.command.Option(name='export-template-dir', short_name='d',
96 help='Set the directory for the template export (%default)',
97 arg=libbe.command.Argument(
98 name='export-template-dir', metavar='DIR',
99 default='./default-templates/',
100 completion_callback=libbe.command.util.complete_path)),
101 libbe.command.Option(name='min-id-length', short_name='l',
102 help='Attempt to truncate bug and comment IDs to this length. Set to -1 for non-truncated IDs (%default)',
103 arg=libbe.command.Argument(
104 name='min-id-length', metavar='INT',
105 default=-1, type='int')),
106 libbe.command.Option(name='verbose', short_name='v',
107 help='Verbose output, default is %default'),
110 def _run(self, **params):
111 if params['export-template'] == True:
114 bugdir = self._get_bugdir()
115 bugdir.load_all_bugs()
116 html_gen = HTMLGen(bugdir,
117 template_dir=params['template-dir'],
118 title=params['title'],
119 header=params['index-header'],
120 min_id_length=params['min-id-length'],
121 verbose=params['verbose'],
123 if params['export-template'] == True:
124 html_gen.write_default_template(params['export-template-dir'])
126 html_gen.run(params['output'])
128 def _long_help(self):
130 Generate a set of html pages representing the current state of the bug
134 Html = HTML # alias for libbe.command.base.get_command_class()
136 class HTMLGen (object):
137 def __init__(self, bd, template_dir=None,
138 title="Site Title", header="Header",
140 verbose=False, encoding=None, stdout=None,
142 self.generation_time = time.ctime()
146 self.verbose = verbose
149 self.encoding = encoding
151 self.encoding = libbe.util.encoding.get_filesystem_encoding()
152 self._load_templates(template_dir)
153 self.min_id_length = min_id_length
155 def run(self, out_dir):
156 if self.verbose == True:
157 print >> self.stdout, \
158 'Creating the html output in %s using templates in %s' \
159 % (out_dir, self.template)
164 bugs = [b for b in self.bd]
168 if b.active == True and b.severity != 'target':
169 bugs_active.append(b)
170 if b.active != True and b.severity != 'target':
171 bugs_inactive.append(b)
172 if b.severity == 'target':
173 bugs_target.append(b)
175 self._create_output_directories(out_dir)
176 self._write_css_file()
178 if b.severity == 'target':
179 up_link = '../../index_target.html'
181 up_link = '../../index.html'
183 up_link = '../../index_inactive.html'
184 self._write_bug_file(
185 b, title=self.title, header=self.header,
187 self._write_index_file(
188 bugs_active, title=self.title,
189 header=self.header, bug_type='active')
190 self._write_index_file(
191 bugs_inactive, title=self.title,
192 header=self.header, bug_type='inactive')
193 self._write_index_file(
194 bugs_target, title=self.title,
195 header=self.header, bug_type='target')
197 def _truncated_bug_id(self, bug):
198 return libbe.util.id._truncate(
199 bug.uuid, bug.sibling_uuids(),
200 min_length=self.min_id_length)
202 def _truncated_comment_id(self, comment):
203 return libbe.util.id._truncate(
204 comment.uuid, comment.sibling_uuids(),
205 min_length=self.min_id_length)
207 def _create_output_directories(self, out_dir):
209 print >> self.stdout, 'Creating output directories'
210 self.out_dir = self._make_dir(out_dir)
211 self.out_dir_bugs = self._make_dir(
212 os.path.join(self.out_dir, 'bugs'))
214 def _write_css_file(self):
216 print >> self.stdout, 'Writing css file'
217 assert hasattr(self, 'out_dir'), \
218 'Must run after ._create_output_directories()'
219 template = self.template.get_template('style.css')
220 self._write_file(template.render(), [self.out_dir, 'style.css'])
222 def _write_bug_file(self, bug, title, header, up_link):
224 print >> self.stdout, '\tCreating bug file for %s' % bug.id.user()
225 assert hasattr(self, 'out_dir_bugs'), \
226 'Must run after ._create_output_directories()'
229 if bug.active == True:
230 index_type = 'Active'
232 index_type = 'Inactive'
233 if bug.severity == 'target':
234 index_type = 'Target'
236 bug.load_comments(load_full=True)
237 bug.comment_root.sort(cmp=libbe.comment.cmp_time, reverse=True)
238 dirname = self._truncated_bug_id(bug)
239 fullpath = os.path.join(self.out_dir_bugs, dirname, 'index.html')
242 'charset': self.encoding,
243 'stylesheet': '../../style.css',
245 'backlinks': self.template.get_template('bug_backlinks.html'),
247 'index_type': index_type,
249 'comment_entry': self.template.get_template(
250 'bug_comment_entry.html'),
251 'comments': [(depth,comment) for depth,comment
252 in bug.comment_root.thread(flatten=False)],
253 'comment_dir': self._truncated_comment_id,
254 'format_body': self._format_comment_body,
255 'div_close': _DivCloser(),
256 'generation_time': self.generation_time,
258 fulldir = os.path.join(self.out_dir_bugs, dirname)
259 if not os.path.exists(fulldir):
261 template = self.template.get_template('bug.html')
262 self._write_file(template.render(template_info), [fullpath])
264 def _write_index_file(self, bugs, title, header, bug_type='active'):
266 print >> self.stdout, 'Writing %s index file for %d bugs' % (bug_type, len(bugs))
267 assert hasattr(self, 'out_dir'), 'Must run after ._create_output_directories()'
269 if bug_type == 'active':
270 filename = 'index.html'
271 elif bug_type == 'inactive':
272 filename = 'index_inactive.html'
273 elif bug_type == 'target':
274 filename = 'index_by_target.html'
276 raise ValueError('unrecognized bug_type: "%s"' % bug_type)
280 'charset': self.encoding,
281 'stylesheet': 'style.css',
283 'active_class': 'tab nsel',
284 'inactive_class': 'tab nsel',
285 'target_class': 'tab nsel',
287 'bug_entry': self.template.get_template('index_bug_entry.html'),
288 'bug_dir': self._truncated_bug_id,
289 'generation_time': self.generation_time,
291 template_info['%s_class' % bug_type] = 'tab sel'
292 if bug_type == 'target':
293 template = self.template.get_template('target_index.html')
294 template_info['targets'] = [
295 (target, sorted(libbe.command.depend.get_blocked_by(
299 template = self.template.get_template('standard_index.html')
301 template.render(template_info)+'\n', [self.out_dir,filename])
303 def _long_to_linked_user(self, text):
305 >>> import libbe.bugdir
306 >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
308 >>> h._long_to_linked_user('A link #abc123/a#, and a non-link #x#y#.')
309 'A link <a href="./a/">abc/a</a>, and a non-link #x#y#.'
312 replacer = libbe.util.id.IDreplacer(
313 [self.bd], self._long_to_linked_user_replacer, wrap=False)
315 libbe.util.id.REGEXP, replacer, text)
317 def _long_to_linked_user_replacer(self, bugdirs, long_id):
319 >>> import libbe.bugdir
320 >>> import libbe.util.id
321 >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
322 >>> a = bd.bug_from_uuid('a')
323 >>> uuid_gen = libbe.util.id.uuid_gen
324 >>> libbe.util.id.uuid_gen = lambda : '0123'
325 >>> c = a.new_comment('comment for link testing')
326 >>> libbe.util.id.uuid_gen = uuid_gen
330 >>> h._long_to_linked_user_replacer([bd], 'abc123')
332 >>> h._long_to_linked_user_replacer([bd], 'abc123/a')
333 '<a href="./a/">abc/a</a>'
334 >>> h._long_to_linked_user_replacer([bd], 'abc123/a/0123')
335 '<a href="./a/#0123">abc/a/012</a>'
336 >>> h._long_to_linked_user_replacer([bd], 'x')
338 >>> h._long_to_linked_user_replacer([bd], '')
343 p = libbe.util.id.parse_user(bugdirs[0], long_id)
344 except (libbe.util.id.MultipleIDMatches,
345 libbe.util.id.NoIDMatches,
346 libbe.util.id.InvalidIDStructure), e:
347 return '#%s#' % long_id # re-wrap failures
348 if p['type'] == 'bugdir':
349 return '#%s#' % long_id
350 elif p['type'] == 'bug':
351 bug,comment = libbe.command.util.bug_comment_from_user_id(
353 return '<a href="./%s/">%s</a>' \
354 % (self._truncated_bug_id(bug), bug.id.user())
355 elif p['type'] == 'comment':
356 bug,comment = libbe.command.util.bug_comment_from_user_id(
358 return '<a href="./%s/#%s">%s</a>' \
359 % (self._truncated_bug_id(bug),
360 self._truncated_comment_id(comment),
362 raise Exception('Invalid id type %s for "%s"'
363 % (p['type'], long_id))
365 def _format_comment_body(self, bug, comment):
366 link_long_ids = False
369 if comment.content_type == 'text/html':
371 elif comment.content_type.startswith('text/'):
372 value = '<pre>\n'+self._escape(value)+'\n</pre>'
374 elif comment.content_type.startswith('image/'):
376 value = '<img src="./%s/%s" />' % (
377 self._truncated_bug_id(bug),
378 self._truncated_comment_id(comment))
381 value = '<a href="./%s/%s">Link to %s file</a>.' % (
382 self._truncated_bug_id(bug),
383 self._truncated_comment_id(comment),
384 comment.content_type)
385 if link_long_ids == True:
386 value = self._long_to_linked_user(value)
387 if save_body == True:
388 per_bug_dir = os.path.join(self.out_dir_bugs, bug.uuid)
389 if not os.path.exists(per_bug_dir):
390 os.mkdir(per_bug_dir)
391 comment_path = os.path.join(per_bug_dir, comment.uuid)
393 '<Files %s>\n ForceType %s\n</Files>' \
394 % (comment.uuid, comment.content_type),
395 [per_bug_dir, '.htaccess'], mode='a')
396 self._write_file(comment.body,
397 [per_bug_dir, comment.uuid], mode='wb')
400 def _escape(self, string):
403 return xml.sax.saxutils.escape(string)
405 def _make_dir(self, dir_path):
406 dir_path = os.path.abspath(os.path.expanduser(dir_path))
407 if not os.path.exists(dir_path):
409 os.makedirs(dir_path)
411 raise libbe.command.UserError(
412 'Cannot create output directory "%s".' % dir_path)
415 def _write_file(self, content, path_array, mode='w'):
416 return libbe.util.encoding.set_file_contents(
417 os.path.join(*path_array), content, mode, self.encoding)
419 def _read_file(self, path_array, mode='r'):
420 return libbe.util.encoding.get_file_contents(
421 os.path.join(*path_array), mode, self.encoding, decode=True)
423 def write_default_template(self, out_dir):
425 print >> self.stdout, 'Creating output directories'
426 self.out_dir = self._make_dir(out_dir)
427 for filename,text in self.template_dict.iteritems():
429 print >> self.stdout, 'Creating %s file'
430 self._write_file(text, [self.out_dir, filename])
432 def _load_templates(self, template_dir=None):
433 if template_dir is not None:
434 template_dir = os.path.abspath(os.path.expanduser(template_dir))
436 self.template_dict = {
440 font-family: "lucida grande", "sans serif";
452 background-color: #fcfcfc;
453 -moz-border-radius: 10px;
466 -moz-border-radius: 10px;
475 padding-bottom: 10px;
478 -moz-border-radius: 10px;
484 border-color: #305275;
485 background-color: #305275;
488 -moz-border-radius-topleft: 8px;
489 -moz-border-radius-topright: 8px;
497 border-spacing: 0px 0px;
508 border-style: dotted;
516 border-color: #c3d9ff;
517 border-collapse: collapse;
525 border-color: #c3d9ff;
526 border-collapse: collapse;
532 img { border-style: none; }
535 list-style-type: none;
548 text-decoration: none;
551 a { color: #553d41; }
552 a:hover { color: #003d41; }
553 a:visited { color: #305275; }
554 .footer a { color: #508d91; }
556 /* bug index pages */
564 background-color: #c3d9ff ;
565 border: 1px solid #c3d9ff;
567 -moz-border-radius-topleft: 15px;
568 -moz-border-radius-topright: 15px;
572 border: 1px solid #c3d9ff;
574 -moz-border-radius-topleft: 5px;
575 -moz-border-radius-topright: 5px;
581 border-color: #c3d9ff;
584 border: 1px solid #c3d9ff;
593 table.target_list.td {
597 tr.wishlist { background-color: #DCFAFF;}
598 tr.wishlist:hover { background-color: #C2DCE1; }
600 tr.minor { background-color: #FFFFA6; }
601 tr.minor:hover { background-color: #E6E696; }
603 tr.serious { background-color: #FF9077;}
604 tr.serious:hover { background-color: #E6826B; }
606 tr.critical { background-color: #FF752A; }
607 tr.critical:hover { background-color: #D63905;}
609 tr.fatal { background-color: #FF3300;}
610 tr.fatal:hover { background-color: #D60000;}
612 td.uuid { width: 5%; border-style: dotted;}
613 td.status { width: 5%; border-style: dotted;}
614 td.severity { width: 5%; border-style: dotted;}
615 td.summary { border-style: dotted;}
616 td.date { width: 25%; border-style: dotted;}
618 /* bug detail pages */
620 td.bug_detail_label { text-align: right; border: none;}
621 td.bug_detail { border: none;}
622 td.bug_comment_label { text-align: right; vertical-align: top; }
634 /* padding-top: 0px; */
635 padding-bottom: 20px;
640 """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
641 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
642 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
644 <title>{{ title }}</title>
645 <meta http-equiv="Content-Type" content="text/html; charset={{ charset }}" />
646 <link rel="stylesheet" href="{{ stylesheet }}" type="text/css" />
649 <div class="header">{{ header }}</div>
651 {% block content %}{% endblock %}
654 <p>Generated by <a href="http://www.bugseverywhere.org/">
655 Bugs Everywhere</a> on {{ generation_time }}</p>
657 <a href="http://validator.w3.org/check?uri=referer">
658 Validate XHTML</a> |
659 <a href="http://jigsaw.w3.org/css-validator/check?uri=referer">
667 """{% extends "base.html" %}
673 <td class="{{ active_class }}"><a href="index.html">Active Bugs</a></td>
674 <td class="{{ inactive_class }}"><a href="index_inactive.html">Inactive Bugs</a></td>
675 <td class="{{ target_class }}"><a href="index_by_target.html">Divided by target</a></td>
680 {% block bug_table %}{% endblock %}
687 'standard_index.html':
688 """{% extends "index.html" %}
690 {% block bug_table %}
691 <table class="bug_list">
702 {% for bug in bugs %}
703 {{ bug_entry.render({'bug':bug, 'dir':bug_dir(bug)}) }}
711 """{% extends "index.html" %}
713 {% block bug_table %}
714 {% for target,bugs in targets %}
715 <table class="target_list">
718 <th class="target_name" colspan="5">
719 Target: {{ target.summary|e }} ({{ target.status|e }})
731 {% for bug in bugs %}
732 {{ bug_entry.render({'bug':bug, 'dir':bug_dir(bug)}) }}
740 'index_bug_entry.html':
741 """<tr class="{{ bug.severity }}">
742 <td class="uuid"><a href="bugs/{{ dir }}/index.html">{{ bug.id.user()|e }}</a></td>
743 <td class="status"><a href="bugs/{{ dir }}/index.html">{{ bug.status|e }}</a></td>
744 <td class="severity"><a href="bugs/{{ dir }}/index.html">{{ bug.severity|e }}</a></td>
745 <td class="summary"><a href="bugs/{{ dir }}/index.html">{{ bug.summary|e }}</a></td>
746 <td class="date"><a href="bugs/{{ dir }}/index.html">{{ (bug.time_string or '')|e }}</a></td>
751 """{% extends "base.html" %}
754 {{ backlinks.render({'up_link': up_link, 'index_type':index_type}) }}
755 <h1>Bug: {{ bug.id.user()|e }}</h1>
759 <tr><td class="bug_detail_label">ID :</td>
760 <td class="bug_detail">{{ bug.uuid|e }}</td></tr>
761 <tr><td class="bug_detail_label">Short name :</td>
762 <td class="bug_detail">{{ bug.id.user()|e }}</td></tr>
763 <tr><td class="bug_detail_label">Status :</td>
764 <td class="bug_detail">{{ bug.status|e }}</td></tr>
765 <tr><td class="bug_detail_label">Severity :</td>
766 <td class="bug_detail">{{ bug.severity|e }}</td></tr>
767 <tr><td class="bug_detail_label">Assigned :</td>
768 <td class="bug_detail">{{ (bug.assigned or '')|e }}</td></tr>
769 <tr><td class="bug_detail_label">Reporter :</td>
770 <td class="bug_detail">{{ (bug.reporter or '')|e }}</td></tr>
771 <tr><td class="bug_detail_label">Creator :</td>
772 <td class="bug_detail">{{ (bug.creator or '')|e }}</td></tr>
773 <tr><td class="bug_detail_label">Created :</td>
774 <td class="bug_detail">{{ (bug.time_string or '')|e }}</td></tr>
775 <tr><td class="bug_detail_label">Summary :</td>
776 <td class="bug_detail">{{ bug.summary|e }}</td></tr>
783 {% for depth,comment in comments %}
785 <div class="comment root" id="C{{ comment_dir(comment) }}">
787 <div class="comment" id="C{{ comment_dir(comment) }}">
789 {{ comment_entry.render({
790 'depth':depth, 'bug': bug, 'comment':comment, 'comment_dir':comment_dir,
791 'format_body': format_body, 'div_close': div_close}) }}
792 {{ div_close(depth) }}
794 {% if comments[-1][0] > 0 %}
800 {{ backlinks.render({'up_link': up_link, 'index_type': index_type}) }}
804 'bug_backlinks.html':
805 """<p class="backlink"><a href="{{ up_link }}">Back to {{ index_type }} Index</a></p>
806 <p class="backlink"><a href="../../index_by_target.html">Back to Target Index</a></p>
809 'bug_comment_entry.html':
813 <td class="bug_comment_label">Comment:</td>
814 <td class="bug_comment">
815 --------- Comment ---------<br/>
816 ID: {{ comment.uuid }}<br/>
817 Short name: {{ comment.id.user() }}<br/>
818 From: {{ (comment.author or '')|e }}<br/>
819 Date: {{ (comment.date or '')|e }}<br/>
821 {{ format_body(bug, comment) }}
829 loader = DictLoader(self.template_dict)
832 file_system_loader = FileSystemLoader(template_dir)
833 loader = ChoiceLoader([file_system_loader, loader])
835 self.template = Environment(loader=loader)
838 class _DivCloser (object):
839 def __init__(self, depth=0):
842 def __call__(self, depth):
844 while self.depth >= depth:
848 return '\n'.join(ret)