Skip to content

Tutorial timestamps #2876

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .jenkins/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ if [[ "${JOB_TYPE}" == "worker" ]]; then
# Files to run must be accessible to subprocessed (at least to `download_data.py`)
export FILES_TO_RUN

# Step 2.1: Add timestamps to .py and .rst files in source directories
bash $DIR/update_timestamps_batch.sh

# Step 3: Run `make docs` to generate HTML files and static files for these tutorialis
pip3 install -e git+https://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme
make docs
Expand Down Expand Up @@ -118,6 +121,10 @@ if [[ "${JOB_TYPE}" == "worker" ]]; then
7z a worker_${WORKER_ID}.7z docs
awsv2 s3 cp worker_${WORKER_ID}.7z s3://${BUCKET_NAME}/${COMMIT_ID}/worker_${WORKER_ID}.7z
elif [[ "${JOB_TYPE}" == "manager" ]]; then

# Step 0.9: Add timestamps to .py and .rst files in source directories
bash $DIR/update_timestamps_batch.sh

# Step 1: Generate no-plot HTML pages for all tutorials
pip3 install -e git+https://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme
make html-noplot
Expand Down
4 changes: 3 additions & 1 deletion .jenkins/get_sphinx_filenames.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pathlib import Path
from typing import List

from get_files_to_run import get_all_files
Expand All @@ -11,3 +10,6 @@ def get_files_for_sphinx() -> List[str]:


SPHINX_SHOULD_RUN = "|".join(get_files_for_sphinx())

if __name__ == "__main__":
print(SPHINX_SHOULD_RUN)
74 changes: 74 additions & 0 deletions .jenkins/update_timestamps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import re
import sys
import os
import datetime
import subprocess


def get_last_commit_timestamp_for_file(file_path: str) -> str:
"""Get the last commit timestamp for a file.

Args:
file_path (str): Path to file

Returns:
str: Last committed timestamp string
"""

git_command = [
"git",
"log",
"-1",
"--date=format:%B %d, %Y",
"--format=%at",
"--",
file_path,
]
timestamp = subprocess.check_output(git_command).decode().strip()

if not timestamp:
# If there is no git commit history, use last modified date
timestamp = str(int(os.path.getmtime(file_path)))

dt = datetime.datetime.fromtimestamp(int(timestamp), tz=datetime.timezone.utc)
return dt.strftime("%I:%M %p, %B %d, %Y")


def update_timestamp(file_path: str):
"""Adds a timestamp of the most recent time the file was edited.

Args:
file_path (str): Path to file
"""
with open(file_path, "r") as file:
lines = file.readlines()

# Get current timestamp
timestamp = get_last_commit_timestamp_for_file(file_path)
if not timestamp:
return

timestamp_line = (
f'{"# " if file_path.endswith("py") else ""}_Last Updated: {timestamp}_\n'
)
timestamp_pattern = r"_Last Updated:\s.+_"

if not lines:
lines = [timestamp_line]
else:
i = len(lines) - 1
while i > 0 and not lines[i].strip():
i -= 1

if re.search(timestamp_pattern, lines[i]):
lines[i] = timestamp_line
else:
lines.append("\n\n" + timestamp_line)

# Write updated lines back to file
with open(file_path, "w") as file:
file.writelines(lines)


file_path = sys.argv[1]
update_timestamp(file_path)
21 changes: 21 additions & 0 deletions .jenkins/update_timestamps_batch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

# Check if SPHINX_SHOULD_RUN is already set
if [ -z "$SPHINX_SHOULD_RUN" ]; then
# If not set, retrieve it using get_sphinx_filenames.py and export it
SPHINX_SHOULD_RUN=$(python "$DIR/get_sphinx_filenames.py")
export SPHINX_SHOULD_RUN
fi

# Convert the pipe-separated filenames into an array
IFS='|' read -r -a file_array <<< "$SPHINX_SHOULD_RUN"

# Loop through each file and update timestamps if it exists
for file in "${file_array[@]}"; do
file="$DIR/../$file"
if [ -f "$file" ]; then
python "$DIR/update_timestamps.py" "$file"
fi
done
Loading