diff --git a/README.md b/README.md index 0e70576..b208ad6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ -linux-helpers -============= +work-helpers +============ Some helper scripts for the day to day work with Ubuntu in WSL2 - **xls2changelog**: reformat copied excel data to the safeguard changelog format + - **sg_mbp_release**: collect and annotate all files used for a safeguard workbook release diff --git a/pyproject.toml b/pyproject.toml index cd768ce..b46dd80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,10 +3,10 @@ requires = ["flit_core >=2,<4"] build-backend = "flit_core.buildapi" [tool.flit.metadata] -module = "linux_helpers" +module = "work_helpers" author = "Holger Frey" author-email = "frey@imtek.de" -home-page = "https://git.cpi.imtek.uni-freiburg.de/holgi/linux-helpers" +home-page = "https://git.cpi.imtek.uni-freiburg.de/holgi/work-helpers" description-file = "README.md" license = "Beerware" @@ -18,4 +18,5 @@ requires = [ [tool.flit.scripts] -xls2changelog = "linux_helpers.excel2changelog:cli" +xls2changelog = "work_helpers.excel2changelog:cli" +sg_mbp_release = "work_helpers.sg_mbp_release:cli" diff --git a/linux_helpers/__init__.py b/work_helpers/__init__.py similarity index 100% rename from linux_helpers/__init__.py rename to work_helpers/__init__.py diff --git a/work_helpers/_natural_sort.py b/work_helpers/_natural_sort.py new file mode 100644 index 0000000..4f0ac21 --- /dev/null +++ b/work_helpers/_natural_sort.py @@ -0,0 +1,30 @@ +""" Sort the content of an list in a natural way + +> l = ["A2", "A10", "A1", "A3"] +> sorted(l) +['A1', 'A10', 'A2', 'A3'] +> natural_sort(l) +['A1', 'A2', 'A3', 'A10'] + +from http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html +""" + +import re + + +_NATURAL_SORT_REGEX_DIGITS = re.compile("([0-9]+)") + + +def _nartural_sort_convert(text): + return int(text) if text.isdigit() else text.lower() + + +def _nartural_sort_alphanum_key(text): + return tuple( + _nartural_sort_convert(part) + for part in _NATURAL_SORT_REGEX_DIGITS.split(text) + ) + + +def natural_sort(iterable): + return sorted(iterable, key=_nartural_sort_alphanum_key) diff --git a/linux_helpers/excel2changelog.py b/work_helpers/excel2changelog.py similarity index 100% rename from linux_helpers/excel2changelog.py rename to work_helpers/excel2changelog.py diff --git a/work_helpers/sg_mbp_release.py b/work_helpers/sg_mbp_release.py new file mode 100644 index 0000000..0de5e70 --- /dev/null +++ b/work_helpers/sg_mbp_release.py @@ -0,0 +1,75 @@ +import click +import shutil + +from pathlib import Path +from datetime import datetime + +from ._natural_sort import natural_sort + +DEVELOPER_DRIVE = Path("/mnt/e/") +PATH_ISSUES = DEVELOPER_DRIVE / "Safeguard-MBP-issues" +PATH_WORKBOOKS = DEVELOPER_DRIVE / "Safeguard MBP Workbooks" +PATH_WIN_DESKTOP = Path("/mnt/c/Users/Holgi/Desktop") + +TODAY = datetime.now().strftime("%y%m%d") + + +def _folder_content(folder): + nondotted = (i for i in folder.iterdir() if not i.stem.startswith(".")) + return (i for i in nondotted if not i.stem.startswith("~")) + + +def _files_in_folder(folder, suffix): + files = (f for f in _folder_content(folder) if f.is_file()) + return (f for f in files if f.suffix == suffix) + + +def get_latest_version(parent=PATH_ISSUES): + folders = (i for i in _folder_content(parent) if i.is_dir()) + versions = natural_sort(f.name for f in folders if f.stem.lower().startswith("v")) + return versions[-1] + + +def copy_changelog(destination, date, latest): + textfiles = _files_in_folder(PATH_ISSUES, ".txt") + changelog = next(f for f in textfiles if f.stem.lower().startswith("change")) + new_path = destination / f"{date}_CHANGELOG_{latest}.txt" + print(changelog.name, "->", new_path) + shutil.copyfile(changelog, new_path) + + +def copy_workbook_changelogs(destination, date, latest): + source = PATH_ISSUES / latest + textfiles = _files_in_folder(source, ".txt") + logs = (f for f in textfiles if f.stem.lower().startswith("change")) + for log_file in logs: + parts = log_file.stem.split("_") + if parts[-1] == latest: + new_path = destination / f"{date}_{log_file.name}" + else: + new_path = destination / f"{date}_{log_file.stem}_{latest}.txt" + print(log_file.name, "->", new_path) + shutil.copyfile(log_file, new_path) + + +def copy_workbooks(destination, date, latest): + for excel_file in _files_in_folder(PATH_WORKBOOKS, ".xlsx"): + new_path = destination / f"{date}_{excel_file.stem}_{latest}.xlsx" + print(excel_file.name, "->", new_path) + shutil.copyfile(excel_file, new_path) + + +@click.command() +def cli(): + latest = get_latest_version() + + new_folder_name = f"{TODAY} {latest}" + new_folder_path = PATH_WIN_DESKTOP / new_folder_name + if new_folder_path.exists(): + raise IOError(f"Folder exists on desktop: {new_folder_name}") + else: + new_folder_path.mkdir() + + copy_workbooks(new_folder_path, TODAY, latest) + copy_workbook_changelogs(new_folder_path, TODAY, latest) + copy_changelog(new_folder_path, TODAY, latest)