~launchpad-pqm/launchpad/devel

1881 by Canonical.com Patch Queue Manager
Comments for 'gpghandler' and 'zeca' configuration sections
1
#!/usr/bin/env python
2
"""Copyright Canonical Limited 2005
3
 Author: Daniel Silverstone <daniel.silverstone@canonical.com>
4
         Celso Providelo <celso.providelo@canonical.com>
5
 Simple tool to upload arbitrary files into Librarian
6
"""
7
from canonical.lp import initZopeless
8
from canonical.launchpad.scripts import execute_zcml_for_scripts
9
from zope.component import getUtility
10
11
from canonical.librarian.interfaces import ILibrarianClient
12
from canonical.launchpad.helpers import filenameToContentType
13
14
from optparse import OptionParser
15
16
import os
17
18
def addfile(filepath, client):
19
    """Add a file to librarian."""        
20
    # verify filepath
21
    if not filepath:
22
        print 'Filepath is required'
23
        return
24
25
    # open given file
26
    try:
27
        fd = open(filepath)
28
    except IOError:
29
        print 'Could not open:', filepath
30
        return
31
32
    # XXX: cprov 20050613
33
    # os.fstat(fd) presents an strange behavior
34
    flen = os.stat(filepath).st_size
35
    filename = os.path.basename(filepath)
36
    ftype = filenameToContentType(filename)
37
38
    alias = client.addFile(filename, flen, fd, contentType=ftype)
39
40
    print 'Added as', alias
41
42
    # commit previous transactions
43
    tm.commit()
44
45
if __name__ == '__main__':
46
    # Parse the commandline...
47
    parser = OptionParser()
48
    parser.add_option("-f","--file", dest="filepath",
49
                      help="filename to import",
50
                      metavar="FILE",
51
                      default=None)
52
    
53
    (options,args) = parser.parse_args()
54
    
55
    filepath = options.filepath
56
57
    # setup a transaction manager to LPDB
58
    tm = initZopeless()
59
60
    # load the zcml configuration
61
    execute_zcml_for_scripts()
62
    
63
    # get an librarian client instance
64
    client = getUtility(ILibrarianClient)
65
66
    addfile(filepath, client)