Skip to content

Commit

Permalink
Fix mypy stub installation
Browse files Browse the repository at this point in the history
Resolves   #775.
  • Loading branch information
evhub committed Jul 27, 2023
1 parent 847629a commit 7f13c92
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
46 changes: 37 additions & 9 deletions coconut/command/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
minimum_recursion_limit,
oserror_retcode,
base_stub_dir,
stub_dir_names,
installed_stub_dir,
interpreter_uses_auto_compilation,
interpreter_uses_coconut_breakpoint,
Expand Down Expand Up @@ -315,19 +316,30 @@ def run_cmd(cmd, show_output=True, raise_errs=True, **kwargs):
return ""


def symlink(link_to, link_from):
"""Link link_from to the directory link_to universally."""
if os.path.islink(link_from):
os.unlink(link_from)
elif os.path.exists(link_from):
def unlink(link_path):
"""Remove a symbolic link if one exists. Return whether anything was done."""
if os.path.islink(link_path):
os.unlink(link_path)
return True
return False


def rm_dir_or_link(dir_to_rm):
"""Safely delete a directory without deleting the contents of symlinks."""
if not unlink(dir_to_rm) and os.path.exists(dir_to_rm):
if WINDOWS:
try:
os.rmdir(link_from)
os.rmdir(dir_to_rm)
except OSError:
logger.log_exc()
shutil.rmtree(link_from)
shutil.rmtree(dir_to_rm)
else:
shutil.rmtree(link_from)
shutil.rmtree(dir_to_rm)


def symlink(link_to, link_from):
"""Link link_from to the directory link_to universally."""
rm_dir_or_link(link_from)
try:
if PY32:
os.symlink(link_to, link_from, target_is_directory=True)
Expand All @@ -341,7 +353,23 @@ def symlink(link_to, link_from):

def install_mypy_stubs():
"""Properly symlink mypy stub files."""
symlink(base_stub_dir, installed_stub_dir)
# unlink stub_dirs so we know rm_dir_or_link won't clear them
for stub_name in stub_dir_names:
unlink(os.path.join(base_stub_dir, stub_name))

# clean out the installed_stub_dir (which shouldn't follow symlinks,
# but we still do the previous unlinking just to be sure)
rm_dir_or_link(installed_stub_dir)

# recreate installed_stub_dir
os.makedirs(installed_stub_dir)

# link stub dirs into the installed_stub_dir
for stub_name in stub_dir_names:
current_stub = os.path.join(base_stub_dir, stub_name)
install_stub = os.path.join(installed_stub_dir, stub_name)
symlink(current_stub, install_stub)

return installed_stub_dir


Expand Down
5 changes: 5 additions & 0 deletions coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ def get_bool_env_var(env_var, default=False):
base_dir = os.path.dirname(os.path.abspath(fixpath(__file__)))

base_stub_dir = os.path.dirname(base_dir)
stub_dir_names = (
"__coconut__",
"_coconut",
"coconut",
)
installed_stub_dir = os.path.join(coconut_home, ".coconut_stubs")

watch_interval = .1 # seconds
Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.2"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 32
DEVELOP = 33
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down

0 comments on commit 7f13c92

Please sign in to comment.