Mercurial > hg
annotate mercurial/archival.py @ 25663:edd2d20ac687
templatekw: correct typo in activebookmark documentation
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Wed, 24 Jun 2015 11:15:00 -0400 |
parents | e93036747902 |
children | c76e8d14383a |
rev | line source |
---|---|
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 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7770
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
7 |
3891 | 8 from i18n import _ |
17108
1894dac619de
subrepo: propagate matcher to subrepos when archiving
Matt Harbison <matt_harbison@yahoo.com>
parents:
16919
diff
changeset
|
9 import match as matchmod |
9614
58edd448da4f
archive: add branch and tag informations to the .hg_archival.txt file
Gilles Moris <gilles.moris@free.fr>
parents:
8778
diff
changeset
|
10 import cmdutil |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13668
diff
changeset
|
11 import scmutil, util, encoding |
13668
9a41af6b9f29
archive: use hardcoded constants when creating .zip archives
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents:
13400
diff
changeset
|
12 import cStringIO, os, tarfile, time, zipfile |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
13 import zlib, gzip |
17628
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
14 import struct |
18967
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
15 import error |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
16 |
17429
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
17 # from unzip source code: |
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
18 _UNX_IFREG = 0x8000 |
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
19 _UNX_IFLNK = 0xa000 |
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
20 |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
21 def tidyprefix(dest, kind, prefix): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 '''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
|
23 safe for consumers.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 if prefix: |
5842
111ed8c871bf
Use util.normpath() instead of direct path string operation.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
4951
diff
changeset
|
26 prefix = util.normpath(prefix) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 else: |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 if not isinstance(dest, str): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 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
|
30 prefix = os.path.basename(dest) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
31 lower = prefix.lower() |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
32 for sfx in exts.get(kind, []): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
33 if lower.endswith(sfx): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 prefix = prefix[:-len(sfx)] |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
35 break |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
36 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
|
37 prefix = util.pconvert(lpfx) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
38 if not prefix.endswith('/'): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
39 prefix += '/' |
24953
5115d03440f4
archive: drop the leading '.' path component from the prefix (issue4634)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24681
diff
changeset
|
40 # Drop the leading '.' path component if present, so Windows can read the |
5115d03440f4
archive: drop the leading '.' path component from the prefix (issue4634)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24681
diff
changeset
|
41 # zip files (issue4634) |
5115d03440f4
archive: drop the leading '.' path component from the prefix (issue4634)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24681
diff
changeset
|
42 if prefix.startswith('./'): |
5115d03440f4
archive: drop the leading '.' path component from the prefix (issue4634)
Matt Harbison <matt_harbison@yahoo.com>
parents:
24681
diff
changeset
|
43 prefix = prefix[2:] |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
44 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
|
45 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
|
46 return prefix |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
47 |
11557
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
48 exts = { |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
49 'tar': ['.tar'], |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
50 'tbz2': ['.tbz2', '.tar.bz2'], |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
51 'tgz': ['.tgz', '.tar.gz'], |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
52 'zip': ['.zip'], |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
53 } |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
54 |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
55 def guesskind(dest): |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
56 for kind, extensions in exts.iteritems(): |
25149
3f0744eeaeaf
cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents:
24953
diff
changeset
|
57 if any(dest.endswith(ext) for ext in extensions): |
11557
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
58 return kind |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
59 return None |
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
60 |
24681
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
61 def _rootctx(repo): |
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
62 # repo[0] may be hidden |
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
63 for rev in repo: |
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
64 return repo[rev] |
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
65 return repo['null'] |
33ab99a6ad9b
archive: look for first visible revision to build repo identity (issue4591)
Yuya Nishihara <yuya@tcha.org>
parents:
24678
diff
changeset
|
66 |
24678
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
67 def buildmetadata(ctx): |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
68 '''build content of .hg_archival.txt''' |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
69 repo = ctx.repo() |
25615
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
70 hex = ctx.hex() |
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
71 if ctx.rev() is None: |
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
72 hex = ctx.p1().hex() |
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
73 if ctx.dirty(): |
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
74 hex += '+' |
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
75 |
24678
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
76 base = 'repo: %s\nnode: %s\nbranch: %s\n' % ( |
25615
dc707fb35550
archive: report the node as "{p1node}+" when archiving a dirty wdir()
Matt Harbison <matt_harbison@yahoo.com>
parents:
25601
diff
changeset
|
77 _rootctx(repo).hex(), hex, encoding.fromlocal(ctx.branch())) |
24678
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
78 |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
79 tags = ''.join('tag: %s\n' % t for t in ctx.tags() |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
80 if repo.tagtype(t) == 'global') |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
81 if not tags: |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
82 repo.ui.pushbuffer() |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
83 opts = {'template': '{latesttag}\n{latesttagdistance}', |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
84 'style': '', 'patch': None, 'git': None} |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
85 cmdutil.show_changeset(repo.ui, repo, opts).show(ctx) |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
86 ltags, dist = repo.ui.popbuffer().split('\n') |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
87 ltags = ltags.split(':') |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
88 changessince = len(repo.revs('only(.,%s)', ltags[0])) |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
89 tags = ''.join('latesttag: %s\n' % t for t in ltags) |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
90 tags += 'latesttagdistance: %s\n' % dist |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
91 tags += 'changessincelatesttag: %s\n' % changessince |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
92 |
fbcace19534f
archive: extract metadata() closure to module-level function
Yuya Nishihara <yuya@tcha.org>
parents:
24677
diff
changeset
|
93 return base + tags |
11557
57bdc2239535
archival: move commands.archive.guess_type to archival.guesskind
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
94 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8527
diff
changeset
|
95 class tarit(object): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 '''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
|
97 or compress with gzip or bzip2.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
99 class GzipFileWithTime(gzip.GzipFile): |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
100 |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
101 def __init__(self, *args, **kw): |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
102 timestamp = None |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
103 if 'timestamp' in kw: |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
104 timestamp = kw.pop('timestamp') |
8527
f9a80054dd3c
use 'x is None' instead of 'x == None'
Martin Geisler <mg@lazybytes.net>
parents:
8312
diff
changeset
|
105 if timestamp is None: |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
106 self.timestamp = time.time() |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
107 else: |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
108 self.timestamp = timestamp |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
109 gzip.GzipFile.__init__(self, *args, **kw) |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
110 |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
111 def _write_gzip_header(self): |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
112 self.fileobj.write('\037\213') # magic header |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
113 self.fileobj.write('\010') # compression method |
18302
16c642a6f07d
archival: avoid touching deprecated gzip name attribute
Mads Kiilerich <madski@unity3d.com>
parents:
18301
diff
changeset
|
114 # Python 2.6 introduced self.name and deprecated self.filename |
16c642a6f07d
archival: avoid touching deprecated gzip name attribute
Mads Kiilerich <madski@unity3d.com>
parents:
18301
diff
changeset
|
115 try: |
16c642a6f07d
archival: avoid touching deprecated gzip name attribute
Mads Kiilerich <madski@unity3d.com>
parents:
18301
diff
changeset
|
116 fname = self.name |
16c642a6f07d
archival: avoid touching deprecated gzip name attribute
Mads Kiilerich <madski@unity3d.com>
parents:
18301
diff
changeset
|
117 except AttributeError: |
16c642a6f07d
archival: avoid touching deprecated gzip name attribute
Mads Kiilerich <madski@unity3d.com>
parents:
18301
diff
changeset
|
118 fname = self.filename |
13102
2956945c3bee
archival: don't set gzip filename header when there's no filename
Brodie Rao <brodie@bitheap.org>
parents:
12323
diff
changeset
|
119 if fname and fname.endswith('.gz'): |
2956945c3bee
archival: don't set gzip filename header when there's no filename
Brodie Rao <brodie@bitheap.org>
parents:
12323
diff
changeset
|
120 fname = fname[:-3] |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
121 flags = 0 |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
122 if fname: |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
123 flags = gzip.FNAME |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
124 self.fileobj.write(chr(flags)) |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
125 gzip.write32u(self.fileobj, long(self.timestamp)) |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
126 self.fileobj.write('\002') |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
127 self.fileobj.write('\377') |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
128 if fname: |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
129 self.fileobj.write(fname + '\000') |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
130 |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
131 def __init__(self, dest, mtime, kind=''): |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
132 self.mtime = mtime |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13144
diff
changeset
|
133 self.fileobj = None |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
134 |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
135 def taropen(name, mode, fileobj=None): |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
136 if kind == 'gz': |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
137 mode = mode[0] |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
138 if not fileobj: |
4731 | 139 fileobj = open(name, mode + 'b') |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
140 gzfileobj = self.GzipFileWithTime(name, mode + 'b', |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
141 zlib.Z_BEST_COMPRESSION, |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
142 fileobj, timestamp=mtime) |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13144
diff
changeset
|
143 self.fileobj = gzfileobj |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
144 return tarfile.TarFile.taropen(name, mode, gzfileobj) |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
145 else: |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
146 return tarfile.open(name, mode + kind, fileobj) |
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
147 |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
148 if isinstance(dest, str): |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
149 self.z = taropen(dest, mode='w:') |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
150 else: |
4357
3f1b0c0fb4fd
Work around python 2.5.1 tarfile regression
Brendan Cully <brendan@kublai.com>
parents:
3615
diff
changeset
|
151 # Python 2.5-2.5.1 have a regression that requires a name arg |
4652
06de65673ec2
timestamp of gzip archives taken from changeset context
csaba.henk@creo.hu
parents:
4370
diff
changeset
|
152 self.z = taropen(name='', mode='w|', fileobj=dest) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
154 def addfile(self, name, mode, islink, data): |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
155 i = tarfile.TarInfo(name) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
156 i.mtime = self.mtime |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
157 i.size = len(data) |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
158 if islink: |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
159 i.type = tarfile.SYMTYPE |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25615
diff
changeset
|
160 i.mode = 0o777 |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
161 i.linkname = data |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
162 data = None |
7770
fd3e5ff53a31
fix disappearing symlinks [issue1509]
Peter van Dijk <mercurial-bugs@selenic.com>
parents:
6913
diff
changeset
|
163 i.size = 0 |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
164 else: |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
165 i.mode = mode |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
166 data = cStringIO.StringIO(data) |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
167 self.z.addfile(i, data) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
168 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
169 def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
170 self.z.close() |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13144
diff
changeset
|
171 if self.fileobj: |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13144
diff
changeset
|
172 self.fileobj.close() |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
173 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8527
diff
changeset
|
174 class tellable(object): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
175 '''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
|
176 response file object.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
177 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
178 def __init__(self, fp): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
179 self.fp = fp |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
180 self.offset = 0 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
181 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
182 def __getattr__(self, key): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
183 return getattr(self.fp, key) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
184 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
185 def write(self, s): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
186 self.fp.write(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
187 self.offset += len(s) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
188 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
189 def tell(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
190 return self.offset |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
191 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8527
diff
changeset
|
192 class zipit(object): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
193 '''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
|
194 or compressed with deflate.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
195 |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
196 def __init__(self, dest, mtime, compress=True): |
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
|
197 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
|
198 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
|
199 dest.tell() |
2169
4564794b6f55
Combine catching exceptions added in dd4ec4576cc8 in one except statement.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2168
diff
changeset
|
200 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
|
201 dest = tellable(dest) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
202 self.z = zipfile.ZipFile(dest, 'w', |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
203 compress and zipfile.ZIP_DEFLATED or |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
204 zipfile.ZIP_STORED) |
12319
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
205 |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
206 # Python's zipfile module emits deprecation warnings if we try |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
207 # to store files with a date before 1980. |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
208 epoch = 315532800 # calendar.timegm((1980, 1, 1, 0, 0, 0, 1, 1, 0)) |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
209 if mtime < epoch: |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
210 mtime = epoch |
381f131220ad
archive: set date to 1980 for very old zip files
Martin Geisler <mg@aragost.com>
parents:
10282
diff
changeset
|
211 |
17628
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
212 self.mtime = mtime |
2477
857591c586e0
use commit time as mtime for file archives.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2353
diff
changeset
|
213 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
|
214 |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
215 def addfile(self, name, mode, islink, data): |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
216 i = zipfile.ZipInfo(name, self.date_time) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
217 i.compress_type = self.z.compression |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
218 # 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
|
219 # set to unix (id 3). |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
220 i.create_system = 3 |
17429
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
221 ftype = _UNX_IFREG |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
222 if islink: |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25615
diff
changeset
|
223 mode = 0o777 |
17429
72fa4ef2245f
declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents:
17108
diff
changeset
|
224 ftype = _UNX_IFLNK |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
225 i.external_attr = (mode | ftype) << 16L |
17628
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
226 # add "extended-timestamp" extra block, because zip archives |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
227 # without this will be extracted with unexpected timestamp, |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
228 # if TZ is not configured as GMT |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
229 i.extra += struct.pack('<hhBl', |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
230 0x5455, # block type: "extended-timestamp" |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
231 1 + 4, # size of this block |
133d13e44544
archival: add "extended-timestamp" extra block for zip archives (issue3600)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17108
diff
changeset
|
232 1, # "modification time is present" |
18301
49ad7030ecc4
archival: pass integer to struct.pack int field instead of float
Mads Kiilerich <madski@unity3d.com>
parents:
17629
diff
changeset
|
233 int(self.mtime)) # last modification (UTC) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
234 self.z.writestr(i, data) |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
235 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
236 def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
237 self.z.close() |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
238 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8527
diff
changeset
|
239 class fileit(object): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
240 '''write archive as files in directory.''' |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
241 |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
242 def __init__(self, name, mtime): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
243 self.basedir = name |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13668
diff
changeset
|
244 self.opener = scmutil.opener(self.basedir) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
245 |
4831
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
246 def addfile(self, name, mode, islink, data): |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
247 if islink: |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
248 self.opener.symlink(data, name) |
6f08bc1bd00b
archive: add symlink support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4830
diff
changeset
|
249 return |
4830
74f36b1027f4
archive: use util.opener when archiving files.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4731
diff
changeset
|
250 f = self.opener(name, "w", atomictemp=True) |
74f36b1027f4
archive: use util.opener when archiving files.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4731
diff
changeset
|
251 f.write(data) |
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
13970
diff
changeset
|
252 f.close() |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
253 destfile = os.path.join(self.basedir, name) |
4830
74f36b1027f4
archive: use util.opener when archiving files.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4731
diff
changeset
|
254 os.chmod(destfile, mode) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
255 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
256 def done(self): |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
257 pass |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
258 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
259 archivers = { |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
260 'files': fileit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
261 'tar': tarit, |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
262 'tbz2': lambda name, mtime: tarit(name, mtime, 'bz2'), |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
263 'tgz': lambda name, mtime: tarit(name, mtime, 'gz'), |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
264 'uzip': lambda name, mtime: zipit(name, mtime, False), |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
265 'zip': zipit, |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
266 } |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
267 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
268 def archive(repo, dest, node, kind, decode=True, matchfn=None, |
24172
e0f06228bb66
archive: change the default prefix to '' from None
Matt Harbison <matt_harbison@yahoo.com>
parents:
23645
diff
changeset
|
269 prefix='', mtime=None, subrepos=False): |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
270 '''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
|
271 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
272 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
|
273 object to write archive to. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
274 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
275 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
|
276 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
277 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
|
278 hgrc. |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
279 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
280 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
|
281 |
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
282 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
|
283 |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
284 if kind == 'files': |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
285 if prefix: |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
286 raise util.Abort(_('cannot give prefix when archiving to files')) |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
287 else: |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
288 prefix = tidyprefix(dest, kind, prefix) |
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
289 |
4951
667290b6c95e
archive: delay extraction of file revisions
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4831
diff
changeset
|
290 def write(name, mode, islink, getdata): |
667290b6c95e
archive: delay extraction of file revisions
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4831
diff
changeset
|
291 data = getdata() |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
292 if decode: |
4005
656e06eebda7
replace filehandle version of wwrite with wwritedata
Matt Mackall <mpm@selenic.com>
parents:
3968
diff
changeset
|
293 data = repo.wwritedata(name, data) |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
294 archiver.addfile(prefix + name, mode, islink, data) |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
295 |
6019
b70a530bdb93
cleanly abort on unknown archive type (issue966)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
4951
diff
changeset
|
296 if kind not in archivers: |
6913
580d5e6bfc1f
move % out of translatable strings
Martin Geisler <mg@daimi.au.dk>
parents:
6749
diff
changeset
|
297 raise util.Abort(_("unknown archive type '%s'") % kind) |
6749
51b0e799352f
manifest: remove execf/linkf methods
Matt Mackall <mpm@selenic.com>
parents:
6747
diff
changeset
|
298 |
51b0e799352f
manifest: remove execf/linkf methods
Matt Mackall <mpm@selenic.com>
parents:
6747
diff
changeset
|
299 ctx = repo[node] |
11558
d8f6458434ec
archival: remove prefix argument from archivers
Martin Geisler <mg@lazybytes.net>
parents:
11557
diff
changeset
|
300 archiver = archivers[kind](dest, mtime or ctx.date()[0]) |
6749
51b0e799352f
manifest: remove execf/linkf methods
Matt Mackall <mpm@selenic.com>
parents:
6747
diff
changeset
|
301 |
6183
0750e4ba9d3d
Add config option to disable putting .hg_archival.txt inside archives.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6020
diff
changeset
|
302 if repo.ui.configbool("ui", "archivemeta", True): |
16919
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
303 name = '.hg_archival.txt' |
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
304 if not matchfn or matchfn(name): |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25615
diff
changeset
|
305 write(name, 0o644, False, lambda: buildmetadata(ctx)) |
9614
58edd448da4f
archive: add branch and tag informations to the .hg_archival.txt file
Gilles Moris <gilles.moris@free.fr>
parents:
8778
diff
changeset
|
306 |
16919
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
307 if matchfn: |
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
308 files = [f for f in ctx.manifest().keys() if matchfn(f)] |
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
309 else: |
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
310 files = ctx.manifest().keys() |
51932c835b74
archive: make progress only show files that are actually archived
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15057
diff
changeset
|
311 total = len(files) |
18967
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
312 if total: |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
313 files.sort() |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
314 repo.ui.progress(_('archiving'), 0, unit=_('files'), total=total) |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
315 for i, f in enumerate(files): |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
316 ff = ctx.flags(f) |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25615
diff
changeset
|
317 write(f, 'x' in ff and 0o755 or 0o644, 'l' in ff, ctx[f].data) |
18967
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
318 repo.ui.progress(_('archiving'), i + 1, item=f, |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
319 unit=_('files'), total=total) |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
320 repo.ui.progress(_('archiving'), None) |
12323
f00953d9533c
subrepo: add support for 'hg archive'
Martin Geisler <mg@aragost.com>
parents:
12321
diff
changeset
|
321 |
f00953d9533c
subrepo: add support for 'hg archive'
Martin Geisler <mg@aragost.com>
parents:
12321
diff
changeset
|
322 if subrepos: |
18364
6252b4f1c4b4
subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents:
18344
diff
changeset
|
323 for subpath in sorted(ctx.substate): |
25601
3ec8351fa6ed
archive: support 'wdir()'
Matt Harbison <matt_harbison@yahoo.com>
parents:
25149
diff
changeset
|
324 sub = ctx.workingsub(subpath) |
17108
1894dac619de
subrepo: propagate matcher to subrepos when archiving
Matt Harbison <matt_harbison@yahoo.com>
parents:
16919
diff
changeset
|
325 submatch = matchmod.narrowmatcher(subpath, matchfn) |
23575
a2f139d25845
subrepo: drop the 'ui' parameter to archive()
Matt Harbison <matt_harbison@yahoo.com>
parents:
18967
diff
changeset
|
326 total += sub.archive(archiver, prefix, submatch) |
18967
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
327 |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
328 if total == 0: |
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
329 raise error.Abort(_('no files match the archive pattern')) |
12323
f00953d9533c
subrepo: add support for 'hg archive'
Martin Geisler <mg@aragost.com>
parents:
12321
diff
changeset
|
330 |
2112
2b03c6733efa
add "archive" command, like "cvs export" only better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
331 archiver.done() |
18967
88d1b59f6906
archive: raise error.Abort if the file pattern matches no files
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
18364
diff
changeset
|
332 return total |