aboutsummaryrefslogtreecommitdiff
blob: 5b81cb39dd4b1291763d6dbe68f35254e011b2c4 (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
import web
import json
import uuid
import re
import StringIO
import base64
from portage.versions import catpkgsplit

# matplotlib requires a writable home directory
import os
os.environ['HOME'] = '/tmp'
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

# check valid uuid

def is_uuid(uuid):
    regex = re.compile(r'^(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$')
    return regex.search(uuid)

# convert uuid string to raw bytes

def uuidbin(string):
    #TODO: string is not a valid uuid
    u = uuid.UUID(string)
    return u.bytes

# custom pkgsplit

def pkgsplit(pkgname):
    cpv={'cat':'','pkg':'','ver':''}
    cpvr = catpkgsplit(pkgname)
    if cpvr is None:
        pkgsplit = pkgname.split('/')
        cpv['cat'] = pkgsplit[0]
        cpv['pkg'] = pkgsplit[1]
    else:
        cpv['cat'] = cpvr[0]
        cpv['pkg'] = cpvr[1]
        cpv['ver'] = cpvr[2]
        if cpvr[3] != 'r0':
            cpv['ver'] = cpv['ver'] + '-' + cpvr[3]
    return cpv

# get functions for index keys
# lookup key and insert if not found

def get_kwkey(db, keyword):
    if keyword is None:
        return None
    db_keyword = db.select('KEYWORDS', vars={'keyword':keyword}, where='KEYWORD=$keyword')
    if len(db_keyword):
        kwkey = db_keyword[0]['KWKEY']
    else:
        kwkey = db.insert('KEYWORDS', KEYWORD=keyword)
    return kwkey

def get_lkey(db, lang):
    if lang is None:
        return None
    db_lang = db.select('LANG', vars={'lang':lang}, where='LANG=$lang')
    if len(db_lang):
        lkey = db_lang[0]['LKEY']
    else:
        lkey = db.insert('LANG', LANG=lang)
    return lkey

def get_fkey(db, feature):
    if feature is None:
        return None
    db_feature = db.select('FEATURES', vars={'feature':feature}, where='FEATURE=$feature')
    if len(db_feature):
        fkey = db_feature[0]['FKEY']
    else:
        fkey = db.insert('FEATURES', FEATURE=feature)
    return fkey

def get_mkey(db, mirror):
    if mirror is None:
        return None
    db_mirror = db.select('GENTOO_MIRRORS', vars={'mirror':mirror}, where='MIRROR=$mirror')
    if len(db_mirror):
        mkey = db_mirror[0]['MKEY']
    else:
        mkey = db.insert('GENTOO_MIRRORS', MIRROR=mirror)
    return mkey

def get_ukey(db, useflag):
    if useflag is None:
        return None
    db_useflag = db.select('USEFLAGS', vars={'useflag':useflag}, where='USEFLAG=$useflag')
    if len(db_useflag):
        ukey = db_useflag[0]['UKEY']
    else:
        ukey = db.insert('USEFLAGS', USEFLAG=useflag)
    return ukey

def get_pkey(db, package):
    if package is None:
        return None
    cpv = pkgsplit(package)
    db_package = db.select('PACKAGES', vars=cpv, where='CAT=$cat and PKG=$pkg and VER=$ver')
    if len(db_package):
        pkey = db_package[0]['PKEY']
    else:
        pkey = db.insert('PACKAGES', CAT=cpv['cat'], PKG=cpv['pkg'], VER=cpv['ver'])
    return pkey

def get_rkey(db, repo):
    if repo is None:
        return None
    db_repo = db.select('REPOSITORIES', vars={'repo':repo}, where='REPO=$repo')
    if len(db_repo):
        rkey = db_repo[0]['RKEY']
    else:
        rkey = db.insert('REPOSITORIES', REPO=repo)
    return rkey

def is_json_request():
    return web.ctx.environ['HTTP_ACCEPT'].find('json') != -1

def serialize(object, human=True):
    if human:
        indent = 2
    else:
        indent = None
    return json.JSONEncoder(indent=indent).encode(object)

def barchart(title, x_label, x_ticklabels, y_label, y_values):
    fig = Figure()
    canvas = FigureCanvas(fig)
    ind = range(len(y_values))

    ax = fig.add_subplot(1, 1, 1)
    ax.set_title(title)
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_xticks(ind)
    ax.set_xticklabels(x_ticklabels)
    ax.bar(ind, y_values, align='center')
 
    ret = StringIO.StringIO()
    canvas.print_figure(ret)
    return base64.b64encode(ret.getvalue())