Skip to content

Commit 24058d9

Browse files
authored
Merge pull request #1057 from xylar/add-wrong-worktree-error
Raise error if mpas_analysis is installed in different worktree
2 parents 103dffc + bda66c8 commit 24058d9

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

mpas_analysis/__main__.py

+82
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import logging
3232
import xarray
3333
import time
34+
import json
35+
from importlib.metadata import Distribution
3436
from importlib.resources import contents
3537

3638
from mache import discover_machine, MachineInfo
@@ -897,11 +899,91 @@ def link_dir(section, option):
897899
link_dir(section=section, option=option)
898900

899901

902+
def get_editable_install_dir(package_name):
903+
"""
904+
Get the directory that the package is installed in if it is installed in
905+
editable mode, or None if it is not.
906+
907+
Parameters
908+
----------
909+
package_name : str
910+
The name of the package
911+
912+
Returns
913+
-------
914+
install_dir : str or None
915+
The directory the package is installed in if in editable mode, or None
916+
"""
917+
918+
direct_url = Distribution.from_name(package_name).read_text(
919+
'direct_url.json')
920+
contents = json.loads(direct_url)
921+
pkg_is_editable = contents.get("dir_info", {}).get("editable", False)
922+
if pkg_is_editable and 'url' in contents:
923+
url = contents['url']
924+
if url.startswith('file://'):
925+
return url[7:]
926+
return None
927+
928+
929+
def is_mpas_analysis_git_base():
930+
"""
931+
Check if the current working directory is the base of an mpas_analysis git
932+
branch or a git worktree.
933+
934+
Returns
935+
-------
936+
is_git_base : bool
937+
True if the current working directory is the base of an mpas_analysis
938+
git branch or a git worktree, False otherwise
939+
"""
940+
mpas_analysis_dir = os.path.join(os.getcwd(), 'mpas_analysis')
941+
if not os.path.isdir(mpas_analysis_dir):
942+
# no package mpas_analysis, so can't be an mpas_analysis git base
943+
return False
944+
945+
git_dir = os.path.join(os.getcwd(), '.git')
946+
if os.path.isdir(git_dir):
947+
# It's a git repository
948+
head_file = os.path.join(git_dir, 'HEAD')
949+
elif os.path.isfile(git_dir):
950+
# It's a git worktree
951+
with open(git_dir, 'r') as f:
952+
git_dir_path = f.read().strip().split(': ')[1]
953+
head_file = os.path.join(git_dir_path, 'HEAD')
954+
else:
955+
return False
956+
957+
if not os.path.isfile(head_file):
958+
return False
959+
960+
with open(head_file, 'r') as f:
961+
head_content = f.read()
962+
if 'ref: refs/heads/' in head_content:
963+
return True
964+
965+
return False
966+
967+
900968
def main():
901969
"""
902970
Entry point for the main script ``mpas_analysis``
903971
"""
904972

973+
mpas_analysis_dir = get_editable_install_dir('mpas_analysis')
974+
if is_mpas_analysis_git_base() and mpas_analysis_dir is not None:
975+
# mpas_analysis is installed in editable mode and this is the base
976+
# of an mpas_analysis git branch
977+
if os.path.abspath(mpas_analysis_dir) != os.getcwd():
978+
raise OSError(
979+
"""
980+
The current working directory is the base of an mpas_analysis git branch,
981+
but the package is installed in editable mode in a different directory.
982+
Please reinstall mpas_analysis in editable mode using:
983+
python -m pip install --no-deps --no-build-isolation -e .
984+
"""
985+
)
986+
905987
parser = argparse.ArgumentParser(
906988
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
907989
parser.add_argument('-v', '--version',

0 commit comments

Comments
 (0)