aboutsummaryrefslogtreecommitdiff
path: root/Tools/scripts/getpatch
blob: 4799c6aa5a4c2dfe2a6510af3034fcfec9ea0f32 (plain) (blame)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Sofian Brabez <sbz@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#
# $FreeBSD$
#
# MAINTAINER=   sbz@FreeBSD.org

import argparse
import codecs
import os
import re
import ssl
import sys
if sys.version_info.major == 3:
    import urllib.request as urllib2
else:
    import urllib2

"""
FreeBSD getpatch handles Gnats and Bugzilla patch attachments
"""


def create_ssl_context(cafile):
    if os.path.exists(cafile):
        return ssl.create_default_context(cafile=cafile)
    else:
        return ssl._create_unverified_context()


class GetPatch(object):

    def __init__(self, pr, category='ports'):
        self.pr = pr
        self.category = category
        self.patchs = []
        self.url = ""
        self.patch = ""
        self.output_stdout = False
        self.default_locale = sys.getdefaultencoding()
        self.ssl_context = create_ssl_context('/usr/local/etc/ssl/cert.pem')

    def fetch(self, *largs, **kwargs):
        raise NotImplementedError()

    def write(self, filename, data):
        if filename.endswith(('.patch', '.txt')):
            filename = "{}.diff".format(filename[:filename.rindex('.')])
        f = codecs.open(filename, encoding=self.default_locale, mode='w')
        f.write(data.decode(self.default_locale))
        f.close()
        self.out("[+] {} created".format(filename))

    def get(self, only_last=False, output_stdout=False):
        self.output_stdout = output_stdout
        self.fetch(self.pr, category=self.category)

        if len(self.patchs) == 0:
            self.out("[-] No patch found")
            sys.exit(os.EX_UNAVAILABLE)

        if only_last:
            self.patchs = [self.patchs.pop()]

        for patch in self.patchs:
            url = patch['url']
            p = patch['name']

            data = urllib2.urlopen(url, context=self.ssl_context).read()

            if self.output_stdout:
                sys.stdout.write(data.decode(self.default_locale))
            else:
                self.write(p, data)

    def add_patch(self, url, name):
        self.patchs.append({'url': url, 'name': name})

    def out(self, s):
        if not self.output_stdout:
            print(s)


class GnatsGetPatch(GetPatch):

    URL_BASE = 'https://www.freebsd.org/cgi'
    URL = '{}/query-pr.cgi?pr='.format(URL_BASE)
    REGEX = r'<b>Download <a href="([^"]*)">([^<]*)</a>'

    def __init__(self, pr, category):
        GetPatch.__init__(self, pr, category)

    def fetch(self, *largs, **kwargs):
        category = kwargs['category']
        target = ("{}/{}".format(category, self.pr),
                  "{}".format(self.pr))[category == '']
        self.out("[+] Fetching patch for pr {}".format(target))
        pattern = re.compile(self.REGEX)
        u = urllib2.urlopen("{}{}".format(self.URL, target),
                            context=self.ssl_context)
        data = u.read()
        if data is None:
            self.out("[-] No patch found")
            sys.exit(os.EX_UNAVAILABLE)

        for patchs in re.findall(pattern, str(data)):
            self.add_patch(patchs[0], patchs[1])


class BzGetPatch(GetPatch):

    URL_BASE = 'https://bugs.freebsd.org/bugzilla/'
    URL_SHOW = '{}/show_bug.cgi?id='.format(URL_BASE)
    REGEX_ATTACHMENTS_TABLE = r'<table id="attachment_table">(.*?)</table>'
    REGEX_ATTACHMENT_TR = r'(<tr id="a\d+"[^<]+>.*?</tr>)'
    REGEX_URL = r'<a href="([^<]+)">Details</a>'
    REGEX = r'<div class="details">([^ ]+) \(text/plain(?:; charset=[-\w]+)?\)'

    def __init__(self, pr, category):
        GetPatch.__init__(self, pr, category)

    def _get_patch_name(self, url):
        data = urllib2.urlopen(url, context=self.ssl_context).read()
        match = re.search(self.REGEX, str(data))
        if match is None:
            return None
        return match.group(1)

    def _get_patch_url(self, data):
        for url in re.findall(self.REGEX_URL, str(data)):
            url = '{}{}'.format(self.URL_BASE, url)
            file_name = self._get_patch_name(url)
            if file_name is None:
                msg = "[-] Could not determine the patch file name in {}." \
                    "Skipping."
                self.out(msg.format(url))
                continue
            download_url = url[:url.find('&')]
            return download_url, file_name

    def _get_patch_urls(self, data):
        patch_urls = {}
        match = re.search(self.REGEX_ATTACHMENTS_TABLE, str(data), re.DOTALL)
        if match is None:
            return patch_urls
        table = match.group(1)
        for tr in re.findall(self.REGEX_ATTACHMENT_TR, str(data), re.DOTALL):
            if (tr.find('bz_tr_obsolete') >= 0):
                continue
            download_url, file_name = self._get_patch_url(tr)
            patch_urls[download_url] = file_name

        return patch_urls

    def fetch(self, *largs, **kwargs):
        category = kwargs['category']
        target = ("{}/{}".format(category, self.pr),
                  "{}".format(self.pr))[category == '']
        self.out("[+] Fetching patch for pr {}".format(target))
        u = urllib2.urlopen("{}{}".format(self.URL_SHOW, self.pr),
                            context=self.ssl_context)
        data = u.read()

        if data is None:
            self.out("[-] No patch found")
            sys.exit(os.EX_UNAVAILABLE)

        patch_urls = self._get_patch_urls(data)
        if not patch_urls:
            self.out("[-] No patch found")
            sys.exit(os.EX_UNAVAILABLE)

        for url, file_name in patch_urls.items():
            self.add_patch(url, file_name)


def main():

    parser = argparse.ArgumentParser(
            description='Gets patch attachments from a Bug Tracking System'
    )
    parser.add_argument('pr', metavar='pr', type=str, nargs=1,
                        help='Pr id number')
    parser.add_argument('--mode', type=str, choices=['gnats', 'bz'],
                        default='bz', help='available modes to retrieve patch')
    parser.add_argument('--last', action='store_true',
                        help='only retrieve the latest iteration of a patch')
    parser.add_argument('--stdout', action='store_true',
                        help='dump patch on stdout')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(os.EX_USAGE)

    args = parser.parse_args()

    category = ""
    pr = str(args.pr[0])

    if pr and '/' in pr:
        category, pr = pr.split('/')

    Clazz = globals()['%sGetPatch' % args.mode.capitalize()]
    gp = Clazz(pr, category)
    gp.get(only_last=args.last, output_stdout=args.stdout)

    return os.EX_OK

if __name__ == '__main__':
    sys.exit(main())