~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: John Arbash Meinel
  • Date: 2010-01-13 16:23:07 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: john@arbash-meinel.com-20100113162307-0bs82td16gzih827
Update the MANIFEST.in file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Lists of ignore files, etc."""
 
18
 
 
19
import errno
 
20
 
 
21
import bzrlib
 
22
from bzrlib import (
 
23
    atomicfile,
 
24
    config,
 
25
    globbing,
 
26
    )
 
27
 
 
28
# This was the full ignore list for bzr 0.8
 
29
# please keep these sorted (in C locale order) to aid merging
 
30
OLD_DEFAULTS = [
 
31
    '#*#',
 
32
    '*$',
 
33
    '*,v',
 
34
    '*.BAK',
 
35
    '*.a',
 
36
    '*.bak',
 
37
    '*.elc',
 
38
    '*.exe',
 
39
    '*.la',
 
40
    '*.lo',
 
41
    '*.o',
 
42
    '*.obj',
 
43
    '*.orig',
 
44
    '*.py[oc]',
 
45
    '*.so',
 
46
    '*.tmp',
 
47
    '*~',
 
48
    '.#*',
 
49
    '.*.sw[nop]',
 
50
    '.*.tmp',
 
51
    # Our setup tests dump .python-eggs in the bzr source tree root
 
52
    './.python-eggs',
 
53
    '.DS_Store',
 
54
    '.arch-ids',
 
55
    '.arch-inventory',
 
56
    '.bzr.log',
 
57
    '.del-*',
 
58
    '.git',
 
59
    '.hg',
 
60
    '.jamdeps'
 
61
    '.libs',
 
62
    '.make.state',
 
63
    '.sconsign*',
 
64
    '.svn',
 
65
    '.sw[nop]',    # vim editing nameless file
 
66
    '.tmp*',
 
67
    'BitKeeper',
 
68
    'CVS',
 
69
    'CVS.adm',
 
70
    'RCS',
 
71
    'SCCS',
 
72
    'TAGS',
 
73
    '_darcs',
 
74
    'aclocal.m4',
 
75
    'autom4te*',
 
76
    'config.h',
 
77
    'config.h.in',
 
78
    'config.log',
 
79
    'config.status',
 
80
    'config.sub',
 
81
    'stamp-h',
 
82
    'stamp-h.in',
 
83
    'stamp-h1',
 
84
    '{arch}',
 
85
]
 
86
 
 
87
 
 
88
# ~/.bazaar/ignore will be filled out using
 
89
# this ignore list, if it does not exist
 
90
# please keep these sorted (in C locale order) to aid merging
 
91
USER_DEFAULTS = [
 
92
    '*.a',
 
93
    '*.o',
 
94
    '*.py[co]',
 
95
    '*.so',
 
96
    '*.sw[nop]',
 
97
    '*~',
 
98
    '.#*',
 
99
    '[#]*#',
 
100
]
 
101
 
 
102
 
 
103
def parse_ignore_file(f):
 
104
    """Read in all of the lines in the file and turn it into an ignore list"""
 
105
    ignored = set()
 
106
    for line in f.read().decode('utf8').split('\n'):
 
107
        line = line.rstrip('\r\n')
 
108
        if not line or line.startswith('#'):
 
109
            continue
 
110
        ignored.add(globbing.normalize_pattern(line))
 
111
    return ignored
 
112
 
 
113
 
 
114
def get_user_ignores():
 
115
    """Get the list of user ignored files, possibly creating it."""
 
116
    path = config.user_ignore_config_filename()
 
117
    patterns = set(USER_DEFAULTS)
 
118
    try:
 
119
        f = open(path, 'rb')
 
120
    except (IOError, OSError), e:
 
121
        # open() shouldn't return an IOError without errno, but just in case
 
122
        err = getattr(e, 'errno', None)
 
123
        if err not in (errno.ENOENT,):
 
124
            raise
 
125
        # Create the ignore file, and just return the default
 
126
        # We want to ignore if we can't write to the file
 
127
        # since get_* should be a safe operation
 
128
        try:
 
129
            _set_user_ignores(USER_DEFAULTS)
 
130
        except (IOError, OSError), e:
 
131
            if e.errno not in (errno.EPERM,):
 
132
                raise
 
133
        return patterns
 
134
 
 
135
    try:
 
136
        return parse_ignore_file(f)
 
137
    finally:
 
138
        f.close()
 
139
 
 
140
 
 
141
def _set_user_ignores(patterns):
 
142
    """Fill out the user ignore file with the given patterns
 
143
 
 
144
    This may raise an error if it doesn't have permission to
 
145
    write to the user ignore file.
 
146
    This is mostly used for testing, since it would be
 
147
    bad form to rewrite a user's ignore list.
 
148
    bzrlib only writes this file if it does not exist.
 
149
    """
 
150
    ignore_path = config.user_ignore_config_filename()
 
151
    config.ensure_config_dir_exists()
 
152
 
 
153
    # Create an empty file
 
154
    f = open(ignore_path, 'wb')
 
155
    try:
 
156
        for pattern in patterns:
 
157
            f.write(pattern.encode('utf8') + '\n')
 
158
    finally:
 
159
        f.close()
 
160
 
 
161
 
 
162
def add_unique_user_ignores(new_ignores):
 
163
    """Add entries to the user's ignore list if not present.
 
164
 
 
165
    :param new_ignores: A list of ignore patterns
 
166
    :return: The list of ignores that were added
 
167
    """
 
168
    ignored = get_user_ignores()
 
169
    to_add = []
 
170
    for ignore in new_ignores:
 
171
        ignore = globbing.normalize_pattern(ignore)
 
172
        if ignore not in ignored:
 
173
            ignored.add(ignore)
 
174
            to_add.append(ignore)
 
175
 
 
176
    if not to_add:
 
177
        return []
 
178
 
 
179
    f = open(config.user_ignore_config_filename(), 'ab')
 
180
    try:
 
181
        for pattern in to_add:
 
182
            f.write(pattern.encode('utf8') + '\n')
 
183
    finally:
 
184
        f.close()
 
185
 
 
186
    return to_add
 
187
 
 
188
 
 
189
_runtime_ignores = set()
 
190
 
 
191
 
 
192
def add_runtime_ignores(ignores):
 
193
    """Add some ignore patterns that only exists in memory.
 
194
 
 
195
    This is used by some plugins that want bzr to ignore files,
 
196
    but don't want to change a users ignore list.
 
197
    (Such as a conversion script that needs to ignore temporary files,
 
198
    but does not want to modify the project's ignore list.)
 
199
 
 
200
    :param ignores: A list or generator of ignore patterns.
 
201
    :return: None
 
202
    """
 
203
    global _runtime_ignores
 
204
    _runtime_ignores.update(set(ignores))
 
205
 
 
206
 
 
207
def get_runtime_ignores():
 
208
    """Get the current set of runtime ignores."""
 
209
    return _runtime_ignores
 
210
 
 
211
 
 
212
def tree_ignores_add_patterns(tree, name_pattern_list):
 
213
    """Retrieve a list of ignores from the ignore file in a tree.
 
214
 
 
215
    :param tree: Tree to retrieve the ignore list from.
 
216
    :return:
 
217
    """
 
218
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
 
219
    if tree.has_filename(ifn):
 
220
        f = open(ifn, 'rt')
 
221
        try:
 
222
            igns = f.read().decode('utf-8')
 
223
        finally:
 
224
            f.close()
 
225
    else:
 
226
        igns = ""
 
227
 
 
228
    # TODO: If the file already uses crlf-style termination, maybe
 
229
    # we should use that for the newly added lines?
 
230
 
 
231
    if igns and igns[-1] != '\n':
 
232
        igns += '\n'
 
233
    for name_pattern in name_pattern_list:
 
234
        igns += name_pattern + '\n'
 
235
 
 
236
    f = atomicfile.AtomicFile(ifn, 'wb')
 
237
    try:
 
238
        f.write(igns.encode('utf-8'))
 
239
        f.commit()
 
240
    finally:
 
241
        f.close()
 
242
 
 
243
    if not tree.path2id('.bzrignore'):
 
244
        tree.add(['.bzrignore'])