File: submission.py

package info (click to toggle)
debiancontributors 0.7.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 404 kB
  • sloc: python: 1,894; makefile: 18
file content (284 lines) | stat: -rw-r--r-- 9,592 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# coding: utf8
# Debian Contributors data source core data structure
#
# Copyright (C) 2013--2014  Enrico Zini <enrico@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from collections import defaultdict
from .types import Contribution
import sys
import os
import io
import six
import json

__all__ = ["Submission"]

DEFAULT_BASE_URL = "https://contributors.debian.org/"


class Submission(object):
    """
    A submission to contributors.debian.org
    """
    def __init__(self, name, auth_token=None, baseurl=DEFAULT_BASE_URL):
        """
        name: data source name
        """
        # Data source name, as in the website
        self.name = name
        # Authentication token
        self.auth_token = None
        # Base URL
        self.baseurl = baseurl
        # List of contributions by identifier
        self.entries = defaultdict(dict)
        if auth_token is not None:
            self.set_auth_token(auth_token)

    def set_auth_token(self, auth_token):
        """
        Set the auth token for this source. If auth_token starts with '@', the
        rest is treated as a pathname to a file that contains the token.
        """
        if not auth_token:
            raise ValueError("auth_token is empty")
        if auth_token[0] == "@":
            with open(auth_token[1:], "r") as fd:
                self.auth_token = fd.read().strip()
        else:
            self.auth_token = auth_token

    def add_contribution(self, identifier, contrib):
        """
        Add information about a contribution.

        identifier: Identifier for the user that made this contribution
        contrib: Contribution object
        """
        entries = self.entries[identifier]
        old = entries.get(contrib.type, None)
        if old is None:
            entries[contrib.type] = contrib
        else:
            entries[contrib.type] = Contribution.merged(old, contrib)

    def add_contribution_data(self, identifier, type, begin=None, end=None, url=None):
        """
        Add information about a contribution.

        identifier: Identifier for the user that made this contribution
        name: contribution name (chosen among the source contribution types)
        begin: start time of this contribution. None to reuse the last start time.
        end: end time of this contribution. None to mean 'now'.
        """
        self.add_contribution(identifier, Contribution(type, begin, end, url))

    def merge_with(self, submission):
        """
        Merge another submission into this one
        """
        if self.name != submission.name:
            raise ValueError("Merging submission for two different sources: {}!={}".format(
                self.name, submission.name))
        for ident, contribs in submission.entries.items():
            old = self.entries.get(ident, None)
            if old is None:
                self.entries[ident] = dict(contribs)
            else:
                self.entries[ident] = merge_contrib_dicts(old, contribs)

    def _gen_records(self):
        """
        Generate DC records for serialization
        """
        for ident, contributions in self.entries.items():
            yield {
                "id": (ident.to_json(),),
                "contributions": [c.to_json() for c in contributions.values()],
            }

    def to_json(self, file=None, indent=None):
        """
        Convert to JSON.

        file: if set to a file-like object, send data there. Else, return the
              JSON data as a string
        indent: passed as-is to the indent parameter of the encoder
        """
        if file is not None:
            return json.dump(list(self._gen_records()), file, indent=indent)
        else:
            return json.dumps(list(self._gen_records()), indent=indent)

    def print_compact(self, file=sys.stdout):
        """
        Make a compact dump of this source to the given file
        """
        for ident, contributions in self.entries.items():
            for ctype, c in sorted(contributions.items()):
                if ident.desc:
                    lead = "{}:{} <{}>".format(ident.type, ident.desc, ident.id)
                else:
                    lead = "{}:{}".format(ident.type, ident.id)

                print("{}: {} from {} to {}".format(lead, c.type, c.begin, c.end), file=file)
                if c.url:
                    print("{}: {} url: {}".format(lead, c.type, c.url), file=file)

    def post(self):
        """
        POST this submission to the contributors server

        Returns a couple (success, info).

        success: a bool, true if everything was imported correctly, false if
                 there has been some problem.
        info:    a dict with detailed status and error information, plus import
                 statistics
        """
        # Yuck! Python's stdlib cannot do file uploads :'(
        # We need to introduce an external dependency for it
        import requests
        from six.moves.urllib.parse import urljoin

        # Build the POST request to contributors.debian.org
        url = urljoin(self.baseurl, '/contributors/post')

        # Prepare the file to post
        try:
            import lzma
            compress_type = "xz"
            compress = lzma.compress
        except ImportError:
            import gzip
            compress_type = "gzip"

            def compress(data):
                out = io.BytesIO()
                with gzip.GzipFile(mode="wb", fileobj=out) as fd:
                    fd.write(data)
                return out.getvalue()

        file_data = io.BytesIO(compress(self.to_json().encode("utf-8")))
        files = {
            "data": file_data
        }

        # POST data
        data = {
            "source": self.name,
            "auth_token": self.auth_token,
            "data_compression": compress_type,
        }

        # POST everything to the server
        args = {
            "data": data,
            "files": files,
        }
        # If we are running on a machine that has special debian CA
        # certificates (like *.debian.org machines), use them
        if os.path.exists("/etc/ssl/ca-debian/ca-certificates.crt"):
            args["verify"] = "/etc/ssl/ca-debian/ca-certificates.crt"
        else:
            args["verify"] = True

        session = requests.Session()
        try:
            # Do a GET before the POST, to do HTTPS negotiation without a huge
            # payload. See #801506
            res = session.get(url)
            res.raise_for_status()
            res = session.post(url, **args)
            res.raise_for_status()
        except requests.ConnectionError as e:
            return False, {
                "code": None,
                "errors": [
                    "Connection error: " + str(e)
                ]
            }
        except requests.HTTPError as e:
            try:
                parsed = json.loads(res.text)
                errors = parsed["errors"]
            except Exception:
                errors = []
            return False, {
                "code": None,
                "errors": [
                    "Server responded with an HTTP error: " + six.text_type(e)
                ] + errors
            }

        # Whether the POST was successful or not, the response body contains
        # information and statistics in JSON format.
        response = res.json()
        if res.status_code == requests.codes.ok:
            return True, response
        else:
            return False, response

    @classmethod
    def from_json(cls, name, data):
        """
        Build a Submission from previously generated JSON

        name: the data source name
        data: the JSON data, either in a string, in a file, or as a parsed data
              structure
        """
        if isinstance(data, six.string_types):
            data = json.loads(data)
        elif hasattr(data, "read"):
            data = json.load(data)

        res = cls(name)

        from .parser import Parser
        parser = Parser()
        for ids, contribs in parser.parse_submission(data):
            for i in ids:
                res.entries[i] = {c.type: c for c in contribs}

        return res


def merge_contrib_dicts(d1, d2):
    """
    Merge two dicts of contributions from the same identifier.

    Contribution types that happen in both lists will have their timespans
    merged
    """
    res = {}

    # Add elements from d1, merging them with d2 if they also exist in d2
    for ctype, c1 in d1.items():
        c2 = d2.get(ctype, None)
        if c2 is None:
            res[ctype] = c1
        else:
            res[ctype] = Contribution.merged(c1, c2)

    # Add the elements that only exist in d2
    for ctype, c2 in d2.items():
        res.setdefault(ctype, c2)

    return res