Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_7zip_extract_can_extract_to_relative_paths(self):
# The setup is a tad complex because we want to have a relative dir
# to the base dir where we run tests from, ie the scancode-toolkit/ dir
# To use relative paths, we use our tmp dir at the root of the code tree
from os.path import dirname, join, abspath
import tempfile
import shutil
from extractcode.sevenzip import extract
test_file = self.get_test_loc('archive/relative_path/basic.zip')
scancode_root = dirname(dirname(dirname(__file__)))
scancode_tmp = join(scancode_root, 'tmp')
fileutils.create_dir(scancode_tmp)
scancode_root_abs = abspath(scancode_root)
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
test_tgt_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
shutil.copy(test_file, test_src_dir)
test_src_file = join(test_src_dir, 'basic.zip')
result = list(extract(test_src_file, test_tgt_dir))
assert [] == result
expected = ['c/a/a.txt', 'c/b/a.txt', 'c/c/a.txt']
check_files(test_tgt_dir, expected)
def test_libarchive_extract_can_extract_to_relative_paths(self):
# The setup is a tad complex because we want to have a relative dir
# to the base dir where we run tests from, ie the scancode-toolkit/ dir
# To use relative paths, we use our tmp dir at the root of the code tree
from os.path import dirname, join, abspath
import tempfile
import shutil
from extractcode.libarchive2 import extract
test_file = self.get_test_loc('archive/relative_path/basic.zip')
scancode_root = dirname(dirname(dirname(__file__)))
scancode_tmp = join(scancode_root, 'tmp')
fileutils.create_dir(scancode_tmp)
scancode_root_abs = abspath(scancode_root)
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
test_tgt_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
shutil.copy(test_file, test_src_dir)
test_src_file = join(test_src_dir, 'basic.zip')
result = list(extract(test_src_file, test_tgt_dir))
assert [] == result
expected = ['c/a/a.txt', 'c/b/a.txt', 'c/c/a.txt']
check_files(test_tgt_dir, expected)
def test_tree_checksum_ignores_some_files_and_directories(self):
test_dir = self.get_test_loc('cache/tree', copy=True)
before = cache.tree_checksum(test_dir)
# create some new pyc file and a dir
with open(os.path.join(test_dir, 'some.pyc'), 'wb') as pyc:
pyc.write('')
fileutils.create_dir(os.path.join(test_dir, 'some dir'))
after = cache.tree_checksum(test_dir)
assert before == after
with open(os.path.join(test_dir, 'some.py'), 'wb') as py:
py.write(' ')
after = cache.tree_checksum(test_dir)
assert before != after
before = after
with open(os.path.join(test_dir, 'some.LICENSE'), 'wb') as f:
f.write(' ')
after = cache.tree_checksum(test_dir)
assert before != after
before = after
from scancode_config import scancode_root_dir
test_tmp_root_dir = path.join(scancode_root_dir, 'tmp')
# now we add a space in the path for testing path with spaces
test_run_temp_dir = fileutils.get_temp_dir(
base_dir=test_tmp_root_dir, prefix='scancode-tk-tests -')
if on_linux and py2:
test_run_temp_dir = fsencode(test_run_temp_dir)
test_run_temp_subdir = fileutils.get_temp_dir(
base_dir=test_run_temp_dir, prefix='')
if sub_dir_path:
# create a sub directory hierarchy if requested
sub_dir_path = to_os_native_path(sub_dir_path)
test_run_temp_subdir = path.join(test_run_temp_subdir, sub_dir_path)
fileutils.create_dir(test_run_temp_subdir)
return test_run_temp_subdir
def test_extractcode_command_works_with_relative_paths(monkeypatch):
# The setup is a tad complex because we want to have a relative dir
# to the base dir where we run tests from, ie the scancode-toolkit/ dir
# To use relative paths, we use our tmp dir at the root of the code tree
from os.path import dirname, join, abspath
from commoncode import fileutils
import extractcode
import tempfile
import shutil
try:
scancode_root = dirname(dirname(dirname(__file__)))
scancode_tmp = join(scancode_root, 'tmp')
fileutils.create_dir(scancode_tmp)
scancode_root_abs = abspath(scancode_root)
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
test_file = test_env.get_test_loc('extract_relative_path/basic.zip')
shutil.copy(test_file, test_src_dir)
test_src_file = join(test_src_dir, 'basic.zip')
test_tgt_dir = join(scancode_root, test_src_file) + extractcode.EXTRACT_SUFFIX
runner = CliRunner()
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
result = runner.invoke(extract_cli.extractcode, [test_src_file])
assert result.exit_code == 0
assert 'Extracting done' in result.output
assert not 'WARNING' in result.output
assert not 'ERROR' in result.output
expected = ['/c/a/a.txt', '/c/b/a.txt', '/c/c/a.txt']
file_result = [as_posixpath(f.replace(test_tgt_dir, '')) for f in fileutils.resource_iter(test_tgt_dir, with_dirs=False)]
directory = path_to_bytes(directory)
errors = []
for top, _, files in os.walk(directory):
for filename in files:
if not (WIN_PATH_SEP in filename or DOTDOT in filename):
continue
try:
new_path = fileutils.as_posixpath(filename)
new_path = new_path.strip(POSIX_PATH_SEP)
new_path = posixpath.normpath(new_path)
new_path = new_path.replace(DOTDOT, POSIX_PATH_SEP)
new_path = new_path.strip(POSIX_PATH_SEP)
new_path = posixpath.normpath(new_path)
segments = new_path.split(POSIX_PATH_SEP)
directory = os.path.join(top, *segments[:-1])
fileutils.create_dir(directory)
shutil.move(os.path.join(top, filename), os.path.join(top, *segments))
except Exception:
errors.append(os.path.join(top, filename))
return errors
def _get_resource_cache_location(self, rid, create=False):
"""
Return the location where to get/put a Resource in the cache given a
Resource `rid`. Create the directories if requested.
"""
if not self.cache_dir:
return
resid = (b'%08x'if (py2 and on_linux) else '%08x') % rid
cache_sub_dir, cache_file_name = resid[-2:], resid
parent = join(self.cache_dir, cache_sub_dir)
if create and not exists(parent):
create_dir(parent)
return join(parent, cache_file_name)
def setup(self):
"""
Setup the cache: must be called at least once globally after cache
initialization.
"""
create_dir(self.cache_scans_dir)
def get_cached_scan_path(self, path):
"""
Return the path where to store a scan in the cache given a path.
"""
dir1, dir2, file_name = scan_keys(path)
if on_linux:
base_path = path_to_bytes(self.cache_scans_dir)
else:
base_path = path_to_unicode(self.cache_scans_dir)
parent = os.path.join(base_path, dir1, dir2)
create_dir(parent)
return posixpath.join(parent, file_name)