Skip to content

Commit

Permalink
Merge pull request #1373 from pllim/win-symlink
Browse files Browse the repository at this point in the history
DEV: Allow symlink in Windows for editable install
  • Loading branch information
pllim authored Jun 10, 2022
2 parents fbb644d + e64423b commit 77d43b5
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,41 @@ def run(self):
if '--user' in sys.prefix: # TODO: is there a better way to find out?
target_dir = user_dir()
target_dir = os.path.join(target_dir)
is_win = sys.platform.startswith('win')

for prefix_target, name in self.prefix_targets:
source = os.path.join('share', 'jupyter', prefix_target, name)
target = os.path.join(target_dir, prefix_target, name)
target_subdir = os.path.dirname(target)
if not os.path.exists(target_subdir):
os.makedirs(target_subdir)
if not is_win:
rel_source = os.path.relpath(os.path.abspath(
source), os.path.abspath(target_subdir))
else: # relpath does not work if source/target on different Windows disks
rel_source = os.path.abspath(source)
abs_source = os.path.abspath(source)

try:
rel_source = os.path.relpath(abs_source, os.path.abspath(target_subdir))
except Exception:
# relpath does not work if source/target on different Windows disks.
do_symlink = False
else:
do_symlink = True

try:
os.remove(target)
except Exception:
try:
shutil.rmtree(target) # Maybe not a symlink
except Exception:
pass

# Cannot symlink without relpath or Windows admin priv in some OS versions.
try:
if not is_win:
os.remove(target)
if do_symlink:
print(rel_source, '->', target)
os.symlink(rel_source, target)
else:
shutil.rmtree(target)
raise OSError('just make copies')
except Exception:
pass
print(rel_source, '->', target)
if not is_win:
os.symlink(rel_source, target)
else: # Cannot symlink without relpath or Windows admin priv in some OS versions
shutil.copytree(rel_source, target)
print(abs_source, '->', target)
shutil.copytree(abs_source, target)

super(DevelopCmd, self).run()

Expand Down

0 comments on commit 77d43b5

Please sign in to comment.