~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: Aaron Bentley
  • Date: 2005-09-13 02:42:07 UTC
  • mto: (1185.1.16)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050913024207-489d573af4b76c4d
Fixed issues with pull not having a default location after branch

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Lists of ignore files, etc."""
18
 
 
19
 
import errno
20
 
 
21
 
from bzrlib import (
22
 
    config,
23
 
    globbing,
24
 
    )
25
 
 
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',
94
 
    '*.sw[nop]',
95
 
    '*~',
96
 
    '.#*',
97
 
    '[#]*#',
98
 
]
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"""
103
 
    ignored = set()
104
 
    for line in f.read().decode('utf8').split('\n'):
105
 
        line = line.rstrip('\r\n')
106
 
        if not line or line.startswith('#'):
107
 
            continue
108
 
        ignored.add(globbing.normalize_pattern(line))
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()
115
 
    patterns = set(USER_DEFAULTS)
116
 
    try:
117
 
        f = open(path, 'rb')
118
 
    except (IOError, OSError), e:
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,):
122
 
            raise
123
 
        # Create the ignore file, and just return the default
124
 
        # We want to ignore if we can't write to the file
125
 
        # since get_* should be a safe operation
126
 
        try:
127
 
            _set_user_ignores(USER_DEFAULTS)
128
 
        except (IOError, OSError), e:
129
 
            if e.errno not in (errno.EPERM,):
130
 
                raise
131
 
        return patterns
132
 
 
133
 
    try:
134
 
        return parse_ignore_file(f)
135
 
    finally:
136
 
        f.close()
137
 
 
138
 
 
139
 
def _set_user_ignores(patterns):
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.
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.
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
 
 
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
 
    """
166
 
    ignored = get_user_ignores()
167
 
    to_add = []
168
 
    for ignore in new_ignores:
169
 
        ignore = globbing.normalize_pattern(ignore)
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
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.
195
 
    (Such as a conversion script that needs to ignore temporary files,
196
 
    but does not want to modify the project's ignore list.)
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