Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if filetype.is_file(test_dir):
test_dir = fileutils.parent_directory(test_dir)
test_dir_path = fileutils.as_posixpath(test_dir)
for top, _, files in os.walk(test_dir):
for f in files:
location = os.path.join(top, f)
locs.append(location)
path = fileutils.as_posixpath(location)
path = path.replace(test_dir_path, '').strip('/')
result.append(path)
assert sorted(expected) == sorted(result)
for location in locs:
assert filetype.is_file(location)
assert not filetype.is_special(location)
assert filetype.is_readable(location)
def is_cargo_toml(location):
return (filetype.is_file(location) and fileutils.file_name(location).lower() == 'cargo.toml')
def checksum(location, name, base64=False):
"""
Return a checksum of `bitsize` length from the content of the file at
`location`. The checksum is a hexdigest or base64-encoded is `base64` is
True.
"""
if not filetype.is_file(location):
return
hasher = _hashmodules_by_name[name]
# fixme: we should read in chunks?
with open(location, 'rb') as f:
hashable = f.read()
hashed = hasher(hashable)
if base64:
return hashed.b64digest()
return hashed.hexdigest()
def is_freebsd_manifest(location):
return (filetype.is_file(location)
and fileutils.file_name(location).lower() == '+compact_manifest')
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 recognize_package(location):
"""
Return a Package object if one was recognized or None for this `location`.
"""
if not filetype.is_file(location):
return
T = contenttype.get_type(location)
ftype = T.filetype_file.lower()
mtype = T.mimetype_file
for package_type in PACKAGE_TYPES:
# Note: default to True if there is nothing to match against
metafiles = package_type.metafiles
if on_linux:
metafiles = (fsencode(m) for m in metafiles)
if location.endswith(tuple(metafiles)):
logger_debug('metafile matching: package_type is of type:', package_type)
return package_type.recognize(location)
if package_type.filetypes:
def get_best_handler(location, kinds=all_kinds):
"""
Return the best handler of None for the file at location.
"""
if on_linux and py2:
location = fileutils.fsencode(location)
location = os.path.abspath(os.path.expanduser(location))
if not filetype.is_file(location):
return
handlers = list(get_handlers(location))
if TRACE_DEEP:
logger.debug('get_best_handler: handlers: %(handlers)r ' % locals())
if handlers:
candidates = score_handlers(handlers)
return candidates and pick_best_handler(candidates, kinds)
def is_data(location, definitions=DATA_TYPE_DEFINITIONS):
"""
Return True isthe file at `location` is a data file.
"""
if on_linux and py2:
location = fileutils.fsencode(location)
if not filetype.is_file(location):
return False
T = get_type(location)
ftype = T.filetype_file.lower()
mtype = T.mimetype_file.lower()
for ddef in definitions:
type_matched = ddef.filetypes and any(t in ftype for t in ddef.filetypes)
mime_matched = ddef.mimetypes and any(m in mtype for m in ddef.mimetypes)
exts = ddef.extensions
if exts:
extension_matched = exts and location.lower().endswith(exts)
if TRACE:
logger_debug('is_data: considering def: %(ddef)r for %(location)s' % locals())
def is_haxelib_json(location):
return (filetype.is_file(location)
and fileutils.file_name(location).lower() == 'haxelib.json')