Merge lp:~bac/launchpad/bug-490224-content-type into lp:launchpad

Proposed by Brad Crittenden
Status: Merged
Approved by: Curtis Hovey
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~bac/launchpad/bug-490224-content-type
Merge into: lp:launchpad
Diff against target: 116 lines (+71/-10)
4 files modified
lib/lp/bugs/model/bug.py (+0/-10)
lib/lp/services/mime.py (+33/-0)
lib/lp/services/tests/test_mime.py (+34/-0)
lib/site.py (+4/-0)
To merge this branch: bzr merge lp:~bac/launchpad/bug-490224-content-type
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Gary Poster (community) code Approve
Canonical Launchpad Engineering Pending
Review via email: mp+15862@code.launchpad.net

Commit message

Customize the mimetypes standard library to support bzip2 encoding, which is missing in python2.5.

To post a comment you must log in.
Revision history for this message
Brad Crittenden (bac) wrote :

= Summary =

The mimetypes standard library in python2.5 does not know about the bzip2 encoding.
This branch provides a centralized place for extending the library so that the
encoding is recognized which will allow files to be served properly.

== Proposed fix ==

Provide a 'customizedMimetypes' method and call it from site.py.

== Pre-implementation notes ==

Long chat with Gary and talks with Curtis.

== Implementation details ==

As above.

== Tests ==

bin/test -vvt test_mime

== Demo and Q/A ==

Add a .tar.bz2 file to a download and ensure it has the proper content type when
downloading using 'wget -S'

= Launchpad lint =

Checking for conflicts. and issues in doctests and templates.
Running jslint, xmllint, pyflakes, and pylint.
Using normal rules.

Linting changed files:
  lib/lp/services/tests/test_mime.py
  lib/lp/services/mime.py
  lib/site.py
  lib/lp/bugs/model/bug.py

== Pylint notices ==

lib/lp/bugs/model/bug.py
    24: [F0401] Unable to import 'email.Utils' (No module named Utils)
    37: [F0401] Unable to import 'lazr.lifecycle.event' (No module named lifecycle)
    39: [F0401] Unable to import 'lazr.lifecycle.snapshot' (No module named lifecycle)

Revision history for this message
Brad Crittenden (bac) wrote :

Note bug 494458 has been filed to correct existing data.

Revision history for this message
Gary Poster (gary) wrote :

Looks good, Brad. Thank you for the fix, and for the help in guiding how we'll do this kind of initialization in the future.

Gary

review: Approve (code)
Revision history for this message
Curtis Hovey (sinzui) wrote :

This looks good to land.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/bugs/model/bug.py'
2--- lib/lp/bugs/model/bug.py 2009-12-09 20:29:17 +0000
3+++ lib/lp/bugs/model/bug.py 2009-12-10 22:07:17 +0000
4@@ -18,7 +18,6 @@
5 ]
6
7
8-import mimetypes
9 import operator
10 import re
11 from cStringIO import StringIO
12@@ -102,15 +101,6 @@
13 from lp.registry.model.pillar import pillar_sort_key
14
15
16-# XXX: GavinPanella 2008-07-04 bug=229040: A fix has been requested
17-# for Intrepid, to add .debdiff to /etc/mime.types, so we may be able
18-# to remove this setting once a new /etc/mime.types has been installed
19-# on the app servers. Additionally, Firefox does not display content
20-# of type text/x-diff inline, so making this text/plain because
21-# viewing .debdiff inline is the most common use-case.
22-mimetypes.add_type('text/plain', '.debdiff')
23-
24-
25 _bug_tag_query_template = """
26 SELECT %(columns)s FROM %(tables)s WHERE
27 %(condition)s GROUP BY BugTag.tag ORDER BY BugTag.tag"""
28
29=== added file 'lib/lp/services/mime.py'
30--- lib/lp/services/mime.py 1970-01-01 00:00:00 +0000
31+++ lib/lp/services/mime.py 2009-12-10 22:07:17 +0000
32@@ -0,0 +1,33 @@
33+# Copyright 2009 Canonical Ltd. This software is licensed under the
34+# GNU Affero General Public License version 3 (see the file LICENSE).
35+
36+"""Methods required to customize the mimetypes library."""
37+
38+__metaclass__ = type
39+__all__ = [
40+ 'customizeMimetypes',
41+ ]
42+
43+import mimetypes
44+
45+
46+def customizeMimetypes():
47+ """Initialize and extend the standard mimetypes library for our needs.
48+
49+ This method is to be called before any requests are processed to ensure
50+ any call site that imports the standard mimetypes module will take
51+ advantage of these customizations.
52+ """
53+ mimetypes.init()
54+
55+ # Add support for bz2 encodings, which is not present in python2.5.
56+ mimetypes.encodings_map.setdefault('.bz2', 'bzip2')
57+ mimetypes.suffix_map.setdefault('.tbz2', '.tar.bz2')
58+
59+ # XXX: GavinPanella 2008-07-04 bug=229040: A fix has been requested
60+ # for Intrepid, to add .debdiff to /etc/mime.types, so we may be able
61+ # to remove this setting once a new /etc/mime.types has been installed
62+ # on the app servers. Additionally, Firefox does not display content
63+ # of type text/x-diff inline, so making this text/plain because
64+ # viewing .debdiff inline is the most common use-case.
65+ mimetypes.add_type('text/plain', '.debdiff')
66
67=== added file 'lib/lp/services/tests/test_mime.py'
68--- lib/lp/services/tests/test_mime.py 1970-01-01 00:00:00 +0000
69+++ lib/lp/services/tests/test_mime.py 2009-12-10 22:07:17 +0000
70@@ -0,0 +1,34 @@
71+# Copyright 2009 Canonical Ltd. This software is licensed under the
72+# GNU Affero General Public License version 3 (see the file LICENSE).
73+
74+"""Tests for mime module."""
75+
76+__metaclass__ = type
77+
78+import unittest
79+
80+import mimetypes
81+from lp.testing import TestCase
82+
83+
84+class TestBzip(TestCase):
85+ """Tests for iter_split."""
86+
87+ def test_bzip2(self):
88+ # Test for '.tar.bzip2' support.
89+ filename = "foo.tar.bz2"
90+ (application, encoding) = mimetypes.guess_type(filename)
91+ self.assertEqual('application/x-tar', application)
92+ self.assertEqual('bzip2', encoding)
93+
94+ def test_tbz2(self):
95+ # Test for '.tar.bzip2' support.
96+ filename = "foo.tbz2"
97+ (application, encoding) = mimetypes.guess_type(filename)
98+ self.assertEqual('application/x-tar', application)
99+ self.assertEqual('bzip2', encoding)
100+
101+
102+
103+def test_suite():
104+ return unittest.TestLoader().loadTestsFromName(__name__)
105
106=== modified file 'lib/site.py'
107--- lib/site.py 2009-10-21 15:18:28 +0000
108+++ lib/site.py 2009-12-10 22:07:17 +0000
109@@ -28,3 +28,7 @@
110 import sys
111 execfile(
112 os.path.join(sys.prefix, 'lib', 'python' + sys.version[:3], 'site.py'))
113+
114+# Perform other start-up initialization.
115+from lp.services.mime import customizeMimetypes
116+customizeMimetypes()