Skip to content

Commit

Permalink
Doing some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thtroyer committed Mar 16, 2022
1 parent c8ea568 commit 82b5987
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
42 changes: 24 additions & 18 deletions simple-text-generator-ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,68 @@
logger = logging.getLogger("ui")


def get_git_revision_hash():
return subprocess.check_output(['git', 'rev-parse', 'HEAD'])


def get_exe_hashes() -> str:
def _get_exe_hashes() -> str:
try:
hash1 = get_simple_text_generator_ui_exe_hash()
hash2 = get_run_training_exe_hash()
hash1 = _get_simple_text_generator_ui_exe_hash()
hash2 = _get_run_training_exe_hash()
except Exception as e:
logger.debug(e)
return ""

return f"simple-text-generator md5sum: {hash1}; run_training.exe: {hash2}"


def get_simple_text_generator_ui_exe_hash() -> str:
def _get_simple_text_generator_ui_exe_hash() -> str:
"""Gets md5sum of Windows UI binary if present."""
return hashlib.md5(open('simple-text-generator-ui.exe', 'rb').read()).hexdigest()


def get_run_training_exe_hash() -> str:
def _get_run_training_exe_hash() -> str:
"""Gets md5sum of Windows training binary if present."""
return hashlib.md5(open('run_training.exe', 'rb').read()).hexdigest()


def get_git_hash_or_empty() -> str:
def _get_git_hash_or_empty() -> str:
"""Returns current git commit hash if running from checked out repository."""
try:
return f"git hash: {get_git_revision_hash()}"
return f"git hash: {_get_git_revision_hash()}"
except Exception as e:
logger.debug(e)
return ""


def create_dir_if_not_exists(directory):
def _get_git_revision_hash():
return subprocess.check_output(['git', 'rev-parse', 'HEAD'])


def _create_dir_if_not_exists(directory):
if not os.path.exists(directory):
os.makedirs(directory)


def log_environment():
def _log_environment():
logger.info(sys.version)
logger.info(f"Platform: {sys.platform}")
logger.info(f"simple-text-generator code version: {version}")
commit = get_git_hash_or_empty()
commit = _get_git_hash_or_empty()
if commit != "":
logger.info(f"commit: {commit}")
hashes = get_exe_hashes()
hashes = _get_exe_hashes()
if hashes != "":
logger.info(f"{hashes}")


if __name__ == "__main__":
pathlib.Path('logs').mkdir(exist_ok=True)

log_file_name = "logs/simple-text-generator-ui.log"
previous_log_file_name = "logs/simple-text-generator-ui_previous.log"
setup_logging(logger, log_file_name, previous_log_file_name)
log_environment()
create_dir_if_not_exists("projects")
create_dir_if_not_exists("projects/archive")
_log_environment()

_create_dir_if_not_exists("projects")
_create_dir_if_not_exists("projects/archive")

try:
window_manager = draw_main_menu()
except Exception as e:
Expand Down
17 changes: 9 additions & 8 deletions simpletextgenerator/logging_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
import sys


def archive_last_log(log_file_name, previous_log_file_name):
if os.path.exists(log_file_name):
if os.path.exists(previous_log_file_name):
os.remove(previous_log_file_name)
os.rename(log_file_name, previous_log_file_name)


def setup_logging(logger, log_file_name, previous_log_file_name, level=logging.DEBUG):
"""Configures passed in logger and handles archiving previous log file."""
logger.setLevel(level)
archive_last_log(log_file_name, previous_log_file_name)
_archive_last_log(log_file_name, previous_log_file_name)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(level)
stdout_handler = logging.StreamHandler(stream=sys.stdout)
Expand All @@ -23,3 +17,10 @@ def setup_logging(logger, log_file_name, previous_log_file_name, level=logging.D
logger.addHandler(file_handler)

return logger


def _archive_last_log(log_file_name, previous_log_file_name):
if os.path.exists(log_file_name):
if os.path.exists(previous_log_file_name):
os.remove(previous_log_file_name)
os.rename(log_file_name, previous_log_file_name)
4 changes: 4 additions & 0 deletions simpletextgenerator/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@


class Train:
"""
Manages the training and text generation steps. Contains the state of the project/job
and interfaces with textgenrnn library to execute the training/generation.
"""
def __init__(self, job: Job, logger):
self.logger = logger
self.job = job
Expand Down

0 comments on commit 82b5987

Please sign in to comment.