Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/style/test_licenses.py
1958 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Tests checking against the existence of licenses in each file."""
4
5
import datetime
6
import framework.utils as utils
7
8
AMAZON_COPYRIGHT_YEARS = range(2018, datetime.datetime.now().year + 1)
9
AMAZON_COPYRIGHT = (
10
"Copyright {} Amazon.com, Inc. or its affiliates. All Rights Reserved."
11
)
12
AMAZON_LICENSE = (
13
"SPDX-License-Identifier: Apache-2.0"
14
)
15
CHROMIUM_COPYRIGHT = (
16
"Copyright 2017 The Chromium OS Authors. All rights reserved."
17
)
18
CHROMIUM_LICENSE = (
19
"Use of this source code is governed by a BSD-style license that can be"
20
)
21
TUNTAP_COPYRIGHT = (
22
"Copyright TUNTAP, 2017 The Chromium OS Authors. All rights reserved."
23
)
24
TUNTAP_LICENSE = (
25
"Use of this source code is governed by a BSD-style license that can be"
26
)
27
ALIBABA_COPYRIGHT = (
28
"Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved."
29
)
30
ALIBABA_LICENSE = (
31
"SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause"
32
)
33
34
EXCLUDE = ["build", ".kernel"]
35
36
37
def _has_amazon_copyright(string):
38
for year in AMAZON_COPYRIGHT_YEARS:
39
if AMAZON_COPYRIGHT.format(year) in string:
40
return True
41
return False
42
43
44
def _look_for_license(file, license_msg):
45
line = file.readline()
46
while line.startswith("//") or line.startswith("#"):
47
if license_msg in line:
48
return True
49
line = file.readline()
50
return False
51
52
53
def _validate_license(filename):
54
"""Validate licenses in all .rs, .py. and .sh file.
55
56
Python and Rust files should have the licenses on the first 2 lines
57
Shell files license is located on lines 3-4 to account for shebang
58
"""
59
with open(filename, 'r+', encoding='utf-8') as file:
60
if filename.endswith('.sh'):
61
# Move iterator to third line without reading file into memory
62
file.readline()
63
file.readline()
64
# The copyright message is always on the first line.
65
copyright_info = file.readline()
66
67
has_amazon_copyright = (
68
_has_amazon_copyright(copyright_info) and
69
_look_for_license(file, AMAZON_LICENSE)
70
)
71
72
has_chromium_copyright = (
73
CHROMIUM_COPYRIGHT in copyright_info and
74
_look_for_license(file, CHROMIUM_LICENSE)
75
)
76
77
has_tuntap_copyright = (
78
TUNTAP_COPYRIGHT in copyright_info and
79
_look_for_license(file, CHROMIUM_LICENSE)
80
)
81
82
has_alibaba_copyright = (
83
ALIBABA_COPYRIGHT in copyright_info and
84
_look_for_license(file, ALIBABA_LICENSE)
85
)
86
return (
87
has_amazon_copyright or
88
has_chromium_copyright or
89
has_tuntap_copyright or
90
has_alibaba_copyright
91
)
92
return True
93
94
95
def test_for_valid_licenses():
96
"""Fail if a file lacks a valid license."""
97
python_files = utils.get_files_from(
98
find_path="..",
99
pattern="*.py",
100
exclude_names=EXCLUDE)
101
rust_files = utils.get_files_from(
102
find_path="..",
103
pattern="*.rs",
104
exclude_names=EXCLUDE)
105
bash_files = utils.get_files_from(
106
find_path="..",
107
pattern="*.sh",
108
exclude_names=EXCLUDE)
109
110
all_files = rust_files + python_files + bash_files
111
error_msg = []
112
for file in all_files:
113
if _validate_license(file) is False:
114
error_msg.append("{}".format(str(file)))
115
assert not error_msg, "Files {} have invalid licenses".format((error_msg))
116
117