Skip to content

Commit dda8f35

Browse files
authored
Merge pull request #6 from canonical/add-lastmod-dates
feat: Add last modified timestamp to nodes
2 parents b2c8e68 + efbe879 commit dda8f35

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

canonicalwebteam/sitemaps_parser/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import subprocess
23
from contextlib import suppress
34
from pathlib import Path
45

@@ -245,9 +246,26 @@ def create_node():
245246
"description": None,
246247
"link": None,
247248
"children": [],
249+
"last_modified": None,
248250
}
249251

250252

253+
def get_git_last_modified_time(path):
254+
"""
255+
Get the last modified time of a file using Git metadata.
256+
"""
257+
try:
258+
result = subprocess.run(
259+
["git", "log", "-1", "--format=%cI", str(path)],
260+
capture_output=True,
261+
text=True,
262+
check=True,
263+
)
264+
return result.stdout.strip()
265+
except subprocess.CalledProcessError:
266+
return None
267+
268+
251269
def scan_directory(path_name, base=None):
252270
"""
253271
We scan a given directory for valid pages and return a tree
@@ -277,6 +295,10 @@ def scan_directory(path_name, base=None):
277295
# Get tags, add as child
278296
tags = get_tags_rolling_buffer(index_path)
279297
node = update_tags(node, tags)
298+
# Add last modified time for index.html
299+
lastmod_time = get_git_last_modified_time(index_path)
300+
if lastmod_time:
301+
node["last_modified"] = lastmod_time
280302

281303
# Cycle through other files in this directory
282304
for child in node_path.iterdir():
@@ -292,6 +314,11 @@ def scan_directory(path_name, base=None):
292314
child_tags["link"] = get_extended_copydoc(
293315
extended_path, base=base
294316
)
317+
# Add last modified time for child paths
318+
child_lastmod_time = get_git_last_modified_time(child)
319+
if child_lastmod_time:
320+
child_tags["last_modified"] = child_lastmod_time
321+
295322
node["children"].append(child_tags)
296323
# If the child is a directory, scan it
297324
if child.is_dir():

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name="canonicalwebteam.sitemaps-parser",
7-
version="1.0.2",
7+
version="1.0.3",
88
author="Canonical webteam",
99
author_email="webteam@canonical.com",
1010
url="https://github.com/canonical/canonicalwebteam.sitemaps-parser",

0 commit comments

Comments
 (0)