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