Skip to content
Snippets Groups Projects
Commit 76f7651c authored by Ronald Jäpel's avatar Ronald Jäpel
Browse files

add CLI

parent 2b5612a3
No related branches found
No related tags found
No related merge requests found
from .utils import ProjectRepo, initialize_git_repo from .utils import ProjectRepo
from .initialize_repo import initialize_git_repo
from .conda_env_utils import prepare_conda_env from .conda_env_utils import prepare_conda_env
...@@ -2,6 +2,15 @@ import urllib.request ...@@ -2,6 +2,15 @@ import urllib.request
import shutil import shutil
import tempfile import tempfile
import click
@click.command()
@click.option('--url', default=None,
help='Url to the environment.yml file.')
def prepare_conda_env_cli(url):
prepare_conda_env(url)
def prepare_conda_env(url: str = None): def prepare_conda_env(url: str = None):
if url is None: if url is None:
...@@ -10,6 +19,6 @@ def prepare_conda_env(url: str = None): ...@@ -10,6 +19,6 @@ def prepare_conda_env(url: str = None):
with tempfile.NamedTemporaryFile(delete=False, prefix="environment_", suffix=".yaml") as tmp_file: with tempfile.NamedTemporaryFile(delete=False, prefix="environment_", suffix=".yaml") as tmp_file:
shutil.copyfileobj(response, tmp_file) shutil.copyfileobj(response, tmp_file)
print("Please then run this command in a terminal (Linux) or anaconda shell (Windows):") print("Please now run this command in a terminal (Linux) or anaconda shell (Windows):\n")
print(f"conda env create -f {tmp_file.name}") print(f"conda env create -f {tmp_file.name}\n")
return return
import os
import click
from cadetrdm.utils import ProjectRepo, ResultsRepo
def add_linebreaks(input_list):
"""
Add linebreaks between each entry in the input_list
"""
return [line + "\n" for line in input_list]
def init_lfs(lfs_filetypes: list, path: str = None):
"""
Initialize lfs in the git repository at the path.
If path is None, the current working directory is used.
:param lfs_filetypes:
List of file types to be handled by lfs.
Format should be e.g. ["*.jpg", "*.png"] for jpg and png files.
"""
if path is not None:
previous_path = os.getcwd()
os.chdir(path)
os.system(f"git lfs install")
lfs_filetypes_string = " ".join(lfs_filetypes)
os.system(f"git lfs track {lfs_filetypes_string}")
if path is not None:
os.chdir(previous_path)
def write_lines_to_file(path, lines, open_type="a"):
"""
Convenience function. Write lines to a file at path with added newlines between each line.
:param path:
Path to file.
:param lines:
List of lines to be written to file.
:param open_type:
The way the file should be opened. I.e. "a" for append and "w" for fresh write.
"""
with open(path, open_type) as f:
f.writelines(add_linebreaks(lines))
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
from shutil import which
return which(name) is not None
@click.command()
@click.option('--output_repo_name', default="output",
help='Name of the folder where the tracked output should be stored.')
@click.option('--gitignore', default=None,
help='List of files to be added to the gitignore file.')
@click.option('--gitattributes', default=None,
help='List of files to be added to the gitattributes file.')
@click.option('--lfs_filetypes', default=None,
help='List of filetypes to be handled by git lfs.')
@click.argument('path_to_repo')
def initialize_git_repo_cli(path_to_repo: str, output_repo_name: (str | bool) = "output", gitignore: list = None,
gitattributes: list = None, lfs_filetypes: list = None,
output_repo_kwargs: dict = None):
initialize_git_repo(path_to_repo, output_repo_name, gitignore,
gitattributes, lfs_filetypes,
output_repo_kwargs)
def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "output", gitignore: list = None,
gitattributes: list = None, lfs_filetypes: list = None,
output_repo_kwargs: dict = None):
"""
Initialize a git repository at the given path with an optional included output results repository.
:param path_to_repo:
Path to main repository.
:param output_repo_name:
Name for the output repository.
:param gitignore:
List of files to be added to the gitignore file.
:param gitattributes:
List of lines to be added to the gittatributes file
:param lfs_filetypes:
List of filetypes to be handled by git lfs.
:param output_repo_kwargs:
kwargs to be given to the creation of the output repo initalization function.
Include gitignore, gitattributes, and lfs_filetypes kwargs.
:return:
"""
if not is_tool("git-lfs"):
raise RuntimeError("Git LFS is not installed. Please install it via e.g. apt-get install git-lfs or the "
"instructions found below \n"
"https://docs.github.com/en/repositories/working-with-files"
"/managing-large-files/installing-git-large-file-storage")
if gitignore is None:
gitignore = [".idea", "*diskcache*", "*tmp*", ".ipynb_checkpoints", "__pycache__"]
if output_repo_name:
gitignore.append(output_repo_name)
gitignore.append(output_repo_name + "_cached")
if gitattributes is None:
gitattributes = []
if lfs_filetypes is None:
lfs_filetypes = ["*.jpg", "*.png", "*.xlsx", "*.m5", "*.ipynb", "*.pfd"]
starting_directory = os.getcwd()
if path_to_repo != ".":
if os.path.exists(path_to_repo) and len(os.listdir(path_to_repo)) > 0:
raise ValueError("Path to repository already exists and is not an empty directory.")
os.makedirs(path_to_repo)
os.chdir(path_to_repo)
os.system(f"git init")
init_lfs(lfs_filetypes)
write_lines_to_file(path=".gitattributes", lines=gitattributes)
write_lines_to_file(path=".gitignore", lines=gitignore)
if output_repo_kwargs is None:
output_repo_kwargs = {"gitattributes": ["log.csv merge=union"]}
if output_repo_name:
# This means we are in the project repo and should now initialize the output_repo
initialize_git_repo(output_repo_name, output_repo_name=False, **output_repo_kwargs)
# This instance of ProjectRepo is therefore the project repo
repo = ProjectRepo(".", output_folder=output_repo_name)
else:
# If output_repo_name is False we are in the output_repo and should finish by committing the changes
repo = ResultsRepo(".")
repo.git.add(".")
repo.git.commit("-m", "initial commit")
os.chdir(starting_directory)
...@@ -3,6 +3,7 @@ import json ...@@ -3,6 +3,7 @@ import json
from datetime import datetime from datetime import datetime
import shutil import shutil
import contextlib import contextlib
import click
try: try:
import git import git
...@@ -471,123 +472,3 @@ class ProjectRepo(BaseRepo): ...@@ -471,123 +472,3 @@ class ProjectRepo(BaseRepo):
class ResultsRepo(BaseRepo): class ResultsRepo(BaseRepo):
pass pass
def add_linebreaks(input_list):
"""
Add linebreaks between each entry in the input_list
"""
return [line + "\n" for line in input_list]
def init_lfs(lfs_filetypes: list, path: str = None):
"""
Initialize lfs in the git repository at the path.
If path is None, the current working directory is used.
:param lfs_filetypes:
List of file types to be handled by lfs.
Format should be e.g. ["*.jpg", "*.png"] for jpg and png files.
"""
if path is not None:
previous_path = os.getcwd()
os.chdir(path)
os.system(f"git lfs install")
lfs_filetypes_string = " ".join(lfs_filetypes)
os.system(f"git lfs track {lfs_filetypes_string}")
if path is not None:
os.chdir(previous_path)
def write_lines_to_file(path, lines, open_type="a"):
"""
Convenience function. Write lines to a file at path with added newlines between each line.
:param path:
Path to file.
:param lines:
List of lines to be written to file.
:param open_type:
The way the file should be opened. I.e. "a" for append and "w" for fresh write.
"""
with open(path, open_type) as f:
f.writelines(add_linebreaks(lines))
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
from shutil import which
return which(name) is not None
def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "output", gitignore: list = None,
gitattributes: list = None, lfs_filetypes: list = None,
output_repo_kwargs: dict = None):
"""
Initialize a git repository at the given path with an optional included output results repository.
:param path_to_repo:
Path to main repository.
:param output_repo_name:
Name for the output repository.
:param gitignore:
List of files to be added to the gitignore file.
:param gitattributes:
List of lines to be added to the gittatributes file
:param lfs_filetypes:
List of filetypes to be handled by git lfs.
:param output_repo_kwargs:
kwargs to be given to the creation of the output repo initalization function.
Include gitignore, gitattributes, and lfs_filetypes kwargs.
:return:
"""
if not is_tool("git-lfs"):
raise RuntimeError("Git LFS is not installed. Please install it via e.g. apt-get install git-lfs or the "
"instructions found below \n"
"https://docs.github.com/en/repositories/working-with-files"
"/managing-large-files/installing-git-large-file-storage")
if gitignore is None:
gitignore = [".idea", "*diskcache*", "*tmp*", ".ipynb_checkpoints", "__pycache__"]
if output_repo_name:
gitignore.append(output_repo_name)
gitignore.append(output_repo_name + "_cached")
if gitattributes is None:
gitattributes = []
if lfs_filetypes is None:
lfs_filetypes = ["*.jpg", "*.png", "*.xlsx", "*.m5", "*.ipynb", "*.pfd"]
starting_directory = os.getcwd()
if path_to_repo != ".":
if os.path.exists(path_to_repo) and len(os.listdir(path_to_repo)) > 0:
raise ValueError("Path to repository already exists and is not an empty directory.")
os.makedirs(path_to_repo)
os.chdir(path_to_repo)
os.system(f"git init")
init_lfs(lfs_filetypes)
write_lines_to_file(path=".gitattributes", lines=gitattributes)
write_lines_to_file(path=".gitignore", lines=gitignore)
if output_repo_kwargs is None:
output_repo_kwargs = {"gitattributes": ["log.csv merge=union"]}
if output_repo_name:
# This means we are in the project repo and should now initialize the output_repo
initialize_git_repo(output_repo_name, output_repo_name=False, **output_repo_kwargs)
# This instance of ProjectRepo is therefore the project repo
repo = ProjectRepo(".", output_folder=output_repo_name)
else:
# If output_repo_name is False we are in the output_repo and should finish by committing the changes
repo = ResultsRepo(".")
repo.git.add(".")
repo.git.commit("-m", "initial commit")
os.chdir(starting_directory)
...@@ -19,6 +19,12 @@ packages = find: ...@@ -19,6 +19,12 @@ packages = find:
python_requires = >=3.8 python_requires = >=3.8
install_requires = install_requires =
gitpython gitpython
include_package_data = True
[options.entry_points]
console_scripts =
initialize_git_repo = cadetrdm.initialize_repo:initialize_git_repo_cli
prepare_conda_env = cadetrdm.conda_env_utils:prepare_conda_env_cli
[options.extras_require] [options.extras_require]
testing = testing =
......
...@@ -69,7 +69,7 @@ def try_initialize_git_repo(path_to_repo): ...@@ -69,7 +69,7 @@ def try_initialize_git_repo(path_to_repo):
if os.path.exists(path_to_repo): if os.path.exists(path_to_repo):
remove_dir(path_to_repo) remove_dir(path_to_repo)
initialize_git_repo(path_to_repo=path_to_repo) initialize_git_repo(path_to_repo)
assert try_init_gitpython_repo(path_to_repo) assert try_init_gitpython_repo(path_to_repo)
assert try_init_gitpython_repo(os.path.join(path_to_repo, "output")) assert try_init_gitpython_repo(os.path.join(path_to_repo, "output"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment