|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +def check_license_count(json_file, threshold): |
| 6 | + try: |
| 7 | + with open(json_file, 'r', encoding='utf-8') as f: |
| 8 | + data = json.load(f) |
| 9 | + |
| 10 | + total_count = 0 |
| 11 | + licenses = {} |
| 12 | + # Extract license information from the 'license_detections' |
| 13 | + for detection in data.get('license_detections', []): |
| 14 | + license_expression = detection['license_expression'] |
| 15 | + detection_count = detection['detection_count'] |
| 16 | + licenses[license_expression] = detection_count |
| 17 | + total_count += detection_count |
| 18 | + |
| 19 | + print(f"Detected licenses: {licenses}") |
| 20 | + print(f"Total number of detected licenses: {total_count}") |
| 21 | + |
| 22 | + if total_count >= threshold: |
| 23 | + print(f"License detection meets the threshold ({threshold}). Test passed!") |
| 24 | + sys.exit(0) # Exit with success |
| 25 | + else: |
| 26 | + print(f"License detection is below the threshold ({threshold}). Test failed.") |
| 27 | + sys.exit(1) # Exit with failure |
| 28 | + |
| 29 | + except Exception as e: |
| 30 | + print(f"Error occurred: {str(e)}") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + if len(sys.argv) < 2: |
| 35 | + print("Usage: python check_license_count.py <path_to_json_file> [threshold]") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + json_file = sys.argv[1] |
| 39 | + |
| 40 | + # Use an environment variable for threshold if available, or default to 3 |
| 41 | + threshold = int(os.getenv('LICENSE_THRESHOLD', 3)) |
| 42 | + |
| 43 | + # If a threshold is provided as an argument, override the default |
| 44 | + if len(sys.argv) > 2: |
| 45 | + try: |
| 46 | + threshold = int(sys.argv[2]) |
| 47 | + except ValueError: |
| 48 | + print("Threshold must be an integer.") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + check_license_count(json_file, threshold) |
0 commit comments