mirror of https://github.com/interlegis/sapl.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
10 years ago
|
from collections import defaultdict
|
||
|
from difflib import SequenceMatcher
|
||
10 years ago
|
from inspect import getsourcelines
|
||
10 years ago
|
from collections import OrderedDict
|
||
10 years ago
|
|
||
10 years ago
|
from migration_base import appconfs, legacy_app
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def is_field_line(line):
|
||
|
return not line.strip().startswith('#') and ' = ' in line
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def get_field(line):
|
||
|
return line.split('=')[0].strip()
|
||
10 years ago
|
|
||
|
|
||
|
def print_commented_source(model):
|
||
|
new_fields = [f.name for f in model._meta.fields]
|
||
|
legacy_fields = [f.name for f in legacy_app.get_model(model.__name__)._meta.fields if f.name != 'ind_excluido']
|
||
|
new_to_old = dict(zip(new_fields, legacy_fields))
|
||
|
lines = getsourcelines(model)[0]
|
||
10 years ago
|
cols = max(map(len, [line for line in lines if is_field_line(line)]))
|
||
10 years ago
|
print '\n'
|
||
|
for line in lines:
|
||
10 years ago
|
if not is_field_line(line):
|
||
10 years ago
|
print line.rstrip()
|
||
|
else:
|
||
10 years ago
|
print '%s # %s' % (line.rstrip().ljust(cols), new_to_old[get_field(line)])
|
||
|
return new_to_old
|
||
|
|
||
|
|
||
10 years ago
|
field_mappings = OrderedDict()
|
||
|
for app in appconfs:
|
||
|
for model in app.models.values():
|
||
|
new_to_old = OrderedDict()
|
||
|
lines = getsourcelines(model)[0]
|
||
|
for line in lines:
|
||
|
if is_field_line(line):
|
||
|
new = get_field(line)
|
||
|
old = line.split('#')[-1].strip()
|
||
|
new_to_old[new] = old
|
||
|
field_mappings[model] = new_to_old
|
||
|
|
||
|
|
||
10 years ago
|
def check_similarity():
|
||
|
|
||
|
def similar(a, b):
|
||
|
return SequenceMatcher(None, a, b).ratio()
|
||
|
|
||
|
different_pairs = defaultdict(list)
|
||
10 years ago
|
for model, new_to_old in field_mappings.items():
|
||
|
for new, old in new_to_old.items():
|
||
|
if similar(new, old) < 0.7:
|
||
|
different_pairs[model].append((new, old))
|
||
10 years ago
|
|
||
|
if different_pairs:
|
||
|
print '\n\n######## Different Pairs #########'
|
||
10 years ago
|
for model, pairs in different_pairs.items():
|
||
|
print '%s (%s)' % (model.__name__, app.name)
|
||
10 years ago
|
for a, b in pairs:
|
||
|
print ' ', a, b
|
||
10 years ago
|
return different_pairs
|