mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-20 13:23:18 -07:00
Make replace_links more DRY and add max_depth for following links
This commit is contained in:
parent
d1f5211e78
commit
9b0d539423
1 changed files with 21 additions and 18 deletions
|
@ -6,8 +6,14 @@ import linktastic
|
||||||
from core import logger
|
from core import logger
|
||||||
from core.utils.paths import make_dir
|
from core.utils.paths import make_dir
|
||||||
|
|
||||||
if os.name == 'nt':
|
try:
|
||||||
import jaraco
|
from jaraco.windows.filesystem import islink, readlink
|
||||||
|
except ImportError:
|
||||||
|
if os.name == 'nt':
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
from os.path import islink
|
||||||
|
from os import readlink
|
||||||
|
|
||||||
|
|
||||||
def copy_link(src, target_link, use_link):
|
def copy_link(src, target_link, use_link):
|
||||||
|
@ -61,24 +67,21 @@ def copy_link(src, target_link, use_link):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def replace_links(link):
|
def replace_links(link, max_depth=10):
|
||||||
n = 0
|
link_depth = 0
|
||||||
target = link
|
target = link
|
||||||
if os.name == 'nt':
|
|
||||||
if not jaraco.windows.filesystem.islink(link):
|
for attempt in range(0, max_depth):
|
||||||
|
if not islink(target):
|
||||||
|
break
|
||||||
|
target = readlink(target)
|
||||||
|
link_depth = attempt
|
||||||
|
|
||||||
|
if not link_depth:
|
||||||
logger.debug('{0} is not a link'.format(link))
|
logger.debug('{0} is not a link'.format(link))
|
||||||
return
|
elif link_depth > max_depth or (link_depth == max_depth and islink(target)):
|
||||||
while jaraco.windows.filesystem.islink(target):
|
logger.warning('Exceeded maximum depth {0} while following link {1}'.format(max_depth, link))
|
||||||
target = jaraco.windows.filesystem.readlink(target)
|
|
||||||
n = n + 1
|
|
||||||
else:
|
else:
|
||||||
if not os.path.islink(link):
|
|
||||||
logger.debug('{0} is not a link'.format(link))
|
|
||||||
return
|
|
||||||
while os.path.islink(target):
|
|
||||||
target = os.readlink(target)
|
|
||||||
n = n + 1
|
|
||||||
if n > 1:
|
|
||||||
logger.info('Changing sym-link: {0} to point directly to file: {1}'.format(link, target), 'COPYLINK')
|
logger.info('Changing sym-link: {0} to point directly to file: {1}'.format(link, target), 'COPYLINK')
|
||||||
os.unlink(link)
|
os.unlink(link)
|
||||||
linktastic.symlink(target, link)
|
linktastic.symlink(target, link)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue