-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmigrate-to-posts.py
More file actions
86 lines (76 loc) · 3.1 KB
/
migrate-to-posts.py
File metadata and controls
86 lines (76 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import subprocess
import datetime
import shutil
from pathlib import Path
def get_last_git_change_date(file_path):
"""
Retrieves the last commit date of a file using Git.
Args:
file_path (str): The path to the file.
Returns:
str: The last commit date in ISO format (YYYY-MM-DD HH:MM:SS), or None if an error occurs or the file is not tracked by Git.
"""
try:
result = subprocess.run(['git', 'log', '-n', '1', '--pretty=format:%aI', '--', file_path], capture_output=True, text=True, check=True)
if result.stdout.strip():
# Convert to datetime object and then format it
datetime_obj = datetime.datetime.fromisoformat(result.stdout.strip().replace('Z', '+00:00'))
return datetime_obj.strftime('%Y-%m-%d')
else:
return None
except subprocess.CalledProcessError:
return None
except FileNotFoundError:
return None
def copy_file(src_file, target_dir, prefix):
new_file = prefix + "-" + os.path.basename(src_file)
target_file = os.path.join(target_dir, new_file)
print(f"new file: {target_file}")
shutil.copy2(src_file, target_file)
def change_src_file_contents(src_file, prefix):
new_file = prefix + "-" + os.path.basename(src_file)
redirect_to = "/blog/" + Path(new_file).stem
with open(src_file, 'w') as file:
new_contents = f'''---
layout: redirect
redirect_to: '{redirect_to}'
---
'''
print(new_contents)
file.write(new_contents)
def file_processed(file_path):
"""Check if the file contains the search string."""
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if 'layout: redirect' in line:
return True
except Exception as e:
print(f"Error reading {file_path}: {e}")
return False
def transform_files(src_dir, target_dir, extensions):
"""
Lists files in a directory with specific extensions and their last Git commit dates.
Args:
directory (str): The directory to search.
extensions (tuple): A tuple of file extensions to filter by (e.g., ('.txt', '.py')).
"""
for root, _, files in os.walk(src_dir):
for file in files:
if file.lower().endswith(extensions):
file_path = os.path.join(root, file)
last_changed = get_last_git_change_date(file_path)
print(f"File: {file_path}, Last Changed: {last_changed if last_changed else 'Not tracked or no commits'}")
if file_processed(file_path):
print(f"File: {file_path} already processed, skip")
continue
else:
print(f"File: {file_path} need process")
copy_file(file_path, target_dir, last_changed)
change_src_file_contents(file_path, last_changed)
if __name__ == "__main__":
src_dir = "./blog" # Current directory
target_dir = "./_posts"
file_extensions = (".md") # Specify the desired file extensions
transform_files(src_dir, target_dir, file_extensions)