mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-07-30 03:28:26 -07:00
Refactor links to utils.links
This commit is contained in:
parent
5c644890e8
commit
a14a286a8e
2 changed files with 86 additions and 76 deletions
|
@ -11,15 +11,15 @@ import time
|
||||||
from babelfish import Language
|
from babelfish import Language
|
||||||
import beets
|
import beets
|
||||||
import guessit
|
import guessit
|
||||||
import linktastic
|
|
||||||
import requests
|
import requests
|
||||||
from six import text_type
|
from six import text_type
|
||||||
import subliminal
|
import subliminal
|
||||||
|
|
||||||
import core
|
import core
|
||||||
from core import extractor, logger
|
from core import extractor, logger
|
||||||
from core.utils.download_info import get_download_info, update_download_info_status
|
|
||||||
from core.utils import shutil_custom
|
from core.utils import shutil_custom
|
||||||
|
from core.utils.download_info import get_download_info, update_download_info_status
|
||||||
|
from core.utils.links import copy_link, replace_links
|
||||||
from core.utils.network import test_connection, wake_on_lan, wake_up
|
from core.utils.network import test_connection, wake_on_lan, wake_up
|
||||||
from core.utils.parsers import (
|
from core.utils.parsers import (
|
||||||
parse_args,
|
parse_args,
|
||||||
|
@ -194,80 +194,6 @@ def is_sample(input_name):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def copy_link(src, target_link, use_link):
|
|
||||||
logger.info('MEDIAFILE: [{0}]'.format(os.path.basename(target_link)), 'COPYLINK')
|
|
||||||
logger.info('SOURCE FOLDER: [{0}]'.format(os.path.dirname(src)), 'COPYLINK')
|
|
||||||
logger.info('TARGET FOLDER: [{0}]'.format(os.path.dirname(target_link)), 'COPYLINK')
|
|
||||||
|
|
||||||
if src != target_link and os.path.exists(target_link):
|
|
||||||
logger.info('MEDIAFILE already exists in the TARGET folder, skipping ...', 'COPYLINK')
|
|
||||||
return True
|
|
||||||
elif src == target_link and os.path.isfile(target_link) and os.path.isfile(src):
|
|
||||||
logger.info('SOURCE AND TARGET files are the same, skipping ...', 'COPYLINK')
|
|
||||||
return True
|
|
||||||
elif src == os.path.dirname(target_link):
|
|
||||||
logger.info('SOURCE AND TARGET folders are the same, skipping ...', 'COPYLINK')
|
|
||||||
return True
|
|
||||||
|
|
||||||
make_dir(os.path.dirname(target_link))
|
|
||||||
try:
|
|
||||||
if use_link == 'dir':
|
|
||||||
logger.info('Directory linking SOURCE FOLDER -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
linktastic.dirlink(src, target_link)
|
|
||||||
return True
|
|
||||||
if use_link == 'junction':
|
|
||||||
logger.info('Directory junction linking SOURCE FOLDER -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
linktastic.dirlink(src, target_link)
|
|
||||||
return True
|
|
||||||
elif use_link == 'hard':
|
|
||||||
logger.info('Hard linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
linktastic.link(src, target_link)
|
|
||||||
return True
|
|
||||||
elif use_link == 'sym':
|
|
||||||
logger.info('Sym linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
linktastic.symlink(src, target_link)
|
|
||||||
return True
|
|
||||||
elif use_link == 'move-sym':
|
|
||||||
logger.info('Sym linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
shutil.move(src, target_link)
|
|
||||||
linktastic.symlink(target_link, src)
|
|
||||||
return True
|
|
||||||
elif use_link == 'move':
|
|
||||||
logger.info('Moving SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
shutil.move(src, target_link)
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning('Error: {0}, copying instead ... '.format(e), 'COPYLINK')
|
|
||||||
|
|
||||||
logger.info('Copying SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
|
||||||
shutil.copy(src, target_link)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def replace_links(link):
|
|
||||||
n = 0
|
|
||||||
target = link
|
|
||||||
if os.name == 'nt':
|
|
||||||
if not jaraco.windows.filesystem.islink(link):
|
|
||||||
logger.debug('{0} is not a link'.format(link))
|
|
||||||
return
|
|
||||||
while jaraco.windows.filesystem.islink(target):
|
|
||||||
target = jaraco.windows.filesystem.readlink(target)
|
|
||||||
n = n + 1
|
|
||||||
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')
|
|
||||||
os.unlink(link)
|
|
||||||
linktastic.symlink(target, link)
|
|
||||||
|
|
||||||
|
|
||||||
def flatten(output_destination):
|
def flatten(output_destination):
|
||||||
logger.info('FLATTEN: Flattening directory: {0}'.format(output_destination))
|
logger.info('FLATTEN: Flattening directory: {0}'.format(output_destination))
|
||||||
for outputFile in list_media_files(output_destination):
|
for outputFile in list_media_files(output_destination):
|
||||||
|
|
84
core/utils/links.py
Normal file
84
core/utils/links.py
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
import linktastic
|
||||||
|
|
||||||
|
from core import logger
|
||||||
|
from core.utils.paths import make_dir
|
||||||
|
|
||||||
|
if os.name == 'nt':
|
||||||
|
import jaraco
|
||||||
|
|
||||||
|
|
||||||
|
def copy_link(src, target_link, use_link):
|
||||||
|
logger.info('MEDIAFILE: [{0}]'.format(os.path.basename(target_link)), 'COPYLINK')
|
||||||
|
logger.info('SOURCE FOLDER: [{0}]'.format(os.path.dirname(src)), 'COPYLINK')
|
||||||
|
logger.info('TARGET FOLDER: [{0}]'.format(os.path.dirname(target_link)), 'COPYLINK')
|
||||||
|
|
||||||
|
if src != target_link and os.path.exists(target_link):
|
||||||
|
logger.info('MEDIAFILE already exists in the TARGET folder, skipping ...', 'COPYLINK')
|
||||||
|
return True
|
||||||
|
elif src == target_link and os.path.isfile(target_link) and os.path.isfile(src):
|
||||||
|
logger.info('SOURCE AND TARGET files are the same, skipping ...', 'COPYLINK')
|
||||||
|
return True
|
||||||
|
elif src == os.path.dirname(target_link):
|
||||||
|
logger.info('SOURCE AND TARGET folders are the same, skipping ...', 'COPYLINK')
|
||||||
|
return True
|
||||||
|
|
||||||
|
make_dir(os.path.dirname(target_link))
|
||||||
|
try:
|
||||||
|
if use_link == 'dir':
|
||||||
|
logger.info('Directory linking SOURCE FOLDER -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
linktastic.dirlink(src, target_link)
|
||||||
|
return True
|
||||||
|
if use_link == 'junction':
|
||||||
|
logger.info('Directory junction linking SOURCE FOLDER -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
linktastic.dirlink(src, target_link)
|
||||||
|
return True
|
||||||
|
elif use_link == 'hard':
|
||||||
|
logger.info('Hard linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
linktastic.link(src, target_link)
|
||||||
|
return True
|
||||||
|
elif use_link == 'sym':
|
||||||
|
logger.info('Sym linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
linktastic.symlink(src, target_link)
|
||||||
|
return True
|
||||||
|
elif use_link == 'move-sym':
|
||||||
|
logger.info('Sym linking SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
shutil.move(src, target_link)
|
||||||
|
linktastic.symlink(target_link, src)
|
||||||
|
return True
|
||||||
|
elif use_link == 'move':
|
||||||
|
logger.info('Moving SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
shutil.move(src, target_link)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Error: {0}, copying instead ... '.format(e), 'COPYLINK')
|
||||||
|
|
||||||
|
logger.info('Copying SOURCE MEDIAFILE -> TARGET FOLDER', 'COPYLINK')
|
||||||
|
shutil.copy(src, target_link)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def replace_links(link):
|
||||||
|
n = 0
|
||||||
|
target = link
|
||||||
|
if os.name == 'nt':
|
||||||
|
if not jaraco.windows.filesystem.islink(link):
|
||||||
|
logger.debug('{0} is not a link'.format(link))
|
||||||
|
return
|
||||||
|
while jaraco.windows.filesystem.islink(target):
|
||||||
|
target = jaraco.windows.filesystem.readlink(target)
|
||||||
|
n = n + 1
|
||||||
|
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')
|
||||||
|
os.unlink(link)
|
||||||
|
linktastic.symlink(target, link)
|
Loading…
Add table
Add a link
Reference in a new issue