Commit 9966f8a7 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common scripts:

- Removed preliminary version of script to update code file headers
parent 6070e0a1
Loading
Loading
Loading
Loading

update_license_headers.py

deleted100644 → 0
+0 −100
Original line number Diff line number Diff line
#!/bin/bash
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, os, re, sys
from io import TextIOWrapper
from pathlib import Path
from typing import Set

logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)

ROOT_PATH = '.'
FILE_PATH_SKIPPED    = 'out-no-header.txt'
FILE_PATH_NO_HEADER  = 'out-excluded.txt'
FILE_PATH_UPDATED    = 'out-updated.txt'
FILE_PATH_EXCLUSIONS = 'out-exclusions.txt'

RE_OLD_COPYRIGHT_1  = re.compile(r'^\#\ Copyright\ 2021\-2023\ H2020\ TeraFlow\ \(https\:\/\/www\.teraflow\-h2020\.eu\/\)$')
STR_NEW_COPYRIGHT_1 = '# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)'

RE_OLD_COPYRIGHT_2  = re.compile(r'^\/\/\ Copyright\ 2021\-2023\ H2020\ TeraFlow\ \(https\:\/\/www\.teraflow\-h2020\.eu\/\)$')
STR_NEW_COPYRIGHT_2 = '// Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)'

def skip_file(file_path : str) -> bool:
    if file_path.endswith('.pyc'): return True
    if file_path.endswith('.png'): return True
    if file_path.endswith('.jar'): return True
    if file_path.endswith('/tstat'): return True
    if file_path.endswith('/coverage/.coverage'): return True
    if '/__pycache__/' in file_path: return True
    if '/.mvn/' in file_path: return True
    if '/automation/target/generated-sources/grpc/' in file_path: return True
    if '/automation/target/kubernetes/' in file_path: return True
    if '/policy/target/generated-sources/grpc/' in file_path: return True
    if '/policy/target/kubernetes/' in file_path: return True
    return False

def process_line(line_in : str) -> str:
    line_out = RE_OLD_COPYRIGHT_1.sub(STR_NEW_COPYRIGHT_1, line_in)
    if line_out != line_in: return line_out

    line_out = RE_OLD_COPYRIGHT_2.sub(STR_NEW_COPYRIGHT_2, line_in)
    if line_out != line_in: return line_out

    return line_in

def process_file(
    file_path : str, file_no_header : TextIOWrapper, file_skipped : TextIOWrapper, file_updated : TextIOWrapper
) -> None:
    if skip_file(file_path):
        file_skipped.write(file_path + '\n')
        return

    LOGGER.info('  File {:s}...'.format(str(file_path)))

    #_,extension = os.path.splitext(file_path)
    #temp_file_path = file_path + '.temp'
    replaced = False
    try:
        with open(file_path, encoding='UTF-8') as source:
            #with open(temp_file_path, 'w', encoding='UTF-8') as target:
            for line_in in source:
                line_out = process_line(line_in)
                #target.write(line_out)
                replaced = replaced or (line_out != line_in)
    except: # pylint: disable=bare-except
        replaced = False

    if not replaced:
        file_no_header.write(file_path + '\n')
    else:
        file_updated.write(file_path + '\n')

    #os.rename(temp_file_path, file_path)

def main() -> int:
    with open(FILE_PATH_NO_HEADER, 'w', encoding='UTF-8') as file_no_header:
        with open(FILE_PATH_SKIPPED, 'w', encoding='UTF-8') as file_skipped:
            with open(FILE_PATH_UPDATED, 'w', encoding='UTF-8') as file_updated:
                for dirpath, _, filenames in os.walk(ROOT_PATH):
                    LOGGER.info('Folder {:s}...'.format(str(dirpath)))
                    for filename in filenames:
                        file_path = os.path.join(dirpath, filename)
                        process_file(file_path, file_no_header, file_skipped, file_updated)
    return 0

if __name__ == '__main__':
    sys.exit(main())