def _create(self, fixture_file_path, less_verbose=0):
try:
self.write_info('Creating fixture %s' % fixture_file_path, 1+less_verbose)
fixture_file_path = re.sub(r'\.zip$', '', fixture_file_path) # we strip away .zip if given
tmp_dir = tempfile.mkdtemp()
# copy media root
shutil.copytree(self._media_root, join(tmp_dir, 'MEDIA_ROOT'))
# database dump
with open(join(tmp_dir, 'db.sql'), 'w') as fp:
return_code = subprocess.call(['pg_dump', '--clean', '--no-owner', self._database_name], stdout=fp)
if return_code != 0:
raise CommandError('pg_dump failed with exit code {}'.format(return_code))
# creating the fixture archive
archive_name = shutil.make_archive(fixture_file_path, 'zip', root_dir=tmp_dir)
self.write_debug(subprocess.check_output(['unzip', '-l', archive_name]))
except:
self.write_debug('Temporary directory %s kept due to exception.' % tmp_dir)
raise
else:
self.write_info('... fixture created', 1+less_verbose)
shutil.rmtree(tmp_dir)
评论列表
文章目录