Skip to content

Commit

Permalink
Only archive fastresume and torrent files)
Browse files Browse the repository at this point in the history
  • Loading branch information
jslay88 committed Feb 21, 2022
1 parent b378cc0 commit d2ceb2a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
10 changes: 5 additions & 5 deletions qbt_migrate/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def convert_slashes(path: str, target_os: str):
return path.replace('\\', '/')


def backup_folder(folder_path: Union[str, os.PathLike], archive_path: Union[str, os.PathLike]):
def backup_folder(folder_path: Union[str, os.PathLike], archive_path: Union[str, os.PathLike],
include_torrents: bool = True):
logger.info(f'Creating Archive {archive_path} ...')
folder_path = Path(folder_path)
archive_path = Path(archive_path)
with zipfile.ZipFile(archive_path, 'w') as archive:
for file in folder_path.iterdir():
if file == archive_path:
continue
logger.debug(f'Archiving {file} into {archive_path}...')
archive.write(file)
if file.name.endswith('.fastresume') or (include_torrents and file.name.endswith('.torrent')):
logger.debug(f'Archiving {file} into {archive_path}...')
archive.write(file)
logger.info('Done!')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='qbt_migrate',
version='2.1.3',
version='2.1.4',
packages=find_packages(),
install_requires=dependencies,
description='Migrate qBittorrent FastResume files.',
Expand Down
39 changes: 33 additions & 6 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,50 @@

def test_backup_folder():
temp_dir = Path(tempfile.mkdtemp()) # Get a tmp dir
file_count = 20 # Number of test files to build
fastresume_file_count = 20 # Number of test fastresume files to build
torrent_file_count = 20 # Number of test torrent files to build
invalid_file_count = 20 # Number of invalid files to build

# Build test files
for x in range(file_count):
with open(temp_dir / f'test_{x}.txt', 'w') as f:
for x in range(fastresume_file_count):
with open(temp_dir / f'test_{x}.fastresume', 'w') as f:
f.write(f'This is file {x}')

for x in range(torrent_file_count):
with open(temp_dir / f'test_{x}.torrent', 'w') as f:
f.write(f'This is file {x}')

for x in range(invalid_file_count):
with open(temp_dir / f'test_{x}.invalid', 'w') as f:
f.write(f'This is invalid file {x}')

# 'test.zip' will fail the filename check in archive if it gets archived
archive_path = temp_dir / 'test.zip'
backup_folder(temp_dir, archive_path) # Call backup_folder
backup_folder(temp_dir, archive_path) # Call backup_folder with torrent files included

# Validate zip file
with zipfile.ZipFile(archive_path, 'r') as archive:
assert(archive.testzip() is None) # Check no errors
assert(len(archive.infolist()) == file_count) # Assert that it has the correct file count
# Assert that it has the correct file count
assert(len(archive.infolist()) == fastresume_file_count + torrent_file_count)
for file in archive.infolist():
filename = re.findall(r'test_(\d+).txt', file.filename) # Get the integer of the file
# Get the integer of the file
filename = re.findall(r'test_(\d+).(?:fastresume)?(?:torrent)?', file.filename)
assert(len(filename) != 0) # Ensure we got the integer
filename = filename[0]
assert(archive.read(file.filename) == bytes(f'This is file {filename}', 'utf-8')) # Integrity Check

archive_path = temp_dir / 'test2.zip'
backup_folder(temp_dir, archive_path, False)

# Validate zip file
with zipfile.ZipFile(archive_path, 'r') as archive:
assert (archive.testzip() is None) # Check no errors
# Assert that it has the correct file count
assert (len(archive.infolist()) == fastresume_file_count)
for file in archive.infolist():
# Get the integer of the file
filename = re.findall(r'test_(\d+).fastresume', file.filename)
assert (len(filename) != 0) # Ensure we got the integer
filename = filename[0]
assert (archive.read(file.filename) == bytes(f'This is file {filename}', 'utf-8')) # Integrity Check

0 comments on commit d2ceb2a

Please sign in to comment.