~launchpad-pqm/launchpad/devel

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