Script which adds yang-revision to yang-file-name 29/10829/2
authorMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Sat, 1 Apr 2023 15:42:23 +0000 (17:42 +0200)
committerMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Sat, 1 Apr 2023 15:45:14 +0000 (15:45 +0000)
- add file
- move tmux-logging.py to helper dir

Issue-ID: OAM-323
Change-Id: I299b7d04c5b9cd1367f71dd78ea7cfe9901507ca
Signed-off-by: Martin Skorupski <martin.skorupski@highstreet-technologies.com>
code/helpers/tmux-logging.py [moved from code/tmux-logging.py with 100% similarity]
code/helpers/yang_file_name_with_revision.py [new file with mode: 0644]

diff --git a/code/helpers/yang_file_name_with_revision.py b/code/helpers/yang_file_name_with_revision.py
new file mode 100644 (file)
index 0000000..6ef6bba
--- /dev/null
@@ -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)