/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: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

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