Skip to content

Commit d649ce5

Browse files
committed
Add last verified script
1 parent 0088d45 commit d649ce5

File tree

3 files changed

+2416
-1
lines changed

3 files changed

+2416
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ html-noplot:
9898
# bash .jenkins/remove_invisible_code_block_batch.sh "$(BUILDDIR)/html"
9999
@echo
100100
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
101-
101+
@echo "Running post-processing script to insert 'Last Verified' dates..."
102+
@python insert_last_verified.py
102103
clean-cache:
103104
make clean
104105
rm -rf advanced beginner intermediate recipes

insert_last_verified.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import json
2+
import os
3+
import subprocess
4+
from bs4 import BeautifulSoup
5+
from datetime import datetime
6+
7+
# Path to the single JSON file
8+
json_file_path = "output.json"
9+
10+
# Base directory for the generated HTML files
11+
build_dir = "_build/html"
12+
13+
# Define the source to build path mapping
14+
source_to_build_mapping = {
15+
"beginner": "beginner_source",
16+
"recipes": "recipes_source",
17+
"distributed": "distributed",
18+
"intermediate": "intermediate_source",
19+
"prototype": "prototype_source",
20+
"advanced": "advanced_source"
21+
}
22+
23+
# Function to get the creation date of a file using git log
24+
def get_creation_date(file_path):
25+
try:
26+
# Run git log to get the date of the first commit for the file
27+
result = subprocess.run(
28+
["git", "log", "--diff-filter=A", "--format=%aD", "--", file_path],
29+
capture_output=True,
30+
text=True,
31+
check=True
32+
)
33+
# Check if the output is not empty
34+
if result.stdout:
35+
creation_date = result.stdout.splitlines()[0]
36+
# Parse and format the date
37+
creation_date = datetime.strptime(creation_date, "%a, %d %b %Y %H:%M:%S %z")
38+
formatted_date = creation_date.strftime("%d %b, %Y")
39+
else:
40+
formatted_date = "Unknown"
41+
return formatted_date
42+
except subprocess.CalledProcessError:
43+
return "Unknown"
44+
45+
# Function to find the source file with any common extension
46+
def find_source_file(base_path):
47+
for ext in ['.rst', '.py']:
48+
source_file_path = base_path + ext
49+
if os.path.exists(source_file_path):
50+
return source_file_path
51+
return None
52+
53+
# Function to process the JSON file
54+
def process_json_file(json_file_path):
55+
with open(json_file_path, "r", encoding="utf-8") as json_file:
56+
json_data = json.load(json_file)
57+
58+
# Process each entry in the JSON data
59+
for entry in json_data:
60+
path = entry["Path"]
61+
last_verified = entry["Last Verified"]
62+
63+
# Format the "Last Verified" date
64+
try:
65+
last_verified_date = datetime.strptime(last_verified, "%Y-%m-%d")
66+
formatted_last_verified = last_verified_date.strftime("%d %b, %Y")
67+
except ValueError:
68+
formatted_last_verified = "Unknown"
69+
70+
# Determine the source directory and file name
71+
for build_subdir, source_subdir in source_to_build_mapping.items():
72+
if path.startswith(build_subdir):
73+
# Construct the path to the HTML file
74+
html_file_path = os.path.join(build_dir, path + ".html")
75+
# Construct the base path to the source file
76+
base_source_path = os.path.join(source_subdir, path[len(build_subdir)+1:])
77+
# Find the actual source file
78+
source_file_path = find_source_file(base_source_path)
79+
break
80+
else:
81+
print(f"Warning: No mapping found for path {path}")
82+
continue
83+
84+
# Check if the HTML file exists
85+
if not os.path.exists(html_file_path):
86+
print(f"Warning: HTML file not found for path {html_file_path}")
87+
continue
88+
89+
# Check if the source file was found
90+
if not source_file_path:
91+
print(f"Warning: Source file not found for path {base_source_path}")
92+
continue
93+
94+
# Get the creation date of the source file
95+
created_on = get_creation_date(source_file_path)
96+
97+
# Open and parse the HTML file
98+
with open(html_file_path, "r", encoding="utf-8") as file:
99+
soup = BeautifulSoup(file, "html.parser")
100+
101+
# Find the first <h1> tag and insert the "Last Verified" and "Created On" dates after it
102+
h1_tag = soup.find("h1")
103+
if h1_tag:
104+
# Create a new tag for the dates
105+
date_info_tag = soup.new_tag("p")
106+
date_info_tag['style'] = "color: #6c6c6d; font-size: small;"
107+
108+
# Add the "Created On" and "Last Verified" information
109+
date_info_tag.string = f"Created On: {created_on} | Last Verified: {formatted_last_verified}"
110+
111+
# Insert the new tag after the <h1> tag
112+
h1_tag.insert_after(date_info_tag)
113+
114+
# Save the modified HTML back to the file
115+
with open(html_file_path, "w", encoding="utf-8") as file:
116+
file.write(str(soup))
117+
else:
118+
print(f"Warning: <h1> tag not found in {html_file_path}")
119+
120+
# Process the single JSON file
121+
process_json_file(json_file_path)
122+
123+
print("Processing complete.")

0 commit comments

Comments
 (0)