summaryrefslogtreecommitdiffstats
path: root/devel/reports.py
blob: ca1a3321b3d27a6f53bca10ca8df5e12333b1efd (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
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
from datetime import timedelta
import pytz

from django.db.models import F
from django.template.defaultfilters import filesizeformat
from django.utils.timezone import now

from .models import DeveloperKey, UserProfile
from main.models import PackageFile
from packages.models import PackageRelation, Depend

class DeveloperReport(object):
    def __init__(self, slug, name, desc, packages_func,
            names=None, attrs=None, personal=True):
        self.slug = slug
        self.name = name
        self.description = desc
        self.packages = packages_func
        self.names = names
        self.attrs = attrs
        self.personal = personal


def old(packages, username):
    cutoff = now() - timedelta(days=365 * 2)
    return packages.filter(
            build_date__lt=cutoff).order_by('build_date')


def outofdate(packages, username):
    cutoff = now() - timedelta(days=30)
    return packages.filter(
            flag_date__lt=cutoff).order_by('flag_date')


def big(packages, username):
    cutoff = 50 * 1024 * 1024
    packages = packages.filter(
            compressed_size__gte=cutoff).order_by('-compressed_size')
    # Format the compressed and installed sizes with MB/GB/etc suffixes
    for package in packages:
        package.compressed_size_pretty = filesizeformat(
            package.compressed_size)
        package.installed_size_pretty = filesizeformat(
            package.installed_size)
    return packages


def badcompression(packages, username):
    cutoff = 0.90 * F('installed_size')
    packages = packages.filter(compressed_size__gt=0, installed_size__gt=0,
            compressed_size__gte=cutoff).order_by('-compressed_size')

    # Format the compressed and installed sizes with MB/GB/etc suffixes
    for package in packages:
        package.compressed_size_pretty = filesizeformat(
            package.compressed_size)
        package.installed_size_pretty = filesizeformat(
            package.installed_size)
        ratio = package.compressed_size / float(package.installed_size)
        package.ratio = '%.3f' % ratio
        package.compress_type = package.filename.split('.')[-1]

    return packages


def uncompressed_man(packages, username):
    # checking for all '.0'...'.9' + '.n' extensions
    bad_files = PackageFile.objects.filter(is_directory=False,
            directory__contains='/man/',
            filename__regex=r'\.[0-9n]').exclude(
            filename__endswith='.gz').exclude(
            filename__endswith='.xz').exclude(
            filename__endswith='.bz2').exclude(
            filename__endswith='.html')
    if username:
        pkg_ids = set(packages.values_list('id', flat=True))
        bad_files = bad_files.filter(pkg__in=pkg_ids)
    bad_files = bad_files.values_list(
            'pkg_id', flat=True).order_by().distinct()
    return packages.filter(id__in=set(bad_files))


def uncompressed_info(packages, username):
    # we don't worry about looking for '*.info-1', etc., given that an
    # uncompressed root page probably exists in the package anyway
    bad_files = PackageFile.objects.filter(is_directory=False,
            directory__endswith='/info/', filename__endswith='.info')
    if username:
        pkg_ids = set(packages.values_list('id', flat=True))
        bad_files = bad_files.filter(pkg__in=pkg_ids)
    bad_files = bad_files.values_list(
            'pkg_id', flat=True).order_by().distinct()
    return packages.filter(id__in=set(bad_files))


def unneeded_orphans(packages, username):
    owned = PackageRelation.objects.all().values('pkgbase')
    required = Depend.objects.all().values('name')
    # The two separate calls to exclude is required to do the right thing
    return packages.exclude(pkgbase__in=owned).exclude(
            pkgname__in=required)


def mismatched_signature(packages, username):
    filtered = []
    packages = packages.select_related(
            'arch', 'repo', 'packager').filter(signature_bytes__isnull=False)
    known_keys = DeveloperKey.objects.select_related(
            'owner').filter(owner__isnull=False)
    known_keys = {dk.key: dk for dk in known_keys}
    for package in packages:
        bad = False
        sig = package.signature
        dev_key = known_keys.get(sig.key_id, None)
        if dev_key:
            package.sig_by = dev_key.owner
            if dev_key.owner_id != package.packager_id:
                bad = True
        else:
            package.sig_by = sig.key_id
            bad = True

        if bad:
            filtered.append(package)
    return filtered


def signature_time(packages, username):
    cutoff = timedelta(hours=24)
    filtered = []
    packages = packages.select_related(
            'arch', 'repo', 'packager').filter(signature_bytes__isnull=False)
    for package in packages:
        sig = package.signature
        sig_date = sig.creation_time.replace(tzinfo=pytz.utc)
        package.sig_date = sig_date.date()
        if sig_date > package.build_date + cutoff:
            filtered.append(package)

    return filtered


REPORT_OLD = DeveloperReport('old', 'Old',
        'Packages last built more than two years ago', old)

REPORT_OUTOFDATE = DeveloperReport('long-out-of-date', 'Long Out-of-date',
        'Packages marked out-of-date more than 30 days ago', outofdate)

REPORT_BIG = DeveloperReport('big', 'Big',
        'Packages with compressed size > 50 MiB', big,
        ['Compressed Size', 'Installed Size'],
        ['compressed_size_pretty', 'installed_size_pretty'])

REPORT_BADCOMPRESS = DeveloperReport('badcompression', 'Bad Compression',
        'Packages with a compression ratio of less than 10%', badcompression,
        ['Compressed Size', 'Installed Size', 'Ratio', 'Type'],
        ['compressed_size_pretty', 'installed_size_pretty','ratio', 'compress_type'])


REPORT_MAN = DeveloperReport('uncompressed-man', 'Uncompressed Manpages',
        'Packages with uncompressed manpages', uncompressed_man)

REPORT_INFO = DeveloperReport('uncompressed-info', 'Uncompressed Info Pages',
        'Packages with uncompressed info pages', uncompressed_info)

REPORT_ORPHANS = DeveloperReport('unneeded-orphans', 'Unneeded Orphans',
        'Packages that have no maintainer and are not required by any '
        + 'other package in any repository', unneeded_orphans,
        personal=False)

REPORT_SIGNATURE = DeveloperReport('mismatched-signature',
        'Mismatched Signatures',
        'Packages where the signing key is unknown or signer != packager',
        mismatched_signature,
        ['Signed By', 'Packager'],
        ['sig_by', 'packager'])

REPORT_SIG_TIME = DeveloperReport('signature-time', 'Signature Time',
        'Packages where the signature timestamp is more than 24 hours '
        + 'after the build timestamp',
        signature_time,
        ['Signature Date', 'Packager'],
        ['sig_date', 'packager'])


def available_reports():
    return (
        REPORT_OLD,
        REPORT_OUTOFDATE,
        REPORT_BIG,
        REPORT_BADCOMPRESS,
        REPORT_MAN,
        REPORT_INFO,
        REPORT_ORPHANS,
        REPORT_SIGNATURE,
        REPORT_SIG_TIME,
    )