Create an ENUM for Countries
[oam.git] / code / helpers / yang_file_name_with_revision.py
1 #!/usr/bin/env python
2 ################################################################################
3 # Copyright 2023 highstreet technologies GmbH
4 #
5 # Licensed under the Apache License, Version 2.0 (the 'License');
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an 'AS IS' BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import os
19 import re
20 import time
21
22 # Specify the directory containing the files
23 directory_path = "."
24
25 # Define the regular expression to search for revision dates
26 revision_date_regex = r"revision\s*\"{0,1}(\d{4}-\d{2}-\d{2})"
27
28 # Loop over each file in the directory
29 for file_name in os.listdir(directory_path):
30     # Check if the file is a .yang file
31     if file_name.endswith(".yang"):
32         # Get the full file path
33         file_path = os.path.join(directory_path, file_name)
34
35         # Open the file and read its contents
36         with open(file_path, "r") as f:
37             yang_contents = f.read()
38
39         # Find all revision dates within the yang contents
40         matches = re.findall(revision_date_regex, yang_contents)
41         print(file_name, matches)
42
43         # Get the latest revision date
44         latest_revision_date = max(matches) if matches else None
45
46         # If a revision date was found, create the new file name with the revision date
47         if latest_revision_date:
48             # Format the latest revision date as "YYYY-MM-DD"
49             revision_date = time.strptime(latest_revision_date, "%Y-%m-%d")
50             revision_date_str = time.strftime("%Y-%m-%d", revision_date)
51
52             # Create the new file name with the revision date
53             file_name_with_date = f"{os.path.splitext(file_path)[0]}@{revision_date_str}{os.path.splitext(file_path)[1]}"
54
55             # Rename the file with the revision date
56             os.rename(file_path, file_name_with_date)
57
58             # Create a symbolic link to the previous file name
59             previous_file_link = f"{os.path.splitext(file_path)[0]}{os.path.splitext(file_path)[1]}"
60             os.symlink(file_name_with_date, previous_file_link)