Show More
@@ -1,211 +1,210 b'' | |||
|
1 | 1 | # archival.py - revision archival for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of |
|
6 | 6 | # the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | 9 | from node import * |
|
10 | 10 | import cStringIO, os, stat, tarfile, time, util, zipfile |
|
11 | 11 | import zlib, gzip |
|
12 | 12 | |
|
13 | 13 | def tidyprefix(dest, prefix, suffixes): |
|
14 | 14 | '''choose prefix to use for names in archive. make sure prefix is |
|
15 | 15 | safe for consumers.''' |
|
16 | 16 | |
|
17 | 17 | if prefix: |
|
18 | 18 | prefix = prefix.replace('\\', '/') |
|
19 | 19 | else: |
|
20 | 20 | if not isinstance(dest, str): |
|
21 | 21 | raise ValueError('dest must be string if no prefix') |
|
22 | 22 | prefix = os.path.basename(dest) |
|
23 | 23 | lower = prefix.lower() |
|
24 | 24 | for sfx in suffixes: |
|
25 | 25 | if lower.endswith(sfx): |
|
26 | 26 | prefix = prefix[:-len(sfx)] |
|
27 | 27 | break |
|
28 | 28 | lpfx = os.path.normpath(util.localpath(prefix)) |
|
29 | 29 | prefix = util.pconvert(lpfx) |
|
30 | 30 | if not prefix.endswith('/'): |
|
31 | 31 | prefix += '/' |
|
32 | 32 | if prefix.startswith('../') or os.path.isabs(lpfx) or '/../' in prefix: |
|
33 | 33 | raise util.Abort(_('archive prefix contains illegal components')) |
|
34 | 34 | return prefix |
|
35 | 35 | |
|
36 | 36 | class tarit: |
|
37 | 37 | '''write archive to tar file or stream. can write uncompressed, |
|
38 | 38 | or compress with gzip or bzip2.''' |
|
39 | 39 | |
|
40 | 40 | class GzipFileWithTime(gzip.GzipFile): |
|
41 | 41 | |
|
42 | 42 | def __init__(self, *args, **kw): |
|
43 | 43 | timestamp = None |
|
44 | 44 | if 'timestamp' in kw: |
|
45 | 45 | timestamp = kw.pop('timestamp') |
|
46 | 46 | if timestamp == None: |
|
47 | 47 | self.timestamp = time.time() |
|
48 | 48 | else: |
|
49 | 49 | self.timestamp = timestamp |
|
50 | 50 | gzip.GzipFile.__init__(self, *args, **kw) |
|
51 | 51 | |
|
52 | 52 | def _write_gzip_header(self): |
|
53 | 53 | self.fileobj.write('\037\213') # magic header |
|
54 | 54 | self.fileobj.write('\010') # compression method |
|
55 | 55 | fname = self.filename[:-3] |
|
56 | 56 | flags = 0 |
|
57 | 57 | if fname: |
|
58 | 58 | flags = gzip.FNAME |
|
59 | 59 | self.fileobj.write(chr(flags)) |
|
60 | 60 | gzip.write32u(self.fileobj, long(self.timestamp)) |
|
61 | 61 | self.fileobj.write('\002') |
|
62 | 62 | self.fileobj.write('\377') |
|
63 | 63 | if fname: |
|
64 | 64 | self.fileobj.write(fname + '\000') |
|
65 | 65 | |
|
66 | 66 | def __init__(self, dest, prefix, mtime, kind=''): |
|
67 | 67 | self.prefix = tidyprefix(dest, prefix, ['.tar', '.tar.bz2', '.tar.gz', |
|
68 | 68 | '.tgz', '.tbz2']) |
|
69 | 69 | self.mtime = mtime |
|
70 | 70 | |
|
71 | 71 | def taropen(name, mode, fileobj=None): |
|
72 | 72 | if kind == 'gz': |
|
73 | 73 | mode = mode[0] |
|
74 | 74 | if not fileobj: |
|
75 | 75 | fileobj = open(name, mode) |
|
76 | 76 | gzfileobj = self.GzipFileWithTime(name, mode + 'b', |
|
77 | 77 | zlib.Z_BEST_COMPRESSION, |
|
78 | 78 | fileobj, timestamp=mtime) |
|
79 | 79 | return tarfile.TarFile.taropen(name, mode, gzfileobj) |
|
80 | 80 | else: |
|
81 | 81 | return tarfile.open(name, mode + kind, fileobj) |
|
82 | 82 | |
|
83 | 83 | if isinstance(dest, str): |
|
84 | 84 | self.z = taropen(dest, mode='w:') |
|
85 | 85 | else: |
|
86 | 86 | # Python 2.5-2.5.1 have a regression that requires a name arg |
|
87 | 87 | self.z = taropen(name='', mode='w|', fileobj=dest) |
|
88 | 88 | |
|
89 | 89 | def addfile(self, name, mode, data): |
|
90 | 90 | i = tarfile.TarInfo(self.prefix + name) |
|
91 | 91 | i.mtime = self.mtime |
|
92 | 92 | i.size = len(data) |
|
93 | 93 | i.mode = mode |
|
94 | 94 | self.z.addfile(i, cStringIO.StringIO(data)) |
|
95 | 95 | |
|
96 | 96 | def done(self): |
|
97 | 97 | self.z.close() |
|
98 | 98 | |
|
99 | 99 | class tellable: |
|
100 | 100 | '''provide tell method for zipfile.ZipFile when writing to http |
|
101 | 101 | response file object.''' |
|
102 | 102 | |
|
103 | 103 | def __init__(self, fp): |
|
104 | 104 | self.fp = fp |
|
105 | 105 | self.offset = 0 |
|
106 | 106 | |
|
107 | 107 | def __getattr__(self, key): |
|
108 | 108 | return getattr(self.fp, key) |
|
109 | 109 | |
|
110 | 110 | def write(self, s): |
|
111 | 111 | self.fp.write(s) |
|
112 | 112 | self.offset += len(s) |
|
113 | 113 | |
|
114 | 114 | def tell(self): |
|
115 | 115 | return self.offset |
|
116 | 116 | |
|
117 | 117 | class zipit: |
|
118 | 118 | '''write archive to zip file or stream. can write uncompressed, |
|
119 | 119 | or compressed with deflate.''' |
|
120 | 120 | |
|
121 | 121 | def __init__(self, dest, prefix, mtime, compress=True): |
|
122 | 122 | self.prefix = tidyprefix(dest, prefix, ('.zip',)) |
|
123 | 123 | if not isinstance(dest, str): |
|
124 | 124 | try: |
|
125 | 125 | dest.tell() |
|
126 | 126 | except (AttributeError, IOError): |
|
127 | 127 | dest = tellable(dest) |
|
128 | 128 | self.z = zipfile.ZipFile(dest, 'w', |
|
129 | 129 | compress and zipfile.ZIP_DEFLATED or |
|
130 | 130 | zipfile.ZIP_STORED) |
|
131 | 131 | self.date_time = time.gmtime(mtime)[:6] |
|
132 | 132 | |
|
133 | 133 | def addfile(self, name, mode, data): |
|
134 | 134 | i = zipfile.ZipInfo(self.prefix + name, self.date_time) |
|
135 | 135 | i.compress_type = self.z.compression |
|
136 | i.flag_bits = 0x08 | |
|
137 | 136 | # unzip will not honor unix file modes unless file creator is |
|
138 | 137 | # set to unix (id 3). |
|
139 | 138 | i.create_system = 3 |
|
140 | 139 | i.external_attr = (mode | stat.S_IFREG) << 16L |
|
141 | 140 | self.z.writestr(i, data) |
|
142 | 141 | |
|
143 | 142 | def done(self): |
|
144 | 143 | self.z.close() |
|
145 | 144 | |
|
146 | 145 | class fileit: |
|
147 | 146 | '''write archive as files in directory.''' |
|
148 | 147 | |
|
149 | 148 | def __init__(self, name, prefix, mtime): |
|
150 | 149 | if prefix: |
|
151 | 150 | raise util.Abort(_('cannot give prefix when archiving to files')) |
|
152 | 151 | self.basedir = name |
|
153 | 152 | self.dirs = {} |
|
154 | 153 | self.oflags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY | |
|
155 | 154 | getattr(os, 'O_BINARY', 0) | |
|
156 | 155 | getattr(os, 'O_NOFOLLOW', 0)) |
|
157 | 156 | |
|
158 | 157 | def addfile(self, name, mode, data): |
|
159 | 158 | destfile = os.path.join(self.basedir, name) |
|
160 | 159 | destdir = os.path.dirname(destfile) |
|
161 | 160 | if destdir not in self.dirs: |
|
162 | 161 | if not os.path.isdir(destdir): |
|
163 | 162 | os.makedirs(destdir) |
|
164 | 163 | self.dirs[destdir] = 1 |
|
165 | 164 | os.fdopen(os.open(destfile, self.oflags, mode), 'wb').write(data) |
|
166 | 165 | |
|
167 | 166 | def done(self): |
|
168 | 167 | pass |
|
169 | 168 | |
|
170 | 169 | archivers = { |
|
171 | 170 | 'files': fileit, |
|
172 | 171 | 'tar': tarit, |
|
173 | 172 | 'tbz2': lambda name, prefix, mtime: tarit(name, prefix, mtime, 'bz2'), |
|
174 | 173 | 'tgz': lambda name, prefix, mtime: tarit(name, prefix, mtime, 'gz'), |
|
175 | 174 | 'uzip': lambda name, prefix, mtime: zipit(name, prefix, mtime, False), |
|
176 | 175 | 'zip': zipit, |
|
177 | 176 | } |
|
178 | 177 | |
|
179 | 178 | def archive(repo, dest, node, kind, decode=True, matchfn=None, |
|
180 | 179 | prefix=None, mtime=None): |
|
181 | 180 | '''create archive of repo as it was at node. |
|
182 | 181 | |
|
183 | 182 | dest can be name of directory, name of archive file, or file |
|
184 | 183 | object to write archive to. |
|
185 | 184 | |
|
186 | 185 | kind is type of archive to create. |
|
187 | 186 | |
|
188 | 187 | decode tells whether to put files through decode filters from |
|
189 | 188 | hgrc. |
|
190 | 189 | |
|
191 | 190 | matchfn is function to filter names of files to write to archive. |
|
192 | 191 | |
|
193 | 192 | prefix is name of path to put before every archive member.''' |
|
194 | 193 | |
|
195 | 194 | def write(name, mode, data): |
|
196 | 195 | if matchfn and not matchfn(name): return |
|
197 | 196 | if decode: |
|
198 | 197 | data = repo.wwritedata(name, data) |
|
199 | 198 | archiver.addfile(name, mode, data) |
|
200 | 199 | |
|
201 | 200 | ctx = repo.changectx(node) |
|
202 | 201 | archiver = archivers[kind](dest, prefix, mtime or ctx.date()[0]) |
|
203 | 202 | m = ctx.manifest() |
|
204 | 203 | items = m.items() |
|
205 | 204 | items.sort() |
|
206 | 205 | write('.hg_archival.txt', 0644, |
|
207 | 206 | 'repo: %s\nnode: %s\n' % (hex(repo.changelog.node(0)), hex(node))) |
|
208 | 207 | for filename, filenode in items: |
|
209 | 208 | write(filename, m.execf(filename) and 0755 or 0644, |
|
210 | 209 | repo.file(filename).read(filenode)) |
|
211 | 210 | archiver.done() |
General Comments 0
You need to be logged in to leave comments.
Login now