From 167843f155e35fafa266a76f4a0eb5632d724fe1 Mon Sep 17 00:00:00 2001 From: Martin Skorupski Date: Sat, 1 Apr 2023 17:42:23 +0200 Subject: [PATCH] Script which adds yang-revision to yang-file-name - add file - move tmux-logging.py to helper dir Issue-ID: OAM-323 Change-Id: I299b7d04c5b9cd1367f71dd78ea7cfe9901507ca Signed-off-by: Martin Skorupski --- code/{ => helpers}/tmux-logging.py | 0 code/helpers/yang_file_name_with_revision.py | 60 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) rename code/{ => helpers}/tmux-logging.py (100%) create mode 100644 code/helpers/yang_file_name_with_revision.py diff --git a/code/tmux-logging.py b/code/helpers/tmux-logging.py similarity index 100% rename from code/tmux-logging.py rename to code/helpers/tmux-logging.py diff --git a/code/helpers/yang_file_name_with_revision.py b/code/helpers/yang_file_name_with_revision.py new file mode 100644 index 0000000..6ef6bba --- /dev/null +++ b/code/helpers/yang_file_name_with_revision.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +################################################################################ +# Copyright 2023 highstreet technologies GmbH +# +# 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 os +import re +import time + +# Specify the directory containing the files +directory_path = "." + +# Define the regular expression to search for revision dates +revision_date_regex = r"revision\s*\"{0,1}(\d{4}-\d{2}-\d{2})" + +# Loop over each file in the directory +for file_name in os.listdir(directory_path): + # Check if the file is a .yang file + if file_name.endswith(".yang"): + # Get the full file path + file_path = os.path.join(directory_path, file_name) + + # Open the file and read its contents + with open(file_path, "r") as f: + yang_contents = f.read() + + # Find all revision dates within the yang contents + matches = re.findall(revision_date_regex, yang_contents) + print(file_name, matches) + + # Get the latest revision date + latest_revision_date = max(matches) if matches else None + + # If a revision date was found, create the new file name with the revision date + if latest_revision_date: + # Format the latest revision date as "YYYY-MM-DD" + revision_date = time.strptime(latest_revision_date, "%Y-%m-%d") + revision_date_str = time.strftime("%Y-%m-%d", revision_date) + + # Create the new file name with the revision date + file_name_with_date = f"{os.path.splitext(file_path)[0]}@{revision_date_str}{os.path.splitext(file_path)[1]}" + + # Rename the file with the revision date + os.rename(file_path, file_name_with_date) + + # Create a symbolic link to the previous file name + previous_file_link = f"{os.path.splitext(file_path)[0]}{os.path.splitext(file_path)[1]}" + os.symlink(file_name_with_date, previous_file_link) -- 2.16.6