Skip to content

Commit

Permalink
Change modify and commit strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
justgigio committed Nov 8, 2023
1 parent 43e1e4a commit be80992
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

.awesome_folder
.__testing_modify_folder
13 changes: 11 additions & 2 deletions src/awesome_git_mosaic/__init__.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 22 additions & 6 deletions src/awesome_git_mosaic/usecases/modify_file.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 16 additions & 5 deletions src/awesome_git_mosaic/usecases/write_mosaic.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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!")
24 changes: 10 additions & 14 deletions tests/usecases/test_modify_file.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import os
import shutil

from awesome_git_mosaic.usecases.modify_file import ModifyFile


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)

0 comments on commit be80992

Please sign in to comment.