~bzr-pqm/bzr/bzr.dev

2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2005, 2006 Canonical Ltd
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
16
17
"""Lists of ignore files, etc."""
18
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
19
import errno
20
3528.2.3 by Jelmer Vernooij
use constant for ignore file filename.
21
import bzrlib
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
22
from bzrlib import (
3528.2.1 by Jelmer Vernooij
Move functionality to add ignores to the ignore file into a separate function.
23
    atomicfile,
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
24
    config,
25
    globbing,
26
    )
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
27
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
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',
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
96
    '*.sw[nop]',
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
97
    '*~',
98
    '.#*',
99
    '[#]*#',
100
]
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
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"""
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
105
    ignored = set()
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
106
    for line in f.read().decode('utf8').split('\n'):
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
107
        line = line.rstrip('\r\n')
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
108
        if not line or line.startswith('#'):
109
            continue
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
110
        ignored.add(globbing.normalize_pattern(line))
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
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()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
117
    patterns = set(USER_DEFAULTS)
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
118
    try:
119
        f = open(path, 'rb')
120
    except (IOError, OSError), e:
1836.1.25 by John Arbash Meinel
cleanups suggested by Martin.
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,):
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
124
            raise
125
        # Create the ignore file, and just return the default
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
126
        # We want to ignore if we can't write to the file
127
        # since get_* should be a safe operation
128
        try:
1836.1.31 by John Arbash Meinel
Make set_user_ignores a private function, and update the doc string to recommend it isn't used.
129
            _set_user_ignores(USER_DEFAULTS)
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
130
        except (IOError, OSError), e:
131
            if e.errno not in (errno.EPERM,):
132
                raise
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
133
        return patterns
134
135
    try:
136
        return parse_ignore_file(f)
137
    finally:
138
        f.close()
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
139
140
1836.1.31 by John Arbash Meinel
Make set_user_ignores a private function, and update the doc string to recommend it isn't used.
141
def _set_user_ignores(patterns):
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
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.
1836.1.31 by John Arbash Meinel
Make set_user_ignores a private function, and update the doc string to recommend it isn't used.
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.
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
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
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
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
    """
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
168
    ignored = get_user_ignores()
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
169
    to_add = []
170
    for ignore in new_ignores:
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
171
        ignore = globbing.normalize_pattern(ignore)
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
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
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
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.
1711.2.105 by John Arbash Meinel
Updated doc
197
    (Such as a conversion script that needs to ignore temporary files,
198
    but does not want to modify the project's ignore list.)
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
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
3528.2.1 by Jelmer Vernooij
Move functionality to add ignores to the ignore file into a separate function.
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.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
216
    :return:
3528.2.1 by Jelmer Vernooij
Move functionality to add ignores to the ignore file into a separate function.
217
    """
3528.2.3 by Jelmer Vernooij
use constant for ignore file filename.
218
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
3528.2.1 by Jelmer Vernooij
Move functionality to add ignores to the ignore file into a separate function.
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'])