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