/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/generate_ids.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from __future__ import absolute_import
20
20
 
21
 
from ..lazy_import import lazy_import
 
21
from .lazy_import import lazy_import
22
22
lazy_import(globals(), """
23
23
import time
24
24
 
28
28
    )
29
29
""")
30
30
 
31
 
from .. import (
 
31
from . import (
 
32
    errors,
32
33
    lazy_regex,
33
34
    )
34
 
from ..sixish import text_type
35
35
 
36
36
# the regex removes any weird characters; we don't escape them
37
37
# but rather just pull them out
38
 
_file_id_chars_re = lazy_regex.lazy_compile(b'[^\\w.]')
39
 
_rev_id_chars_re = lazy_regex.lazy_compile(b'[^-\\w.+@]')
 
38
_file_id_chars_re = lazy_regex.lazy_compile('[^\\w.]')
 
39
_rev_id_chars_re = lazy_regex.lazy_compile('[^-\\w.+@]')
40
40
_gen_file_id_suffix = None
41
41
_gen_file_id_serial = 0
42
42
 
59
59
    #           suffix forever.
60
60
    global _gen_file_id_suffix, _gen_file_id_serial
61
61
    if _gen_file_id_suffix is None:
62
 
        _gen_file_id_suffix = ("-%s-%s-" % (
63
 
            osutils.compact_date(time.time()), osutils.rand_chars(16))
 
62
        _gen_file_id_suffix =  ("-%s-%s-" % (
 
63
                osutils.compact_date(time.time()), osutils.rand_chars(16))
64
64
            ).encode("ascii")
65
65
    _gen_file_id_serial += 1
66
66
    return b"%s%d" % (_gen_file_id_suffix, _gen_file_id_serial)
71
71
 
72
72
    The uniqueness is supplied from _next_id_suffix.
73
73
    """
74
 
    if isinstance(name, text_type):
75
 
        name = name.encode('ascii', 'replace')
76
74
    # The real randomness is in the _next_id_suffix, the
77
75
    # rest of the identifier is just to be nice.
78
76
    # So we:
84
82
    #    filesystems
85
83
    # 4) Removing starting '.' characters to prevent the file ids from
86
84
    #    being considered hidden.
87
 
    ascii_word_only = _file_id_chars_re.sub(b'', name.lower())
 
85
    ascii_word_only = _file_id_chars_re.sub('', name.lower()).encode('ascii')
88
86
    short_no_dots = ascii_word_only.lstrip(b'.')[:20]
89
87
    return short_no_dots + _next_id_suffix()
90
88
 
110
108
 
111
109
    user_or_email = user_or_email.lower()
112
110
    user_or_email = user_or_email.replace(' ', '_')
113
 
    user_or_email = _rev_id_chars_re.sub(b'', user_or_email.encode('utf-8'))
 
111
    user_or_email = _rev_id_chars_re.sub('', user_or_email)
114
112
 
115
113
    # This gives 36^16 ~= 2^82.7 ~= 83 bits of entropy
116
 
    unique_chunk = osutils.rand_chars(16).encode('utf-8')
 
114
    unique_chunk = osutils.rand_chars(16)
117
115
 
118
116
    if timestamp is None:
119
117
        timestamp = time.time()
120
118
 
121
 
    rev_id = b'-'.join((user_or_email,
122
 
                        osutils.compact_date(timestamp).encode('utf-8'),
 
119
    rev_id = u'-'.join((user_or_email,
 
120
                        osutils.compact_date(timestamp),
123
121
                        unique_chunk))
124
 
    return rev_id
 
122
    return rev_id.encode('utf8')