Skip to content

Commit

Permalink
Merge pull request #1057 from xylar/add-wrong-worktree-error
Browse files Browse the repository at this point in the history
Raise error if mpas_analysis is installed in different worktree
  • Loading branch information
xylar authored Feb 3, 2025
2 parents 103dffc + bda66c8 commit 24058d9
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions mpas_analysis/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import logging
import xarray
import time
import json
from importlib.metadata import Distribution
from importlib.resources import contents

from mache import discover_machine, MachineInfo
Expand Down Expand Up @@ -897,11 +899,91 @@ def link_dir(section, option):
link_dir(section=section, option=option)


def get_editable_install_dir(package_name):
"""
Get the directory that the package is installed in if it is installed in
editable mode, or None if it is not.
Parameters
----------
package_name : str
The name of the package
Returns
-------
install_dir : str or None
The directory the package is installed in if in editable mode, or None
"""

direct_url = Distribution.from_name(package_name).read_text(
'direct_url.json')
contents = json.loads(direct_url)
pkg_is_editable = contents.get("dir_info", {}).get("editable", False)
if pkg_is_editable and 'url' in contents:
url = contents['url']
if url.startswith('file://'):
return url[7:]
return None


def is_mpas_analysis_git_base():
"""
Check if the current working directory is the base of an mpas_analysis git
branch or a git worktree.
Returns
-------
is_git_base : bool
True if the current working directory is the base of an mpas_analysis
git branch or a git worktree, False otherwise
"""
mpas_analysis_dir = os.path.join(os.getcwd(), 'mpas_analysis')
if not os.path.isdir(mpas_analysis_dir):
# no package mpas_analysis, so can't be an mpas_analysis git base
return False

git_dir = os.path.join(os.getcwd(), '.git')
if os.path.isdir(git_dir):
# It's a git repository
head_file = os.path.join(git_dir, 'HEAD')
elif os.path.isfile(git_dir):
# It's a git worktree
with open(git_dir, 'r') as f:
git_dir_path = f.read().strip().split(': ')[1]
head_file = os.path.join(git_dir_path, 'HEAD')
else:
return False

if not os.path.isfile(head_file):
return False

with open(head_file, 'r') as f:
head_content = f.read()
if 'ref: refs/heads/' in head_content:
return True

return False


def main():
"""
Entry point for the main script ``mpas_analysis``
"""

mpas_analysis_dir = get_editable_install_dir('mpas_analysis')
if is_mpas_analysis_git_base() and mpas_analysis_dir is not None:
# mpas_analysis is installed in editable mode and this is the base
# of an mpas_analysis git branch
if os.path.abspath(mpas_analysis_dir) != os.getcwd():
raise OSError(
"""
The current working directory is the base of an mpas_analysis git branch,
but the package is installed in editable mode in a different directory.
Please reinstall mpas_analysis in editable mode using:
python -m pip install --no-deps --no-build-isolation -e .
"""
)

parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-v', '--version',
Expand Down

0 comments on commit 24058d9

Please sign in to comment.