]>
git.proxmox.com Git - mirror_edk2.git/blob - .pytool/Plugin/LicenseCheck/LicenseCheck.py
5733f7bf4ec03bf4fa83e7e61ddea3da3c15ac40
1 # @file LicenseCheck.py
3 # Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
4 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 from io
import StringIO
11 from typing
import List
, Tuple
12 from edk2toolext
.environment
.plugintypes
.ci_build_plugin
import ICiBuildPlugin
13 from edk2toolext
.environment
.var_dict
import VarDict
14 from edk2toollib
.utility_functions
import RunCmd
17 class LicenseCheck(ICiBuildPlugin
):
20 A CiBuildPlugin to check the license for new added files.
22 Configuration options:
28 license_format_preflix
= 'SPDX-License-Identifier'
30 bsd2_patent
= 'BSD-2-Clause-Patent'
32 Readdedfileformat
= re
.compile(r
'\+\+\+ b\/(.*)')
34 file_extension_list
= [".c", ".h", ".inf", ".dsc", ".dec", ".py", ".bat", ".sh", ".uni", ".yaml",
35 ".fdf", ".inc", "yml", ".asm", ".asm16", ".asl", ".vfr", ".s", ".S", ".aslc",
36 ".nasm", ".nasmb", ".idf", ".Vfr", ".H"]
38 def GetTestName(self
, packagename
: str, environment
: VarDict
) -> tuple:
39 """ Provide the testcase name and classname for use in reporting
40 testclassname: a descriptive string for the testcase can include whitespace
41 classname: should be patterned <packagename>.<plugin>.<optionally any unique condition>
44 packagename: string containing name of package to build
45 environment: The VarDict for the test to run in
47 a tuple containing the testcase name and the classname
48 (testcasename, classname)
50 return ("Check for license for " + packagename
, packagename
+ ".LicenseCheck")
53 # External function of plugin. This function is used to perform the task of the ci_build_plugin Plugin
55 # - package is the edk2 path to package. This means workspace/packagepath relative.
56 # - edk2path object configured with workspace and packages path
57 # - PkgConfig Object (dict) for the pkg
59 # - Plugin Manager Instance
60 # - Plugin Helper Obj Instance
62 # - output_stream the StringIO output stream from this plugin via logging
63 def RunBuildPlugin(self
, packagename
, Edk2pathObj
, pkgconfig
, environment
, PLM
, PLMHelper
, tc
, output_stream
=None):
64 return_buffer
= StringIO()
65 params
= "diff --unified=0 origin/master HEAD"
66 RunCmd("git", params
, outstream
=return_buffer
)
67 p
= return_buffer
.getvalue().strip()
72 if "IgnoreFiles" in pkgconfig
:
73 ignore_files
= pkgconfig
["IgnoreFiles"]
76 self
.startcheck
= False
78 self
.all_file_pass
= True
82 if line
.startswith('--- /dev/null'):
83 nextline
= patch
[line_index
+ 1]
84 added_file
= self
.Readdedfileformat
.search(nextline
).group(1)
85 added_file_extension
= os
.path
.splitext(added_file
)[1]
86 if added_file_extension
in self
.file_extension_list
and packagename
in added_file
:
87 if (self
.IsIgnoreFile(added_file
, ignore_files
)):
88 line_index
= line_index
+ 1
90 self
.startcheck
= True
92 if self
.startcheck
and self
.license_format_preflix
in line
:
93 if self
.bsd2_patent
in line
:
95 if line_index
+ 1 == count
or patch
[line_index
+ 1].startswith('diff --') and self
.startcheck
:
97 self
.all_file_pass
= False
98 error_message
= "Invalid license in: " + added_file
+ " Hint: Only BSD-2-Clause-Patent is accepted."
99 logging
.error(error_message
)
100 self
.startcheck
= False
102 line_index
= line_index
+ 1
104 if self
.all_file_pass
:
108 tc
.SetFailed("License Check {0} Failed. ".format(packagename
), "LICENSE_CHECK_FAILED")
111 def IsIgnoreFile(self
, file: str, ignore_files
: List
[str]) -> bool:
112 for f
in ignore_files
: