~canonical-platform-qa/qakit/trunk

« back to all changes in this revision

Viewing changes to kpi/time_in_proposed.py

  • Committer: Jean-Baptiste Lallement
  • Date: 2017-12-22 10:55:27 UTC
  • Revision ID: jean-baptiste.lallement@ubuntu.com-20171222105527-onu05ebwivpazebh
Refactored script time_in_proposed

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
"""
6
6
from pprint import pprint
7
7
 
 
8
import argparse
 
9
from datetime import date
 
10
import json
8
11
import logging
 
12
import os
 
13
import sqlite3
 
14
import distro_info
9
15
from launchpadlib.launchpad import Launchpad
10
 
import sqlite3
11
 
import json
12
16
 
13
17
DEBUGLEVEL = logging.DEBUG
 
18
DEFAULT_DB = 'errors.sqlite'
 
19
 
 
20
try:
 
21
    RELEASE_NAME = distro_info.UbuntuDistroInfo().devel(result='codename')
 
22
    RELEASE_VER = \
 
23
        distro_info.UbuntuDistroInfo().devel(result='release').split(' ')[0]
 
24
except distro_info.DistroDataOutdated:
 
25
    RELEASE_NAME = 'bionic'
 
26
    RELEASE_VER = '18.04'
 
27
 
14
28
 
15
29
# Launchpad credentials
16
 
lp = None
17
 
ubuntu = None
18
 
archive = None
19
 
distros = {}
20
 
components = ('main', 'restricted', 'universe', 'multiverse')
21
 
suites = []
22
 
 
23
 
RELEASE_NAME = 'bionic'
24
 
RELEASE_VER = '18.04'
25
 
 
26
 
"""
 
30
LP = None
 
31
UBUNTU = None
 
32
ARCHIVE = None
 
33
DISTROS = {}
 
34
COMPONENTS = ('main', 'restricted', 'universe', 'multiverse')
 
35
SUITES = []
 
36
 
 
37
SQLTABLE_PUBLICATIONS = """
27
38
CREATE TABLE publications(
28
39
    release    TEXT,
29
40
    package    TEXT,
30
41
    version    TEXT,
31
42
    release_date    DATE,
32
43
    time_to_release    FLOAT
33
 
);
 
44
)"""
34
45
 
 
46
SQLINDEX_PK_PUBLICATIONS = """
35
47
CREATE INDEX pk_publications ON publications(
36
48
    release,
37
49
    package,
38
50
    version
39
 
    );
40
 
"""
 
51
    )"""
41
52
 
42
53
 
43
54
# def strip_epoch(verstr):
47
58
#        return l[1]
48
59
#    return verstr
49
60
 
 
61
def set_logging(debugmode=False):
 
62
    """Initialize logging"""
 
63
    logging.basicConfig(
 
64
        level=logging.DEBUG if debugmode else logging.INFO,
 
65
        format="%(asctime)s %(levelname)s %(message)s")
 
66
    logging.debug('Debug mode enabled')
 
67
 
 
68
 
 
69
def create_db(path, delete=False):
 
70
    """Create bugs database
 
71
 
 
72
    :param path: path to sqlite database
 
73
    """
 
74
    if os.path.exists(path):
 
75
        if delete:
 
76
            logging.info('Deleting database as requested')
 
77
            os.unlink(path)
 
78
        else:
 
79
            logging.debug('db %s already exists, skipping creation', path)
 
80
            return
 
81
 
 
82
    logging.debug('Creating db %s', path)
 
83
    conn = sqlite3.connect(path)
 
84
    cur = conn.cursor()
 
85
    logging.debug("Creating table publications ...")
 
86
    cur.execute(SQLTABLE_PUBLICATIONS)
 
87
    logging.debug("Creating primary index on publications ...")
 
88
    cur.execute(SQLINDEX_PK_PUBLICATIONS)
 
89
    cur.close()
 
90
    conn.close()
 
91
 
 
92
 
 
93
def _parse_arguments():
 
94
    """Parse command-line args, returning an argparse dict."""
 
95
 
 
96
    parser = argparse.ArgumentParser("Export number of crash reports for a "
 
97
                                     "package set")
 
98
    parser.add_argument('-d', '--debug', action='store_true',
 
99
                        default=False, help='enable debug mode')
 
100
    parser.add_argument('-D', '--dbname', default=DEFAULT_DB,
 
101
                        help='Name of the database to store results (default: '
 
102
                        '{})'.format(DEFAULT_DB))
 
103
    parser.add_argument('--delete', action='store_true',
 
104
                        default=False, help='Delete the database if it exists')
 
105
    return parser.parse_args()
 
106
 
50
107
 
51
108
def load_packageset(distroname, name):
52
109
    """ List packages in a packageset """
53
 
    logging.debug("Fetching packageset for %s/%s" % (distroname,
54
 
                                                     name))
55
 
    pkgset = lp.packagesets.getByName(distroseries=distros[distroname], name=name)
 
110
    logging.debug("Fetching packageset for %s/%s", distroname, name)
 
111
    pkgset = LP.packagesets.getByName(distroseries=DISTROS[distroname], name=name)
56
112
    srcs = pkgset.getSourcesIncluded()
57
113
    return list(srcs)
58
114
 
59
115
 
60
 
def write_packageset(srcs, format='text'):
 
116
def write_packageset(srcs, output_fmt='text'):
61
117
    """ Dump package set to file """
62
 
    with open('desktop.pkgset', 'w') as outfile:
63
 
        if format == 'text':
 
118
    with open('desktop.pkgset.%s' % date.today().strftime('%Y%m%d'), 'w') \
 
119
            as outfile:
 
120
        if output_fmt == 'text':
64
121
            for src in srcs:
65
122
                outfile.write(src + '\n')
66
 
        elif format == 'json':
67
 
                json.dump(srcs, outfile)
 
123
        elif output_fmt == 'json':
 
124
            json.dump(srcs, outfile)
68
125
        else:
69
126
            logging.error('Unsupported format: %s', format)
70
127
 
71
128
 
72
129
def publishing_history(distroname, sourcename):
73
130
    ''' Load publishing history from LP'''
74
 
    global archive, distros
75
 
    logging.debug("Fetching publishing history for %s/%s" % (distroname, sourcename))
76
 
    pubs = archive.getPublishedSources(source_name=sourcename, exact_match=True, distro_series=distros[distroname])
 
131
    global ARCHIVE, DISTROS
 
132
    logging.debug("Fetching publishing history for %s/%s", distroname, sourcename)
 
133
    pubs = ARCHIVE.getPublishedSources(source_name=sourcename,
 
134
                                       exact_match=True,
 
135
                                       distro_series=DISTROS[distroname])
77
136
    records = {}
78
137
    for pub in pubs:
79
138
        if pub.source_package_version not in records:
85
144
        records[pub.source_package_version][pub.pocket.lower()] = pub.date_published
86
145
        if records[pub.source_package_version]['release'] and \
87
146
           records[pub.source_package_version]['proposed']:
88
 
            records[pub.source_package_version]['delta'] = records[pub.source_package_version]['release'] - records[pub.source_package_version]['proposed']
 
147
            records[pub.source_package_version]['delta'] = \
 
148
                records[pub.source_package_version]['release'] -\
 
149
                records[pub.source_package_version]['proposed']
89
150
 
90
151
    return records
91
152
 
92
153
 
93
154
def lpinit():
94
155
    ''' Init LP credentials, archive and distro list '''
95
 
    global lp, ubuntu, archive, distros, suites
 
156
    global LP, UBUNTU, ARCHIVE, DISTROS, SUITES
96
157
    logging.debug("Initializing LP Credentials")
97
 
    lp = Launchpad.login_anonymously('sru-report', 'production')
98
 
    ubuntu = lp.distributions['ubuntu']
99
 
    archive = ubuntu.getArchive(name='primary')
100
 
    for serie in ubuntu.series:
 
158
    LP = Launchpad.login_anonymously('sru-report', 'production')
 
159
    UBUNTU = LP.distributions['ubuntu']
 
160
    ARCHIVE = UBUNTU.getArchive(name='primary')
 
161
    for serie in UBUNTU.series:
101
162
        if serie.active:
102
 
            suites.append(serie.name)
103
 
            distros[serie.name] = serie
104
 
    logging.debug("Active releases found: %s" % " ".join(suites))
 
163
            SUITES.append(serie.name)
 
164
            DISTROS[serie.name] = serie
 
165
    logging.debug("Active releases found: %s", " ".join(SUITES))
105
166
 
106
167
 
107
168
def main():
108
 
    logging.basicConfig(level=logging.DEBUG,
109
 
                        format="%(asctime)s - %(levelname)s - %(message)s")
 
169
    ''' Main routine '''
 
170
    args = _parse_arguments()
 
171
    set_logging(args.debug)
 
172
 
 
173
    pprint(args)
 
174
 
110
175
    lpinit()
111
176
 
112
177
    pkgs = load_packageset(RELEASE_NAME, 'ubuntu-desktop')
113
178
    write_packageset(pkgs)
114
179
 
115
 
    conn = sqlite3.connect("errors.sqlite")
 
180
    create_db(args.dbname, args.delete)
 
181
    conn = sqlite3.connect(args.dbname)
 
182
 
116
183
    cur = conn.cursor()
117
184
 
118
185
    for pkg in pkgs:
130
197
                            version,
131
198
                            release_date,
132
199
                            time_to_release)
133
 
                            VALUES(?,?,?,?,?)
134
 
                            """, [
135
 
                            'Ubuntu %s' % RELEASE_VER,
136
 
                            pkg,
137
 
                            version,
138
 
                            v['release'].strftime('%Y-%m-%d'),
139
 
                            v['delta'].total_seconds()])
 
200
                            VALUES(?,?,?,?,?)""",
 
201
                            [
 
202
                                'Ubuntu %s' % RELEASE_VER,
 
203
                                pkg,
 
204
                                version,
 
205
                                v['release'].strftime('%Y-%m-%d'),
 
206
                                v['delta'].total_seconds()
 
207
                            ])
140
208
 
141
209
                print('%s,%s,%s,%s,%s' % (
142
210
                    pkg,
149
217
 
150
218
 
151
219
if __name__ == "__main__":
152
 
    main()
 
220
    exit(main())