author | Patrick Mezard <pmezard@gmail.com> |
Wed, 20 Jun 2007 00:10:21 +0200 | |
changeset 4639 | c7371aa0c153 |
parent 4357 | 3f1b0c0fb4fd |
child 4370 | 6af107c742bf |
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 |
|
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
40 |
def __init__(self, dest, prefix, mtime, kind=''): |
2112
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', |
3615
1d2fdea875bc
Fixed directory name having an extra dot on "hg archive -t tbz2 foo.tbz2".
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2857
diff
changeset
|
42 |
'.tgz', '.tbz2']) |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
43 |
self.mtime = mtime |
2112
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: |
4357
3f1b0c0fb4fd
Work around python 2.5.1 tarfile regression
Brendan Cully <brendan@kublai.com>
parents:
3615
diff
changeset
|
47 |
# Python 2.5-2.5.1 have a regression that requires a name arg |
3f1b0c0fb4fd
Work around python 2.5.1 tarfile regression
Brendan Cully <brendan@kublai.com>
parents:
3615
diff
changeset
|
48 |
self.z = tarfile.open(name='', mode='w|'+kind, fileobj=dest) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
49 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 |
i = tarfile.TarInfo(self.prefix + name) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 |
i.mtime = self.mtime |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
53 |
i.size = len(data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 |
i.mode = mode |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 |
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
|
56 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 |
self.z.close() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 |
class tellable: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
61 |
'''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
|
62 |
response file object.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
63 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 |
def __init__(self, fp): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 |
self.fp = fp |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 |
self.offset = 0 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
67 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 |
def __getattr__(self, key): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 |
return getattr(self.fp, key) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
70 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
71 |
def write(self, s): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
72 |
self.fp.write(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
73 |
self.offset += len(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
74 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
75 |
def tell(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
76 |
return self.offset |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
77 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 |
class zipit: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 |
'''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
|
80 |
or compressed with deflate.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 |
|
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
82 |
def __init__(self, dest, prefix, mtime, compress=True): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 |
self.prefix = tidyprefix(dest, prefix, ('.zip',)) |
2168
dd4ec4576cc8
Proper check to see if zip dest needs to be wrapped in tellable
Colin McMillen <mcmillen@cs.cmu.edu>
parents:
2112
diff
changeset
|
84 |
if not isinstance(dest, str): |
dd4ec4576cc8
Proper check to see if zip dest needs to be wrapped in tellable
Colin McMillen <mcmillen@cs.cmu.edu>
parents:
2112
diff
changeset
|
85 |
try: |
dd4ec4576cc8
Proper check to see if zip dest needs to be wrapped in tellable
Colin McMillen <mcmillen@cs.cmu.edu>
parents:
2112
diff
changeset
|
86 |
dest.tell() |
2169
4564794b6f55
Combine catching exceptions added in dd4ec4576cc8 in one except statement.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2168
diff
changeset
|
87 |
except (AttributeError, IOError): |
2168
dd4ec4576cc8
Proper check to see if zip dest needs to be wrapped in tellable
Colin McMillen <mcmillen@cs.cmu.edu>
parents:
2112
diff
changeset
|
88 |
dest = tellable(dest) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
89 |
self.z = zipfile.ZipFile(dest, 'w', |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
90 |
compress and zipfile.ZIP_DEFLATED or |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 |
zipfile.ZIP_STORED) |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
92 |
self.date_time = time.gmtime(mtime)[:6] |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 |
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
|
96 |
i.compress_type = self.z.compression |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 |
i.flag_bits = 0x08 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
# 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
|
99 |
# set to unix (id 3). |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
100 |
i.create_system = 3 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 |
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
|
102 |
self.z.writestr(i, data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 |
self.z.close() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
107 |
class fileit: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
108 |
'''write archive as files in directory.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
109 |
|
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
110 |
def __init__(self, name, prefix, mtime): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
111 |
if prefix: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
112 |
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
|
113 |
self.basedir = name |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
114 |
self.dirs = {} |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
115 |
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
|
116 |
getattr(os, 'O_BINARY', 0) | |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
117 |
getattr(os, 'O_NOFOLLOW', 0)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
118 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
119 |
def addfile(self, name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
120 |
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
|
121 |
destdir = os.path.dirname(destfile) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
122 |
if destdir not in self.dirs: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 |
if not os.path.isdir(destdir): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
124 |
os.makedirs(destdir) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 |
self.dirs[destdir] = 1 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
126 |
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
|
127 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
128 |
def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
129 |
pass |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
130 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
131 |
archivers = { |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
132 |
'files': fileit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
133 |
'tar': tarit, |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
134 |
'tbz2': lambda name, prefix, mtime: tarit(name, prefix, mtime, 'bz2'), |
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
135 |
'tgz': lambda name, prefix, mtime: tarit(name, prefix, mtime, 'gz'), |
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
136 |
'uzip': lambda name, prefix, mtime: zipit(name, prefix, mtime, False), |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
137 |
'zip': zipit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
138 |
} |
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 |
def archive(repo, dest, node, kind, decode=True, matchfn=None, |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
141 |
prefix=None, mtime=None): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
142 |
'''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
|
143 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
144 |
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
|
145 |
object to write archive to. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
147 |
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
|
148 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
149 |
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
|
150 |
hgrc. |
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 |
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
|
153 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
154 |
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
|
155 |
|
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
156 |
def write(name, mode, data): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
157 |
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
|
158 |
if decode: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
159 |
fp = cStringIO.StringIO() |
2353
0c0bfea3f72a
Pass filename to decoder for 'hg archive' (fixes issue267)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2169
diff
changeset
|
160 |
repo.wwrite(name, data, fp) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
161 |
data = fp.getvalue() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
162 |
archiver.addfile(name, mode, data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
163 |
|
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
164 |
change = repo.changelog.read(node) |
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
165 |
mn = change[0] |
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
166 |
archiver = archivers[kind](dest, prefix, mtime or change[2][0]) |
2857
18cf5349a361
Fix some bugs introduced during the manifest refactoring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2840
diff
changeset
|
167 |
m = repo.manifest.read(mn) |
18cf5349a361
Fix some bugs introduced during the manifest refactoring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2840
diff
changeset
|
168 |
items = m.items() |
18cf5349a361
Fix some bugs introduced during the manifest refactoring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2840
diff
changeset
|
169 |
items.sort() |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
170 |
write('.hg_archival.txt', 0644, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
171 |
'repo: %s\nnode: %s\n' % (hex(repo.changelog.node(0)), hex(node))) |
2857
18cf5349a361
Fix some bugs introduced during the manifest refactoring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2840
diff
changeset
|
172 |
for filename, filenode in items: |
18cf5349a361
Fix some bugs introduced during the manifest refactoring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2840
diff
changeset
|
173 |
write(filename, m.execf(filename) and 0755 or 0644, |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
174 |
repo.file(filename).read(filenode)) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
175 |
archiver.done() |