~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
#!/usr/bin/python
#
# findsbmstatus.py
#
# PLEASE THINK TWICE BEFORE USING --batch_limit 0
# OTHERWISE THE LOADING IS HEAVY
#
# $ ./findsbmstatus.py -u <username>  -k <api key> -c 201103-7435 -g 2014-04-12 -l 2014-04-13 -n 'wireless/wireless_connection_wpa_n' -r unsupported --batch_limit 0
# 201103-7435 https://certification.canonical.com/hardware/201103-7435/submission/97884/attachments/729912/
#
# This means CID 201103-7435 created between 2014-04-12 and 2014-04-13 has
# a testing wireless/wireless_connection_wpa_n with status unsupported
#
# TODO:
#   1. what if one CID with multiple submissions during the time interval?
#   2. modify the hardcode of the attachment name

import argparse
import requests
import logging

di_submission_di = {}

def getMachineRptNoByCID(args):
    api_uri = args.instance_uri + \
              'api/v1/reports/hardware/?created_at__gt=' + \
              args.time_after + \
              '&created_at__lt=' + \
              args.time_before
    api_params = {'username': args.username, 'api_key': args.apikey, 'limit': args.batch_limit}
    result = requests.get(api_uri, params=api_params)
    json = result.json()
    for rpt in json['objects']:
        if rpt['canonical_id'] == args.cid:
            contents = {}
            contents['cid'] = args.cid
            for att in rpt['attachments']:
                if att['name'] == 'lspci_attachment' : # TODO: you don't need to hardcode
                    contents['attachment'] = args.instance_uri + att['url'][1:]
            di_submission_di[str(rpt['id'])] = contents


def showResult(args, rpt_no):
    api_uri = args.instance_uri + \
              'api/v1/testresults/?report=' + \
              rpt_no + \
              '&test__name=' + \
              args.name + \
              '&status=' + \
              args.status
    api_params = {'username': args.username, 'api_key': args.apikey, 'limit': args.batch_limit}
    result = requests.get(api_uri, params=api_params)
    json = result.json()
    if json['meta']['total_count'] == 1:
        print "%s %s" % (di_submission_di[rpt_no]['cid'], di_submission_di[rpt_no]['attachment'])


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('-c', '--cid',
                        help='canonical ID')
    parser.add_argument('-g', '--time-after',
                        help='submission created after')
    parser.add_argument('-l', '--time-before',
                        help='submission created before')
    parser.add_argument('-n', '--name',
                        help='the name of the testing item')
    parser.add_argument('-r', '--status',
                        help='result of the test status, will be pass, fail or unsupported')
    parser.add_argument("-d", "--debug", help="Set debug mode",
                        # action='store_const', const=logging.DEBUG,
                        default=logging.WARNING)

    args = parser.parse_args()

    logging.basicConfig(level=args.debug)

    getMachineRptNoByCID(args)

    for rpt_no in di_submission_di.keys():
        showResult(args, rpt_no)


if __name__ == "__main__":
    main()