1
- import datetime as dt
1
+ import time
2
2
from pathlib import Path
3
3
import subprocess
4
4
@@ -23,7 +23,7 @@ class PEPFooter(transforms.Transform):
23
23
24
24
def apply (self ) -> None :
25
25
pep_source_path = Path (self .document ["source" ])
26
- if not pep_source_path .match ("pep-* " ):
26
+ if not pep_source_path .match ("pep-????.??? " ):
27
27
return # not a PEP file, exit early
28
28
29
29
# Iterate through sections from the end of the document
@@ -62,42 +62,44 @@ def _add_source_link(pep_source_path: Path) -> nodes.paragraph:
62
62
def _add_commit_history_info (pep_source_path : Path ) -> nodes .paragraph :
63
63
"""Use local git history to find last modified date."""
64
64
try :
65
- since_epoch = LAST_MODIFIED_TIMES [pep_source_path .name ]
65
+ iso_time = _LAST_MODIFIED_TIMES [pep_source_path .stem ]
66
66
except KeyError :
67
67
return nodes .paragraph ()
68
68
69
- epoch_dt = dt .datetime .fromtimestamp (since_epoch , dt .timezone .utc )
70
- iso_time = epoch_dt .isoformat (sep = " " )
71
69
commit_link = f"https://github.com/python/peps/commits/main/{ pep_source_path .name } "
72
70
link_node = nodes .reference ("" , f"{ iso_time } GMT" , refuri = commit_link )
73
71
return nodes .paragraph ("" , "Last modified: " , link_node )
74
72
75
73
76
74
def _get_last_modified_timestamps ():
77
75
# get timestamps and changed files from all commits (without paging results)
78
- args = ["git" , "--no-pager" , "log" , "--format=#%at" , "--name-only" ]
79
- with subprocess .Popen (args , stdout = subprocess .PIPE ) as process :
80
- all_modified = process .stdout .read ().decode ("utf-8" )
81
- process .stdout .close ()
82
- if process .wait (): # non-zero return code
83
- return {}
76
+ args = ("git" , "--no-pager" , "log" , "--format=#%at" , "--name-only" )
77
+ ret = subprocess .run (args , stdout = subprocess .PIPE , text = True , encoding = "utf-8" )
78
+ if ret .returncode : # non-zero return code
79
+ return {}
80
+ all_modified = ret .stdout
84
81
85
82
# set up the dictionary with the *current* files
86
- last_modified = {path .name : 0 for path in Path ().glob ("pep-*" ) if path .suffix in {".txt" , ".rst" }}
83
+ peps_dir = Path (__file__ , ".." , ".." , ".." , ".." ).resolve ()
84
+ last_modified = {path .stem : "" for path in peps_dir .glob ("pep-????.???" ) if path .suffix in {".txt" , ".rst" }}
87
85
88
86
# iterate through newest to oldest, updating per file timestamps
89
87
change_sets = all_modified .removeprefix ("#" ).split ("#" )
90
88
for change_set in change_sets :
91
89
timestamp , files = change_set .split ("\n " , 1 )
92
90
for file in files .strip ().split ("\n " ):
93
- if file .startswith ("pep-" ) and file [- 3 :] in {"txt" , "rst" }:
94
- if last_modified .get (file ) == 0 :
95
- try :
96
- last_modified [file ] = float (timestamp )
97
- except ValueError :
98
- pass # if float conversion fails
91
+ if not file .startswith ("pep-" ) or not file .endswith ((".rst" , ".txt" )):
92
+ continue # not a PEP
93
+ file = file [:- 4 ]
94
+ if last_modified .get (file ) != "" :
95
+ continue # most recent modified date already found
96
+ try :
97
+ y , m , d , hh , mm , ss , * _ = time .gmtime (float (timestamp ))
98
+ except ValueError :
99
+ continue # if float conversion fails
100
+ last_modified [file ] = f"{ y :04} -{ m :02} -{ d :02} { hh :02} :{ mm :02} :{ ss :02} "
99
101
100
102
return last_modified
101
103
102
104
103
- LAST_MODIFIED_TIMES = _get_last_modified_timestamps ()
105
+ _LAST_MODIFIED_TIMES = _get_last_modified_timestamps ()
0 commit comments