def rm(self, cert, file_id): """Removes a file given its id""" import logging from libviri.objects import File if File.exists(self.db, file_id): logging.info('File {} removed'.format(file_id)) File.delete(self.db, {'file_id': file_id}) return (SUCCESS, 'File successfully removed') else: return (ERROR, 'File not found')
def mv(self, cert, file_id, new_file_name): """Renames file with the given id""" import logging from libviri.objects import File if File.exists(self.db, file_id): logging.info('File {} renamed to {}'.format( file_id, new_file_name)) File.update(self.db, {'file_name': new_file_name}, {'file_id': file_id}) return (SUCCESS, 'File successfully renamed') else: return (ERROR, 'File not found')
def mv(self, cert, file_id, new_file_name): """Renames file with the given id""" import logging from libviri.objects import File if File.exists(self.db, file_id): logging.info('File {} renamed to {}'.format(file_id, new_file_name)) File.update(self.db, {'file_name': new_file_name}, {'file_id': file_id}) return (SUCCESS, 'File successfully renamed') else: return (ERROR, 'File not found')
def ls(self, cert): """List files on the database""" from libviri.objects import File return (SUCCESS, File.query(self.db, fields=('file_name', 'file_id', 'saved'), order=('saved',)))
def ls(self, cert): """List files on the database""" from libviri.objects import File return (SUCCESS, File.query(self.db, fields=('file_name', 'file_id', 'saved'), order=('saved', )))
def get(self, cert, file_name_or_id): """Returns the content of a file""" import xmlrpc.client from libviri.objects import File try: return (SUCCESS, xmlrpc.client.Binary( File.get_content(self.db, file_name_or_id))) except File.Missing: return (ERROR, 'File not found')
def put(self, cert, file_name, file_content): """Receives a script or a data file from viric, and saves it in the viri database. A content hash is used as id, so if the file exists, it's not saved again. Arguments: file_name -- original file name file_content -- content of the file processed using xmlrpc.client.Binary.encode() """ import logging from libviri.objects import File logging.info('File {} saved'.format(file_name)) return (SUCCESS, File.create(self.db, dict(file_name=file_name, content=file_content.data) )['file_id'])
def put(self, cert, file_name, file_content): """Receives a script or a data file from viric, and saves it in the viri database. A content hash is used as id, so if the file exists, it's not saved again. Arguments: file_name -- original file name file_content -- content of the file processed using xmlrpc.client.Binary.encode() """ import logging from libviri.objects import File logging.info('File {} saved'.format(file_name)) return (SUCCESS, File.create( self.db, dict(file_name=file_name, content=file_content.data))['file_id'])
def execute(self, cert, file_name_or_id, args): """Executes a script and returns the script id and the execution result, which can be the return of the script in case of success or the traceback and error message in case of failure. It is possible to execute a script already sent, given its id, or to send the script to execute, using the original script file name and the script (file) content. """ import traceback from libviri.objects import File try: success, res = File.execute(self.db, file_name_or_id, args, self.context, cert) except File.Missing: return (ERROR, 'File not found') except: return (ERROR, traceback.format_exc()) else: return (SUCCESS if success else ERROR, res)
def exists(self, cert, file_id): """Returns True if a file with the given id exists.""" from libviri.objects import File return (SUCCESS, File.exists(self.db, file_id))