summaryrefslogtreecommitdiffstats
path: root/server/xmlrpc_API1.py
blob: 95390bd27c4c8b04449e7a3a0caaff02c0db572e (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
#   xmlrpc_API1.py
#   XML-RPC functions supported by the API1 version for the rteval server
#
#   Copyright 2009      David Sommerseth <davids@redhat.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#
import os
import bz2
import base64
import libxml2
import string
import inspect
import rtevaldb


class XMLRPC_API1():
    def __init__(self, config=None, debug=False, nodbaction=False):
        # Some defaults
        self.fnametrans = string.maketrans("/\\", "::") # replace path delimiters in filenames
        self.debug = debug
        self.nodbaction = nodbaction
        self.config = config


    def __mkdatadir(self, dirpath):
        startdir = os.getcwd()
        for dir in dirpath.split("/"):
            if dir is '':
                continue
            if not os.path.exists(dir):
                os.mkdir(dir, 0700)
            os.chdir(dir)
        os.chdir(startdir)


    def __getfilename(self, dir, fname, comp):
        idx = 0
        if comp:
            filename = "%s/%s/%s.bz2" % (self.config.datadir, dir, fname.translate(self.fnametrans))
        else:
            filename = "%s/%s/%s" % (self.config.datadir, dir, fname.translate(self.fnametrans))

        while 1:
            if not os.path.exists(filename):
                return filename
            idx += 1
            if comp:
                filename = "%s/%s/%s-{%i}.bz2" % (self.config.datadir, dir,
                                                  fname.translate(self.fnametrans), idx)
            else:
                filename = "%s/%s/%s-{%i}" % (self.config.datadir, dir,
                                              fname.translate(self.fnametrans), idx)


    def Dispatch(self, method, params):
        # Call the method requested
        # FIXME: Improve checking for valid methods
        func = getattr(self, method)
        return func(*params)


    def _dispatch(self, method, params):
        "Wrapper method for Dispatch(), used by xmlrpclib in a local test server. Only used for testing."
        return self.Dispatch(method, params)


    def SendReport(self, clientid, xmlbzb64):
        decompr = bz2.BZ2Decompressor()
        xmldoc = libxml2.parseDoc(decompr.decompress(base64.b64decode(xmlbzb64)))

        # Save a copy of the report on the file system
        # Make sure we have a directory to write files into
        self.__mkdatadir(self.config.datadir + '/reports/' + clientid)
        fname = self.__getfilename('reports/' + clientid,'report.xml', False)
        xmldoc.saveFormatFileEnc(fname,'UTF-8',1)
        if self.debug:
            print "Copy of report: %s" % fname

        # Register the report into a database and return the rteval run id
        (syskey, rterid) = rtevaldb.register_report(self.config, xmldoc, fname,
                                                        debug=self.debug, noaction=self.nodbaction)
        if self.nodbaction:
            rterid = 999999999 # Fake ID when no database registration is done

        return rterid


    def StoreRawFile(self, clientid, filename, bzb64data, decompdata):
        if decompdata is True:
            decompr = bz2.BZ2Decompressor()
            data = decompr.decompress(base64.b64decode(bzb64data))
        else:
            data = base64.b64decode(bzb64data)

        # Make sure we have a directory to write files into
        self.__mkdatadir(self.datadir + '/uploads/' + clientid)

        # Get a unique filename, as close as possible to the input filename
        fname = self.__getfilename('uploads/' + clientid, filename, not decompdata)

        # Save and return filename used server side
        f = open(fname, "w")
        f.write(data)
        f.close()
        return fname