author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Fri, 21 Apr 2006 15:27:57 -0700 | |
changeset 2112 | 2b03c6733efa |
child 2168 | dd4ec4576cc8 |
permissions | -rw-r--r-- |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1 |
# archival.py - revision archival for mercurial |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
2 |
# |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
4 |
# |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
6 |
# the GNU General Public License, incorporated herein by reference. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
7 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
8 |
from demandload import * |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
9 |
from i18n import gettext as _ |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
10 |
from node import * |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
11 |
demandload(globals(), 'cStringIO os stat tarfile time util zipfile') |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
12 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
13 |
def tidyprefix(dest, prefix, suffixes): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
14 |
'''choose prefix to use for names in archive. make sure prefix is |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
15 |
safe for consumers.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
16 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
17 |
if prefix: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
18 |
prefix = prefix.replace('\\', '/') |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
19 |
else: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 |
if not isinstance(dest, str): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 |
raise ValueError('dest must be string if no prefix') |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 |
prefix = os.path.basename(dest) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 |
lower = prefix.lower() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 |
for sfx in suffixes: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 |
if lower.endswith(sfx): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
26 |
prefix = prefix[:-len(sfx)] |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 |
break |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 |
lpfx = os.path.normpath(util.localpath(prefix)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 |
prefix = util.pconvert(lpfx) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
30 |
if not prefix.endswith('/'): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
31 |
prefix += '/' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
32 |
if prefix.startswith('../') or os.path.isabs(lpfx) or '/../' in prefix: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
33 |
raise util.Abort(_('archive prefix contains illegal components')) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 |
return prefix |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
35 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
36 |
class tarit: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
37 |
'''write archive to tar file or stream. can write uncompressed, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
38 |
or compress with gzip or bzip2.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
39 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
40 |
def __init__(self, dest, prefix, kind=''): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
41 |
self.prefix = tidyprefix(dest, prefix, ['.tar', '.tar.bz2', '.tar.gz', |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
42 |
'.tgz', 'tbz2']) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
43 |
self.mtime = int(time.time()) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
44 |
if isinstance(dest, str): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
45 |
self.z = tarfile.open(dest, mode='w:'+kind) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
46 |
else: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
47 |
self.z = tarfile.open(mode='w|'+kind, fileobj=dest) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
48 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
49 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 |
i = tarfile.TarInfo(self.prefix + name) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 |
i.mtime = self.mtime |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 |
i.size = len(data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
53 |
i.mode = mode |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 |
self.z.addfile(i, cStringIO.StringIO(data)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
56 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 |
self.z.close() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 |
class tellable: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 |
'''provide tell method for zipfile.ZipFile when writing to http |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
61 |
response file object.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
62 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
63 |
def __init__(self, fp): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 |
self.fp = fp |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 |
self.offset = 0 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
67 |
def __getattr__(self, key): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 |
return getattr(self.fp, key) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
70 |
def write(self, s): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
71 |
self.fp.write(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
72 |
self.offset += len(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
73 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
74 |
def tell(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
75 |
return self.offset |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
76 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
77 |
class zipit: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 |
'''write archive to zip file or stream. can write uncompressed, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 |
or compressed with deflate.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 |
def __init__(self, dest, prefix, compress=True): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 |
self.prefix = tidyprefix(dest, prefix, ('.zip',)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 |
if not isinstance(dest, str) and not hasattr(dest, 'tell'): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 |
dest = tellable(dest) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
85 |
self.z = zipfile.ZipFile(dest, 'w', |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
86 |
compress and zipfile.ZIP_DEFLATED or |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
87 |
zipfile.ZIP_STORED) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
88 |
self.date_time = time.gmtime(time.time())[:6] |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
89 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
90 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 |
i = zipfile.ZipInfo(self.prefix + name, self.date_time) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 |
i.compress_type = self.z.compression |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 |
i.flag_bits = 0x08 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 |
# unzip will not honor unix file modes unless file creator is |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 |
# set to unix (id 3). |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 |
i.create_system = 3 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 |
i.external_attr = (mode | stat.S_IFREG) << 16L |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
self.z.writestr(i, data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
100 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 |
self.z.close() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
102 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 |
class fileit: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 |
'''write archive as files in directory.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 |
def __init__(self, name, prefix): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
107 |
if prefix: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
108 |
raise util.Abort(_('cannot give prefix when archiving to files')) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
109 |
self.basedir = name |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
110 |
self.dirs = {} |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
111 |
self.oflags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY | |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
112 |
getattr(os, 'O_BINARY', 0) | |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
113 |
getattr(os, 'O_NOFOLLOW', 0)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
114 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
115 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
116 |
destfile = os.path.join(self.basedir, name) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
117 |
destdir = os.path.dirname(destfile) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
118 |
if destdir not in self.dirs: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
119 |
if not os.path.isdir(destdir): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
120 |
os.makedirs(destdir) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
121 |
self.dirs[destdir] = 1 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
122 |
os.fdopen(os.open(destfile, self.oflags, mode), 'wb').write(data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
124 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 |
pass |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
126 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
127 |
archivers = { |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
128 |
'files': fileit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
129 |
'tar': tarit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
130 |
'tbz2': lambda name, prefix: tarit(name, prefix, 'bz2'), |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
131 |
'tgz': lambda name, prefix: tarit(name, prefix, 'gz'), |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
132 |
'uzip': lambda name, prefix: zipit(name, prefix, False), |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
133 |
'zip': zipit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
134 |
} |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
135 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
136 |
def archive(repo, dest, node, kind, decode=True, matchfn=None, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
137 |
prefix=None): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
138 |
'''create archive of repo as it was at node. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
139 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
140 |
dest can be name of directory, name of archive file, or file |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
141 |
object to write archive to. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
142 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
143 |
kind is type of archive to create. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
144 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
145 |
decode tells whether to put files through decode filters from |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 |
hgrc. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
147 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
148 |
matchfn is function to filter names of files to write to archive. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
149 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
150 |
prefix is name of path to put before every archive member.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
151 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
152 |
def write(name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 |
if matchfn and not matchfn(name): return |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
154 |
if decode: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
155 |
fp = cStringIO.StringIO() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
156 |
repo.wwrite(None, data, fp) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
157 |
data = fp.getvalue() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
158 |
archiver.addfile(name, mode, data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
159 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
160 |
archiver = archivers[kind](dest, prefix) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
161 |
mn = repo.changelog.read(node)[0] |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
162 |
mf = repo.manifest.read(mn).items() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
163 |
mff = repo.manifest.readflags(mn) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
164 |
mf.sort() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
165 |
write('.hg_archival.txt', 0644, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
166 |
'repo: %s\nnode: %s\n' % (hex(repo.changelog.node(0)), hex(node))) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
167 |
for filename, filenode in mf: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
168 |
write(filename, mff[filename] and 0755 or 0644, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
169 |
repo.file(filename).read(filenode)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
170 |
archiver.done() |