Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | import argparse |
| 9 | import datetime |
| 10 | import os |
| 11 | import 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). |
| 16 | try: |
| 17 | import suds |
| 18 | except 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. |
| 24 | coverity_host = os.environ.get("coverity_host", "coverity.cambridge.arm.com") |
| 25 | |
| 26 | parser = argparse.ArgumentParser() |
| 27 | |
| 28 | parser.add_argument("--description", help="Snapshot description filter") |
| 29 | parser.add_argument("--file", dest="output_file", help="Output file. Mandatory") |
| 30 | parser.add_argument("--old", default=10, help="Max snapshot age in days") |
| 31 | parser.add_argument("--host", default=coverity_host, help="Coverity server") |
| 32 | parser.add_argument("--version", help="Snapshot version filter") |
| 33 | parser.add_argument("stream_name") |
| 34 | |
| 35 | opts = parser.parse_args() |
| 36 | |
| 37 | if 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. |
| 42 | try: |
| 43 | user = os.environ["TFCIBOT_USER"] |
| 44 | password = os.environ["TFCIBOT_PASSWORD"] |
| 45 | except: |
| 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 |
| 52 | client = suds.client.Client("http://{}/ws/v9/configurationservice?wsdl".format(opts.host)) |
| 53 | security = suds.wsse.Security() |
| 54 | token = suds.wsse.UsernameToken(user, password) |
| 55 | security.tokens.append(token) |
| 56 | client.set_options(wsse=security) |
| 57 | |
| 58 | # Construct stream ID data object |
| 59 | streamid_obj = client.factory.create("streamIdDataObj") |
| 60 | streamid_obj.name = opts.stream_name |
| 61 | |
| 62 | # Snapshot filter |
| 63 | filter_obj = client.factory.create("snapshotFilterSpecDataObj") |
| 64 | |
| 65 | # Filter snapshots for age |
| 66 | past = datetime.date.today() - datetime.timedelta(days=opts.old) |
| 67 | filter_obj.startDate = past.strftime("%Y-%m-%d") |
| 68 | |
| 69 | if opts.version: |
| 70 | filter_obj.versionPattern = opts.version |
| 71 | |
| 72 | if opts.description: |
| 73 | filter_obj.descriptionPattern = opts.description |
| 74 | |
| 75 | # Query server |
| 76 | results = client.service.getSnapshotsForStream(streamid_obj, filter_obj) |
| 77 | |
| 78 | # Print ID of the last snapshot if results were returned |
| 79 | if 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 |