-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from aisingapore/47-docsdiff
Resolves #47
- Loading branch information
Showing
86 changed files
with
3,206 additions
and
3,667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Run it in the root folder: python extras/generate_diffs.py | ||
import os | ||
import difflib | ||
|
||
def generate_diffs(): | ||
working_dir = "{{cookiecutter.repo_name}}" | ||
problem_templates_dir = os.path.join(working_dir, "problem-templates") | ||
|
||
for problem_domain in os.listdir(problem_templates_dir): | ||
src_dir = os.path.join(problem_templates_dir, problem_domain) | ||
|
||
for root, dirs, files in os.walk(src_dir): | ||
for file_name in files: | ||
if not file_name.endswith('.diff'): | ||
src_file_path = os.path.join(root, file_name) | ||
equivalent_working_file_path = src_file_path.replace(src_dir, working_dir) | ||
|
||
if os.path.exists(equivalent_working_file_path): | ||
# Read the source file and the equivalent working file | ||
with open(src_file_path, 'r') as src_file: | ||
src_lines = src_file.readlines() | ||
|
||
with open(equivalent_working_file_path, 'r') as working_file: | ||
working_lines = working_file.readlines() | ||
|
||
# Use difflib to create a unified diff | ||
diff = difflib.unified_diff(working_lines, src_lines, fromfile=equivalent_working_file_path, | ||
tofile=src_file_path) | ||
|
||
# Create diff file path | ||
diff_file_path = src_file_path + '.diff' | ||
|
||
# Write the diff to the diff file | ||
with open(diff_file_path, 'w') as diff_file: | ||
diff_file.writelines(diff) | ||
|
||
# Remove the original file in src_dir | ||
os.remove(src_file_path) | ||
|
||
if __name__ == "__main__": | ||
generate_diffs() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
import shutil | ||
from unidiff import PatchSet | ||
|
||
|
||
def apply_patch(patch: PatchSet, src_dir: str) -> None: | ||
|
||
for patched_file in patch: | ||
source_file_path = os.path.join(src_dir, patched_file.source_file) | ||
target_file_path = os.path.join(src_dir, patched_file.target_file) | ||
|
||
if not os.path.exists(target_file_path): | ||
os.makedirs(os.path.dirname(target_file_path), exist_ok=True) | ||
with open(target_file_path, 'w') as f: | ||
pass # Create an empty file if it does not exist | ||
|
||
modified_lines = [] | ||
|
||
with open(source_file_path, 'r') as f: | ||
original_lines = f.readlines() | ||
|
||
line_index = 0 | ||
for hunk in patched_file: | ||
# Apply each hunk | ||
while line_index < hunk.source_start - 1: | ||
modified_lines.append(original_lines[line_index]) | ||
line_index += 1 | ||
|
||
for line in hunk: | ||
if line.is_removed or line.is_context: | ||
# Skip original lines (those that are removed) | ||
line_index += 1 | ||
if line.is_added or line.is_context: | ||
# Add new lines (those that are added) | ||
modified_lines.append(line.value) | ||
|
||
# Append any remaining lines after the last hunk | ||
while line_index < len(original_lines): | ||
modified_lines.append(original_lines[line_index]) | ||
line_index += 1 | ||
|
||
# Write the patched content back into the file | ||
with open(target_file_path, 'w') as f: | ||
f.writelines(modified_lines) | ||
|
||
|
||
def generate_template_scripts() -> None: | ||
|
||
working_dir = os.path.join(os.getcwd(), "{{cookiecutter.repo_name}}") | ||
problem_templates_dir = os.path.join(working_dir, "problem-templates") | ||
for problem_domain in os.listdir(problem_templates_dir): | ||
src_dir = os.path.join(working_dir, "problem-templates", problem_domain) | ||
|
||
if not os.path.isdir(src_dir): | ||
continue | ||
|
||
for root, dirs, files in os.walk(src_dir): | ||
for file_name in files: | ||
if file_name.endswith('.diff'): | ||
diff_file_path = os.path.join(root, file_name) | ||
|
||
with open(diff_file_path, 'r') as diff_file: | ||
patch_set = PatchSet(diff_file.read()) | ||
|
||
apply_patch(patch_set, os.getcwd()) | ||
|
||
# Remove the diff file after applying | ||
os.remove(diff_file_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
generate_template_scripts() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,4 +160,4 @@ venv.bak/ | |
# mypy | ||
.mypy_cache/ | ||
|
||
#################### | ||
#################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,4 +171,4 @@ venv.bak/ | |
# mypy | ||
.mypy_cache/ | ||
|
||
#################### | ||
#################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ Indices and tables | |
|
||
* :ref:`genindex` | ||
* :ref:`modindex` | ||
* :ref:`search` | ||
* :ref:`search` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ goto end | |
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% | ||
|
||
:end | ||
popd | ||
popd |
Oops, something went wrong.