From be80992a4a259856077b6207a8aa9df4fe8b0a10 Mon Sep 17 00:00:00 2001 From: Giovane Costa Date: Wed, 8 Nov 2023 12:29:46 -0300 Subject: [PATCH] Change modify and commit strategy --- .gitignore | 3 ++ src/awesome_git_mosaic/__init__.py | 13 +++++++-- .../usecases/modify_file.py | 28 +++++++++++++++---- .../usecases/write_mosaic.py | 21 ++++++++++---- tests/usecases/test_modify_file.py | 24 +++++++--------- 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..c368065 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +.awesome_folder +.__testing_modify_folder \ No newline at end of file diff --git a/src/awesome_git_mosaic/__init__.py b/src/awesome_git_mosaic/__init__.py index 2b2a150..5383e55 100644 --- a/src/awesome_git_mosaic/__init__.py +++ b/src/awesome_git_mosaic/__init__.py @@ -1,5 +1,14 @@ from awesome_git_mosaic.usecases.write_mosaic import WriteMosaic -def write(text: str, strength: int = 5, multiply: int = 1, background: bool = False): - WriteMosaic().write(text, strength, multiply, background) # pragma: no cover +def write( + text: str, + strength: int = 5, + multiply: int = 1, + with_spaces: bool = True, + background: bool = False, + inverted: bool = False, +): + WriteMosaic().write( + text, strength, multiply, with_spaces, background, inverted + ) # pragma: no cover diff --git a/src/awesome_git_mosaic/usecases/modify_file.py b/src/awesome_git_mosaic/usecases/modify_file.py index dd38519..e6ff6e7 100644 --- a/src/awesome_git_mosaic/usecases/modify_file.py +++ b/src/awesome_git_mosaic/usecases/modify_file.py @@ -1,14 +1,30 @@ +import os import uuid -from random import random from typing import Optional +FOLDER_NAME = ".awesome_folder" + class ModifyFile: - def __init__(self, filename: str = ".awesome_file"): - self.filename = filename + def __init__(self, folder_name: str = FOLDER_NAME): + self.folder_name = folder_name + self._mkdir() + + def modify(self, namespace: str = "default", content: Optional[str] = None): + path = self._mkdir(namespace) + filename = str(uuid.uuid4()) + content = content or filename + + self._write(path, filename, content) + + def _mkdir(self, name: str = "") -> str: + path = os.path.join(self.folder_name, name) + if not os.path.exists(path): + os.mkdir(path) + + return path - def modify(self, content: Optional[str] = None): - content = content or f"{uuid.uuid4()} {random()}" - f = open(self.filename, "w+") + def _write(self, path: str, filename: str, content: str): + f = open(os.path.join(path, filename), "w+") f.write(content) f.close() diff --git a/src/awesome_git_mosaic/usecases/write_mosaic.py b/src/awesome_git_mosaic/usecases/write_mosaic.py index e2decc8..2a56827 100644 --- a/src/awesome_git_mosaic/usecases/write_mosaic.py +++ b/src/awesome_git_mosaic/usecases/write_mosaic.py @@ -1,5 +1,4 @@ from datetime import datetime -from random import random from awesome_git_mosaic.adapters.git_mosaic.git_mosaic_adapter import GitMosaicAdapter from awesome_git_mosaic.gateways.git.git_gateway import GitGateway @@ -26,7 +25,7 @@ def write( background: bool = False, inverted: bool = False, ): - timestamps = [] + timestamps: list[datetime] = [] if background: bgstr = " " * (len(message) * multiply) timestamps += self.git_mosaic_adapter.output( @@ -43,12 +42,15 @@ def write( total = len(timestamps) count = 0.0 last_pct = 0 - print(f"Creating {total} commits...") + + print(f"Disabling git Garbage Collector...") self.git_gateway.disable_garbage_collector() + + print(f"Creating {total} commits...") for timestamp in timestamps: - self.modify_file.modify() + self.modify_file.modify(timestamp.ctime()) self.git_gateway.add() - self.git_gateway.commit(f"{timestamp.ctime()} {random()}", timestamp) + self.git_gateway.commit(f"{timestamp.ctime()} {count}", timestamp) count += 1 pct = round(count * 100 / total) @@ -57,5 +59,14 @@ def write( print(f"{pct}%") last_pct = pct + if count % 500 == 0: + print(f"{count} commits created, pushing") + self.git_gateway.push() + + print("All commits created. Last push...") self.git_gateway.push() + + print(f"Re-enabling git Garbage Collector...") self.git_gateway.enable_garbage_collector() + + print("That's all, folks! Bye!") diff --git a/tests/usecases/test_modify_file.py b/tests/usecases/test_modify_file.py index 9617bd8..626ac73 100644 --- a/tests/usecases/test_modify_file.py +++ b/tests/usecases/test_modify_file.py @@ -1,4 +1,5 @@ import os +import shutil from awesome_git_mosaic.usecases.modify_file import ModifyFile @@ -6,20 +7,15 @@ class TestModifyFile: def test_file_is_modified(self): - filename = '.__testing_modify_file' - initial_content = 'initial content' + foldername = '.__testing_modify_folder' - f = open(filename, 'w+') - f.write(initial_content) - f.close() + mf = ModifyFile(foldername) + mf.modify('namespace') + mf.modify('namespace') + mf.modify('namespace', 'something') + mf.modify('namespace2') - mf = ModifyFile(filename) - mf.modify() + assert len(os.listdir(foldername)) == 2 + assert len(os.listdir(os.path.join(foldername, 'namespace'))) == 3 - f = open(filename, 'r') - actual_content = f.read() - f.close() - - os.remove(filename) - - assert initial_content != actual_content + shutil.rmtree(foldername)