|
| 1 | +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 2 | +# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com> |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# |
| 8 | +# * Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# * Redistributions in binary form must reproduce the above |
| 12 | +# copyright notice, this list of conditions and the following |
| 13 | +# disclaimer in the documentation and/or other materials provided |
| 14 | +# with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of the Nokia Corporation and/or its |
| 17 | +# subsidiary(-ies) nor the names of its contributors may be used |
| 18 | +# to endorse or promote products derived from this software |
| 19 | +# without specific prior written permission. |
| 20 | +# |
| 21 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + |
| 33 | +from os.path import exists |
| 34 | +import os |
| 35 | +import re |
| 36 | + |
| 37 | +def convert_py(file): |
| 38 | + print " Converting ", file |
| 39 | + inf = open(file) |
| 40 | + outf_name = file+'.tmp~' |
| 41 | + outf = open(outf_name, "w") |
| 42 | + fixes = {} |
| 43 | + for line in inf: |
| 44 | + if re.search("Field.*maxlength=\d", line): |
| 45 | + line = line.replace("maxlength=", "max_length=") |
| 46 | + fixes['changed maxlength to max_length'] = True |
| 47 | + if re.search("^\s*from django import newforms as forms\s*$", line): |
| 48 | + line = "from django import forms\n" |
| 49 | + fixes['changed newforms to forms'] = True |
| 50 | + if re.search("^\s*import django.newforms as forms\s*$", line): |
| 51 | + line = "from django import forms\n" |
| 52 | + fixes['changed newforms to forms'] = True |
| 53 | + if re.search("^\s*from django.core.validators import email_re\s*$", line): |
| 54 | + line = "from django.forms.fields import email_re\n" |
| 55 | + fixes['changed email_re import'] = True |
| 56 | + if re.search("ForeignKey.*raw_id_admin=True", line): |
| 57 | + line = re.sub(",\s*raw_id_admin=True", "", line) |
| 58 | + fixes['removed raw_id_admin'] = True |
| 59 | + if re.search("ForeignKey.*edit_inline=True", line): |
| 60 | + line = re.sub(",\s*edit_inline=True", "", line) |
| 61 | + fixes['removed edit_inline'] = True |
| 62 | + if re.search("ForeignKey.*edit_inline=models\.\w+", line): |
| 63 | + line = re.sub(",\s*edit_inline=models\.\w+", "", line) |
| 64 | + fixes['removed edit_inline'] = True |
| 65 | + if re.search("ForeignKey.*num_in_admin=\d+", line): |
| 66 | + line = re.sub(",\s*num_in_admin=\d+", "", line) |
| 67 | + fixes['removed num_in_admin'] = True |
| 68 | + if re.search("ForeignKey.*max_num_in_admin=\d+", line): |
| 69 | + line = re.sub(",\s*max_num_in_admin=\d+", "", line) |
| 70 | + fixes['removed max_num_in_admin'] = True |
| 71 | + if re.search("ManyToManyField.*filter_interface=models\.\w+", line): |
| 72 | + line = re.sub(",\s*filter_interface=models\.\w+", "", line) |
| 73 | + fixes['removed filter_interface'] = True |
| 74 | + if re.search("(Field|ForeignKey).*core=True", line): |
| 75 | + line = re.sub(",\s*core=True", "", line) |
| 76 | + fixes['removed core'] = True |
| 77 | + if re.search("\.clean_data", line): |
| 78 | + line = re.sub("\.clean_data", ".cleaned_data", line) |
| 79 | + fixes['cleaned_data'] = True |
| 80 | + outf.write(line) |
| 81 | + inf.close() |
| 82 | + fixes_list = fixes.keys() |
| 83 | + fixes_list.sort() |
| 84 | + something_fixed = len(fixes_list) > 0 |
| 85 | + if something_fixed: |
| 86 | + outf.write("\n# changes done by convert-096.py:") |
| 87 | + outf.write("\n# ".join(fixes_list)) |
| 88 | + outf.write("\n") |
| 89 | + outf.close() |
| 90 | + if something_fixed: |
| 91 | + os.rename(file, file+'.backup~') |
| 92 | + os.rename(outf_name, file) |
| 93 | + print " Fixes: "+", ".join(fixes_list) |
| 94 | + else: |
| 95 | + os.remove(outf_name) |
| 96 | + |
| 97 | +if not exists("settings.py"): |
| 98 | + raise Exception("Please run this in directory containing settings.py") |
| 99 | + |
| 100 | +for root, dirs, files in os.walk("."): |
| 101 | + if root.find(".svn") >= 0: |
| 102 | + continue |
| 103 | + print "Processing ", root |
| 104 | + for file in files: |
| 105 | + if file.endswith(".py"): |
| 106 | + convert_py(root+"/"+file) |
| 107 | + |
| 108 | + |
0 commit comments