1
# Copyright (C) 2005, 2006 Canonical Development 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
98
def parse_ignore_file(f):
99
"""Read in all of the lines in the file and turn it into an ignore list"""
101
for line in f.read().decode('utf8').split('\n'):
102
line = line.rstrip('\r\n')
103
if not line or line.startswith('#'):
109
def get_user_ignores():
110
"""Get the list of user ignored files, possibly creating it."""
111
path = config.user_ignore_config_filename()
112
patterns = USER_DEFAULTS[:]
115
except (IOError, OSError), e:
116
# open() shouldn't return an IOError without errno, but just in case
117
err = getattr(e, 'errno', None)
118
if err not in (errno.ENOENT,):
120
# Create the ignore file, and just return the default
121
# We want to ignore if we can't write to the file
122
# since get_* should be a safe operation
124
set_user_ignores(USER_DEFAULTS)
125
except (IOError, OSError), e:
126
if e.errno not in (errno.EPERM,):
131
return parse_ignore_file(f)
136
def set_user_ignores(patterns):
137
"""Fill out the user ignore file with the given patterns
139
This may raise an error if it doesn't have permission to
140
write to the user ignore file.
142
ignore_path = config.user_ignore_config_filename()
143
config.ensure_config_dir_exists()
145
# Create an empty file
146
f = open(ignore_path, 'wb')
148
for pattern in patterns:
149
f.write(pattern.encode('utf8') + '\n')
154
def add_unique_user_ignores(new_ignores):
155
"""Add entries to the user's ignore list if not present.
157
:param new_ignores: A list of ignore patterns
158
:return: The list of ignores that were added
160
ignored = set(get_user_ignores())
162
for ignore in new_ignores:
163
if ignore not in ignored:
165
to_add.append(ignore)
170
f = open(config.user_ignore_config_filename(), 'ab')
172
for pattern in to_add:
173
f.write(pattern.encode('utf8') + '\n')