/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-13 00:07:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1871.
  • Revision ID: john@arbash-meinel.com-20060713000738-974459fa90527670
Workaround plugins modifying the DEFAULT_IGNORE list when testing exact results.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Development 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 config
 
22
 
 
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
]
 
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"""
 
100
    ignored = []
 
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
 
105
        ignored.append(line)
 
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()
 
112
    patterns = USER_DEFAULTS[:]
 
113
    try:
 
114
        f = open(path, 'rb')
 
115
    except (IOError, OSError), e:
 
116
        if e.errno not in (errno.ENOENT,):
 
117
            raise
 
118
        # Create the ignore file, and just return the default
 
119
        # We want to ignore if we can't write to the file
 
120
        # since get_* should be a safe operation
 
121
        try:
 
122
            set_user_ignores(USER_DEFAULTS)
 
123
        except (IOError, OSError), e:
 
124
            if e.errno not in (errno.EPERM,):
 
125
                raise
 
126
        return patterns
 
127
 
 
128
    try:
 
129
        return parse_ignore_file(f)
 
130
    finally:
 
131
        f.close()
 
132
 
 
133
 
 
134
def set_user_ignores(patterns):
 
135
    """Fill out the user ignore file with the given patterns
 
136
 
 
137
    This may raise an error if it doesn't have permission to
 
138
    write to the user ignore file.
 
139
    """
 
140
    ignore_path = config.user_ignore_config_filename()
 
141
    config.ensure_config_dir_exists()
 
142
 
 
143
    # Create an empty file
 
144
    f = open(ignore_path, 'wb')
 
145
    try:
 
146
        for pattern in patterns:
 
147
            f.write(pattern.encode('utf8') + '\n')
 
148
    finally:
 
149
        f.close()
 
150
 
 
151
 
 
152
def add_unique_user_ignores(new_ignores):
 
153
    """Add entries to the user's ignore list if not present.
 
154
 
 
155
    :param new_ignores: A list of ignore patterns
 
156
    :return: The list of ignores that were added
 
157
    """
 
158
    ignored = set(get_user_ignores())
 
159
    to_add = []
 
160
    for ignore in new_ignores:
 
161
        if ignore not in ignored:
 
162
            ignored.add(ignore)
 
163
            to_add.append(ignore)
 
164
 
 
165
    if not to_add:
 
166
        return []
 
167
 
 
168
    f = open(config.user_ignore_config_filename(), 'ab')
 
169
    try:
 
170
        for pattern in to_add:
 
171
            f.write(pattern.encode('utf8') + '\n')
 
172
    finally:
 
173
        f.close()
 
174
 
 
175
    return to_add