38 lines
1.1 KiB
Python

from argparse import ArgumentTypeError
import os
import subprocess
def get_full_path(relative_path):
if not os.path.isabs(relative_path):
current_dir = os.getcwd()
full_path = os.path.join(current_dir, relative_path)
else:
full_path = relative_path
return full_path
def dir_arg_type(path):
full_path = get_full_path(path)
if os.path.isdir(full_path):
return full_path
else:
raise ArgumentTypeError(f"{path} nor {full_path} are not a valid path")
def file_arg_type(path):
full_path = get_full_path(path)
if not os.path.isfile(full_path):
raise ArgumentTypeError(f"{path} nor {full_path} are not a valid file")
return full_path
def find_git_base_dir(starting_path=None):
if starting_path is None:
starting_path = os.getcwd()
try:
base_dir = subprocess.check_output(
['git', 'rev-parse', '--show-toplevel'],
cwd=starting_path,
text=True
).strip()
return base_dir
except subprocess.CalledProcessError:
return None