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