Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cleanup_temp(directory):
"""
This function searches for the subdirectory created for extraction and deletes it.
:param directory: The directory to clean.
"""
logger.info('Cleaning up...')
listdir = os.listdir(directory)
if config.EXTRACTION_TEMP_DIR_NAME in listdir:
try:
logger.info('Going to delete {}'.format(os.path.join(directory, config.EXTRACTION_TEMP_DIR_NAME)))
shutil.rmtree(os.path.join(directory, config.EXTRACTION_TEMP_DIR_NAME))
except OSError:
logger.exception('Failed to delete directory {}!'.format(os.path.join(
directory, config.EXTRACTION_TEMP_DIR_NAME)))
def process_directory(directory):
"""
Chooses between copying and moving RARs (to conserve the original torrent files).
:param directory: The directory to process.
"""
torrent_name = os.path.basename(os.path.dirname(directory))
logger.info('Processing directory {} for torrent {}'.format(directory, torrent_name))
# If directory has extracted RARs.
directories_list = os.listdir(directory)
if config.EXTRACTION_TEMP_DIR_NAME in directories_list:
_handle_directory(os.path.join(directory, config.EXTRACTION_TEMP_DIR_NAME), shutil.move, torrent_name)
# If directory has content only.
else:
_handle_directory(directory, shutil.move, torrent_name)
def process_directory(directory):
"""
Chooses between copying and moving RARs (to conserve the original torrent files).
:param directory: The directory to process.
"""
torrent_name = os.path.basename(os.path.dirname(directory))
logger.info('Processing directory {} for torrent {}'.format(directory, torrent_name))
# If directory has extracted RARs.
directories_list = os.listdir(directory)
if config.EXTRACTION_TEMP_DIR_NAME in directories_list:
_handle_directory(os.path.join(directory, config.EXTRACTION_TEMP_DIR_NAME), shutil.move, torrent_name)
# If directory has content only.
else:
_handle_directory(directory, shutil.move, torrent_name)
def extract_all(directory):
"""
recursively extracts all archives in directory.
recursive extraction is iterative and is saved under:
/directory/config.EXTRACTION_TEMP_DIR_NAME/unpacked_{iteration number}
:param directory: The directory to extract archives from.
"""
current_dir = directory
archives_to_extract = _find_target_archives(current_dir)
if len(archives_to_extract) > 0:
iteration = 1
extracted_root = os.path.join(directory, config.EXTRACTION_TEMP_DIR_NAME)
os.mkdir(extracted_root)
while len(archives_to_extract) > 0:
current_dir = os.path.join(extracted_root, 'unpacked_{}'.format(iteration))
os.mkdir(current_dir)
for target_archive in archives_to_extract:
logger.info('Extracting {} to {}'.format(target_archive, current_dir))
_extract(target_archive, current_dir)
iteration += 1
archives_to_extract = _find_target_archives(current_dir)
else:
logger.info('Found no archives in {}!'.format(current_dir))