paths.extend(glob.glob(search, recursive=True))
for _file in paths:
- with open(_file, 'rb') as src, gzip.open('{}.gz'.format(_file), 'wb') as dest:
- shutil.copyfileobj(src, dest)
- os.remove(_file)
+ # glob may follow symlink paths that open can't find
+ if os.path.exists(_file):
+ log.debug("Compressing file {}".format(_file))
+ with open(_file, 'rb') as src, gzip.open('{}.gz'.format(_file), 'wb') as dest:
+ shutil.copyfileobj(src, dest)
+ os.remove(_file)
+ else:
+ log.info("Could not open path from glob {}".format(_file))
os.chdir(save_dir)
:arg str pattern: Space-separated list of Unix style glob patterns.
(default: None)
"""
- archives_dir = os.path.join(workspace, 'archives')
+ archives_dir = os.path.join(workspace, "archives")
dest_dir = os.getcwd()
log.debug('Copying files from {} with pattern \'{}\' to {}.'.format(
for file_or_dir in os.listdir(archives_dir):
f = os.path.join(archives_dir, file_or_dir)
try:
- log.debug('Moving {}'.format(f))
- shutil.move(f, dest_dir)
+ log.debug('Copying {}'.format(f))
+ shutil.copy2(f, dest_dir, follow_symlinks=False)
+ except IsADirectoryError:
+ os.makedirs(os.path.join(dest_dir, file_or_dir),
+ exist_ok=True)
except shutil.Error as e:
log.error(e)
- raise OSError(errno.EPERM, 'Could not move to', archives_dir)
+ raise OSError(errno.EPERM, 'Could not copy to', archives_dir)
else:
log.error('Archives dir {} does not exist.'.format(archives_dir))
raise OSError(errno.ENOENT, 'Missing directory', archives_dir)
if len(os.path.basename(src)) > 255:
log.warn('Filename {} is over 255 characters. Skipping...'.format(
os.path.basename(src)))
+ continue
dest = os.path.join(dest_dir, src[len(workspace)+1:])
log.debug('{} -> {}'.format(src, dest))
if os.path.isfile(src):
try:
shutil.move(src, dest)
- except IOError as e: # Switch to FileNotFoundError when Python 2 support is dropped.
+ except FileNotFoundError as e:
log.debug("Missing path, will create it {}.\n{}".format(os.path.dirname(dest), e))
- os.makedirs(os.path.dirname(dest))
+ os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.move(src, dest)
else:
log.info('Not copying directories: {}.'.format(src))
import os
import sys
+import tempfile
import pytest
import requests
assert os.path.exists(os.path.join(stage_dir, 'test.log'))
+@pytest.mark.datafiles(
+ os.path.join(FIXTURE_DIR, 'deploy'),
+ )
+def test_copy_archive_dir_symlinks(cli_runner, datafiles):
+ """Test copy_archives() command with symlink loop and a broken symlink, in
+ order to test that these conditions are handled properly."""
+ os.chdir(str(datafiles))
+ workspace_dir = os.path.join(str(datafiles), 'workspace-symlinks')
+ stage_dir = str(datafiles.mkdir("stage_archive"))
+
+ os.chdir(stage_dir)
+ result = cli_runner.invoke(
+ cli.cli,
+ ['--debug', 'deploy', 'copy-archives', workspace_dir],
+ obj={})
+ assert result.exit_code == 0
+
+ assert os.path.exists(os.path.join(stage_dir, 'test.log'))
+
+
@pytest.mark.datafiles(
os.path.join(FIXTURE_DIR, 'deploy'),
)
)
def test_deploy_archive2(datafiles):
"""Test deploy_archives() command when archives dir is missing."""
- os.chdir(str(datafiles))
+ os.chdir(tempfile.mkdtemp(prefix='lftools-test.'))
workspace_dir = os.path.join(str(datafiles), 'workspace-noarchives')
with pytest.raises(OSError) as excinfo:
)
def test_deploy_archive3(datafiles):
"""Test deploy_archives() command when archives dir is a file instead of a dir."""
- os.chdir(str(datafiles))
+ os.chdir(tempfile.mkdtemp(prefix='lftools-test.'))
workspace_dir = os.path.join(str(datafiles), 'workspace-archivesfile')
with pytest.raises(OSError) as excinfo:
)
def test_deploy_archive4(cli_runner, datafiles, responses):
"""Test deploy_archives() command when using duplicated patterns."""
- os.chdir(str(datafiles))
+ os.chdir(tempfile.mkdtemp(prefix='lftools-test.'))
workspace_dir = os.path.join(str(datafiles), 'workspace-patternfile')
pattern=["**/*.log", "**/hs_err_*.log", "**/target/**/feature.xml", "**/target/failsafe-reports/failsafe-summary.xml", "**/target/surefire-reports/*-output.txt", "**/target/surefire-reports/*-output.txt", "**/target/failsafe-reports/failsafe-summary.xml", "**/*"]
result = deploy_sys.copy_archives(workspace_dir, pattern)