Mercurial > bitten > bitten-test
changeset 37:35e7b490496f
Add a simple utility module for creating snapshot archives of a repository.
author | cmlenz |
---|---|
date | Wed, 22 Jun 2005 16:32:36 +0000 |
parents | 47433f32e1fc |
children | 49eaa98230c5 |
files | bitten/util/archive.py |
diffstat | 1 files changed, 70 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/bitten/util/archive.py @@ -0,0 +1,70 @@ +# -*- coding: iso8859-1 -*- +# +# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> +# +# Bitten is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Trac is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# Author: Christopher Lenz <cmlenz@gmx.de> + +import os.path +import tarfile + +_formats = {'gzip': ('.tar.gz', 'gz'), 'bzip2': ('.tar.bz2', 'bz2'), + 'zip': ('.zip', None)} + +def make_archive(env, path=None, rev=None, format='gzip'): + repos = env.get_repository() + root = repos.get_node(path or '/', rev) + if not root.isdir: + raise Exception, '"%s" is not a directory' % path + + assert format in _formats, 'Unknown archive format: %s' % format + + filedir = os.path.join(env.path, 'tarballs') + if not os.access(filedir, os.R_OK + os.W_OK): + raise IOError, 'Insufficient permissions to create tarball' + prefix = '%s-%s' % (root.path.replace('/', '-'), root.rev) + filename = os.path.join(filedir, prefix + _formats[format][0]) + + if format in ('bzip2', 'gzip'): + _make_tar_archive(env, root, filename, prefix, format) + else: + _make_zip_archive(env, root, filename, prefix) + +def _make_tar_archive(env, root, filename, prefix, format='gzip'): + tar = tarfile.open(filename, 'w:' + _formats[format][1]) + + def _add_entry(prefix, node): + name = node.path[len(root.path):] + if name.startswith('/'): + name = name[1:] + if node.isdir: + for entry in node.get_entries(): + _add_entry(os.path.join(prefix, name), entry) + else: + info = tarfile.TarInfo(os.path.join(prefix, name)) + info.type = tarfile.REGTYPE + info.mtime = node.last_modified + info.size = node.content_length + tar.addfile(info, node.get_content()) + _add_entry(prefix, root) + + tar.close() + + +if __name__ == '__main__': + from trac.env import Environment + env = Environment('/var/trac/bitten') + make_archive(env, 'bitten/trunk')