Files
public-tools/fix_dir_mtime.py
T

35 lines
703 B
Python
Executable File

#!/usr/bin/env python3
import os
import sys
from pathlib import Path
def update_dir_mtime(path: Path):
if not path.is_dir():
return path.stat().st_mtime
newest = 0
for child in path.iterdir():
if child.is_dir():
child_mtime = update_dir_mtime(child)
else:
child_mtime = child.stat().st_mtime
newest = max(newest, child_mtime)
if newest > 0:
os.utime(path, (newest, newest))
return max(newest, path.stat().st_mtime)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: fix_dir_mtime.py <directory>")
sys.exit(1)
root = Path(sys.argv[1]).resolve()
update_dir_mtime(root)