Exemplo n.º 1
0
    def delete(self, request, user, data, message=None):
        '''Delete file(s) from repository
        '''
        files_to_del = data.get('files')
        if not files_to_del:
            raise DataError('Nothing to delete')
        # convert to list if not already
        if not isinstance(files_to_del, (list, tuple)):
            files_to_del = [files_to_del]

        filenames = []
        path = self.path

        for file in files_to_del:
            filepath = os.path.join(path, self._format_filename(file))
            # remove only files that really exist and not dirs
            if os.path.exists(filepath) and os.path.isfile(filepath):
                # remove from disk
                os.remove(filepath)
                filename = get_rel_dir(filepath, self.repo.path)
                filenames.append(filename)

        if filenames:
            rm(self.repo, filenames)
            if not message:
                message = 'Deleted %s' % ';'.join(filenames)

            return commit(self.repo, _b(message),
                          committer=_b(user.username))
Exemplo n.º 2
0
def get_context_files(app):
    '''Load static context
    '''
    ctx = {}
    location = content_location(app, 'context')
    if location and os.path.isdir(location):
        for dirpath, dirs, filenames in os.walk(location, topdown=False):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            for filename in filenames:
                if skipfile(filename):
                    continue
                file_bits = filename.split('.')
                bits = [file_bits[0]]

                prefix = get_rel_dir(dirpath, location)
                while prefix:
                    prefix, tail = os.path.split(prefix)
                    bits.append(tail)

                filename = os.path.join(dirpath, filename)
                suffix = get_reader(app, filename).suffix
                name = '_'.join(reversed(bits))
                if suffix:
                    name = '%s_%s' % (suffix, name)
                ctx[name] = filename
    return ctx
Exemplo n.º 3
0
def all_files(app, router, src):
    '''Generator of all files within a directory
    '''
    if os.path.isdir(src):
        for dirpath, _, filenames in os.walk(src):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            rel_dir = get_rel_dir(dirpath, src)
            for filename in filenames:
                if skipfile(filename):
                    continue
                name, ext = split(filename)
                name = os.path.join(rel_dir, name)
                fpath = os.path.join(dirpath, filename)
                yield name, fpath, ext
    #
    elif os.path.isfile(src):
        dirpath, filename = os.path.split(src)
        assert not filename.startswith('.')
        name, ext = split(filename)
        fpath = os.path.join(dirpath, filename)
        yield name, dirpath, ext
    #
    else:
        raise BuildError("'%s' not found." % src)
Exemplo n.º 4
0
def all_files(app, router, src):
    """Generator of all files within a directory
    """
    if os.path.isdir(src):
        for dirpath, _, filenames in os.walk(src):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            rel_dir = get_rel_dir(dirpath, src)
            for filename in filenames:
                if skipfile(filename):
                    continue
                name, ext = split(filename)
                name = os.path.join(rel_dir, name)
                fpath = os.path.join(dirpath, filename)
                yield name, fpath, ext
    #
    elif os.path.isfile(src):
        dirpath, filename = os.path.split(src)
        assert not filename.startswith(".")
        name, ext = split(filename)
        fpath = os.path.join(dirpath, filename)
        yield name, dirpath, ext
    #
    else:
        raise BuildError("'%s' not found." % src)
Exemplo n.º 5
0
def static_context(app, location, context):
    '''Load static context from ``location``
    '''
    ctx = {}
    if os.path.isdir(location):
        for dirpath, dirs, filenames in os.walk(location, topdown=False):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            for filename in filenames:
                if skipfile(filename):
                    continue
                file_bits = filename.split('.')
                bits = [file_bits[0]]

                prefix = get_rel_dir(dirpath, location)
                while prefix:
                    prefix, tail = os.path.split(prefix)
                    bits.append(tail)

                filename = os.path.join(dirpath, filename)
                reader = get_reader(app, filename)
                name = '_'.join(reversed(bits))
                content = reader.read(filename, name)
                if content.suffix:
                    name = '%s_%s' % (content.suffix, name)
                ctx[name] = content.render(context)
                context[name] = ctx[name]
    return ctx
Exemplo n.º 6
0
 def all(self, request):
     '''Return list of all files stored in repo
     '''
     path = self.path
     files = glob.glob(os.path.join(path, '*.%s' % self.ext))
     for file in files:
         filename = get_rel_dir(file, path)
         yield self.read(request, filename).json(request)
Exemplo n.º 7
0
    def write(self, request, user, data, new=False, message=None):
        '''Write a file into the repository

        When ``new`` the file must not exist, when not
        ``new``, the file must exist.
        '''
        name = data['name']
        path = self.path
        filepath = os.path.join(path, self._format_filename(name))
        if new:
            if not message:
                message = "Created %s" % name
            if os.path.isfile(filepath):
                raise DataError('%s not available' % name)
            else:
                dir = os.path.dirname(filepath)
                if not os.path.isdir(dir):
                    os.makedirs(dir)
        else:
            if not message:
                message = "Updated %s" % name
            if not os.path.isfile(filepath):
                raise DataError('%s not available' % name)

        content = self.content(data)

        # write file
        with open(filepath, 'wb') as f:
            f.write(_b(content))

        filename = get_rel_dir(filepath, self.repo.path)

        add(self.repo, [filename])
        committer = user.username if user.is_authenticated() else 'anonymous'
        commit_hash = commit(self.repo, _b(message), committer=_b(committer))

        return dict(hash=commit_hash.decode('utf-8'),
                    body=content,
                    filename=filename,
                    name=name)