Path: blob/main/tests/integration_tests/style/test_licenses.py
1958 views
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02"""Tests checking against the existence of licenses in each file."""34import datetime5import framework.utils as utils67AMAZON_COPYRIGHT_YEARS = range(2018, datetime.datetime.now().year + 1)8AMAZON_COPYRIGHT = (9"Copyright {} Amazon.com, Inc. or its affiliates. All Rights Reserved."10)11AMAZON_LICENSE = (12"SPDX-License-Identifier: Apache-2.0"13)14CHROMIUM_COPYRIGHT = (15"Copyright 2017 The Chromium OS Authors. All rights reserved."16)17CHROMIUM_LICENSE = (18"Use of this source code is governed by a BSD-style license that can be"19)20TUNTAP_COPYRIGHT = (21"Copyright TUNTAP, 2017 The Chromium OS Authors. All rights reserved."22)23TUNTAP_LICENSE = (24"Use of this source code is governed by a BSD-style license that can be"25)26ALIBABA_COPYRIGHT = (27"Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved."28)29ALIBABA_LICENSE = (30"SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause"31)3233EXCLUDE = ["build", ".kernel"]343536def _has_amazon_copyright(string):37for year in AMAZON_COPYRIGHT_YEARS:38if AMAZON_COPYRIGHT.format(year) in string:39return True40return False414243def _look_for_license(file, license_msg):44line = file.readline()45while line.startswith("//") or line.startswith("#"):46if license_msg in line:47return True48line = file.readline()49return False505152def _validate_license(filename):53"""Validate licenses in all .rs, .py. and .sh file.5455Python and Rust files should have the licenses on the first 2 lines56Shell files license is located on lines 3-4 to account for shebang57"""58with open(filename, 'r+', encoding='utf-8') as file:59if filename.endswith('.sh'):60# Move iterator to third line without reading file into memory61file.readline()62file.readline()63# The copyright message is always on the first line.64copyright_info = file.readline()6566has_amazon_copyright = (67_has_amazon_copyright(copyright_info) and68_look_for_license(file, AMAZON_LICENSE)69)7071has_chromium_copyright = (72CHROMIUM_COPYRIGHT in copyright_info and73_look_for_license(file, CHROMIUM_LICENSE)74)7576has_tuntap_copyright = (77TUNTAP_COPYRIGHT in copyright_info and78_look_for_license(file, CHROMIUM_LICENSE)79)8081has_alibaba_copyright = (82ALIBABA_COPYRIGHT in copyright_info and83_look_for_license(file, ALIBABA_LICENSE)84)85return (86has_amazon_copyright or87has_chromium_copyright or88has_tuntap_copyright or89has_alibaba_copyright90)91return True929394def test_for_valid_licenses():95"""Fail if a file lacks a valid license."""96python_files = utils.get_files_from(97find_path="..",98pattern="*.py",99exclude_names=EXCLUDE)100rust_files = utils.get_files_from(101find_path="..",102pattern="*.rs",103exclude_names=EXCLUDE)104bash_files = utils.get_files_from(105find_path="..",106pattern="*.sh",107exclude_names=EXCLUDE)108109all_files = rust_files + python_files + bash_files110error_msg = []111for file in all_files:112if _validate_license(file) is False:113error_msg.append("{}".format(str(file)))114assert not error_msg, "Files {} have invalid licenses".format((error_msg))115116117