hgext/convert/common.py
author Patrick Mezard <pmezard@gmail.com>
Sat, 26 Jan 2008 20:04:31 +0100
changeset 5959 0162c6cc045e
parent 5832 2192ed187319
parent 5934 e495f3f35b2d
child 5996 3f9ce63da18c
permissions -rw-r--r--
Merge with crew-stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4536
cc9b79216a76 Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents: 4532
diff changeset
     1
# common code for the convert extension
5510
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
     2
import base64, errno
5832
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
     3
import os
5127
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
     4
import cPickle as pickle
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
     5
from mercurial import util
5513
f0c58fd4b798 convert: add support for Subversion as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5512
diff changeset
     6
from mercurial.i18n import _
5127
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
     7
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
     8
def encodeargs(args):
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
     9
    def encodearg(s):
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    10
        lines = base64.encodestring(s)
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    11
        lines = [l.splitlines()[0] for l in lines]
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    12
        return ''.join(lines)
5143
d4fa6bafc43a Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5127
diff changeset
    13
5127
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    14
    s = pickle.dumps(args)
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    15
    return encodearg(s)
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    16
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    17
def decodeargs(s):
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    18
    s = base64.decodestring(s)
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
    19
    return pickle.loads(s)
4513
ac2fe196ac9b Turns convert.py into a real extension
Edouard Gomez <ed.gomez@free.fr>
parents: 4512
diff changeset
    20
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
    21
def checktool(exe, name=None):
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
    22
    name = name or exe
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
    23
    if not util.find_exe(exe):
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
    24
        raise util.Abort('cannot find required "%s" tool' % name)
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
    25
3953
fad134931327 convert-repo: add basic CVS import support
Matt Mackall <mpm@selenic.com>
parents: 3939
diff changeset
    26
class NoRepo(Exception): pass
3938
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    27
5400
d9057f00d343 convert: change SKIPREV to 'SKIP'
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
    28
SKIPREV = 'SKIP'
5374
e710874247d1 convert: allow the converter_source to say "skip this revision"
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5373
diff changeset
    29
4448
af013ae3ca10 use documented convert-repo interface
Daniel Holth <dholth@fastmail.fm>
parents: 4447
diff changeset
    30
class commit(object):
5439
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5400
diff changeset
    31
    def __init__(self, author, date, desc, parents, branch=None, rev=None,
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5400
diff changeset
    32
                 extra={}):
5012
be25decfdb13 convert: make commit constructor clearer and less magical
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    33
        self.author = author
be25decfdb13 convert: make commit constructor clearer and less magical
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    34
        self.date = date
5024
7963438881f5 convert: empty log messages are OK as of 7f5c3fb0a37d
Bryan O'Sullivan <bos@serpentine.com>
parents: 5012
diff changeset
    35
        self.desc = desc
5012
be25decfdb13 convert: make commit constructor clearer and less magical
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    36
        self.parents = parents
be25decfdb13 convert: make commit constructor clearer and less magical
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    37
        self.branch = branch
be25decfdb13 convert: make commit constructor clearer and less magical
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    38
        self.rev = rev
5439
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5400
diff changeset
    39
        self.extra = extra
3954
9af4b853ed4d convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents: 3953
diff changeset
    40
4448
af013ae3ca10 use documented convert-repo interface
Daniel Holth <dholth@fastmail.fm>
parents: 4447
diff changeset
    41
class converter_source(object):
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    42
    """Conversion source interface"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    43
5556
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
    44
    def __init__(self, ui, path=None, rev=None):
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    45
        """Initialize conversion source (or raise NoRepo("message")
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    46
        exception if path is not a valid repository)"""
4810
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    47
        self.ui = ui
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    48
        self.path = path
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    49
        self.rev = rev
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    50
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    51
        self.encoding = 'utf-8'
4812
a5209b0487e0 convert: export revmap to source.
Brendan Cully <brendan@kublai.com>
parents: 4810
diff changeset
    52
5356
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    53
    def before(self):
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    54
        pass
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    55
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    56
    def after(self):
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    57
        pass
f0931c0240b4 convert: add before/after hooks for converter sources
Bryan O'Sullivan <bos@serpentine.com>
parents: 5287
diff changeset
    58
5510
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
    59
    def setrevmap(self, revmap):
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
    60
        """set the map of already-converted revisions"""
4813
1fcdf2fe3d7c convert: svn: use revmap to parse only new revisions in incremental conversions
Brendan Cully <brendan@kublai.com>
parents: 4812
diff changeset
    61
        pass
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    62
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    63
    def getheads(self):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    64
        """Return a list of this repository's heads"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    65
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    66
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    67
    def getfile(self, name, rev):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    68
        """Return file contents as a string"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    69
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    70
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    71
    def getmode(self, name, rev):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    72
        """Return file mode, eg. '', 'x', or 'l'"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    73
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    74
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    75
    def getchanges(self, version):
5121
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 5112
diff changeset
    76
        """Returns a tuple of (files, copies)
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 5112
diff changeset
    77
        Files is a sorted list of (filename, id) tuples for all files changed
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 5112
diff changeset
    78
        in version, where id is the source revision id of the file.
4516
96d8a56d4ef9 Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4515
diff changeset
    79
5121
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 5112
diff changeset
    80
        copies is a dictionary of dest: source
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 5112
diff changeset
    81
        """
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    82
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    83
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    84
    def getcommit(self, version):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    85
        """Return the commit object for version"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    86
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    87
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    88
    def gettags(self):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    89
        """Return the tags as a dictionary of name: revision"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    90
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
    91
4759
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
    92
    def recode(self, s, encoding=None):
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
    93
        if not encoding:
4810
c2d529f288a1 convert: move some code into common init function
Brendan Cully <brendan@kublai.com>
parents: 4807
diff changeset
    94
            encoding = self.encoding or 'utf-8'
4957
cdd33a048289 removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4873
diff changeset
    95
5287
c6f932d3e0f6 Don't decode unicode strings.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5173
diff changeset
    96
        if isinstance(s, unicode):
c6f932d3e0f6 Don't decode unicode strings.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5173
diff changeset
    97
            return s.encode("utf-8")
4759
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
    98
        try:
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
    99
            return s.decode(encoding).encode("utf-8")
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   100
        except:
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   101
            try:
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   102
                return s.decode("latin-1").encode("utf-8")
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   103
            except:
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   104
                return s.decode(encoding, "replace").encode("utf-8")
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4590
diff changeset
   105
5377
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   106
    def getchangedfiles(self, rev, i):
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   107
        """Return the files changed by rev compared to parent[i].
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5556
diff changeset
   108
5377
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   109
        i is an index selecting one of the parents of rev.  The return
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   110
        value should be the list of files that are different in rev and
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   111
        this parent.
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   112
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   113
        If rev has no parents, i is None.
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5556
diff changeset
   114
5377
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   115
        This function is only needed to support --filemap
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   116
        """
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   117
        raise NotImplementedError()
756a43a30e34 convert: readd --filemap
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5374
diff changeset
   118
5554
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5529
diff changeset
   119
    def converted(self, rev, sinkrev):
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5529
diff changeset
   120
        '''Notify the source that a revision has been converted.'''
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5529
diff changeset
   121
        pass
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5529
diff changeset
   122
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5529
diff changeset
   123
4448
af013ae3ca10 use documented convert-repo interface
Daniel Holth <dholth@fastmail.fm>
parents: 4447
diff changeset
   124
class converter_sink(object):
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   125
    """Conversion sink (target) interface"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   126
4513
ac2fe196ac9b Turns convert.py into a real extension
Edouard Gomez <ed.gomez@free.fr>
parents: 4512
diff changeset
   127
    def __init__(self, ui, path):
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   128
        """Initialize conversion sink (or raise NoRepo("message")
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   129
        exception if path is not a valid repository)
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   130
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   131
        created is a list of paths to remove if a fatal error occurs
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   132
        later"""
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   133
        self.ui = ui
5440
b4ae8535f834 convert: add default constructor for converter_sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5439
diff changeset
   134
        self.path = path
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
   135
        self.created = []
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   136
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   137
    def getheads(self):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   138
        """Return a list of this repository's heads"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   139
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   140
5011
89fbb0a5e8e3 convert: rename mapfile to revmapfile, so we can map more than just revs
Bryan O'Sullivan <bos@serpentine.com>
parents: 4957
diff changeset
   141
    def revmapfile(self):
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   142
        """Path to a file that will contain lines
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   143
        source_rev_id sink_rev_id
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   144
        mapping equivalent revision identifiers for each system."""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   145
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   146
4589
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
   147
    def authorfile(self):
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
   148
        """Path to a file that will contain lines
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
   149
        srcauthor=dstauthor
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
   150
        mapping equivalent authors identifiers for each system."""
4590
80fb4ec512b5 convert: fix various authormap handling bugs
Brendan Cully <brendan@kublai.com>
parents: 4589
diff changeset
   151
        return None
4589
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
   152
4447
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   153
    def putfile(self, f, e, data):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   154
        """Put file for next putcommit().
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   155
        f: path to file
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   156
        e: '', 'x', or 'l' (regular file, executable, or symlink)
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   157
        data: file contents"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   158
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   159
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   160
    def delfile(self, f):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   161
        """Delete file for next putcommit().
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   162
        f: path to file"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   163
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   164
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   165
    def putcommit(self, files, parents, commit):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   166
        """Create a revision with all changed files listed in 'files'
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   167
        and having listed parents. 'commit' is a commit object containing
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   168
        at a minimum the author, date, and message for this changeset.
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   169
        Called after putfile() and delfile() calls. Note that the sink
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   170
        repository is not told to update itself to a particular revision
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   171
        (or even what that revision would be) before it receives the
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   172
        file data."""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   173
        raise NotImplementedError()
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   174
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   175
    def puttags(self, tags):
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   176
        """Put tags into sink.
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   177
        tags: {tagname: sink_rev_id, ...}"""
1b75e0eff532 document conversion interface
Daniel Holth <dholth@fastmail.fm>
parents: 4114
diff changeset
   178
        raise NotImplementedError()
5127
39b6eaee6fd7 convert: replace fork with subprocess call.
Patrick Mezard <pmezard@gmail.com>
parents: 5121
diff changeset
   179
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
   180
    def setbranch(self, branch, pbranches):
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
   181
        """Set the current branch name. Called before the first putfile
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
   182
        on the branch.
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
   183
        branch: branch name for subsequent commits
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5441
diff changeset
   184
        pbranches: (converted parent revision, parent branch) tuples"""
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
   185
        pass
5378
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   186
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   187
    def setfilemapmode(self, active):
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   188
        """Tell the destination that we're using a filemap
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   189
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   190
        Some converter_sources (svn in particular) can claim that a file
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   191
        was changed in a revision, even if there was no change.  This method
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   192
        tells the destination that we're using a filemap and that it should
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   193
        filter empty revisions.
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   194
        """
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5377
diff changeset
   195
        pass
5510
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   196
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   197
    def before(self):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   198
        pass
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   199
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   200
    def after(self):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   201
        pass
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   202
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   203
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   204
class commandline(object):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   205
    def __init__(self, ui, command):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   206
        self.ui = ui
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   207
        self.command = command
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   208
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   209
    def prerun(self):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   210
        pass
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   211
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   212
    def postrun(self):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   213
        pass
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   214
5832
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   215
    def _cmdline(self, cmd, *args, **kwargs):
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   216
        cmdline = [self.command, cmd] + list(args)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   217
        for k, v in kwargs.iteritems():
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   218
            if len(k) == 1:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   219
                cmdline.append('-' + k)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   220
            else:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   221
                cmdline.append('--' + k.replace('_', '-'))
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   222
            try:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   223
                if len(k) == 1:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   224
                    cmdline.append('' + v)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   225
                else:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   226
                    cmdline[-1] += '=' + v
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   227
            except TypeError:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   228
                pass
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   229
        cmdline = [util.shellquote(arg) for arg in cmdline]
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   230
        cmdline += ['<', util.nulldev]
5529
5499dbb445de convert: fix util.popen regression in darcs converter
Patrick Mezard <pmezard@gmail.com>
parents: 5513
diff changeset
   231
        cmdline = ' '.join(cmdline)
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   232
        self.ui.debug(cmdline, '\n')
5832
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   233
        return cmdline
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   234
5832
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   235
    def _run(self, cmd, *args, **kwargs):
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   236
        cmdline = self._cmdline(cmd, *args, **kwargs)
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   237
        self.prerun()
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   238
        try:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   239
            return util.popen(cmdline)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   240
        finally:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   241
            self.postrun()
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   242
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   243
    def run(self, cmd, *args, **kwargs):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   244
        fp = self._run(cmd, *args, **kwargs)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   245
        output = fp.read()
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   246
        self.ui.debug(output)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   247
        return output, fp.close()
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   248
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   249
    def checkexit(self, status, output=''):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   250
        if status:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   251
            if output:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   252
                self.ui.warn(_('%s error:\n') % self.command)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   253
                self.ui.warn(output)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   254
            msg = util.explain_exit(status)[0]
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   255
            raise util.Abort(_('%s %s') % (self.command, msg))
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   256
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   257
    def run0(self, cmd, *args, **kwargs):
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   258
        output, status = self.run(cmd, *args, **kwargs)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   259
        self.checkexit(status, output)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   260
        return output
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   261
5832
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   262
    def getargmax(self):
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   263
        if '_argmax' in self.__dict__:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   264
            return self._argmax
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   265
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   266
        # POSIX requires at least 4096 bytes for ARG_MAX
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   267
        self._argmax = 4096
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   268
        try:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   269
            self._argmax = os.sysconf("SC_ARG_MAX")
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   270
        except:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   271
            pass
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   272
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   273
        # Windows shells impose their own limits on command line length,
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   274
        # down to 2047 bytes for cmd.exe under Windows NT/2k and 2500 bytes
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   275
        # for older 4nt.exe. See http://support.microsoft.com/kb/830473 for
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   276
        # details about cmd.exe limitations.
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   277
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   278
        # Since ARG_MAX is for command line _and_ environment, lower our limit
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   279
        # (and make happy Windows shells while doing this).
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   280
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   281
        self._argmax = self._argmax/2 - 1
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   282
        return self._argmax
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   283
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   284
    def limit_arglist(self, arglist, cmd, *args, **kwargs):
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   285
        limit = self.getargmax() - len(self._cmdline(cmd, *args, **kwargs))
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   286
        bytes = 0
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   287
        fl = []
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   288
        for fn in arglist:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   289
            b = len(fn) + 3
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   290
            if bytes + b < limit or len(fl) == 0:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   291
                fl.append(fn)
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   292
                bytes += b
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   293
            else:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   294
                yield fl
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   295
                fl = [fn]
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   296
                bytes = b
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   297
        if fl:
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   298
            yield fl
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   299
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   300
    def xargs(self, arglist, cmd, *args, **kwargs):
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   301
        for l in self.limit_arglist(arglist, cmd, *args, **kwargs):
2192ed187319 convert: add commandline.xargs(), use it in svn_sink class
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   302
            self.run0(cmd, *(list(args) + l), **kwargs)
5510
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   303
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   304
class mapfile(dict):
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   305
    def __init__(self, ui, path):
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   306
        super(mapfile, self).__init__()
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   307
        self.ui = ui
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   308
        self.path = path
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   309
        self.fp = None
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   310
        self.order = []
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   311
        self._read()
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   312
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   313
    def _read(self):
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   314
        try:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   315
            fp = open(self.path, 'r')
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   316
        except IOError, err:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   317
            if err.errno != errno.ENOENT:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   318
                raise
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   319
            return
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   320
        for line in fp:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   321
            key, value = line[:-1].split(' ', 1)
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   322
            if key not in self:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   323
                self.order.append(key)
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   324
            super(mapfile, self).__setitem__(key, value)
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   325
        fp.close()
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5556
diff changeset
   326
5510
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   327
    def __setitem__(self, key, value):
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   328
        if self.fp is None:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   329
            try:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   330
                self.fp = open(self.path, 'a')
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   331
            except IOError, err:
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   332
                raise util.Abort(_('could not open map file %r: %s') %
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   333
                                 (self.path, err.strerror))
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   334
        self.fp.write('%s %s\n' % (key, value))
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   335
        self.fp.flush()
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   336
        super(mapfile, self).__setitem__(key, value)
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   337
11d7908a3ea8 convert: abstract map files into a class
Bryan O'Sullivan <bos@serpentine.com>
parents: 5497
diff changeset
   338
    def close(self):
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   339
        if self.fp:
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   340
            self.fp.close()
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5510
diff changeset
   341
            self.fp = None