1
# Copyright (C) 2005, 2006 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
"""Lists of ignore files, etc."""
 
 
26
# This was the full ignore list for bzr 0.8
 
 
27
# please keep these sorted (in C locale order) to aid merging
 
 
49
    # Our setup tests dump .python-eggs in the bzr source tree root
 
 
63
    '.sw[nop]',    # vim editing nameless file
 
 
86
# ~/.bazaar/ignore will be filled out using
 
 
87
# this ignore list, if it does not exist
 
 
88
# please keep these sorted (in C locale order) to aid merging
 
 
101
def parse_ignore_file(f):
 
 
102
    """Read in all of the lines in the file and turn it into an ignore list"""
 
 
104
    for line in f.read().decode('utf8').split('\n'):
 
 
105
        line = line.rstrip('\r\n')
 
 
106
        if not line or line.startswith('#'):
 
 
108
        ignored.add(globbing.normalize_pattern(line))
 
 
112
def get_user_ignores():
 
 
113
    """Get the list of user ignored files, possibly creating it."""
 
 
114
    path = config.user_ignore_config_filename()
 
 
115
    patterns = set(USER_DEFAULTS)
 
 
118
    except (IOError, OSError), e:
 
 
119
        # open() shouldn't return an IOError without errno, but just in case
 
 
120
        err = getattr(e, 'errno', None)
 
 
121
        if err not in (errno.ENOENT,):
 
 
123
        # Create the ignore file, and just return the default
 
 
124
        # We want to ignore if we can't write to the file
 
 
125
        # since get_* should be a safe operation
 
 
127
            _set_user_ignores(USER_DEFAULTS)
 
 
128
        except (IOError, OSError), e:
 
 
129
            if e.errno not in (errno.EPERM,):
 
 
134
        return parse_ignore_file(f)
 
 
139
def _set_user_ignores(patterns):
 
 
140
    """Fill out the user ignore file with the given patterns
 
 
142
    This may raise an error if it doesn't have permission to
 
 
143
    write to the user ignore file.
 
 
144
    This is mostly used for testing, since it would be
 
 
145
    bad form to rewrite a user's ignore list.
 
 
146
    bzrlib only writes this file if it does not exist.
 
 
148
    ignore_path = config.user_ignore_config_filename()
 
 
149
    config.ensure_config_dir_exists()
 
 
151
    # Create an empty file
 
 
152
    f = open(ignore_path, 'wb')
 
 
154
        for pattern in patterns:
 
 
155
            f.write(pattern.encode('utf8') + '\n')
 
 
160
def add_unique_user_ignores(new_ignores):
 
 
161
    """Add entries to the user's ignore list if not present.
 
 
163
    :param new_ignores: A list of ignore patterns
 
 
164
    :return: The list of ignores that were added
 
 
166
    ignored = get_user_ignores()
 
 
168
    for ignore in new_ignores:
 
 
169
        ignore = globbing.normalize_pattern(ignore)
 
 
170
        if ignore not in ignored:
 
 
172
            to_add.append(ignore)
 
 
177
    f = open(config.user_ignore_config_filename(), 'ab')
 
 
179
        for pattern in to_add:
 
 
180
            f.write(pattern.encode('utf8') + '\n')
 
 
187
_runtime_ignores = set()
 
 
190
def add_runtime_ignores(ignores):
 
 
191
    """Add some ignore patterns that only exists in memory.
 
 
193
    This is used by some plugins that want bzr to ignore files,
 
 
194
    but don't want to change a users ignore list.
 
 
195
    (Such as a conversion script that needs to ignore temporary files,
 
 
196
    but does not want to modify the project's ignore list.)
 
 
198
    :param ignores: A list or generator of ignore patterns.
 
 
201
    global _runtime_ignores
 
 
202
    _runtime_ignores.update(set(ignores))
 
 
205
def get_runtime_ignores():
 
 
206
    """Get the current set of runtime ignores."""
 
 
207
    return _runtime_ignores