blob: 3ddd37718f225a769e73920fe2b08ea065f61463 [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
3# Copyright (c) 2019, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import argparse
9import datetime
10import os
11import sys
12
13# suds is not a standard library package. Although it's installed in the Jenkins
14# slaves, it might not be so in the user's machine (when running Coverity scan
15# on there).
16try:
17 import suds
18except ImportError:
19 print(" You need to have suds Python3 package to query Coverity server")
20 print(" pip3 install suds-py3")
21 sys.exit(0)
22
23# Get coverity host from environment, or fall back to the default one.
24coverity_host = os.environ.get("coverity_host", "coverity.cambridge.arm.com")
25
26parser = argparse.ArgumentParser()
27
28parser.add_argument("--description", help="Snapshot description filter")
29parser.add_argument("--file", dest="output_file", help="Output file. Mandatory")
30parser.add_argument("--old", default=10, help="Max snapshot age in days")
31parser.add_argument("--host", default=coverity_host, help="Coverity server")
32parser.add_argument("--version", help="Snapshot version filter")
33parser.add_argument("stream_name")
34
35opts = parser.parse_args()
36
37if not opts.output_file:
38 raise Exception("Must specify an output file")
39
40# We output the snapshot ID to the specified file. In case of any errors, we
41# remove the file, and Coverity wrapper can test for its existence.
42try:
43 user = os.environ["TFCIBOT_USER"]
44 password = os.environ["TFCIBOT_PASSWORD"]
45except:
46 print(" Unable to get credentials for user tfcibot")
47 print(" For potentially faster analysis, suggest set "
48 "TFCIBOT_PASSWORD and TFCIBOT_PASSWORD in the environment")
49 sys.exit(0)
50
51# SOAP magic stuff
52client = suds.client.Client("http://{}/ws/v9/configurationservice?wsdl".format(opts.host))
53security = suds.wsse.Security()
54token = suds.wsse.UsernameToken(user, password)
55security.tokens.append(token)
56client.set_options(wsse=security)
57
58# Construct stream ID data object
59streamid_obj = client.factory.create("streamIdDataObj")
60streamid_obj.name = opts.stream_name
61
62# Snapshot filter
63filter_obj = client.factory.create("snapshotFilterSpecDataObj")
64
65# Filter snapshots for age
66past = datetime.date.today() - datetime.timedelta(days=opts.old)
67filter_obj.startDate = past.strftime("%Y-%m-%d")
68
69if opts.version:
70 filter_obj.versionPattern = opts.version
71
72if opts.description:
73 filter_obj.descriptionPattern = opts.description
74
75# Query server
76results = client.service.getSnapshotsForStream(streamid_obj, filter_obj)
77
78# Print ID of the last snapshot if results were returned
79if results:
80 try:
81 with open(opts.output_file, "w") as fd:
82 print(results[-1].id, file=fd)
83 except:
84 os.remove(opts.output_file)
85 raise