def _process(self, document_version): if self.command_exiftool: temporary_folder = mkdtemp() path_temporary_file = Path(temporary_folder, document_version.document.label) try: with path_temporary_file.open( mode='xb') as temporary_fileobject: document_version.save_to_file( file_object=temporary_fileobject) temporary_fileobject.seek(0) try: result = self.command_exiftool( temporary_fileobject.name) except sh.ErrorReturnCode_1 as exception: result = json.loads(s=exception.stdout)[0] if result.get('Error', '') == 'Unknown file type': # Not a fatal error return result else: return json.loads(s=result.stdout)[0] except Exception as exception: logger.error('Error processing document version: %s; %s', document_version, exception) raise finally: fs_cleanup(filename=str(path_temporary_file)) else: logger.warning( 'EXIFTool binary not found, not processing document ' 'version: %s', document_version)
def test_config_backup_creation_no_tags(self): path_config_backup = Path(settings.CONFIGURATION_LAST_GOOD_FILEPATH) fs_cleanup(filename=force_text(s=path_config_backup)) Setting.save_last_known_good() self.assertTrue(path_config_backup.exists()) with path_config_backup.open(mode='r') as file_object: self.assertFalse('!!python/' in file_object.read())
def test_document_empty(self): self._create_test_index() self.test_index.node_templates.create( parent=self.test_index.template_root, expression=TEST_NODE_EXPRESSION, link_documents=True) index_filesystem = IndexFilesystem(index_slug=self.test_index.slug) self._upload_test_document() fs_cleanup(filename=self.test_document.latest_version.file.file.name) self.assertEqual( index_filesystem.getattr(path='/{}/{}'.format( TEST_NODE_EXPRESSION, self.test_document.label))['st_size'], 0)
def handle(self, *app_labels, **options): # Create the media/convertdb folder convertdb_folder_path = force_text( Path( settings.MEDIA_ROOT, CONVERTDB_FOLDER ) ) try: os.makedirs(convertdb_folder_path) except OSError as exception: if exception.errno == errno.EEXIST: pass convertdb_file_path = force_text( Path( convertdb_folder_path, CONVERTDB_OUTPUT_FILENAME ) ) management.call_command(command_name='purgeperiodictasks') management.call_command( 'dumpdata', *app_labels, all=True, database=options['from'], natural_primary=True, natural_foreign=True, output=convertdb_file_path, interactive=False, format='json' ) if DocumentType.objects.using(options['to']).count() and not options['force']: fs_cleanup(convertdb_file_path) raise CommandError( 'There is existing data in the database that will be ' 'used for the import. If you proceed with the conversion ' 'you might lose data. Please check your settings.' ) management.call_command( 'loaddata', convertdb_file_path, database=options['to'], interactive=False, verbosity=3 ) fs_cleanup(convertdb_file_path)
def tearDown(self): fs_cleanup(self.temporary_directory) super(StagingFolderViewTestCase, self).tearDown()
def soffice(self): """ Executes LibreOffice as a sub process """ if not self.command_libreoffice: raise OfficeConversionError( _('LibreOffice not installed or not found.')) with NamedTemporaryFile() as temporary_file_object: # Copy the source file object of the converter instance to a # named temporary file to be able to pass it to the LibreOffice # execution. self.file_object.seek(0) shutil.copyfileobj(fsrc=self.file_object, fdst=temporary_file_object) self.file_object.seek(0) temporary_file_object.seek(0) libreoffice_home_directory = mkdtemp() args = ( temporary_file_object.name, '--outdir', setting_temporary_directory.value, '-env:UserInstallation=file://{}'.format( os.path.join(libreoffice_home_directory, 'LibreOffice_Conversion')), ) kwargs = {'_env': {'HOME': libreoffice_home_directory}} if self.mime_type == 'text/plain': kwargs.update({'infilter': 'Text (encoded):UTF8,LF,,,'}) try: self.command_libreoffice(*args, **kwargs) except sh.ErrorReturnCode as exception: temporary_file_object.close() raise OfficeConversionError(exception) except Exception as exception: temporary_file_object.close() logger.error('Exception launching Libre Office; %s', exception) raise finally: fs_cleanup(filename=libreoffice_home_directory) # LibreOffice return a PDF file with the same name as the input # provided but with the .pdf extension. # Get the converted output file path out of the temporary file # name plus the temporary directory filename, extension = os.path.splitext( os.path.basename(temporary_file_object.name)) logger.debug('filename: %s', filename) logger.debug('extension: %s', extension) converted_file_path = os.path.join( setting_temporary_directory.value, os.path.extsep.join((filename, 'pdf'))) logger.debug('converted_file_path: %s', converted_file_path) # Don't use context manager with the NamedTemporaryFile on purpose # so that it is deleted when the caller closes the file and not # before. temporary_converted_file_object = NamedTemporaryFile() # Copy the LibreOffice output file to a new named temporary file # and delete the converted file with open(converted_file_path, mode='rb') as converted_file_object: shutil.copyfileobj(fsrc=converted_file_object, fdst=temporary_converted_file_object) fs_cleanup(filename=converted_file_path) temporary_converted_file_object.seek(0) return temporary_converted_file_object
def tearDown(self): fs_cleanup(filename=self.temporary_directory) super().tearDown()
def tearDown(self): fs_cleanup(filename=self.temporary_directory) super(ZipCompressedPassthroughStorageTestCase, self).tearDown()
def test_config_backup_creation(self): path_config_backup = Path(settings.CONFIGURATION_LAST_GOOD_FILEPATH) fs_cleanup(filename=force_text(s=path_config_backup)) Setting.save_last_known_good() self.assertTrue(path_config_backup.exists())
def tearDown(self): fs_cleanup( filename=setting_backend_arguments.value['index_path'] ) setting_backend_arguments.set(value=self.old_value) super().tearDown()
def tearDown(self): fs_cleanup(filename=self.temporary_directory) super(CacheTestMixin, self).tearDown()
def tearDown(self): for test_staging_folder in self.test_staging_folders: fs_cleanup(filename=test_staging_folder.folder_path) self.test_staging_folders.remove(test_staging_folder) super(StagingFolderAPIViewTestMixin, self).tearDown()
def tearDown(self): if self.test_config_file_object: fs_cleanup(filename=self.test_config_file_object.name) super(SmartSettingTestMixin, self).tearDown()
def tearDown(self): fs_cleanup(filename=self.test_setting_config_file_object.name) SettingNamespace.invalidate_cache_all() super(SmartSettingsTestCaseMixin, self).tearDown()
def tearDown(self): fs_cleanup(filename=self.test_setting_config_file_object.name) super(SmartSettingsTestCaseMixin, self).tearDown()