Skip to content

Commit

Permalink
Merge pull request #48 from aisingapore/47-docsdiff
Browse files Browse the repository at this point in the history
Resolves #47
  • Loading branch information
Syakyr authored Sep 25, 2024
2 parents 8e71403 + 2af1131 commit 8da8b01
Show file tree
Hide file tree
Showing 86 changed files with 3,206 additions and 3,667 deletions.
41 changes: 41 additions & 0 deletions extras/generate_diffs.py
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()
1 change: 1 addition & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def remove_redundant_files() -> None:
if ORCH != 'runai':
shutil.rmtree(RUNAI_YAML_PATH)


if __name__ == "__main__":
generate_template_scripts()
remove_redundant_files()
19 changes: 14 additions & 5 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
ERROR_MSG_LIST = []


def check_input_length(cookie_input_key, cookie_input_val):
def check_input_length(
cookie_input_key: str,
cookie_input_val: str
) -> None:

global ERROR_MSG_LIST

Expand All @@ -47,7 +50,10 @@ def check_input_length(cookie_input_key, cookie_input_val):
% (cookie_input_key, cookie_input_val["user_input"]))


def check_input_regex(cookie_input_key, cookie_input_val):
def check_input_regex(
cookie_input_key: str,
cookie_input_val: str
) -> None:

global ERROR_MSG_LIST

Expand All @@ -70,7 +76,10 @@ def check_input_regex(cookie_input_key, cookie_input_val):
% (cookie_input_key, cookie_input_val["user_input"]))


def check_not_implemented(cookie_input_key, cookie_input_val):
def check_not_implemented(
cookie_input_key: str,
cookie_input_val: str
) -> None:

global ERROR_MSG_LIST

Expand All @@ -79,7 +88,7 @@ def check_not_implemented(cookie_input_key, cookie_input_val):
% (cookie_input_key, cookie_input_val["user_input"]))


def check_cookiecutter_inputs():
def check_cookiecutter_inputs() -> None:

global COOKIE_INPUTS
global ERROR_MSG_LIST
Expand All @@ -99,4 +108,4 @@ def check_cookiecutter_inputs():


if __name__ == "__main__":
check_cookiecutter_inputs()
check_cookiecutter_inputs()
72 changes: 72 additions & 0 deletions hooks/pre_prompt.py
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()
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ venv.bak/
# mypy
.mypy_cache/

####################
####################
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,4 @@ venv.bak/
# mypy
.mypy_cache/

####################
####################
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ git push -u origin main
```

[ccutter]: https://github.com/cookiecutter/cookiecutter
[readme]: https://github.com/aisingapore/kapitan-hull/blob/main/README.md
[readme]: https://github.com/aisingapore/kapitan-hull/blob/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,4 @@ run that looks similar to the following:

??? info "Reference Link(s)"

- [MLflow Docs - MLflow Tracking](https://www.mlflow.org/docs/latest/tracking.html)
- [MLflow Docs - MLflow Tracking](https://www.mlflow.org/docs/latest/tracking.html)
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ if you intend to use Jupyter notebooks within the VSCode environment.

??? info "Reference Link(s)"

- [Using Docker-in-Docker for your CI or testing environment? Think twice. - jpetazzo](https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/)
- [Using Docker-in-Docker for your CI or testing environment? Think twice. - jpetazzo](https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dependencies required for this guide.

```bash
conda env create -f {{cookiecutter.repo_name}}-conda-env.yaml
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,4 @@ examples off the top:
- automate the deployment of the FastAPI servers to Kubernetes clusters

There's much more that can be done but whatever has been shared thus
far is hopefully enough for one to get started with CI/CD.
far is hopefully enough for one to get started with CI/CD.
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ Inputs provided to `cookiecutter` for the generation of this template:
│ the project's development environment.
└── README.md <- The top-level README containing the basic
guide for using the repository.
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ markdown_extensions:
- pymdownx.tasklist:
custom_checkbox: true
- pymdownx.tilde
- pymdownx.snippets
- pymdownx.snippets
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/conf/logging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ handlers:

root:
level: INFO
handlers: [console, debug_file_handler, info_file_handler, error_file_handler]
handlers: [console, debug_file_handler, info_file_handler, error_file_handler]
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ WORKDIR ${HOME_DIR}
COPY --chown=${NON_ROOT_USER}:${NON_ROOT_GID} ${REPO_DIR} {{cookiecutter.repo_name}}

# Install pip requirements
RUN pip install -r {{cookiecutter.repo_name}}/requirements.txt
RUN pip install -r {{cookiecutter.repo_name}}/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ RUN pip install -r {{cookiecutter.repo_name}}/requirements.txt
#RUN micromamba shell init -s bash -p ~/micromamba
#RUN micromamba install python=3.12.4 -c defaults -n base -y
#RUN micromamba run -n base pip install -r {{cookiecutter.repo_name}}/requirements.txt
#RUN echo 'alias python="micromamba run -n base python"' >> "${HOME_DIR}/.bashrc"
#RUN echo 'alias python="micromamba run -n base python"' >> "${HOME_DIR}/.bashrc"
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Indices and tables

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
* :ref:`search`
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/docs/make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ goto end
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
popd
Loading

0 comments on commit 8da8b01

Please sign in to comment.