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_file_name_on_path_and_location_1(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'a/.a/file'
expected_name = 'file'
result = fileutils.file_name(test_file)
assert expected_name == result
result = fileutils.file_name((os.path.join(test_dir, test_file)))
assert expected_name == result
def test_file_name_on_path_and_location_3(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'a/b/.a.b'
expected_name = '.a.b'
result = fileutils.file_name(test_file)
assert expected_name == result
result = fileutils.file_name((os.path.join(test_dir, test_file)))
assert expected_name == result
def is_metadata_rb(location):
return (filetype.is_file(location)
and fileutils.file_name(location).lower() == 'metadata.rb')
def _is_build_manifest(cls, location):
if not filetype.is_file(location):
return False
fn = fileutils.file_name(location)
return any(fn == mf for mf in cls.metafiles)
def dwarf_source_path(location):
"""
Collect unique paths to compiled source code found in Elf binaries DWARF
sections for D2D.
"""
location = location
T = contenttype.get_type(location)
if not (T.is_elf or T.is_stripped_elf):
return
seen_paths = set()
path_file_names = set()
bare_file_names = set()
for dpath in chain(get_dwarf1(location), get_dwarf2(location)):
if dpath in seen_paths:
continue
fn = fileutils.file_name(dpath)
if fn == dpath:
bare_file_names.add(fn)
continue
else:
path_file_names.add(fn)
seen_paths.add(dpath)
yield dpath
# only yield filename that do not exist as full paths
for bfn in sorted(bare_file_names):
if bfn not in path_file_names and bfn not in seen_paths:
yield bfn
seen_paths.add(bfn)
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 __attrs_post_init__(self, *args, **kwargs):
if not self.text_file:
# for SPDX or tests only
if not self.stored_text :
raise Exception('Invalid rule without its corresponding text file: {}'.format(self))
self.identifier = '_tst_' + str(len(self.stored_text))
else:
self.identifier = file_name(self.text_file)
if self.data_file:
try:
self.load()
except Exception as e:
data_file = self.data_file
trace = traceback.format_exc()
message = 'While loading: file://{data_file}\n{trace}'.format(**locals())
raise Exception(message)
if self.relevance and self.relevance != 100:
self.has_stored_relevance = True
if self.minimum_coverage:
self.has_stored_minimum_coverage = True
root_is_file = False
root_data = self._create_empty_resource_data()
root_resource = self._create_root_resource(root_name, root_path, root_is_file, root_data)
# To help recreate the resource tree we keep a mapping by path of any
# parent resource
parent_by_path = {root_path: root_resource}
for resource_data in resources_data:
path = resource_data.get('path')
# Append virtual_root path to imported Resource path if we are merging multiple scans
if multiple_input:
path = os.path.join(root_path, path)
name = resource_data.get('name', None)
if not name:
name = file_name(path)
is_file = resource_data.get('type', 'file') == 'file'
existing_parent = parent_by_path.get(path)
if existing_parent:
# We update the empty parent Resouorce we in _get_or_create_parent() with the data
# from the scan
for k, v in resource_data.items():
setattr(existing_parent, k, v)
self.save_resource(existing_parent)
else:
# Note: `root_path`: `root_resource` must be in `parent_by_path` in order for
# `_get_or_create_parent` to work
parent = self._get_or_create_parent(path, parent_by_path)
resource = self._create_resource(name, parent, is_file, path, resource_data)
# Files are not parents (for now), so we do not need to add this
* pad a directory name with _X where X is an incremented number.
* pad a file base name with _X where X is an incremented number and keep
the extension unchanged.
"""
assert location
if on_linux:
location = path_to_bytes(location)
location = location.rstrip(PATHS_SEPS)
assert location
parent = fileutils.parent_directory(location)
# all existing files or directory as lower case
siblings_lower = set(s.lower() for s in os.listdir(parent))
filename = fileutils.file_name(location)
# corner case
if filename in (DOT, DOT):
filename = UNDERSCORE
# if unique, return this
if filename.lower() not in siblings_lower:
return os.path.join(parent, filename)
# otherwise seek a unique name
if is_dir:
# directories do not have an "extension"
base_name = filename
ext = EMPTY_STRING
else:
base_name, dot, ext = filename.partition(DOT)