8687.15.18
by Karl Fogel
Add the copyright header block to files under lib/canonical/. |
1 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
3 |
|
4 |
__metaclass__ = type |
|
7140.7.1
by Celso Providelo
Fixing bug #279348 (restricted librarian files are streamed with inappropriate encoding). Also encapsulating the librarian special-encoding map to be used in both places. |
5 |
__all__ = [ |
6 |
'copy_and_close', |
|
7 |
'filechunks', |
|
8 |
'guess_librarian_encoding', |
|
9 |
'sha1_from_path', |
|
10 |
]
|
|
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
11 |
|
10293.1.1
by Barry Warsaw, Max Bowsher
Switch from using sha and md5 to hashlib. Also use hashlib.sha256 instead of |
12 |
|
13 |
import hashlib |
|
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
14 |
|
14612.2.1
by William Grant
format-imports on lib/. So many imports. |
15 |
|
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
16 |
MEGABYTE = 1024*1024 |
17 |
||
3691.212.2
by Malcolm Cleaton
More refactoring |
18 |
|
3691.93.25
by Christian Reis
Fix architecture handling in the queue builder. Make architecturehintlist NOT NULL and fix sampledata for it. Clean up the portions of the buildmaster script related to build creation. Severely enhance and clarify the buildd-queuebuilder test. As an added bonus clean up the filechunks() methods and instead use librarian.utils. |
19 |
def filechunks(file, chunk_size=4*MEGABYTE): |
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
20 |
"""Return an iterator which reads chunks of the given file."""
|
21 |
return iter(lambda: file.read(chunk_size), '') |
|
22 |
||
23 |
||
24 |
def copy_and_close(from_file, to_file): |
|
25 |
"""Copy from_file to to_file and close both.
|
|
26 |
||
27 |
It requires both arguments to be opened file-like objects.
|
|
28 |
'filechunks' trick is used reduce the buffers memory demanded
|
|
29 |
when handling large files.
|
|
30 |
It's suitable to copy contents from ILibraryFileAlias instances to the
|
|
31 |
local filesystem.
|
|
32 |
Both file_descriptors are closed before return.
|
|
33 |
"""
|
|
3691.93.25
by Christian Reis
Fix architecture handling in the queue builder. Make architecturehintlist NOT NULL and fix sampledata for it. Clean up the portions of the buildmaster script related to build creation. Severely enhance and clarify the buildd-queuebuilder test. As an added bonus clean up the filechunks() methods and instead use librarian.utils. |
34 |
for chunk in filechunks(from_file): |
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
35 |
to_file.write(chunk) |
36 |
from_file.close() |
|
37 |
to_file.close() |
|
38 |
||
3691.212.2
by Malcolm Cleaton
More refactoring |
39 |
|
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
40 |
def sha1_from_path(path): |
41 |
"""Return the hexdigest SHA1 for the contents of the path."""
|
|
42 |
the_file = open(path) |
|
10293.1.1
by Barry Warsaw, Max Bowsher
Switch from using sha and md5 to hashlib. Also use hashlib.sha256 instead of |
43 |
the_hash = hashlib.sha1() |
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
44 |
|
3691.93.25
by Christian Reis
Fix architecture handling in the queue builder. Make architecturehintlist NOT NULL and fix sampledata for it. Clean up the portions of the buildmaster script related to build creation. Severely enhance and clarify the buildd-queuebuilder test. As an added bonus clean up the filechunks() methods and instead use librarian.utils. |
45 |
for chunk in filechunks(the_file): |
3500.2.38
by Celso Providelo
Checkpoint version, don't use it tests are broken, will need more time to fix 'publishing from content class' and don't want to loose this changeset |
46 |
the_hash.update(chunk) |
47 |
||
48 |
the_file.close() |
|
49 |
||
50 |
return the_hash.hexdigest() |
|
7140.7.1
by Celso Providelo
Fixing bug #279348 (restricted librarian files are streamed with inappropriate encoding). Also encapsulating the librarian special-encoding map to be used in both places. |
51 |
|
52 |
||
53 |
def guess_librarian_encoding(filename, mimetype): |
|
54 |
"""Return the appropriate encoding for the given filename and mimetype.
|
|
55 |
||
56 |
Files with the following extensions will be served as
|
|
57 |
'Content-Encoding: gzip' and 'Content-Type: text/plain',
|
|
58 |
which indicates to browsers that, after being unzipped,
|
|
59 |
their contents can be rendered inline.
|
|
60 |
||
61 |
* 'txt.gz': gzipped sources buildlogs;
|
|
62 |
* 'diff.gz': gzipped sources diffs;
|
|
63 |
||
64 |
:param filename: string containing the filename to be guessed;
|
|
65 |
:param mimetype: string containing the stored mimetype;
|
|
66 |
||
7140.7.4
by Celso Providelo
applying review comments, r=gmb. |
67 |
:return: a tuple containing the appropriate 'encoding' and 'mimetype'
|
7140.7.1
by Celso Providelo
Fixing bug #279348 (restricted librarian files are streamed with inappropriate encoding). Also encapsulating the librarian special-encoding map to be used in both places. |
68 |
that should be used to serve the file.
|
69 |
"""
|
|
70 |
if filename.endswith('txt.gz'): |
|
71 |
encoding = 'gzip' |
|
72 |
mimetype = 'text/plain' |
|
73 |
elif filename.endswith('diff.gz'): |
|
74 |
encoding = 'gzip' |
|
75 |
mimetype = 'text/plain' |
|
76 |
else: |
|
77 |
encoding = None |
|
78 |
mimetype = mimetype.encode('ascii') |
|
79 |
||
80 |
return encoding, mimetype |