Пример #1
0
 def upload(self, filename, data, crc, compressed = False):
     """This method uploads a file to the server.
     
     @param filename: file name of the file to download.
     @param data: the contents of the file being uploaded in base64 format.
     @param crc: the CRC-16-IBM (CRC16) Cyclic Redundancy Check for the data.
     @param compressed: if True the data will be decompressed with zlib.
     
     """
     
     # decode the base64 encoded string
     data_ = base64.b64decode(data)
     # decompress the data if needed
     if compressed:
         data_ = zlib.decompress(data)
     
     # make sure we recieved what we were expecting by computing the crc value
     if crc16(data_) != crc:
         self.log.debug('FileTransfer.upload: crc values did not match for request %s' % filename)
         raise JSONRPCAssertionError('crc value does not match')
     
     cfg = getConfig(file)
     ul_plugins = dict([[x.__class__.__name__, x] for x in self.upload_plugins])
     for plugin in cfg['plugins']:
         if ul_plugins.has_key(plugin):
             plugin = ul_plugins[plugin]
             if plugin.handles(cfg['head'], cfg['tail']):
                 plugin.upload(cfg['head'], cfg['tail'], data_, cfg['rootpath'])
                 break
         else:
             self.log.debug("FileTransfer.upload: Skipping plugin %s, not found" % plugin)
     else:   # default upload handling, just save the data
         path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
         if not os.path.exists(path):
             raise ApplicationError('IOError: No such file or directory: %s' % filename)
         if not os.isfile(path):
             raise ApplicationError('IOError: %s is a directory' % filename)
         
         f = open(path, 'wb')
         f.write(data_)
         f.close()
Пример #2
0
    def description(self, update):
        """Returns the description of a given update.
        
        @param update: The name of the update.
        @return: The updates description or an empty string if no description is given.
        
        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)

        return meta.get('description', '')
Пример #3
0
    def timestamp(self, update):
        """Returns the creation timestamp of a given update.
        
        @param update: The name of the update
        @return: A float with the updates timestamp in seconds since Epoch (January 1, 1970).
        
        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)

        return meta['timestamp']
Пример #4
0
    def versionString(self, update):
        """Returns the string representation of the version of a given update.
        
        @param update: The name of the update.
        @return: A string containing the version of the update.
        
        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)
        else:
            return '%d.%d' % tuple(meta['version'])\
Пример #5
0
 def download(self, file, compress = False):
     """This method is used to request a file from the server.
     
     @param file: path of the file to download.
     @param compress: if True the contents of the file will be compressed
         with zlib before sending.
     @return: a JSON-RPC Object with keys data, crc, and compressed.
     
     """
     
     cfg = getConfig(file)
     dl_plugins = dict([[x.__class__.__name__, x] for x in self.download_plugins])
     for plugin in cfg['plugins']:
         if dl_plugins.has_key(plugin):
             plugin = dl_plugins[plugin]
             if plugin.handles(cfg['head'], cfg['tail']):
                 data = plugin.download(cfg['head'], cfg['tail'], cfg['rootpath'])
                 break
         else:
             self.log.debug("FileTransfer.download: Skipping plugin %s, not found" % plugin)
     else:   # default download handling, just send the file
         path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
         if not os.path.exists(path):
             raise ApplicationError('IOError: No such file or directory: %s' % file)
         if not os.isfile(path):
             raise ApplicationError('IOError: %s is a directory' % file)
         data = open(path, 'rb').read()
     
     # compute the crc of the data
     crc = crc16(data)
     
     # compress the data if so requested
     if compress:
         data = zlib.compress(data)
     
     # encode the data in base64
     data = base64.b64encode(data)
     
     return {'data': data, 'crc': crc}
Пример #6
0
    def timestampString(self, update):
        """Returns the creation timestamp of a given update as an ISO 8601 Format string.
        
        @param update: The name of the update.
        @return: The creation timestamp as an ISO Formated string.
        
        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)

        return time.strftime('%Y-%m-%dT%H:%M:%S',
                             time.localtime(meta['timestamp']))
Пример #7
0
 def listDir(self, path):
     """Returns a directory listing of the given path.
     
     @param dir: the directory to list the contents of.
     @return: An Array of Objects each one listing the contents of the directory.
         Each Object has the keys name, size,
         dir (if true object is a directory),
         and readonly (if true object is read-only).
     
     """
     
     cfg = getConfig(path)
     list_plugins = dict([[x.__class__.__name__, x] for x in self.listdir_plugins])
     for plugin in cfg['plugins']:
         if list_plugins.has_key(plugin):
             plugin = list_plugins[plugin]
             data = plugin.listDir(cfg['head'], cfg['tail'], cfg['rootpath'])
             break
         else:
             self.log.debug("FileTransfer.listDir: Skipping plugin %s, not found" % plugin)
     else:   # default listDir handling
         path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
         if not os.path.exists(path):
             raise ApplicationError('IOError: No such file or directory: %s' % path)
         if not os.isdir(path):
             raise ApplicationError('IOError: %s is not a directory' % path)
         
         out = []
         for i in os.listdir(path):
             name = i
             size = os.path.getsize(os.path.join(path, i))
             isdir = os.path.isdir(os.path.join(path, i))
             readonly = os.access(os.path.join(path, i), os.W_OK)
             out.append(makeDirectoryEntry(name, size, isdir, readonly))
             
         return out
Пример #8
0
    def metaData(self, update):
        """Returns the meta data of a given update.
        
        @param update: The name of the update.
        @return: An Object containging the meta data with the following attributes::
            name: string <package name>,
            version: [major, minor] <update version>,
            timestamp: number <timestamp of update creation>,
            description: string <description>, (optional)
            files: [
              {
                file: string <file name>,
                dlpath: string <download path>,
                path: string <install path>,    (optional default local root directory)
              },
              ...
            ]

        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)

        out = {}
        out['name'] = meta['name']
        out['version'] = meta['version']
        out['timestamp'] = meta['timestamp']
        if meta.had_key('description'):
            out['description'] = meta['description']
        files = []

        dlroot = self.config.get('update', {}).get('dlroot',
                                                   'update').strip('/')

        for f in meta['files']:
            tmp = {}
            tmp['file'] = f['file']
            tmp['dlpath'] = '/%s/%s/%s' % (dlroot, update, f['file'])
            if f.has_key('path'):
                tmp['path'] = f['path']
            files.append(tmp)

        out['files'] = files

        return out