aboutsummaryrefslogtreecommitdiff
blob: 20d1caa684fa0a2e0e6d5df22da8af8d05a81a25 (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
#! /usr/bin/env python
import os, sys
from datetime import datetime
from lxml.html import fromstring
from optparse import OptionParser

path = os.path.join(os.path.dirname(__file__), os.path.pardir)
sys.path.insert(0, path)
del path

from grumpy import app
from grumpy.models import db, Package, PkgIssue, Setting
from grumpy.utils import compare_version

PLUGIN_NAME='ver_bumper::'

def gc_collect(timestamp):
    """Remove old QA issues from database returning number of rows deleted."""
    db.session.expire_all()
    print ("DEBUG: Deleted %d old issues." % PkgIssue.query \
                        .filter_by(plugin=PLUGIN_NAME) \
                        .filter(PkgIssue.created_on < timestamp).delete(False))

def insert_issues(invalid):
    """Insert QA issues into db."""
    if 'maintainer-needed@gentoo.org' in invalid:
        h = Developer.query.filter_by(email='maintainer-needed@gentoo.org').first()
        for pkg in h.packages:
            pkg.qaissues.append(PkgIssue(pkg, PLUGIN_NAME, 'maintainer-needed'))
        invalid.remove('maintainer-needed@gentoo.org')
    for dev in invalid:
        d = Developer.query.filter_by(email=dev).first()
        for pkg in d.packages:
            pkg.qaissues.append(PkgIssue(pkg, PLUGIN_NAME, 'unknown-dev', \
                      'Maintainer %s listed in metadata.xml is not as ' \
                      'an active Gentoo developer.' % dev))
    db.session.commit()

if __name__ == '__main__':
    from grumpy.vdb.pypi import PyPi
    parser = OptionParser(usage="usage: %prog [options] CONFFILE")
    (opts, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("provide path to configuration file as first argument")
        sys.exit(1)
    # ...
    pypi = PyPi()
    items = pypi.fetch_and_parse_all()
    # Gather package versions
    updates = {}
    missing = []
    for p in pypi.pkgs:
        pyp = pypi.pkgs[p]
        if pyp not in items.keys():
            missing.append([p, pyp])
        else:
            updates[p] = items[pyp]
    PLUGIN_NAME = PLUGIN_NAME + pypi.__class__.__name__
    with app.test_request_context():
        timestamp = datetime.now()
        app.config.from_pyfile(args[0])
        try:
            # Delete existing issues
            print ("DEBUG: Deleted %d old issues." % PkgIssue.query \
                        .filter_by(plugin=PLUGIN_NAME) \
                        .filter(PkgIssue.created_on < timestamp).delete(False))
            db.session.commit()

            # File 'missing' package mapping issues
            for item in missing:
                p = Package.query.filter_by(key=item).first()
                if not p:
                    print "DEBUG: package '%s' is missing from upstream." % item
                    continue
                p.qaissues.append(PkgIssue(p, PLUGIN_NAME, \
                    'missing-upstream', \
                    'No package "%s" found in upstream: %s' % \
                        (item, pypi.__class__.__name__)))
            # Run version checks
            # TODO: Figure out slotted ebuilds
            for pkg, ver in updates.iteritems():
                p = Package.query.filter_by(key=pkg).first()
                if not p:
                    print "DEBUG: package '%s' is missing from portage." % item
                    continue
                # We need to sort pkg versions
                ver = sorted(ver, compare_version, reverse=True)
                res = compare_version(ver[0], p.versions[0])
                if res == -1:
                    p.qaissues.append(PkgIssue(p, PLUGIN_NAME, \
                    'version-mismatch', 'Package "%s" has newer version in ' \
                    'Portage than on "%s" upstream' % \
                    (pkg, pypi.__class__.__name__)))
                elif res == 1:
                    p.qaissues.append(PkgIssue(p, PLUGIN_NAME, \
                    'version-bump', 'Package "%s" has newer version "%s" '
                    'available in "%s" upstream ' % \
                    (pkg, p.versions[0], pypi.__class__.__name__)))
                db.session.commit()
            # Update settings and add info about last run..
            Setting.query.filter_by(name=PLUGIN_NAME).delete(False)
            db.session.add(Setting(PLUGIN_NAME, str(timestamp)))
            db.session.commit()
        except Exception:
            db.session.rollback()