blob: a8067300f92c482ae6f78d55b019f36c7e33c76a [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")
Zelalem219df412020-05-17 19:21:20 -050025coverity_port = os.environ.get("coverity_port", "8443")
Fathi Boudra422bf772019-12-02 11:10:16 +020026
27parser = argparse.ArgumentParser()
28
29parser.add_argument("--description", help="Snapshot description filter")
30parser.add_argument("--file", dest="output_file", help="Output file. Mandatory")
31parser.add_argument("--old", default=10, help="Max snapshot age in days")
32parser.add_argument("--host", default=coverity_host, help="Coverity server")
Zelalem219df412020-05-17 19:21:20 -050033parser.add_argument("--https-port", default=coverity_port, help="Coverity Secure port")
Fathi Boudra422bf772019-12-02 11:10:16 +020034parser.add_argument("--version", help="Snapshot version filter")
35parser.add_argument("stream_name")
36
37opts = parser.parse_args()
38
39if not opts.output_file:
40 raise Exception("Must specify an output file")
41
42# We output the snapshot ID to the specified file. In case of any errors, we
43# remove the file, and Coverity wrapper can test for its existence.
44try:
45 user = os.environ["TFCIBOT_USER"]
46 password = os.environ["TFCIBOT_PASSWORD"]
47except:
48 print(" Unable to get credentials for user tfcibot")
49 print(" For potentially faster analysis, suggest set "
50 "TFCIBOT_PASSWORD and TFCIBOT_PASSWORD in the environment")
51 sys.exit(0)
52
53# SOAP magic stuff
Zelalem219df412020-05-17 19:21:20 -050054client = suds.client.Client("https://{}/ws/v9/configurationservice?wsdl".format(opts.host))
Fathi Boudra422bf772019-12-02 11:10:16 +020055security = suds.wsse.Security()
56token = suds.wsse.UsernameToken(user, password)
57security.tokens.append(token)
58client.set_options(wsse=security)
59
60# Construct stream ID data object
61streamid_obj = client.factory.create("streamIdDataObj")
62streamid_obj.name = opts.stream_name
63
64# Snapshot filter
65filter_obj = client.factory.create("snapshotFilterSpecDataObj")
66
67# Filter snapshots for age
68past = datetime.date.today() - datetime.timedelta(days=opts.old)
69filter_obj.startDate = past.strftime("%Y-%m-%d")
70
71if opts.version:
72 filter_obj.versionPattern = opts.version
73
74if opts.description:
75 filter_obj.descriptionPattern = opts.description
76
77# Query server
78results = client.service.getSnapshotsForStream(streamid_obj, filter_obj)
79
80# Print ID of the last snapshot if results were returned
81if results:
82 try:
83 with open(opts.output_file, "w") as fd:
84 print(results[-1].id, file=fd)
85 except:
86 os.remove(opts.output_file)
87 raise