-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Tutorial timestamps #2876
Changes from 2 commits
fdd6f92
54e17f1
dddf2c2
743b232
05e3818
e9da6fc
eb8f1d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import re | ||
import sys | ||
import os | ||
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", "--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))) | ||
|
||
date_command = ["date", "-d", "@" + timestamp, "+%I:%M %p, %B %d, %Y"] | ||
return subprocess.check_output(date_command).decode().strip() | ||
|
||
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) | ||
timestamp_line = f'{"# " if file_path.endswith("py") else ""}**Updated:** *{timestamp}*\n' | ||
timestamp_pattern = r'\*\*Updated:\*\*\s\**\d{1,2}:\d{2} [AP]M, \w+ \d{1,2}, \d{4}\*' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather try to avoid regexes like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated it! For my own knowledge, may I ask why? Is it because it's too specific? I was trying to be specific just so I didn't grab any lines unexpectedly from the tutorial files. |
||
|
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
SOURCEDIR=$1 | ||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" | ||
|
||
directories=("$SOURCEDIR/beginner_source" "$SOURCEDIR/intermediate_source" "$SOURCEDIR/advanced_source") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have other directories as well and what if we add a new gallery directory, this won't work anymore? You could try perhaps to get them from the conf.py sphinx-gallery settings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated it to get the files from the python script get_sphinx_filenames. This is what is used in conf.py, so can I assume that is a complete set? |
||
|
||
for dir in "${directories[@]}"; do | ||
# Process .py and .rst files in the current directory | ||
for file in "$dir"/*.{py,rst}; do | ||
if [ -f "$file" ]; then | ||
python "$DIR/update_timestamps.py" "$file" | ||
fi | ||
done | ||
done |
Uh oh!
There was an error while loading. Please reload this page.