/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
1
#!/usr/bin/env python
2
"""\
3
Common entries, like strings, etc, for the changeset reading + writing code.
4
"""
5
6
header_str = 'Bazaar-NG (bzr) changeset v'
7
version = (0, 0, 5)
8
9
def get_header():
10
    return [
11
        header_str + '.'.join([str(v) for v in version]),
12
        'This changeset can be applied with bzr apply-changeset',
13
        ''
14
    ]
15
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
16
def canonicalize_revision(branch, revnos):
17
    """Turn some sort of revision information into a single
18
    set of from-to revision ids.
19
20
    A revision id can be None if there is no associated revison.
21
22
    :param revnos:  A list of revisions to lookup, should be at most 2 long
23
    :return: (old, new)
24
    """
25
    # If only 1 entry is given, then we assume we want just the
26
    # changeset between that entry and it's base (we assume parents[0])
27
    if len(revnos) == 0:
28
        revnos = [None, None]
29
    elif len(revnos) == 1:
30
        revnos = [None, revnos[0]]
31
32
    if revnos[1] is None:
33
        new = branch.last_patch()
34
    else:
35
        new = branch.lookup_revision(revnos[1])
36
    if revnos[0] is None:
37
        old = branch.get_revision(new).parents[0].revision_id
38
    else:
39
        old = branch.lookup_revision(revnos[0])
40
41
    return old, new
42
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
43
class ChangesetTree(object):
44
    """This class is designed to take a base tree, and re-create
45
    a final tree based on the information contained within a
46
    changeset.
47
    """
48
49
    def __init__(self, branch, changeset_info):
50
        """Initialize this ChangesetTree.
51
52
        :param branch:  This is where information will be acquired
53
                        and updated.
54
        :param changeset_info:  Information about a given changeset,
55
                                so that we can identify the base,
56
                                and other information.
57
        """
58
        self.branch = branch
59
        self.changeset_info = changeset_info
60
61
        self._build_tree()
62
63
    def _build_tree(self):
64
        """Build the final description of the tree, based on
65
        the changeset_info object.
66
        """
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
67
        self.base_tree = self.branch.revision_tree(self.changeset_info.base)
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
68
        
69
def format_highres_date(t, offset=0):
70
    """Format a date, such that it includes higher precision in the
71
    seconds field.
72
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
73
    :param t:   The local time in fractional seconds since the epoch
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
74
    :type t: float
75
    :param offset:  The timezone offset in integer seconds
76
    :type offset: int
77
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
78
    Example: format_highres_date(time.time(), -time.timezone)
79
    this will return a date stamp for right now,
80
    formatted for the local timezone.
81
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
82
    >>> from bzrlib.osutils import format_date
83
    >>> format_date(1120153132.350850105, 0)
84
    'Thu 2005-06-30 17:38:52 +0000'
85
    >>> format_highres_date(1120153132.350850105, 0)
86
    'Thu 2005-06-30 17:38:52.350850105 +0000'
87
    >>> format_date(1120153132.350850105, -5*3600)
88
    'Thu 2005-06-30 12:38:52 -0500'
89
    >>> format_highres_date(1120153132.350850105, -5*3600)
90
    'Thu 2005-06-30 12:38:52.350850105 -0500'
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
91
    >>> format_highres_date(1120153132.350850105, 7200)
92
    'Thu 2005-06-30 19:38:52.350850105 +0200'
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
93
    """
94
    import time
95
    assert isinstance(t, float)
96
    
97
    # This has to be formatted for "original" date, so that the
98
    # revision XML entry will be reproduced faithfully.
99
    if offset == None:
100
        offset = 0
101
    tt = time.gmtime(t + offset)
102
103
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
104
            + ('%.9f' % (t - int(t)))[1:] # Get the high-res seconds, but ignore the 0
105
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
106
107
def unpack_highres_date(date):
108
    """This takes the high-resolution date stamp, and
109
    converts it back into the tuple (timestamp, timezone)
110
    Where timestamp is in real seconds, and timezone is an integer
111
    number of seconds offset.
112
113
    :param date: A date formated by format_highres_date
114
    :type date: string
115
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
116
    >>> import time, random
117
    >>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
118
    (1120153132.3508501, -18000)
119
    >>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
120
    (1120153132.3508501, 0)
121
    >>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
122
    (1120153132.3508501, 7200)
123
    >>> from bzrlib.osutils import local_time_offset
124
    >>> t = time.time()
125
    >>> o = local_time_offset()
126
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
127
    >>> t == t2
128
    True
129
    >>> o == o2
130
    True
131
    >>> for count in xrange(500):
132
    ...   t += random.random()*24*3600*365*2 - 24*3600*364 # Random time within +/- 1 year
133
    ...   o = random.randint(-12,12)*3600 # Random timezone
134
    ...   date = format_highres_date(t, o)
135
    ...   t2, o2 = unpack_highres_date(date)
136
    ...   if t != t2 or o != o2:
137
    ...      print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
138
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
139
    """
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
140
    #from bzrlib.errors import BzrError
141
    from bzrlib.osutils import local_time_offset
142
    import time
143
    # Up until the first period is a datestamp that is generated
144
    # as normal from time.strftime, so use time.strptime to
145
    # parse it
146
    dot_loc = date.find('.')
147
    if dot_loc == -1:
148
        raise ValueError('Date string does not contain high-precision seconds: %r' % date)
149
    base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
150
    fract_seconds, offset = date[dot_loc:].split()
151
    fract_seconds = float(fract_seconds)
152
    offset = int(offset)
153
    offset = int(offset / 100) * 3600 + offset % 100
154
    
155
    # mktime returns the a local timestamp, not the timestamp based
156
    # on the offset given in the file, so we need to adjust based
157
    # on what the local offset is, and then re-adjust based on
158
    # offset read
159
    timestamp = time.mktime(base_time)
160
    timestamp += local_time_offset(timestamp) - offset
161
    # Add back in the fractional seconds
162
    timestamp += fract_seconds
163
    return (timestamp, offset)
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
164
165
if __name__ == '__main__':
166
    import doctest
167
    doctest.testmod()
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
168