/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:
20
20
 
21
21
import errno
22
22
import os
 
23
from cStringIO import StringIO
23
24
 
24
 
import breezy
25
 
from .lazy_import import lazy_import
 
25
import bzrlib
 
26
from bzrlib.lazy_import import lazy_import
26
27
lazy_import(globals(), """
27
 
from breezy import (
 
28
from bzrlib import (
28
29
    atomicfile,
29
30
    config,
30
31
    globbing,
31
32
    trace,
32
33
    )
33
34
""")
34
 
from breezy.sixish import (
35
 
    BytesIO,
36
 
    )
37
35
 
38
36
# ~/.bazaar/ignore will be filled out using
39
37
# this ignore list, if it does not exist
68
66
    except UnicodeDecodeError:
69
67
        # Otherwise go though line by line and pick out the 'good'
70
68
        # decodable lines
71
 
        lines = ignore_file.split(b'\n')
 
69
        lines = ignore_file.split('\n')
72
70
        unicode_lines = []
73
71
        for line_number, line in enumerate(lines):
74
72
            try:
94
92
    patterns = set(USER_DEFAULTS)
95
93
    try:
96
94
        f = open(path, 'rb')
97
 
    except (IOError, OSError) as e:
 
95
    except (IOError, OSError), e:
98
96
        # open() shouldn't return an IOError without errno, but just in case
99
97
        err = getattr(e, 'errno', None)
100
98
        if err not in (errno.ENOENT,):
104
102
        # since get_* should be a safe operation
105
103
        try:
106
104
            _set_user_ignores(USER_DEFAULTS)
107
 
        except (IOError, OSError) as e:
 
105
        except (IOError, OSError), e:
108
106
            if e.errno not in (errno.EPERM,):
109
107
                raise
110
108
        return patterns
122
120
    write to the user ignore file.
123
121
    This is mostly used for testing, since it would be
124
122
    bad form to rewrite a user's ignore list.
125
 
    breezy only writes this file if it does not exist.
 
123
    bzrlib only writes this file if it does not exist.
126
124
    """
127
125
    ignore_path = config.user_ignore_config_filename()
128
126
    config.ensure_config_dir_exists()
129
127
 
130
128
    # Create an empty file
131
 
    with open(ignore_path, 'wb') as f:
 
129
    f = open(ignore_path, 'wb')
 
130
    try:
132
131
        for pattern in patterns:
133
 
            f.write(pattern.encode('utf8') + b'\n')
 
132
            f.write(pattern.encode('utf8') + '\n')
 
133
    finally:
 
134
        f.close()
134
135
 
135
136
 
136
137
def add_unique_user_ignores(new_ignores):
150
151
    if not to_add:
151
152
        return []
152
153
 
153
 
    with open(config.user_ignore_config_filename(), 'ab') as f:
 
154
    f = open(config.user_ignore_config_filename(), 'ab')
 
155
    try:
154
156
        for pattern in to_add:
155
 
            f.write(pattern.encode('utf8') + b'\n')
 
157
            f.write(pattern.encode('utf8') + '\n')
 
158
    finally:
 
159
        f.close()
156
160
 
157
161
    return to_add
158
162
 
190
194
    :return: None
191
195
    """
192
196
    # read in the existing ignores set
193
 
    ifn = tree.abspath(tree._format.ignore_filename)
 
197
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
194
198
    if tree.has_filename(ifn):
195
 
        with open(ifn, 'rbU') as f:
 
199
        f = open(ifn, 'rU')
 
200
        try:
196
201
            file_contents = f.read()
197
202
            # figure out what kind of line endings are used
198
203
            newline = getattr(f, 'newlines', None)
199
 
            if isinstance(newline, tuple):
 
204
            if type(newline) is tuple:
200
205
                newline = newline[0]
201
206
            elif newline is None:
202
 
                newline = os.linesep.encode()
 
207
                newline = os.linesep
 
208
        finally:
 
209
            f.close()
203
210
    else:
204
 
        file_contents = b""
205
 
        newline = os.linesep.encode()
206
 
 
207
 
    sio = BytesIO(file_contents)
 
211
        file_contents = ""
 
212
        newline = os.linesep
 
213
    
 
214
    sio = StringIO(file_contents)
208
215
    try:
209
216
        ignores = parse_ignore_file(sio)
210
217
    finally:
211
218
        sio.close()
212
 
 
 
219
    
213
220
    # write out the updated ignores set
214
221
    f = atomicfile.AtomicFile(ifn, 'wb')
215
222
    try:
216
223
        # write the original contents, preserving original line endings
217
 
        f.write(newline.join(file_contents.split(b'\n')))
218
 
        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'):
219
226
            f.write(newline)
220
227
        for pattern in name_pattern_list:
221
228
            if not pattern in ignores:
225
232
    finally:
226
233
        f.close()
227
234
 
228
 
    if not tree.is_versioned(tree._format.ignore_filename):
229
 
        tree.add([tree._format.ignore_filename])
 
235
    if not tree.path2id(bzrlib.IGNORE_FILENAME):
 
236
        tree.add([bzrlib.IGNORE_FILENAME])