~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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
21
from bzrlib import (
22
    config,
23
    globbing,
24
    )
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
25
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.
26
# This was the full ignore list for bzr 0.8
27
# please keep these sorted (in C locale order) to aid merging
28
OLD_DEFAULTS = [
29
    '#*#',
30
    '*$',
31
    '*,v',
32
    '*.BAK',
33
    '*.a',
34
    '*.bak',
35
    '*.elc',
36
    '*.exe',
37
    '*.la',
38
    '*.lo',
39
    '*.o',
40
    '*.obj',
41
    '*.orig',
42
    '*.py[oc]',
43
    '*.so',
44
    '*.tmp',
45
    '*~',
46
    '.#*',
47
    '.*.sw[nop]',
48
    '.*.tmp',
49
    # Our setup tests dump .python-eggs in the bzr source tree root
50
    './.python-eggs',
51
    '.DS_Store',
52
    '.arch-ids',
53
    '.arch-inventory',
54
    '.bzr.log',
55
    '.del-*',
56
    '.git',
57
    '.hg',
58
    '.jamdeps'
59
    '.libs',
60
    '.make.state',
61
    '.sconsign*',
62
    '.svn',
63
    '.sw[nop]',    # vim editing nameless file
64
    '.tmp*',
65
    'BitKeeper',
66
    'CVS',
67
    'CVS.adm',
68
    'RCS',
69
    'SCCS',
70
    'TAGS',
71
    '_darcs',
72
    'aclocal.m4',
73
    'autom4te*',
74
    'config.h',
75
    'config.h.in',
76
    'config.log',
77
    'config.status',
78
    'config.sub',
79
    'stamp-h',
80
    'stamp-h.in',
81
    'stamp-h1',
82
    '{arch}',
83
]
84
85
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
89
USER_DEFAULTS = [
90
    '*.a',
91
    '*.o',
92
    '*.py[co]',
93
    '*.so',
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
94
    '*.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.
95
    '*~',
96
    '.#*',
97
    '[#]*#',
98
]
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
99
100
101
def parse_ignore_file(f):
102
    """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.
103
    ignored = set()
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
104
    for line in f.read().decode('utf8').split('\n'):
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
105
        line = line.rstrip('\r\n')
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
106
        if not line or line.startswith('#'):
107
            continue
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
108
        ignored.add(globbing.normalize_pattern(line))
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
109
    return ignored
110
111
112
def get_user_ignores():
113
    """Get the list of user ignored files, possibly creating it."""
114
    path = config.user_ignore_config_filename()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
115
    patterns = set(USER_DEFAULTS)
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
116
    try:
117
        f = open(path, 'rb')
118
    except (IOError, OSError), e:
1836.1.25 by John Arbash Meinel
cleanups suggested by Martin.
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,):
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
122
            raise
123
        # 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.
124
        # We want to ignore if we can't write to the file
125
        # since get_* should be a safe operation
126
        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.
127
            _set_user_ignores(USER_DEFAULTS)
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
128
        except (IOError, OSError), e:
129
            if e.errno not in (errno.EPERM,):
130
                raise
1836.1.13 by John Arbash Meinel
Adding functions for getting user ignores.
131
        return patterns
132
133
    try:
134
        return parse_ignore_file(f)
135
    finally:
136
        f.close()
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
137
138
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.
139
def _set_user_ignores(patterns):
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
140
    """Fill out the user ignore file with the given patterns
141
142
    This may raise an error if it doesn't have permission to
143
    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.
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.
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
147
    """
148
    ignore_path = config.user_ignore_config_filename()
149
    config.ensure_config_dir_exists()
150
151
    # Create an empty file
152
    f = open(ignore_path, 'wb')
153
    try:
154
        for pattern in patterns:
155
            f.write(pattern.encode('utf8') + '\n')
156
    finally:
157
        f.close()
158
159
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
160
def add_unique_user_ignores(new_ignores):
161
    """Add entries to the user's ignore list if not present.
162
163
    :param new_ignores: A list of ignore patterns
164
    :return: The list of ignores that were added
165
    """
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
166
    ignored = get_user_ignores()
1836.1.14 by John Arbash Meinel
Adding a helper function that will only add patterns if they are missing.
167
    to_add = []
168
    for ignore in new_ignores:
2298.8.1 by Kent Gibson
Normalise ignore patterns to use '/' path separator.
169
        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.
170
        if ignore not in ignored:
171
            ignored.add(ignore)
172
            to_add.append(ignore)
173
174
    if not to_add:
175
        return []
176
177
    f = open(config.user_ignore_config_filename(), 'ab')
178
    try:
179
        for pattern in to_add:
180
            f.write(pattern.encode('utf8') + '\n')
181
    finally:
182
        f.close()
183
184
    return to_add
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
185
186
187
_runtime_ignores = set()
188
189
190
def add_runtime_ignores(ignores):
191
    """Add some ignore patterns that only exists in memory.
192
193
    This is used by some plugins that want bzr to ignore files,
194
    but don't want to change a users ignore list.
1711.2.105 by John Arbash Meinel
Updated doc
195
    (Such as a conversion script that needs to ignore temporary files,
196
    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.
197
198
    :param ignores: A list or generator of ignore patterns.
199
    :return: None
200
    """
201
    global _runtime_ignores
202
    _runtime_ignores.update(set(ignores))
203
204
205
def get_runtime_ignores():
206
    """Get the current set of runtime ignores."""
207
    return _runtime_ignores