/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/ignores.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Lists of ignore files, etc."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import errno
20
 
from io import BytesIO
21
22
import os
22
23
 
23
24
import breezy
25
26
lazy_import(globals(), """
26
27
from breezy import (
27
28
    atomicfile,
 
29
    config,
28
30
    globbing,
29
31
    trace,
30
32
    )
31
33
""")
32
 
from . import (
33
 
    bedding,
 
34
from breezy.sixish import (
 
35
    BytesIO,
34
36
    )
35
37
 
36
 
# ~/.config/breezy/ignore will be filled out using
 
38
# ~/.bazaar/ignore will be filled out using
37
39
# this ignore list, if it does not exist
38
40
# please keep these sorted (in C locale order) to aid merging
39
41
USER_DEFAULTS = [
50
52
]
51
53
 
52
54
 
 
55
 
53
56
def parse_ignore_file(f):
54
57
    """Read in all of the lines in the file and turn it into an ignore list
55
 
 
56
 
    Continue in the case of utf8 decoding errors, and emit a warning when
57
 
    such and error is found. Optimise for the common case -- no decoding
 
58
    
 
59
    Continue in the case of utf8 decoding errors, and emit a warning when 
 
60
    such and error is found. Optimise for the common case -- no decoding 
58
61
    errors.
59
62
    """
60
63
    ignored = set()
65
68
    except UnicodeDecodeError:
66
69
        # Otherwise go though line by line and pick out the 'good'
67
70
        # decodable lines
68
 
        lines = ignore_file.split(b'\n')
 
71
        lines = ignore_file.split('\n')
69
72
        unicode_lines = []
70
73
        for line_number, line in enumerate(lines):
71
74
            try:
73
76
            except UnicodeDecodeError:
74
77
                # report error about line (idx+1)
75
78
                trace.warning(
76
 
                    '.bzrignore: On Line #%d, malformed utf8 character. '
77
 
                    'Ignoring line.' % (line_number + 1))
 
79
                        '.bzrignore: On Line #%d, malformed utf8 character. '
 
80
                        'Ignoring line.' % (line_number+1))
78
81
 
79
82
    # Append each line to ignore list if it's not a comment line
80
83
    for line in unicode_lines:
87
90
 
88
91
def get_user_ignores():
89
92
    """Get the list of user ignored files, possibly creating it."""
90
 
    path = bedding.user_ignore_config_path()
 
93
    path = config.user_ignore_config_filename()
91
94
    patterns = set(USER_DEFAULTS)
92
95
    try:
93
96
        f = open(path, 'rb')
101
104
        # since get_* should be a safe operation
102
105
        try:
103
106
            _set_user_ignores(USER_DEFAULTS)
104
 
        except EnvironmentError as e:
105
 
            if e.errno not in (errno.EPERM, errno.ENOENT):
 
107
        except (IOError, OSError) as e:
 
108
            if e.errno not in (errno.EPERM,):
106
109
                raise
107
110
        return patterns
108
111
 
121
124
    bad form to rewrite a user's ignore list.
122
125
    breezy only writes this file if it does not exist.
123
126
    """
124
 
    ignore_path = bedding.user_ignore_config_path()
125
 
    bedding.ensure_config_dir_exists()
 
127
    ignore_path = config.user_ignore_config_filename()
 
128
    config.ensure_config_dir_exists()
126
129
 
127
130
    # Create an empty file
128
 
    with open(ignore_path, 'wb') as f:
 
131
    f = open(ignore_path, 'wb')
 
132
    try:
129
133
        for pattern in patterns:
130
 
            f.write(pattern.encode('utf8') + b'\n')
 
134
            f.write(pattern.encode('utf8') + '\n')
 
135
    finally:
 
136
        f.close()
131
137
 
132
138
 
133
139
def add_unique_user_ignores(new_ignores):
147
153
    if not to_add:
148
154
        return []
149
155
 
150
 
    with open(bedding.user_ignore_config_path(), 'ab') as f:
 
156
    f = open(config.user_ignore_config_filename(), 'ab')
 
157
    try:
151
158
        for pattern in to_add:
152
 
            f.write(pattern.encode('utf8') + b'\n')
 
159
            f.write(pattern.encode('utf8') + '\n')
 
160
    finally:
 
161
        f.close()
153
162
 
154
163
    return to_add
155
164
 
187
196
    :return: None
188
197
    """
189
198
    # read in the existing ignores set
190
 
    ifn = tree.abspath(tree._format.ignore_filename)
 
199
    ifn = tree.abspath(breezy.IGNORE_FILENAME)
191
200
    if tree.has_filename(ifn):
192
 
        with open(ifn, 'rb') as f:
 
201
        f = open(ifn, 'rU')
 
202
        try:
193
203
            file_contents = f.read()
194
 
            if file_contents.find(b'\r\n') != -1:
195
 
                newline = b'\r\n'
196
 
            else:
197
 
                newline = b'\n'
 
204
            # figure out what kind of line endings are used
 
205
            newline = getattr(f, 'newlines', None)
 
206
            if isinstance(newline, tuple):
 
207
                newline = newline[0]
 
208
            elif newline is None:
 
209
                newline = os.linesep
 
210
        finally:
 
211
            f.close()
198
212
    else:
199
 
        file_contents = b""
200
 
        newline = os.linesep.encode()
201
 
 
202
 
    with BytesIO(file_contents) as sio:
 
213
        file_contents = ""
 
214
        newline = os.linesep
 
215
    
 
216
    sio = BytesIO(file_contents)
 
217
    try:
203
218
        ignores = parse_ignore_file(sio)
204
 
 
 
219
    finally:
 
220
        sio.close()
 
221
    
205
222
    # write out the updated ignores set
206
 
    with atomicfile.AtomicFile(ifn, 'wb') as f:
 
223
    f = atomicfile.AtomicFile(ifn, 'wb')
 
224
    try:
207
225
        # write the original contents, preserving original line endings
208
 
        f.write(file_contents)
209
 
        if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
 
226
        f.write(newline.join(file_contents.split('\n')))
 
227
        if len(file_contents) > 0 and not file_contents.endswith('\n'):
210
228
            f.write(newline)
211
229
        for pattern in name_pattern_list:
212
 
            if pattern not in ignores:
 
230
            if not pattern in ignores:
213
231
                f.write(pattern.encode('utf-8'))
214
232
                f.write(newline)
 
233
        f.commit()
 
234
    finally:
 
235
        f.close()
215
236
 
216
 
    if not tree.is_versioned(tree._format.ignore_filename):
217
 
        tree.add([tree._format.ignore_filename])
 
237
    if not tree.path2id(breezy.IGNORE_FILENAME):
 
238
        tree.add([breezy.IGNORE_FILENAME])