Exemplo n.º 1
0
    def fileMetaData(self, update, filename):
        """Returns the meta data for file within a given update.
        
        @param update: The name of the update.
        @param filename: The file to retrieve the meta data for.
        @return: An Objects with members::
            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)

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

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

                return tmp
        else:
            raise ApplicationError('update %s does not contain file %s' %
                                   (update, filename))
Exemplo n.º 2
0
    def files(self, update):
        """Returns an array of file meta data for a given update.
        
        @param update: The name of the update.
        @return: An Array of Objects with members::
            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)

        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)

        return files
Exemplo n.º 3
0
    def download(self, head, tail, rootpath):
        """Fetch the data to be sent.
        
        This should return a string with the data to be returned.
        
        @param head: A string with the requested path that precedes tail.
        @param tail: the requested path relative to rootpath.
        @param rootpath: A string with the local rootpath for the path of the request. 
        """

        tail_ = tail.lstrip('/')
        tmp = tail_.split('/')
        update = tmp[0]
        path = '/'.join(tmp[1:])

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

        for f in meta['files']:
            if f['file'] == path:
                file_meta = f
        else:
            raise IOError('File %s is missing from update %s' %
                          (path, meta['name']))

        # is this a file system based update
        if meta['is_dir'] == True:
            data = open(os.path.join(meta['path'], path)).read()
        else:  # the update is an archive
            arch = zipfile.ZipFile(meta['path'], 'r', zipfile.ZIP_DEFLATED)
            data = arch.read(path)

        hash = hashlib.sha1()
        hash.update(data)

        if file_meta['hash'] != hash.hexdigest():
            self.log.debug(
                'file (%s) sha1 hash does not match update configuration.' %
                path)
            raise ApplicationError(
                'file (%s) sha1 hash does not match update configuration.' %
                path)

        return data
Exemplo n.º 4
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()
Exemplo n.º 5
0
    def version(self, update):
        """Returns the numeric version of a given update.
        
        @param update: the name of the update.
        @return: An Array containing 2 numbers, with the major and minor version numbers of the update.
        
        """

        meta = self.getUpdateMetadata(update)
        if meta == None:
            raise ApplicationError('unknown update %s' % update)
        else:
            return meta['version']
Exemplo n.º 6
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', '')
Exemplo n.º 7
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']
Exemplo n.º 8
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'])\
Exemplo n.º 9
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}
Exemplo n.º 10
0
    def filePath(self, update, filename):
        """Returns the install path for a file within a given update.
        
        @param update: The name of the update.
        @param filename: The file to retrieve the meta data for.
        @return: A string containing the install path for the file.
        
        """

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

        for f in meta['files']:
            if f['file'] == filename:
                path = ''
                if f.has_key('path'):
                    path = f['path']

                return path
        else:
            raise ApplicationError('update %s does not contain file %s' %
                                   (update, filename))
Exemplo n.º 11
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']))
Exemplo n.º 12
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
Exemplo n.º 13
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