regex sub to remove 'userdata/' from paths

The misguided authors who put userdata/ in their paths cause problems not just for non-Windows users, but fellow Windows users who chose not to put userdata in the install directory. This error can be removed by an approach similar to that just used to purge backslashes:

* if 'userdata': A basic filter to cut down the number of lines being run through complicated regex speeds up performance.
* while: It is possible, though rare, for a line to contain more than one path with userdata. Points about the regex: a) We continue to use precomment, though it would be well to correct commented-out old paths also, lest they mislead any more UMC writers.  b) In case you're wondering why I made one 'data/' string optional, there's a set of add-ons in 1.4 that use "userdata/campaigns" instead of "userdata/data/campaigns".  c) The '[ac]' at the end is something of an artifact of the time before I excluded comments, but it provides another safety measure insuring that the string is actually a value.
* regex object: This splits precomment into groups. Notes: a) Some authors begin with an unnecessary "../", might as well get rid of it as well. (As far as I can tell, this prefix has no effect anywhere I've seen it used, but I'd want to be positive that it ALWAYS does nothing before having wmllint replace it everywhere.) b) The first two groups have been made non-capturing; we will not need to refer to them.  c) For reporting to stdout, group(1) is extended to the next '/', though this part of the match is optional, to insure that there's no way to get trapped in a while loop.
* precomment: Here, we reconstruct precomment based on the regex object, except we simply drop what's outside group(1).
* print: In case designers don't get the point from seeing the elimination logged in stdout, I include an all-caps admonition against "userdata/". This is a really irritating bug.
* This code was inserted before the reconstruction of lines[i] from precomment and comment.
This commit is contained in:
groggydice 2013-04-01 12:57:31 -04:00
parent 8b01e0b19c
commit 89673c9672

View file

@ -1496,6 +1496,13 @@ def hack_syntax(filename, lines):
precomment = precomment[:backslash.start()] + fronted + precomment[backslash.end():]
print '"%s", line %d: %s -> %s -- please use frontslash (/) for cross-platform compatibility' \
% (filename, i+1, backslash.group(), fronted)
# Then get rid of the 'userdata/' headache.
if 'userdata/' in precomment:
while re.search(r'user(data/)?data/[ac]', precomment):
userdata = re.search(r'(?:\.\./)?user(?:data/)?(data/[ac][^/]*/?)', precomment)
precomment = precomment[:userdata.start()] + userdata.group(1) + precomment[userdata.end():]
print '"%s", line %d: %s -> %s -- DO NOT PREFIX PATHS WITH "userdata/"' \
% (filename, i+1, userdata.group(), userdata.group(1))
lines[i] = precomment + comment
# Ensure that every attack has a translatable description.
for i in range(len(lines)):