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

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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