~pieq/hwcert-tools/tpelab-tools

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
#!/usr/bin/python
#
# pickup-fglrx-needed-system.py
#
# PLEASE THINK TWICE BEFORE USING --batch_limit 0
# OTHERWISE THE LOADING IS HEAVY
#
# A script to pick up the systems needing fglrx to pass
# certification based on the data on C3.
# This is not completed and not it could only find out
# the not with title "roprietary" and body containing "video".
# Please see the function is_proprietary_needed
#
# TODO: implement function is_amd_ait_gfx
#
# Usage:
#   ./pickup-fglrx-needed-system.py -u USER -k API_KEY CID --batch_limit 0
#
#   You may edit releases_precise to specify the releases
#   you want to scan.
#
#   --batch_limit 0 to help you get multiple certificates for one CID.
#   please make sure you won't make the server have heavy loading before
#   using it.
#

import argparse
import requests
import logging

# I want to scan 12.04 LTS to 12.04.4 LTS
# Please note I exclude 12.04.5 LTS
releases_precise = ["12.04.4 LTS",
                    "12.04.3 LTS",
                    "12.04.2 LTS",
                    "12.04.1 LTS",
                    "12.04 LTS"]

def get_machine_id_from_hardware_json(json_hardware):
    """
    @json_hardware, a json-to-python data structure.
    @return: int, id_machine, which could be used to request certificates.
    """
    return json_hardware['id']

def get_machine_id_by_cid(args, cid):
    """
    cid: 201201-10383
    id_machine, int, which could be used to request certificates.
    """
    api_uri = args.instance_uri + 'api/v1/hardware/' + cid
    api_params = {'username': args.username,
                   'api_key': args.apikey,
                   'limit': args.batch_limit}
    request_hardware = requests.get(api_uri, params=api_params)
    id_machine = get_machine_id_from_hardware_json(request_hardware.json())
    return id_machine


def get_certificates_by_machine_id(args, id_machine):
    """
    id_machine: machine id, which could be used to request certificates.
    @json_certificates: the requested json certificate info from C3.
    """
    api_uri = args.instance_uri + 'api/v1/certificates/?machine='\
                                + str(id_machine)
    api_params = {'username': args.username,
                   'api_key': args.apikey,
                   'limit': args.batch_limit}
    request_certificates = requests.get(api_uri, params=api_params)
    return request_certificates.json()


def get_release_certificates(json_certificates):
    """
    @json_certificates: the requested json certificate info from C3.
    @release_certificates: list,
        release_certificates, [release_name_1, release_name_2,...],
        the release names are strings.
    """
    release_certificates = []
    for certificate in json_certificates['objects']:
        release_certificates.append(certificate['release']['release'])
    # TODO: validate there is no duplicate certified release.
    return release_certificates


def get_latest_release(release_certificates, release="precise"):
    """
    @release_certificates: list,
            release_certificates, [release_name_1, release_name_2,...],
            the release names are strings.
    @release: precise, saucy, trusty ...etc.
    @point_release_latest: string, the latest release, e.g. 12.04.5
    """
    if release == "precise" :
        for point_release in releases_precise:
            try:
                release_certificates.index(point_release)
                point_release_latest = point_release
                break
            except ValueError:
                pass
    return point_release_latest


def get_certificate_from_json(json_certificates, release):
    """
    @json_certificates: the requested json certificate info from C3.
    @release: string, e.g. "12.04.5 LTS"
    @certificate: dict info for a certificate
    """
    for certificate in json_certificates['objects']:
        if certificate['release']['release'] == release :
            return certificate
        else:
            logging.warning("release name %s was not found." % release)


def is_amd_ait_gfx(json_certificate):
    """
    @json_certificate: json info for a certificate
    """
    # TODO: implement it when knowing how to use C3 API
    return True


def is_proprietary_needed(certificate):
    """
    check whether there is a note to say that needing
    the proprietary driver to pass the certification.
    @certificate: dict info for a certificate
    """
    for note in certificate['notes']:
        # TODO: the search condition could be smarter
        if (note['title'].find('roprietary') > 0) and (note['body'].find('video') > 0):
            return True
    return False

def main():
    # parsing the arguments and initializing the parameters
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_limit', type=int, default=1,
                        help='Number of element in a batch result. \
                        0 for as many as possible')
    parser.add_argument('--instance_uri',
                        default="https://certification.canonical.com/",
                        help='Certification site URI. ')
    parser.add_argument('-u', '--username', help='User name', required=True)
    parser.add_argument('-k', '--apikey', help='API key', required=True)
    parser.add_argument('-m', '--mode',
                        default='all',
                        help='pass, fail or skipped. Default is all')
    parser.add_argument('cid',
                        help='the canonical cid, e.g. 201201-10383')
    parser.add_argument("-d", "--debug", help="Set debug mode",
#                        action='store_const', const=logging.DEBUG,
                        default=logging.WARNING)

    args = parser.parse_args()
    cid = args.cid

    logging.basicConfig(level=args.debug)

    machine_id = get_machine_id_by_cid(args, cid)

    json_certificates = get_certificates_by_machine_id(args, machine_id)

    json_certificate = get_certificate_from_json(json_certificates,
                get_latest_release(get_release_certificates(json_certificates)))

    if is_amd_ait_gfx(json_certificate) and is_proprietary_needed(json_certificate):
        #print "%s has AMD/ATI graphic cards and need fglrx" % args.cid
        #print "https://certification.canonical.com/hardware/%s" % args.cid
        print "%s https://certification.canonical.com/certificates/%s" % (args.cid, json_certificate['name'])

if __name__ == "__main__":
    main()