1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2008 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """This module contains classes to set Subversion properties on files."""
24 from cStringIO
import StringIO
26 from cvs2svn_lib
.common
import warning_prefix
27 from cvs2svn_lib
.log
import Log
30 class SVNPropertySetter
:
31 """Abstract class for objects that can set properties on a SVNCommitItem."""
33 def set_properties(self
, s_item
):
34 """Set any properties that can be determined for S_ITEM.
36 S_ITEM is an instance of SVNCommitItem. This method should modify
37 S_ITEM.svn_props in place."""
39 raise NotImplementedError
42 class CVSRevisionNumberSetter(SVNPropertySetter
):
43 """Store the CVS revision number to an SVN property."""
45 def __init__(self
, propname
='cvs2svn:cvs-rev'):
46 self
.propname
= propname
48 def set_properties(self
, s_item
):
49 if self
.propname
in s_item
.svn_props
:
52 s_item
.svn_props
[self
.propname
] = s_item
.cvs_rev
.rev
53 s_item
.svn_props_changed
= True
56 class ExecutablePropertySetter(SVNPropertySetter
):
57 """Set the svn:executable property based on cvs_rev.cvs_file.executable."""
59 propname
= 'svn:executable'
61 def set_properties(self
, s_item
):
62 if self
.propname
in s_item
.svn_props
:
65 if s_item
.cvs_rev
.cvs_file
.executable
:
66 s_item
.svn_props
[self
.propname
] = '*'
69 class DescriptionPropertySetter(SVNPropertySetter
):
70 """Set the cvs:description property based on cvs_rev.cvs_file.description."""
72 def __init__(self
, propname
='cvs:description'):
73 self
.propname
= propname
75 def set_properties(self
, s_item
):
76 if self
.propname
in s_item
.svn_props
:
79 if s_item
.cvs_rev
.cvs_file
.description
:
80 s_item
.svn_props
[self
.propname
] = s_item
.cvs_rev
.cvs_file
.description
83 class CVSBinaryFileEOLStyleSetter(SVNPropertySetter
):
84 """Set the eol-style to None for files with CVS mode '-kb'."""
86 propname
= 'svn:eol-style'
88 def set_properties(self
, s_item
):
89 if self
.propname
in s_item
.svn_props
:
92 if s_item
.cvs_rev
.cvs_file
.mode
== 'b':
93 s_item
.svn_props
[self
.propname
] = None
96 class MimeMapper(SVNPropertySetter
):
97 """A class that provides mappings from file names to MIME types."""
99 propname
= 'svn:mime-type'
101 def __init__(self
, mime_types_file
):
104 for line
in file(mime_types_file
):
105 if line
.startswith("#"):
108 # format of a line is something like
110 extensions
= line
.split()
111 if len(extensions
) < 2:
113 type = extensions
.pop(0)
114 for ext
in extensions
:
115 if ext
in self
.mappings
and self
.mappings
[ext
] != type:
117 "%s: ambiguous MIME mapping for *.%s (%s or %s)\n"
118 % (warning_prefix
, ext
, self
.mappings
[ext
], type)
120 self
.mappings
[ext
] = type
122 def set_properties(self
, s_item
):
123 if self
.propname
in s_item
.svn_props
:
126 basename
, extension
= os
.path
.splitext(s_item
.cvs_rev
.cvs_file
.basename
)
128 # Extension includes the dot, so strip it (will leave extension
129 # empty if filename ends with a dot, which is ok):
130 extension
= extension
[1:]
132 # If there is no extension (or the file ends with a period), use
133 # the base name for mapping. This allows us to set mappings for
134 # files such as README or Makefile:
138 mime_type
= self
.mappings
.get(extension
, None)
139 if mime_type
is not None:
140 s_item
.svn_props
[self
.propname
] = mime_type
143 class AutoPropsPropertySetter(SVNPropertySetter
):
144 """Set arbitrary svn properties based on an auto-props configuration.
146 This class supports case-sensitive or case-insensitive pattern
147 matching. The command-line default is case-insensitive behavior,
148 consistent with Subversion (see
149 http://subversion.tigris.org/issues/show_bug.cgi?id=2036).
151 As a special extension to Subversion's auto-props handling, if a
152 property name is preceded by a '!' then that property is forced to
155 If a property specified in auto-props has already been set to a
156 different value, print a warning and leave the old property value
159 Python's treatment of whitespaces in the ConfigParser module is
160 buggy and inconsistent. Usually spaces are preserved, but if there
161 is at least one semicolon in the value, and the *first* semicolon is
162 preceded by a space, then that is treated as the start of a comment
163 and the rest of the line is silently discarded."""
165 property_name_pattern
= r
'(?P<name>[^\!\=\s]+)'
166 property_unset_re
= re
.compile(
167 r
'^\!\s*' + property_name_pattern
+ r
'$'
169 property_set_re
= re
.compile(
170 r
'^' + property_name_pattern
+ r
'\s*\=\s*(?P<value>.*)$'
172 property_novalue_re
= re
.compile(
173 r
'^' + property_name_pattern
+ r
'$'
176 quoted_re
= re
.compile(
179 comment_re = re.compile(r'\s
;')
182 """Describes the properties to be set for files matching a pattern."""
184 def __init__(self, pattern, propdict):
185 # A glob-like pattern:
186 self.pattern = pattern
187 # A dictionary of properties that should be set:
188 self.propdict = propdict
190 def match(self, basename):
191 """Does the file with the specified basename match pattern?"""
193 return fnmatch.fnmatch(basename, self.pattern)
195 def __init__(self, configfilename, ignore_case=True):
196 config = ConfigParser.ConfigParser()
198 self.transform_case = self.squash_case
200 config.optionxform = self.preserve_case
201 self.transform_case = self.preserve_case
203 configtext = open(configfilename).read()
204 if self.comment_re.search(configtext):
206 '%s: Please be aware that a space followed by a
\n'
207 'semicolon
is sometimes treated
as a comment
in configuration
\n'
208 'files
. This pattern was seen
in\n'
210 'Please make sure that you have
not inadvertently commented
\n'
211 'out part of an important line
.'
212 % (warning_prefix, configfilename,)
215 config.readfp(StringIO(configtext), configfilename)
217 sections = config.sections()
219 for section in sections:
220 if self.transform_case(section) == 'auto
-props
':
221 patterns = config.options(section)
223 for pattern in patterns:
224 value = config.get(section, pattern)
226 self._add_pattern(pattern, value)
228 def squash_case(self, s):
231 def preserve_case(self, s):
234 def _add_pattern(self, pattern, props):
236 if self.quoted_re.match(pattern):
238 '%s: Quoting
is not supported
in auto
-props
; please verify rule
\n'
239 'for %r. (Using pattern including quotation marks
.)\n'
240 % (warning_prefix, pattern,)
242 for prop in props.split(';'):
244 m = self.property_unset_re.match(prop)
246 name = m.group('name
')
248 'auto
-props
: For
%r, leaving
%r unset
.' % (pattern, name,)
250 propdict[name] = None
253 m = self.property_set_re.match(prop)
255 name = m.group('name
')
256 value = m.group('value
')
257 if self.quoted_re.match(value):
259 '%s: Quoting
is not supported
in auto
-props
; please verify
\n'
260 'rule
%r for pattern
%r. (Using value
\n'
261 'including quotation marks
.)\n'
262 % (warning_prefix, prop, pattern,)
265 'auto
-props
: For
%r, setting
%r to
%r.' % (pattern, name, value,)
267 propdict[name] = value
270 m = self.property_novalue_re.match(prop)
272 name = m.group('name
')
274 'auto
-props
: For
%r, setting
%r to the empty string
'
281 '%s: in auto
-props line
for %r, value
%r cannot be
parsed (ignored
)'
282 % (warning_prefix, pattern, prop,)
285 self.patterns.append(self.Pattern(self.transform_case(pattern), propdict))
287 def get_propdict(self, cvs_file):
288 basename = self.transform_case(cvs_file.basename)
290 for pattern in self.patterns:
291 if pattern.match(basename):
292 for (key,value) in pattern.propdict.items():
294 if propdict[key] != value:
296 "Contradictory values set for property '%s' for file %s."
299 propdict[key] = value
303 def set_properties(self, s_item):
304 propdict = self.get_propdict(s_item.cvs_rev.cvs_file)
305 for (k,v) in propdict.items():
306 if k in s_item.svn_props:
307 if s_item.svn_props[k] != v:
309 "Property '%s' already set to %r for file %s; "
310 "auto-props value (%r) ignored."
311 % (k, s_item.svn_props[k], s_item.cvs_rev.cvs_path, v,))
313 s_item.svn_props[k] = v
316 class CVSBinaryFileDefaultMimeTypeSetter(SVNPropertySetter):
317 """If the file is binary and its svn:mime-type property is not yet
318 set, set it to 'application
/octet
-stream
'."""
320 propname = 'svn
:mime
-type'
322 def set_properties(self, s_item):
323 if self.propname in s_item.svn_props:
326 if s_item.cvs_rev.cvs_file.mode == 'b
':
327 s_item.svn_props[self.propname] = 'application
/octet
-stream
'
330 class EOLStyleFromMimeTypeSetter(SVNPropertySetter):
331 """Set svn:eol-style based on svn:mime-type.
333 If svn:mime-type is known but svn:eol-style is not, then set
334 svn:eol-style based on svn:mime-type as follows: if svn:mime-type
335 starts with 'text
/', then set svn:eol-style to native; otherwise,
336 force it to remain unset. See also issue #39."""
338 propname = 'svn
:eol
-style
'
340 def set_properties(self, s_item):
341 if self.propname in s_item.svn_props:
344 if s_item.svn_props.get('svn
:mime
-type', None) is not None:
345 if s_item.svn_props['svn
:mime
-type'].startswith("text/"):
346 s_item.svn_props[self.propname] = 'native
'
348 s_item.svn_props[self.propname] = None
351 class DefaultEOLStyleSetter(SVNPropertySetter):
352 """Set the eol-style if one has not already been set."""
354 propname = 'svn
:eol
-style
'
356 def __init__(self, value):
357 """Initialize with the specified default VALUE."""
361 def set_properties(self, s_item):
362 if self.propname in s_item.svn_props:
365 s_item.svn_props[self.propname] = self.value
368 class SVNBinaryFileKeywordsPropertySetter(SVNPropertySetter):
369 """Turn off svn:keywords for files with binary svn:eol-style."""
371 propname = 'svn
:keywords
'
373 def set_properties(self, s_item):
374 if self.propname in s_item.svn_props:
377 if not s_item.svn_props.get('svn
:eol
-style
'):
378 s_item.svn_props[self.propname] = None
381 class KeywordsPropertySetter(SVNPropertySetter):
382 """If the svn:keywords property is not yet set, set it based on the
383 file's mode
. See issue
#2."""
385 propname
= 'svn:keywords'
387 def __init__(self
, value
):
388 """Use VALUE for the value of the svn:keywords property if it is
393 def set_properties(self
, s_item
):
394 if self
.propname
in s_item
.svn_props
:
397 if s_item
.cvs_rev
.cvs_file
.mode
in [None, 'kv', 'kvl']:
398 s_item
.svn_props
[self
.propname
] = self
.value