diff --git a/.jenkins/build.sh b/.jenkins/build.sh index db0438eb91b..e45d965de36 100755 --- a/.jenkins/build.sh +++ b/.jenkins/build.sh @@ -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 @@ -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 diff --git a/.jenkins/get_sphinx_filenames.py b/.jenkins/get_sphinx_filenames.py index b84267b48a3..838958c943e 100644 --- a/.jenkins/get_sphinx_filenames.py +++ b/.jenkins/get_sphinx_filenames.py @@ -1,4 +1,3 @@ -from pathlib import Path from typing import List from get_files_to_run import get_all_files @@ -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) diff --git a/.jenkins/update_timestamps.py b/.jenkins/update_timestamps.py new file mode 100644 index 00000000000..6961f58ca3a --- /dev/null +++ b/.jenkins/update_timestamps.py @@ -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) diff --git a/.jenkins/update_timestamps_batch.sh b/.jenkins/update_timestamps_batch.sh new file mode 100755 index 00000000000..dd36fd2d56d --- /dev/null +++ b/.jenkins/update_timestamps_batch.sh @@ -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