10637.3.1
by Guilherme Salgado
Use the default python version instead of a hard-coded version |
1 |
#!/usr/bin/python -S
|
8687.15.22
by Karl Fogel
Add the copyright header block to the remaining .py files. |
2 |
#
|
3 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
|
4 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
5 |
||
8486.12.1
by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted. |
6 |
# pylint: disable-msg=W0403
|
7 |
"""Simple tool to upload arbitrary files into Librarian."""
|
|
8 |
||
9641.1.7
by Gary Poster
fix scripts to work |
9 |
import _pythonpath |
10 |
||
8486.12.1
by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted. |
11 |
import logging |
12 |
import os |
|
13 |
||
1881
by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections |
14 |
from zope.component import getUtility |
15 |
||
8486.12.1
by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted. |
16 |
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet |
1881
by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections |
17 |
from canonical.launchpad.helpers import filenameToContentType |
8486.12.1
by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted. |
18 |
from lp.services.scripts.base import ( |
19 |
LaunchpadScript, LaunchpadScriptFailure) |
|
20 |
||
21 |
||
22 |
class LibrarianUploader(LaunchpadScript): |
|
23 |
description = "Upload a file to librarian." |
|
24 |
usage = "usage: %prog <f|--file> <filename>" |
|
25 |
loglevel = logging.INFO |
|
26 |
||
27 |
def add_my_options(self): |
|
28 |
self.parser.set_defaults(format='simple') |
|
29 |
self.parser.add_option( |
|
30 |
"-f", "--file", dest="filepath", metavar="FILE", |
|
31 |
help="filename to upload") |
|
32 |
||
33 |
def main(self): |
|
34 |
"""Upload file, commit the transaction and prints the file URL."""
|
|
35 |
if self.options.filepath is None: |
|
36 |
raise LaunchpadScriptFailure('File not provided.') |
|
37 |
||
38 |
library_file = self.upload_file(self.options.filepath) |
|
39 |
||
40 |
self.txn.commit() |
|
41 |
self.logger.info(library_file.http_url) |
|
42 |
||
43 |
def upload_file(self, filepath): |
|
44 |
"""Upload given file to Librarian.
|
|
45 |
||
46 |
:param filepath: path to the file on disk that should be uploaded to
|
|
47 |
Librarian.
|
|
48 |
:raise: `LaunchpadScriptFailure` if the given filepath could not be
|
|
49 |
opened.
|
|
50 |
:return: the `LibraryFileAlias` record corresponding to the uploaded
|
|
51 |
file.
|
|
52 |
"""
|
|
53 |
try: |
|
54 |
file = open(filepath) |
|
55 |
except IOError: |
|
56 |
raise LaunchpadScriptFailure('Could not open: %s' % filepath) |
|
57 |
||
58 |
flen = os.stat(filepath).st_size |
|
59 |
filename = os.path.basename(filepath) |
|
60 |
ftype = filenameToContentType(filename) |
|
61 |
library_file = getUtility(ILibraryFileAliasSet).create( |
|
62 |
filename, flen, file, contentType=ftype) |
|
63 |
return library_file |
|
64 |
||
1881
by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections |
65 |
|
66 |
if __name__ == '__main__': |
|
8486.12.1
by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted. |
67 |
script = LibrarianUploader('librarian-uploader') |
68 |
script.run() |