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."""
 
 
21
from bzrlib import config
 
 
23
# This was the full ignore list for bzr 0.8
 
 
24
# please keep these sorted (in C locale order) to aid merging
 
 
46
    # Our setup tests dump .python-eggs in the bzr source tree root
 
 
60
    '.sw[nop]',    # vim editing nameless file
 
 
83
# ~/.bazaar/ignore will be filled out using
 
 
84
# this ignore list, if it does not exist
 
 
85
# please keep these sorted (in C locale order) to aid merging
 
 
99
def parse_ignore_file(f):
 
 
100
    """Read in all of the lines in the file and turn it into an ignore list"""
 
 
102
    for line in f.read().decode('utf8').split('\n'):
 
 
103
        line = line.rstrip('/\r\n')
 
 
104
        if not line or line.startswith('#'):
 
 
110
def get_user_ignores():
 
 
111
    """Get the list of user ignored files, possibly creating it."""
 
 
112
    path = config.user_ignore_config_filename()
 
 
113
    patterns = set(USER_DEFAULTS)
 
 
116
    except (IOError, OSError), e:
 
 
117
        # open() shouldn't return an IOError without errno, but just in case
 
 
118
        err = getattr(e, 'errno', None)
 
 
119
        if err not in (errno.ENOENT,):
 
 
121
        # Create the ignore file, and just return the default
 
 
122
        # We want to ignore if we can't write to the file
 
 
123
        # since get_* should be a safe operation
 
 
125
            _set_user_ignores(USER_DEFAULTS)
 
 
126
        except (IOError, OSError), e:
 
 
127
            if e.errno not in (errno.EPERM,):
 
 
132
        return parse_ignore_file(f)
 
 
137
def _set_user_ignores(patterns):
 
 
138
    """Fill out the user ignore file with the given patterns
 
 
140
    This may raise an error if it doesn't have permission to
 
 
141
    write to the user ignore file.
 
 
142
    This is mostly used for testing, since it would be
 
 
143
    bad form to rewrite a user's ignore list.
 
 
144
    bzrlib only writes this file if it does not exist.
 
 
146
    ignore_path = config.user_ignore_config_filename()
 
 
147
    config.ensure_config_dir_exists()
 
 
149
    # Create an empty file
 
 
150
    f = open(ignore_path, 'wb')
 
 
152
        for pattern in patterns:
 
 
153
            f.write(pattern.encode('utf8') + '\n')
 
 
158
def add_unique_user_ignores(new_ignores):
 
 
159
    """Add entries to the user's ignore list if not present.
 
 
161
    :param new_ignores: A list of ignore patterns
 
 
162
    :return: The list of ignores that were added
 
 
164
    ignored = get_user_ignores()
 
 
166
    for ignore in new_ignores:
 
 
167
        ignore = ignore.rstrip('/')
 
 
168
        if ignore not in ignored:
 
 
170
            to_add.append(ignore)
 
 
175
    f = open(config.user_ignore_config_filename(), 'ab')
 
 
177
        for pattern in to_add:
 
 
178
            f.write(pattern.encode('utf8') + '\n')
 
 
185
_runtime_ignores = set()
 
 
188
def add_runtime_ignores(ignores):
 
 
189
    """Add some ignore patterns that only exists in memory.
 
 
191
    This is used by some plugins that want bzr to ignore files,
 
 
192
    but don't want to change a users ignore list.
 
 
193
    (Such as a conversion script that needs to ignore temporary files,
 
 
194
    but does not want to modify the project's ignore list.)
 
 
196
    :param ignores: A list or generator of ignore patterns.
 
 
199
    global _runtime_ignores
 
 
200
    _runtime_ignores.update(set(ignores))
 
 
203
def get_runtime_ignores():
 
 
204
    """Get the current set of runtime ignores."""
 
 
205
    return _runtime_ignores