Skip to content

Commit f3946fa

Browse files
committed
Refactor Git processing: move batch processing functionality to a dedicated module and update references
1 parent 3f72c90 commit f3946fa

3 files changed

Lines changed: 100 additions & 84 deletions

File tree

exporters/git/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
clone_target_repository_to_temp
1111
)
1212
from .generate_init_commit_for_doc import generate_init_commit_for_document
13+
from .batch_processor import process_files_with_git_batch
1314

1415
__all__ = [
1516
'prepare_git_branch',
@@ -19,5 +20,6 @@
1920
'configure_git_remote',
2021
'push_to_target_repository',
2122
'clone_target_repository_to_temp',
22-
'generate_init_commit_for_document'
23+
'generate_init_commit_for_document',
24+
'process_files_with_git_batch'
2325
]

exporters/git/batch_processor.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Batch processing functionality for git exports.
4+
5+
This module handles batch processing of multiple SFS documents to git repository.
6+
"""
7+
8+
import os
9+
import shutil
10+
import subprocess
11+
from datetime import datetime
12+
from pathlib import Path
13+
import random
14+
import json
15+
16+
from exporters.git import clone_target_repository_to_temp
17+
from exporters.git.git_utils import GIT_TIMEOUT
18+
19+
20+
def process_files_with_git_batch(json_files, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links):
21+
"""Process files with git batch workflow."""
22+
# Clone target repository once for all documents
23+
repo_dir, original_cwd = clone_target_repository_to_temp(verbose=verbose)
24+
if repo_dir is None:
25+
print("Fel: Kunde inte klona target repository, faller tillbaka på lokal bearbetning")
26+
# Fallback to normal processing
27+
from sfs_processor import make_document
28+
for json_file in json_files:
29+
try:
30+
with open(json_file, 'r', encoding='utf-8') as f:
31+
data = json.load(f)
32+
except (json.JSONDecodeError, FileNotFoundError) as e:
33+
print(f"Fel vid läsning av {json_file}: {e}")
34+
continue
35+
make_document(data, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links)
36+
return
37+
38+
try:
39+
# Change to cloned repository directory
40+
os.chdir(repo_dir)
41+
42+
# Create unique branch name for this batch
43+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
44+
random_suffix = random.randint(1000, 9999)
45+
unique_branch = f"{git_branch}_batch_{timestamp}_{random_suffix}"
46+
47+
# Create and checkout new branch directly
48+
try:
49+
subprocess.run(['git', 'checkout', '-b', unique_branch],
50+
check=True, capture_output=True, timeout=GIT_TIMEOUT)
51+
if verbose:
52+
print(f"Skapade och bytte till branch '{unique_branch}' för batch-commits")
53+
except subprocess.CalledProcessError as e:
54+
print(f"Fel: Kunde inte skapa git branch: {e}")
55+
return
56+
57+
# Process each JSON file
58+
from sfs_processor import make_document
59+
for json_file in json_files:
60+
# Use absolute path since we changed working directory
61+
abs_json_file = Path(original_cwd) / json_file
62+
try:
63+
with open(abs_json_file, 'r', encoding='utf-8') as f:
64+
data = json.load(f)
65+
except (json.JSONDecodeError, FileNotFoundError) as e:
66+
print(f"Fel vid läsning av {abs_json_file}: {e}")
67+
continue
68+
69+
# Create documents in the cloned repository
70+
make_document(data, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links)
71+
72+
# Push all commits to target repository
73+
if verbose:
74+
print(f"Pushar batch till target repository...")
75+
76+
subprocess.run(['git', 'push', 'origin', unique_branch],
77+
check=True, capture_output=True, timeout=GIT_TIMEOUT)
78+
79+
print(f"Batch pushad till target repository som branch '{unique_branch}'")
80+
81+
except subprocess.CalledProcessError as e:
82+
print(f"Fel vid git batch processing: {e}")
83+
if hasattr(e, 'stderr') and e.stderr:
84+
print(f"Git stderr: {e.stderr.decode('utf-8', errors='replace')}")
85+
except Exception as e:
86+
print(f"Oväntat fel vid git batch processing: {e}")
87+
finally:
88+
# Always change back to original directory
89+
os.chdir(original_cwd)
90+
# Clean up temporary directory
91+
try:
92+
shutil.rmtree(repo_dir.parent)
93+
except Exception as e:
94+
if verbose:
95+
print(f"Varning: Kunde inte rensa temporär katalog: {e}")

sfs_processor.py

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -560,88 +560,6 @@ def create_ignored_markdown_content(data: Dict[str, Any], reason: str) -> str:
560560
return markdown_body
561561

562562

563-
def _process_files_with_git_batch(json_files, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links):
564-
"""Process files with git batch workflow."""
565-
import shutil
566-
import subprocess
567-
from datetime import datetime
568-
import random
569-
from exporters.git import clone_target_repository_to_temp
570-
from exporters.git.git_utils import GIT_TIMEOUT
571-
572-
# Clone target repository once for all documents
573-
repo_dir, original_cwd = clone_target_repository_to_temp(verbose=verbose)
574-
if repo_dir is None:
575-
print("Fel: Kunde inte klona target repository, faller tillbaka på lokal bearbetning")
576-
# Fallback to normal processing
577-
for json_file in json_files:
578-
try:
579-
with open(json_file, 'r', encoding='utf-8') as f:
580-
data = json.load(f)
581-
except (json.JSONDecodeError, FileNotFoundError) as e:
582-
print(f"Fel vid läsning av {json_file}: {e}")
583-
continue
584-
make_document(data, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links)
585-
return
586-
587-
try:
588-
# Change to cloned repository directory
589-
os.chdir(repo_dir)
590-
591-
# Create unique branch name for this batch
592-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
593-
random_suffix = random.randint(1000, 9999)
594-
unique_branch = f"{git_branch}_batch_{timestamp}_{random_suffix}"
595-
596-
# Create and checkout new branch directly
597-
try:
598-
subprocess.run(['git', 'checkout', '-b', unique_branch],
599-
check=True, capture_output=True, timeout=GIT_TIMEOUT)
600-
if verbose:
601-
print(f"Skapade och bytte till branch '{unique_branch}' för batch-commits")
602-
except subprocess.CalledProcessError as e:
603-
print(f"Fel: Kunde inte skapa git branch: {e}")
604-
return
605-
606-
# Process each JSON file
607-
for json_file in json_files:
608-
# Use absolute path since we changed working directory
609-
abs_json_file = Path(original_cwd) / json_file
610-
try:
611-
with open(abs_json_file, 'r', encoding='utf-8') as f:
612-
data = json.load(f)
613-
except (json.JSONDecodeError, FileNotFoundError) as e:
614-
print(f"Fel vid läsning av {abs_json_file}: {e}")
615-
continue
616-
617-
# Create documents in the cloned repository
618-
make_document(data, output_dir, output_modes, year_folder, verbose, git_branch, predocs, apply_links)
619-
620-
# Push all commits to target repository
621-
if verbose:
622-
print(f"Pushar batch till target repository...")
623-
624-
subprocess.run(['git', 'push', 'origin', unique_branch],
625-
check=True, capture_output=True, timeout=GIT_TIMEOUT)
626-
627-
print(f"Batch pushad till target repository som branch '{unique_branch}'")
628-
629-
except subprocess.CalledProcessError as e:
630-
print(f"Fel vid git batch processing: {e}")
631-
if hasattr(e, 'stderr') and e.stderr:
632-
print(f"Git stderr: {e.stderr.decode('utf-8', errors='replace')}")
633-
except Exception as e:
634-
print(f"Oväntat fel vid git batch processing: {e}")
635-
finally:
636-
# Always change back to original directory
637-
os.chdir(original_cwd)
638-
# Clean up temporary directory
639-
try:
640-
shutil.rmtree(repo_dir.parent)
641-
except Exception as e:
642-
if verbose:
643-
print(f"Varning: Kunde inte rensa temporär katalog: {e}")
644-
645563

646564
def main():
647565
"""Main function to process all JSON files in the json directory."""
@@ -733,7 +651,8 @@ def main():
733651

734652
# Handle git mode with batch processing
735653
if "git" in output_modes:
736-
_process_files_with_git_batch(json_files, output_dir, output_modes, args.year_folder, args.verbose, args.git_branch, args.predocs, args.apply_links)
654+
from exporters.git import process_files_with_git_batch
655+
process_files_with_git_batch(json_files, output_dir, output_modes, args.year_folder, args.verbose, args.git_branch, args.predocs, args.apply_links)
737656
else:
738657
# Convert each JSON file normally
739658
for json_file in json_files:

0 commit comments

Comments
 (0)