~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
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
9
import logging
10
import os
11
12
import _pythonpath
1881 by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections
13
from zope.component import getUtility
14
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
15
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
1881 by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections
16
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.
17
from lp.services.scripts.base import (
18
    LaunchpadScript, LaunchpadScriptFailure)
19
20
21
class LibrarianUploader(LaunchpadScript):
22
    description = "Upload a file to librarian."
23
    usage = "usage: %prog <f|--file> <filename>"
24
    loglevel = logging.INFO
25
26
    def add_my_options(self):
27
        self.parser.set_defaults(format='simple')
28
        self.parser.add_option(
29
        "-f", "--file", dest="filepath", metavar="FILE",
30
        help="filename to upload")
31
32
    def main(self):
33
        """Upload file, commit the transaction and prints the file URL."""
34
        if self.options.filepath is None:
35
            raise LaunchpadScriptFailure('File not provided.')
36
37
        library_file = self.upload_file(self.options.filepath)
38
39
        self.txn.commit()
40
        self.logger.info(library_file.http_url)
41
42
    def upload_file(self, filepath):
43
        """Upload given file to Librarian.
44
45
        :param filepath: path to the file on disk that should be uploaded to
46
            Librarian.
47
        :raise: `LaunchpadScriptFailure` if the given filepath could not be
48
            opened.
49
        :return: the `LibraryFileAlias` record corresponding to the uploaded
50
            file.
51
        """
52
        try:
53
            file = open(filepath)
54
        except IOError:
55
            raise LaunchpadScriptFailure('Could not open: %s' % filepath)
56
57
        flen = os.stat(filepath).st_size
58
        filename = os.path.basename(filepath)
59
        ftype = filenameToContentType(filename)
60
        library_file = getUtility(ILibraryFileAliasSet).create(
61
            filename, flen, file, contentType=ftype)
62
        return library_file
63
1881 by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections
64
65
if __name__ == '__main__':
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
66
    script = LibrarianUploader('librarian-uploader')
67
    script.run()