-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add last verified script #3154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add last verified script #3154
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d649ce5
Add last verified script
svekars c87914c
Add tutorial status info
svekars 68a6d4f
Merge branch 'main' into add-last-reviewed
svekars 55f16ae
Update date format
svekars 5540cd2
Merge branch 'main' into add-last-reviewed
svekars deb5732
Update Makefile to download the json file
svekars 6abb558
Update
svekars 570cba9
Mount the .git dir in the Docker container
svekars c6f8bff
Update
svekars ff33d1f
Set fetch-depth: 0
svekars bdc6f6f
Remove mounting .git dir in the container
svekars fda1f62
Merge branch 'main' into add-last-reviewed
svekars 960efa3
Address feedback
svekars dc429ef
Update
svekars 137f995
Update
svekars f21b5c0
Address feedback
svekars 849873b
Update
svekars 243b96a
Merge branch 'main' into add-last-reviewed
svekars e995428
Merge branch 'main' into add-last-reviewed
svekars d4f28c7
Merge branch 'main' into add-last-reviewed
svekars f8c9047
Apply suggestions from code review
malfet b980a2f
Update .jenkins/insert_last_verified.py
malfet b6279c4
Merge branch 'main' into add-last-reviewed
svekars dcee5e6
Apply suggestions from code review
malfet a7f8678
Merge branch 'main' into add-last-reviewed
svekars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
from datetime import datetime | ||
|
||
from bs4 import BeautifulSoup | ||
|
||
|
||
json_file_path = "tutorials-review-data.json" | ||
|
||
# paths to skip from the post-processing script | ||
paths_to_skip = [ | ||
"beginner/examples_autograd/two_layer_net_custom_function", # not present in the repo | ||
"beginner/examples_nn/two_layer_net_module", # not present in the repo | ||
"beginner/examples_tensor/two_layer_net_numpy", # not present in the repo | ||
"beginner/examples_tensor/two_layer_net_tensor", # not present in the repo | ||
"beginner/examples_autograd/two_layer_net_autograd", # not present in the repo | ||
"beginner/examples_nn/two_layer_net_optim", # not present in the repo | ||
"beginner/examples_nn/two_layer_net_nn", # not present in the repo | ||
"intermediate/coding_ddpg", # not present in the repo - will delete the carryover | ||
] | ||
# Mapping of source directories to build directories | ||
source_to_build_mapping = { | ||
"beginner": "beginner_source", | ||
"recipes": "recipes_source", | ||
"distributed": "distributed", | ||
"intermediate": "intermediate_source", | ||
"prototype": "prototype_source", | ||
"advanced": "advanced_source", | ||
"": "", # root dir for index.rst | ||
} | ||
|
||
def get_git_log_date(file_path, git_log_args): | ||
try: | ||
result = subprocess.run( | ||
["git", "log"] + git_log_args + ["--", file_path], | ||
capture_output=True, | ||
text=True, | ||
check=True, | ||
) | ||
if result.stdout: | ||
date_str = result.stdout.splitlines()[0] | ||
return datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z") | ||
except subprocess.CalledProcessError: | ||
pass | ||
raise ValueError(f"Could not find date for {file_path}") | ||
|
||
def get_creation_date(file_path): | ||
return get_git_log_date(file_path, ["--diff-filter=A", "--format=%aD"]).strftime("%b %d, %Y") | ||
malfet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_last_updated_date(file_path): | ||
return get_git_log_date(file_path, ["-1", "--format=%aD"]).strftime("%b %d, %Y") | ||
|
||
# Try to find the source file with the given base path and the extensions .rst and .py | ||
def find_source_file(base_path): | ||
for ext in [".rst", ".py"]: | ||
source_file_path = base_path + ext | ||
if os.path.exists(source_file_path): | ||
return source_file_path | ||
return None | ||
|
||
|
||
# Function to process a JSON file and insert the "Last Verified" information into the HTML files | ||
def process_json_file(build_dir , json_file_path): | ||
with open(json_file_path, "r", encoding="utf-8") as json_file: | ||
json_data = json.load(json_file) | ||
|
||
for entry in json_data: | ||
path = entry["Path"] | ||
last_verified = entry["Last Verified"] | ||
status = entry.get("Status", "") | ||
if path in paths_to_skip: | ||
print(f"Skipping path: {path}") | ||
continue | ||
if status in ["needs update", "not verified"]: | ||
formatted_last_verified = "Not Verified" | ||
elif last_verified: | ||
try: | ||
last_verified_date = datetime.strptime(last_verified, "%Y-%m-%d") | ||
formatted_last_verified = last_verified_date.strftime("%b %d, %Y") | ||
except ValueError: | ||
formatted_last_verified = "Unknown" | ||
else: | ||
formatted_last_verified = "Not Verified" | ||
if status == "deprecated": | ||
formatted_last_verified += "Deprecated" | ||
|
||
for build_subdir, source_subdir in source_to_build_mapping.items(): | ||
if path.startswith(build_subdir): | ||
html_file_path = os.path.join(build_dir, path + ".html") | ||
base_source_path = os.path.join( | ||
source_subdir, path[len(build_subdir) + 1 :] | ||
) | ||
source_file_path = find_source_file(base_source_path) | ||
break | ||
else: | ||
print(f"Warning: No mapping found for path {path}") | ||
continue | ||
|
||
if not os.path.exists(html_file_path): | ||
print( | ||
f"Warning: HTML file not found for path {html_file_path}." | ||
"If this is a new tutorial, please add it to the audit JSON file and set the Verified status and todays's date." | ||
) | ||
continue | ||
|
||
if not source_file_path: | ||
print(f"Warning: Source file not found for path {base_source_path}.") | ||
continue | ||
|
||
created_on = get_creation_date(source_file_path) | ||
last_updated = get_last_updated_date(source_file_path) | ||
|
||
with open(html_file_path, "r", encoding="utf-8") as file: | ||
soup = BeautifulSoup(file, "html.parser") | ||
# Check if the <p> tag with class "date-info-last-verified" already exists | ||
existing_date_info = soup.find("p", {"class": "date-info-last-verified"}) | ||
if existing_date_info: | ||
print( | ||
f"Warning: <p> tag with class 'date-info-last-verified' already exists in {html_file_path}" | ||
) | ||
continue | ||
|
||
h1_tag = soup.find("h1") # Find the h1 tag to insert the dates | ||
if h1_tag: | ||
date_info_tag = soup.new_tag("p", **{"class": "date-info-last-verified"}) | ||
date_info_tag["style"] = "color: #6c6c6d; font-size: small;" | ||
# Add the "Created On", "Last Updated", and "Last Verified" information | ||
date_info_tag.string = ( | ||
f"Created On: {created_on} | " | ||
f"Last Updated: {last_updated} | " | ||
f"Last Verified: {formatted_last_verified}" | ||
) | ||
# Insert the new tag after the <h1> tag | ||
h1_tag.insert_after(date_info_tag) | ||
# Save back to the HTML. | ||
with open(html_file_path, "w", encoding="utf-8") as file: | ||
file.write(str(soup)) | ||
else: | ||
print(f"Warning: <h1> tag not found in {html_file_path}") | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 2: | ||
print("Error: Build directory not provided. Exiting.") | ||
exit(1) | ||
malfet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
build_dir = sys.argv[1] | ||
print(f"Build directory: {build_dir}") | ||
process_json_file(build_dir , json_file_path) | ||
print( | ||
"Finished processing JSON file. Please check the output for any warnings. " | ||
"Pages like `nlp/index.html` are generated only during the full `make docs` " | ||
"or `make html` build. Warnings about these files when you run `make html-noplot` " | ||
"can be ignored." | ||
) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.