-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_licenses.py
101 lines (76 loc) · 2.7 KB
/
show_licenses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -------------------------------------------------------------------------------
# (c) 2019-2022 Siemens AG
# All Rights Reserved.
# Author: [email protected]
#
# Licensed under the MIT license.
# SPDX-License-Identifier: MIT
# -------------------------------------------------------------------------------
"""
Show all licenses and copyrights of a component
call syntax
show_licenses filename
"""
import sys
from typing import Any, List
from colorama import Fore, init
import cli_support
# initialize colorama
init()
class ShowLicenses():
"""Application class"""
def __init__(self) -> None:
self.cli_filename: str = ""
self.global_license_list: List[str] = []
@classmethod
def print_license_list(cls, license_list: List[str]) -> None:
"""Displays the licenses color-coded"""
for lic in license_list:
color = Fore.RESET
check = lic.upper()
if "GPL" in check:
color = Fore.LIGHTYELLOW_EX
if "EPL" in check:
color = Fore.LIGHTYELLOW_EX
if "MPL" in check:
color = Fore.LIGHTYELLOW_EX
if "CDDL" in check:
color = Fore.LIGHTYELLOW_EX
if "CPL" in check:
color = Fore.LIGHTYELLOW_EX
print(" " + color + lic)
print(Fore.RESET)
def process_cli_file(self, cli_filename: str) -> None:
"""Processes a single CLI file"""
clifile = cli_support.CLI.CliFile()
try:
clifile.read_from_file(cli_filename)
except OSError as ex:
print(Fore.LIGHTRED_EX)
print(" Error reading CLI file: " + cli_filename)
print(" Error '{0}' occurred. Arguments {1}.".format(ex.errno, ex.args))
print(Fore.RESET)
return
license_list: List[str] = []
for lic in clifile.licenses:
license_list.append(lic.name)
if lic.name not in self.global_license_list:
self.global_license_list.append(lic.name)
def process_commandline(self, argv: Any) -> Any:
"""Reads the command line arguments"""
if len(argv) < 2:
sys.exit(
Fore.LIGHTRED_EX +
" No CLI file specified!" +
Fore.RESET)
self.cli_filename = argv[1]
def main(self) -> None:
"""Main method()"""
print("\nShow all licenses and copyrights of a component:\n")
self.process_commandline(sys.argv)
self.global_license_list = []
self.process_cli_file(self.cli_filename)
self.print_license_list(self.global_license_list)
if __name__ == '__main__':
APP = ShowLicenses()
APP.main()