Skip to content

Instantly share code, notes, and snippets.

@jdunck
Last active August 29, 2015 14:02
Show Gist options
  • Save jdunck/ec527e56fc7052ffcba3 to your computer and use it in GitHub Desktop.
Save jdunck/ec527e56fc7052ffcba3 to your computer and use it in GitHub Desktop.
Generate south depends_on based on the current django project tree
depends_on = (
('about', '0004_fake_user_fk'),
('accounts', '0029_auto__del_registration'),
('activity_feed', '0002_fake_user_fk'),
('analytics', '0006_add_labels'),
('art', '0036_auto__chg_field_packpurchase_royalty_millicents_per_credit__chg_field_'),
('bots', '0002_fake_user_fk'),
('comments', '0002_fake_user_fk'),
('core', '0012_auto__del_field_creatorprofile_points_balance__add_field_creatorprofil'),
('creator', '0002_fake_user_fk'),
('dev', '0002_fake_user_fk'),
('display_case', '0003_auto'),
('emotes', '0009_fake_user_fk'),
('front_admin', '0002_fake_user_fk'),
('home_page', '0005_fake_user_fk'),
('jasmine', '0002_fake_user_fk'),
('lessons', '0002_fake_user_fk'),
('mail', '0004_fake_user_fk'),
('nm', '0002_fake_user_fk'),
('notifications', '0002_fake_user_fk'),
('payments', '0015_auto__add_royaltypayout'),
('rewards', '0009_delete_invite_based_rewards'),
('search', '0002_fake_user_fk'),
('social', '0003_fake_user_fk'),
('tips', '0002_auto__add_field_tip_start__add_field_tip_end'),
('trades', '0004_auto__del_index_tradeoffer_role'),
)
import subprocess
from collections import defaultdict
def ls_migrations(project_path):
command = ['find', project_path, '-ipath', '*/migrations/*', '-iname', '*.py']
finder = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in finder.stdout.read().split('\n'):
if not '/migrations/' in line:
continue
before_migration, after_migration = line.split('/migrations/')
app_name = before_migration.split('/')[-1]
step_name = after_migration.split('.')[0]
if step_name == '__init__':
continue
step_number = int(step_name.split('_')[0])
yield app_name.lower(), step_name, step_number
def max_migrations(migrations):
max_per_app = defaultdict(int)
step_per_app = {}
for app, step, step_number in migrations:
if max_per_app[app] < step_number:
max_per_app[app] = step_number
step_per_app[app] = step
return step_per_app
def format_depends_on(per_app):
if not per_app:
print "depends_on = (,)"
return
print "depends_on = ("
for (app, step) in sorted(per_app.items()):
print " ('%s', '%s')," % (app, step)
print ")"
if __name__ == '__main__':
import sys
format_depends_on(max_migrations(ls_migrations(sys.argv[1])))
@jdunck
Copy link
Author

jdunck commented Jun 23, 2014

python make_depends_on.py /path/to/django/project/

@jimmylee
Copy link

Nice.

@glowskir
Copy link

glowskir commented Nov 4, 2014

Useful. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment