aboutsummaryrefslogtreecommitdiff
path: root/load_copyright.py
blob: 2f545e1eeed3a5bbb51e4d31417d461efce067c6 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

import debian.deb822
import pandas as pd

#from IPython.core import ultratb
#sys.excepthook = ultratb.FormattedTB(mode='Verbose',
#     color_scheme='Linux', call_pdb=1)

class BadFormat(Exception):
    pass

def read_copyright(fh):
    paras = debian.deb822.Deb822.iter_paragraphs(fh)

    try:
        header = paras.next()

        header = dict(header)
        paras = [(p.keys()[0], dict(p)) for p in paras]
    except (KeyError, TypeError, StopIteration):
        raise BadFormat('not in DEP-5 format?')
        return

    if 'Format' not in header:
        raise ValueError('no Format field')
        return

    files = []
    licences = []
    #file_fields = set([
    #    'Authors', 'Files', 'Comment', 'Copyright', 'Disclaimer', 'Homepage',
    #    'License', 'License-Alias', 'Upstream-Authors', 'X-Comment',
    #    'X-Notes'])

    for (type, d) in paras:
        type = type.replace('Licence', 'License')

        if 'Licence' in d:
            # !!!
            d['License'] = d['Licence']
            del d['Licence']

        if type == 'Files':
            if 'debian' in d['Files']:
                continue
            if 'License' not in d:
                raise ValueError('no license: ' + repr(d))
                return

            #keys = set(d.keys())
            #assert keys <= file_fields, keys
            files.append(d)
        elif type == 'License':
            # XXX constrain permissible keys here?
            #print d.keys()
            licences.append(d)
        else:
            # Be conservative. Missing license information is a problem.
            raise ValueError('bad para: ' + type)

    return (header, files, licences)

def import_one(pkgname, fh):
    try:
        (header, files, licences) = read_copyright(fh)
    except BadFormat:
        print 'info: not readable'
        return None
    except ValueError, e:
        print 'err:', e
        #print 'err:', repr(e)
        return None

    header['_srcpkg'] = pkgname
    header['_license'] = header.get('License', '').split('\n')[0]

    if 'Upstream-Name' in header:
        # Make spaces breakable (!).
        # Conceivably other characters need replacing.
        header['Upstream-Name'] = \
            header['Upstream-Name'].replace(u'\xa0', ' ')
            #copy_summary['Upstream-Name'].replace('\xc2\xa0', ' ')
        if '@' in header['Upstream-Name']:
            header['Upstream-Name'] = pkgname

    copy_summary = pd.DataFrame([header])
    #print copy_summary.T.to_string()
    #print

    for d in files:
        d['_srcpkg'] = pkgname
        d['_license'] = d['License'].split('\n')[0]

    for d in licences:
        d['_srcpkg'] = pkgname
        d['_license'] = d['License'].split('\n')[0]

    copy_files = pd.DataFrame(files)
    licence = pd.DataFrame(licences)
    return (copy_summary, copy_files, licence)

def get_pkgname(path):
    (dir, base) = os.path.split(path)

    if base in ('current', 'copyright'):
        return get_pkgname(dir)
    else:
        return base

def main(paths):
    summaries = []
    files = []
    licenses = []

    for path in paths:
        pkgname = get_pkgname(path)
        print pkgname, path
        data = import_one(pkgname, file(path))

        if data is not None:
            (summary, file_, license) = data
            summaries.append(summary)
            files.append(file_)
            licenses.append(license)

        print

    summaries = pd.concat(summaries)
    files = pd.concat(files)
    licenses = pd.concat(licenses)

    #from IPython import embed
    #embed()

    #from IPython.core.debugger import Pdb
    #Pdb().set_trace()

    store = pd.HDFStore('cp.h5')
    store['cp_summary'] = summaries
    store['cp_files'] = files
    store['licenses'] = licenses
    store.close()

if __name__ == '__main__':
    main(sys.argv[1:])