annotate extensions/kwarchive.py @ 32:cfa53f8a6607

Refactor the class' monkey-patching
author Franz Glasner <hg@dom66.de>
date Sat, 11 Nov 2017 17:17:00 +0100
parents 0597395e2d18
children dc8db9693262
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
2 """archive with keyword expansion into selected files
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
3 """
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
4
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
5 from __future__ import absolute_import
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
6
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
7
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
8 __version__ = "0.0.dev1"
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
9
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
10 __author__ = "Franz Glasner"
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
11
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
12
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
13 import os
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
14
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
15 from mercurial.i18n import _
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
16 from mercurial import (archival, commands, cmdutil, error, pycompat,
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
17 scmutil, util)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
18
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
19
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20 testedwith = "4.3.2"
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23 cmdtable = {}
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25 command = cmdutil.command(cmdtable)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
27
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
28 @command('kwarchive',
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
29 [('', 'no-decode', None, _('do not pass files through decoders')),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
30 ('p', 'prefix', '', _('directory prefix for files in archive'),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
31 _('PREFIX')),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
32 ('r', 'rev', '', _('revision to distribute'), _('REV')),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33 ('t', 'type', '', _('type of distribution to create'), _('TYPE')),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
34 ] + cmdutil.subrepoopts + cmdutil.walkopts,
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
35 _('[OPTION]... DEST'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
36 def kwarchive(ui, repo, dest, **opts):
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
37 '''create an unversioned archive of a repository revision with some keywords expanded
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
38
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
39 By default, the revision used is the parent of the working
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
40 directory; use -r/--rev to specify a different revision.
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
41
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
42 The archive type is automatically detected based on file
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
43 extension (to override, use -t/--type).
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
44
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
45 .. container:: verbose
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
46
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
47 Examples:
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
48
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
49 - create a zip file containing the 1.0 release::
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
50
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
51 hg archive -r 1.0 project-1.0.zip
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
52
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
53 - create a tarball excluding .hg files::
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
54
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
55 hg archive project.tar.gz -X ".hg*"
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
56
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
57 Valid types are:
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
58
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
59 :``files``: a directory full of files (default)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
60 :``tar``: tar archive, uncompressed
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
61 :``tbz2``: tar archive, compressed using bzip2
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
62 :``tgz``: tar archive, compressed using gzip
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
63 :``uzip``: zip archive, uncompressed
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
64 :``zip``: zip archive, compressed using deflate
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
65
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
66 The exact name of the destination archive or directory is given
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
67 using a format string; see :hg:`help export` for details.
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
68
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
69 Each member added to an archive file has a directory prefix
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
70 prepended. Use -p/--prefix to specify a format string for the
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
71 prefix. The default is the basename of the archive, with suffixes
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
72 removed.
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
73
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
74 Returns 0 on success.
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
75 '''
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
76
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
77 opts = pycompat.byteskwargs(opts)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
78 ctx = scmutil.revsingle(repo, opts.get('rev'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
79 if not ctx:
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
80 raise error.Abort(_('no working directory: please specify a revision'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
81 node = ctx.node()
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
82 dest = cmdutil.makefilename(repo, dest, node)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
83 if os.path.realpath(dest) == repo.root:
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
84 raise error.Abort(_('repository root cannot be destination'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
85
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
86 kind = opts.get('type') or archival.guesskind(dest) or 'files'
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
87 prefix = opts.get('prefix')
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
88
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
89 if dest == '-':
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
90 if kind == 'files':
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
91 raise error.Abort(_('cannot archive plain files to stdout'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
92 dest = cmdutil.makefileobj(repo, dest)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
93 if not prefix:
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
94 prefix = os.path.basename(repo.root) + '-%h'
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
95
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
96 prefix = cmdutil.makefilename(repo, prefix, node)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
97 matchfn = scmutil.match(ctx, [], opts)
29
58f47399691c New successful PoC: monkey-patch all relevant archiver's "addfile()" method
Franz Glasner <hg@dom66.de>
parents: 28
diff changeset
98
58f47399691c New successful PoC: monkey-patch all relevant archiver's "addfile()" method
Franz Glasner <hg@dom66.de>
parents: 28
diff changeset
99 #
31
0597395e2d18 Comment: better explanation of what is monkey-patched
Franz Glasner <hg@dom66.de>
parents: 30
diff changeset
100 # Monkey patch archival's archivers classes so that an archiver's "addfile()"
29
58f47399691c New successful PoC: monkey-patch all relevant archiver's "addfile()" method
Franz Glasner <hg@dom66.de>
parents: 28
diff changeset
101 # expands keywords
58f47399691c New successful PoC: monkey-patch all relevant archiver's "addfile()" method
Franz Glasner <hg@dom66.de>
parents: 28
diff changeset
102 #
32
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
103 for ac in ("fileit", "tarit", "zipit",):
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
104 patch_archiver_class(ac)
30
1670760f695f Monkey-patch all current archivers in mercurial.archival: fileit, tarit and ziptit
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
105
28
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
106 archival.archive(repo, dest, node, kind, not opts.get('no_decode'),
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
107 matchfn, prefix, subrepos=opts.get('subrepos'))
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
108
32
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
109 # XXX FIXME: Should the original methods be restored here?
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
110
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
111
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
112 def patch_archiver_class(archivername):
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
113 """Patch an archiver class and return the original unbound method"""
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
114
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
115 archiver_class = getattr(archival, archivername)
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
116 orig_addfile = getattr(archiver_class, "addfile")
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
117
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
118 def new_addfile(self, name, mode, isline, data):
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
119 print "==> ARCHIVER(%s)" % archivername, name, mode, isline
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
120 return orig_addfile(self, name, mode, isline, data)
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
121
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
122 setattr(archiver_class, "addfile", new_addfile)
cfa53f8a6607 Refactor the class' monkey-patching
Franz Glasner <hg@dom66.de>
parents: 31
diff changeset
123 return orig_addfile
29
58f47399691c New successful PoC: monkey-patch all relevant archiver's "addfile()" method
Franz Glasner <hg@dom66.de>
parents: 28
diff changeset
124
28
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
125
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
126 def filter2(s, params, ui, **kwargs):
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
127 print "=============>", repr(params), repr(kwargs)
c8b7d0635656 PoC for archive with keyword expansion: works w/o touching subrepos
Franz Glasner <hg@dom66.de>
parents:
diff changeset
128 return s