1
+ #!/usr/bin/env python
2
+
3
+ '''
4
+ Copyright (C) 2024 Synopsys, Inc.
5
+ http://www.blackducksoftware.com/
6
+
7
+ Licensed to the Apache Software Foundation (ASF) under one
8
+ or more contributor license agreements. See the NOTICE file
9
+ distributed with this work for additional information
10
+ regarding copyright ownership. The ASF licenses this file
11
+ to you under the Apache License, Version 2.0 (the
12
+ "License"); you may not use this file except in compliance
13
+ with the License. You may obtain a copy of the License at
14
+
15
+ http://www.apache.org/licenses/LICENSE-2.0
16
+
17
+ Unless required by applicable law or agreed to in writing,
18
+ software distributed under the License is distributed on an
19
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
+ KIND, either express or implied. See the License for the
21
+ specific language governing permissions and limitations
22
+ under the License.
23
+
24
+ usage: match_snippets.py [-h] --base-url BASE_URL --token-file TOKEN_FILE [--no-verify] [--input INPUT]
25
+
26
+ options:
27
+ -h, --help show this help message and exit
28
+ --base-url BASE_URL Hub server URL e.g. https://your.blackduck.url
29
+ --token-file TOKEN_FILE
30
+ containing access token
31
+ --no-verify disable TLS certificate verification
32
+ --input INPUT File containing code snippet or stdin
33
+
34
+ Match a snippet of a code.
35
+ This functionality requires 'Generative AI Compliance' option licenses
36
+
37
+
38
+ Examples:
39
+
40
+ Curl file content from github and match it against Black Duck KB
41
+ and format the output using jq utility
42
+ curl https://raw.githubusercontent.com/apache/kafka/trunk/shell/src/main/java/org/apache/kafka/shell/state/MetadataShellState.java | \
43
+ python3 examples/client/match_snippet.py --base-url=$BD_URL --token-file=<(echo $API_TOKEN) --no-verify | \
44
+ jq .
45
+
46
+ This will produce something like:
47
+ {
48
+ "snippetMatches": {
49
+ "PERMISSIVE": [
50
+ {
51
+ "projectName": "Apache Kafka",
52
+ "releaseVersion": "3.5.0",
53
+ "licenseDefinition": {
54
+ "name": "Apache License 2.0",
55
+ "spdxId": "Apache-2.0",
56
+ "ownership": "OPEN_SOURCE",
57
+ "licenseDisplayName": "Apache License 2.0"
58
+ . . .
59
+
60
+ '''
61
+ import argparse
62
+ import json
63
+ import logging
64
+ import sys
65
+
66
+ from blackduck import Client
67
+
68
+ parser = argparse .ArgumentParser ('match_snippets.py' )
69
+ parser .add_argument ("--base-url" , required = True , help = "Hub server URL e.g. https://your.blackduck.url" )
70
+ parser .add_argument ("--token-file" , dest = 'token_file' , required = True , help = "containing access token" )
71
+ parser .add_argument ("--no-verify" , dest = 'verify' , action = 'store_false' , help = "disable TLS certificate verification" )
72
+ parser .add_argument ("--input" , required = False , help = "File containing code snippet or stdin" )
73
+ args = parser .parse_args ()
74
+
75
+
76
+ logging .basicConfig (format = '%(asctime)s:%(levelname)s:%(message)s' , stream = sys .stderr , level = logging .DEBUG )
77
+ logging .getLogger ("requests" ).setLevel (logging .WARNING )
78
+ logging .getLogger ("urllib3" ).setLevel (logging .WARNING )
79
+ logging .getLogger ("blackduck" ).setLevel (logging .WARNING )
80
+
81
+ with open (args .token_file , 'r' ) as tf :
82
+ access_token = tf .readline ().strip ()
83
+
84
+ bd = Client (
85
+ base_url = args .base_url ,
86
+ token = access_token ,
87
+ verify = args .verify
88
+ )
89
+
90
+ if args .input :
91
+ with open (args .input , 'r' ) as content_file :
92
+ content = content_file .read ()
93
+ else :
94
+ with sys .stdin as content_file :
95
+ content = content_file .read ()
96
+
97
+ endpoint = '/api/snippet-matching'
98
+ headers = {"Content-Type" : "text/plain" }
99
+
100
+ response = bd .session .post (url = endpoint , headers = headers , data = content )
101
+ if response .ok :
102
+ data = response .json ()
103
+ import json
104
+ print (json .dumps (data ))
0 commit comments