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_parent_directory_on_path_and_location_8(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'f.a/a.c'
expected_name = 'f.a/'
result = fileutils.parent_directory(test_file)
result = fileutils.as_posixpath(result)
assert expected_name == result
result = fileutils.parent_directory((os.path.join(test_dir, test_file)))
result = fileutils.as_posixpath(result)
assert result.endswith(expected_name)
def test_parent_directory_on_path_and_location_6(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'a/f.a'
expected_name = 'a/'
result = fileutils.parent_directory(test_file)
result = fileutils.as_posixpath(result)
assert expected_name == result
result = fileutils.parent_directory((os.path.join(test_dir, test_file)))
result = fileutils.as_posixpath(result)
assert result.endswith(expected_name)
def test_virtual_codebase_cache_default(self):
scan_data = self.get_test_loc('resource/virtual_codebase/cache2.json')
virtual_codebase = VirtualCodebase(location=scan_data)
assert virtual_codebase.temp_dir
assert virtual_codebase.cache_dir
virtual_codebase.cache_dir
root = virtual_codebase.root
cp = virtual_codebase._get_resource_cache_location(root.rid, create=False)
assert not exists(cp)
cp = virtual_codebase._get_resource_cache_location(root.rid, create=True)
assert not exists(cp)
assert exists(parent_directory(cp))
child = virtual_codebase._create_resource('child', root, is_file=True)
child.size = 12
virtual_codebase.save_resource(child)
child_2 = virtual_codebase.get_resource(child.rid)
assert child == child_2
def test_parent_directory_on_path_and_location_9(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'f.a/'
expected_name = '/'
result = fileutils.parent_directory(test_file)
result = fileutils.as_posixpath(result)
assert expected_name == result
result = fileutils.parent_directory((os.path.join(test_dir, test_file)))
result = fileutils.as_posixpath(result)
assert result.endswith(expected_name)
def recognize(cls, location):
if not cls._is_build_manifest(location):
return
# we use the parent directory as a name
name = fileutils.file_name(fileutils.parent_directory(location))
# we could use checksums as version in the future
version = None
# there is an optional array of license file names in targets that we could use
# declared_license = None
# there is are dependencies we could use
# dependencies = []
yield cls(
name=name,
version=version)
the provided `template` identifier. The template defaults to the standard HTML
template format or can point to the path of a custom template file.
"""
# FIXME: This code is highly coupled with actual scans and may not
# support adding new scans at all
from licensedcode.cache import get_licenses_db
# FIXME: factor out the html vs custom from this function: we should get a template path
if template == 'html':
template = get_template(get_template_dir('html'))
else:
# load a custom template
tpath = fileutils.as_posixpath(os.path.abspath(os.path.expanduser(template)))
assert os.path.isfile(tpath)
tdir = fileutils.parent_directory(tpath)
tfile = fileutils.file_name(tpath)
template = get_template(tdir, tfile)
converted = OrderedDict()
converted_infos = OrderedDict()
converted_packages = OrderedDict()
licenses = {}
LICENSES = 'licenses'
COPYRIGHTS = 'copyrights'
PACKAGES = 'packages'
URLS = 'urls'
EMAILS = 'emails'
# Create a flattened data dict keyed by path
for scanned_file in scanned_files:
def is_metadata_json(location):
"""
Return True if `location` path is for a Chef metadata.json file.
The metadata.json is also used in Python installed packages in a 'dist-info'
directory.
"""
return (
filetype.is_file(location)
and fileutils.file_name(location).lower() == 'metadata.json'
and not fileutils.file_name(fileutils.parent_directory(location))
.lower().endswith('dist-info')
)
def parse_metadata(location):
"""
Return a Package object from the Python wheel 'metadata.json' file
at 'location' or None. Check if the parent directory of 'location'
contains both a 'METADATA' and a 'DESCRIPTION.rst' file to ensure
this is a proper metadata.json file.
"""
if not location or not location.endswith('metadata.json'):
if TRACE: logger_debug('parse_metadata: not metadata.json:', location)
return
parent_dir = fileutils.parent_directory(location)
# FIXME: is the absence of these two files a show stopper?
paths = [os.path.join(parent_dir, n) for n in ('METADATA', 'DESCRIPTION.rst')]
if not all(os.path.exists(p) for p in paths):
if TRACE: logger_debug('parse_metadata: not extra paths', paths)
return
with open(location, 'rb') as infs:
infos = json.load(infs, object_pairs_hook=OrderedDict)
extensions = infos.get('extensions')
if TRACE: logger_debug('parse_metadata: extensions:', extensions)
details = extensions and extensions.get('python.details')
urls = details and details.get('project_urls')
homepage_url = urls and urls.get('Home')
parties = []
name = args.get('name')
if not name:
continue
license_files = args.get('licenses')
yield BuckPackage(
name=name,
declared_license=license_files,
root_path=fileutils.parent_directory(location)
)
else:
# If we don't find anything in the BUCK file, we yield a Package with
# the parent directory as the name, like the default implementation of
# `recognize()` for `BaseBuildManifestPackage`
yield BuckPackage(
# we use the parent directory as a name
name=fileutils.file_name(fileutils.parent_directory(location))
)
def _get_or_create_parent(self, path, parent_by_path):
"""
Return a parent resource for a given `path` from `parent_by_path`.
If a parent resource for a `path` does not exist in `parent_by_path`, it is created recursively.
Note: the root path and root Resource must already be in `parent_by_path` or else this
function does not work.
"""
parent_path = parent_directory(path).rstrip('/').rstrip('\\')
existing_parent = parent_by_path.get(parent_path)
if existing_parent:
return existing_parent
parent_parent = self._get_or_create_parent(parent_path, parent_by_path)
parent_name = file_base_name(parent_path)
parent_is_file = False
parent_resource_data = self._create_empty_resource_data()
parent_resource = self._create_resource(parent_name, parent_parent, parent_is_file, parent_path, parent_resource_data)
parent_by_path[parent_path] = parent_resource
return parent_resource