Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
base_dir = self.get_temp_dir()
self.assertRaises(AssertionError, new_name, '', is_dir=False)
test_file = base_dir + '/'
renamed = new_name(test_file, is_dir=False)
assert renamed
assert not exists(renamed)
test_file = join(base_dir, '.')
renamed = new_name(test_file, is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
test_dir = base_dir + '/'
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert result
test_dir = join(base_dir, '.')
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
test_file = join(base_dir, '.')
renamed = new_name(test_file, is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
test_dir = base_dir + '/'
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert result
test_dir = join(base_dir, '.')
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
renamed = new_name(join(test_dir, 'test.txt'), is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'test_3.txt' == result
renamed = new_name(join(test_dir, 'TEST.txt'), is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'TEST_3.txt' == result
renamed = new_name(join(test_dir, 'TEST.tXt'), is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'TEST_3.tXt' == result
renamed = new_name(join(test_dir, 'test.txt'), is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'test.txt_2' == result
renamed = new_name(join(test_dir, 'teST.txt'), is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'teST.txt_2' == result
def test_new_name_without_extensions(self):
test_dir = self.get_test_loc('new_name/noext', copy=True)
renamed = new_name(join(test_dir, 'test'), is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'test_4' == result
renamed = new_name(join(test_dir, 'TEST'), is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'TEST_4' == result
renamed = new_name(join(test_dir, 'test_1'), is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert 'test_1_1' == result
def test_new_name_with_empties(self):
base_dir = self.get_temp_dir()
self.assertRaises(AssertionError, new_name, '', is_dir=False)
test_file = base_dir + '/'
renamed = new_name(test_file, is_dir=False)
assert renamed
assert not exists(renamed)
test_file = join(base_dir, '.')
renamed = new_name(test_file, is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
test_dir = base_dir + '/'
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert result
def test_new_name_with_empties(self):
base_dir = self.get_temp_dir()
self.assertRaises(AssertionError, new_name, '', is_dir=False)
test_file = base_dir + '/'
renamed = new_name(test_file, is_dir=False)
assert renamed
assert not exists(renamed)
test_file = join(base_dir, '.')
renamed = new_name(test_file, is_dir=False)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
test_dir = base_dir + '/'
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert result
test_dir = join(base_dir, '.')
renamed = new_name(test_dir, is_dir=True)
assert not exists(renamed)
result = fileutils.file_name(renamed)
assert '_' == result
# TODO: also rename directories to a new name if needed segment by segment
dir_path = os.path.join(abs_target_dir, clean_path)
fileutils.create_dir(dir_path)
return dir_path
# note: here isfile=True
try:
# create parent directories if needed
target_path = os.path.join(abs_target_dir, clean_path)
parent_path = os.path.dirname(target_path)
# TODO: also rename directories to a new name if needed segment by segment
fileutils.create_dir(parent_path)
# TODO: return some warning when original path has been renamed?
unique_path = extractcode.new_name(target_path, is_dir=False)
chunk_len = 10240
sbuffer = create_string_buffer(chunk_len)
with open(unique_path, 'wb') as target:
chunk_size = 1
while chunk_size:
chunk_size = read_entry_data(self.archive.archive_struct,
sbuffer, chunk_len)
data = sbuffer.raw[0:chunk_size]
target.write(data)
os.utime(unique_path, (self.time, self.time))
return target_path
except ArchiveWarning, aw:
msg = aw.args and '\n'.join(aw.args) or 'No message provided.'
if msg not in self.warnings: