Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_validation_test(rule, test_name):
"""
Build and return a test function closing on tests arguments.
"""
if py2 and isinstance(test_name, compat.unicode):
test_name = test_name.encode('utf-8')
if py3 and isinstance(test_name, bytes):
test_name = test_name.decode('utf-8')
if rule.is_negative or rule.is_false_positive:
def closure_test_function(*args, **kwargs):
check_special_rule_can_be_detected(rule)
else:
def closure_test_function(*args, **kwargs):
check_rule_or_license_can_be_self_detected_exactly(rule)
check_ignorable_clues(rule)
closure_test_function.__name__ = test_name
closure_test_function.funcname = test_name
return closure_test_function
def path_progress_message(item, verbose=False, prefix='Scanned: '):
"""
Return a styled message suitable for progress display when processing a path
for an `item` tuple of (location, rid, scan_errors, *other items)
"""
if not item:
return ''
location = item[0]
errors = item[2]
location = compat.unicode(toascii(location))
progress_line = location
if not verbose:
max_file_name_len = file_name_max_len()
# do not display a file name in progress bar if there is no space available
if max_file_name_len <= 10:
return ''
progress_line = fixed_width_file_name(location, max_file_name_len)
color = 'red' if errors else 'green'
return style(prefix) + style(progress_line, fg=color)
try:
# FIXME: latin-1 may never fail
s = line.decode('LATIN-1')
except UnicodeDecodeError:
try:
# Convert some byte string to ASCII characters as Unicode including
# replacing accented characters with their non- accented NFKD
# equivalent. Non ISO-Latin and non ASCII characters are stripped
# from the output. Does not preserve the original length offsets.
# For Unicode NFKD equivalence, see:
# http://en.wikipedia.org/wiki/Unicode_equivalence
s = unicodedata.normalize('NFKD', line).encode('ASCII')
except UnicodeDecodeError:
try:
enc = chardet.detect(line)['encoding']
s = compat.unicode(line, enc)
except UnicodeDecodeError:
# fall-back to strings extraction if all else fails
s = strings.string_from_string(s)
return s
POSIX_PATH_SEP = b'/'
WIN_PATH_SEP = b'\\'
EMPTY_STRING = b''
DOT = b'.'
PATH_SEP = bytes(os.sep)
PATH_ENV_VAR = b'PATH'
PATH_ENV_SEP = bytes(os.pathsep)
else:
PATH_TYPE = compat.unicode
POSIX_PATH_SEP = '/'
WIN_PATH_SEP = '\\'
EMPTY_STRING = ''
DOT = '.'
PATH_SEP = compat.unicode(os.sep)
PATH_ENV_VAR = 'PATH'
PATH_ENV_SEP = compat.unicode(os.pathsep)
ALL_SEPS = POSIX_PATH_SEP + WIN_PATH_SEP
"""
File, paths and directory utility functions.
"""
#
# DIRECTORIES
#
def create_dir(location):
"""
Create directory and all sub-directories recursively at location ensuring these
are readable and writeable.
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, compat.string_types) and a or repr(a) for a in args))
def fingerprint(self):
key = self.key
if not isinstance(key, compat.unicode):
key = unidecode(key)
fp = fingerprints.generate(key)
if TRACE_TEXT or TRACE_FP:
logger_debug('Text.fingerprint:key: ', repr(self.key))
logger_debug('Text.fingerprint:fp : ', fingerprints.generate(unidecode(self.key)))
self.key = fp
def prepare_path(pth):
"""
Return the `pth` path string either as encoded bytes if on Linux and using
Python 2 or as a unicode/text otherwise.
"""
if on_linux and py2:
if not isinstance(pth, bytes):
pth = fsencode(pth)
return pth
else:
if not isinstance(pth, compat.unicode):
return fsdecode(pth)
return pth
def check_error(result, func, args): # NOQA
"""
ctypes error handler/checker: Check for errors and raise an exception or
return the result otherwise.
"""
is_int = isinstance(result, int)
is_bytes = isinstance(result, bytes)
is_text = isinstance(result, compat.unicode)
if (result is None
or (is_int and result < 0)
or (is_bytes and compat.unicode(result, encoding='utf-8').startswith('cannot open'))
or (is_text and result.startswith('cannot open'))):
err = _magic_error(args[0])
raise MagicException(err)
else:
return result
def display_extract_summary():
"""
Display a summary of warnings and errors if any.
"""
has_warnings = False
has_errors = False
summary = []
for xev in extract_results:
has_errors = has_errors or bool(xev.errors)
has_warnings = has_warnings or bool(xev.warnings)
source = fileutils.as_posixpath(xev.source)
if not isinstance(source, compat.unicode):
source = toascii(source, translit=True).decode('utf-8', 'replace')
source = get_relative_path(path=source, len_base_path=len_base_path, base_is_dir=base_is_dir)
for e in xev.errors:
echo_stderr('ERROR extracting: %(source)s: %(e)s' % locals(), fg='red')
for warn in xev.warnings:
echo_stderr('WARNING extracting: %(source)s: %(warn)s' % locals(), fg='yellow')
summary_color = 'green'
if has_warnings:
summary_color = 'yellow'
if has_errors:
summary_color = 'red'
echo_stderr('Extracting done.', fg=summary_color, reset=True)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, compat.string_types) and a or repr(a) for a in args))