diff options
author | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2009-02-23 21:17:30 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2009-02-23 21:17:30 +0530 |
commit | 79c53ca62f9bbf48b2c8d8a4a0307b3530a85e3d (patch) | |
tree | 8f9be95950d8adf287ba44b44693b938580d7909 /master | |
parent | Fix things broken by previous changes to setup-master.py (diff) | |
download | autotua-79c53ca62f9bbf48b2c8d8a4a0307b3530a85e3d.tar.gz autotua-79c53ca62f9bbf48b2c8d8a4a0307b3530a85e3d.tar.bz2 autotua-79c53ca62f9bbf48b2c8d8a4a0307b3530a85e3d.zip |
Fix setup-master.py to not suck monkey balls
* Reorganise the little shit. Now you just need to type `make` followed by
`make DESTDIR=<somewhere> install`.
* It's also much more readable now.
Diffstat (limited to 'master')
-rw-r--r-- | master/Makefile | 3 | ||||
-rwxr-xr-x | master/setup-master.py | 206 |
2 files changed, 128 insertions, 81 deletions
diff --git a/master/Makefile b/master/Makefile index 6c9d89d..de441a0 100644 --- a/master/Makefile +++ b/master/Makefile @@ -30,4 +30,5 @@ clean: install: all test -n "$(DESTDIR)" || ( echo "Please define DESTDIR" ; exit 1 ) - ./setup-master.py install --final $(DESTDIR) + for dir in $(SUBDIRS) ; do ( cd $$dir ; $(MAKE) DESTDIR=$(DESTDIR)install ) ; done + ./setup-master.py path-install "$(DESTDIR)" diff --git a/master/setup-master.py b/master/setup-master.py index 0da1288..f6b50a0 100755 --- a/master/setup-master.py +++ b/master/setup-master.py @@ -1,30 +1,35 @@ #!/usr/bin/env python # vim: set sw=4 sts=4 et : -# Copyright: 2008 Gentoo Foundation +# Copyright: 2008-09 Gentoo Foundation # Author(s): Nirbheek Chauhan <nirbheek.chauhan@gmail.com> # License: AGPL-3 # # Immortal lh! # -# FIXME: This is so fucking ugly. -import sys, os, shutil +from __future__ import with_statement +import atexit, sys, os, shutil import os.path as osp try: from django.core import management except ImportError: print "You need to install django-1.0 first." sys.exit(1) +else: + if management.get_version() < '0.99': + print "You need django-1.0 to use autotua-master." + sys.exit(1) -# Remove current directory from search path -sys.path = sys.path[1:] -# XXX: Document this in the help -if os.environ.has_key('PYTHONPATH'): - sys.path.insert(0, os.environ['PYTHONPATH']) - -DESTDIR = 'autotua_master' -SYMLINKS = True -srcdir = None +STAGES = ['install_master', 'syncdb_master', 'master_gpg', + 'setup_sample_slave', 'slave_gpg', 'setup_sample_job'] +STAGE = None +ACTION = sys.argv[1] +PROJDIR = 'autotua_master' +RELPROJPATH = sys.argv[2] +PROJPATH = osp.abspath(sys.argv[2]) +SYMLINKS = True +SRCDIR = osp.abspath(osp.dirname(sys.argv[0])) +DATAPATH = osp.abspath(osp.join(SRCDIR, 'custom')) def print_help(): print \ @@ -43,45 +48,79 @@ You then need to edit the database settings in /home/me/projects/autotua_master/settings.py After this, running - %(file)s syncdb /home/me/projects/autotua_master -will initialize the db + %(file)s resume /home/me/projects/autotua_master +will initialize the db and do other magic By default, the directory will have the files copied. Toggle `SYMLINKS` to symlink the files instead. """ % {'file': sys.argv[0]} +def save_current_stage(): + open(osp.join(PROJPATH, '.last_stage'), 'w').write(STAGE) + +def next_stage(): + global STAGE + STAGE = STAGES[STAGES.index(STAGE)+1] + +def resume_last_stage(): + global STAGE + stages = None + if STAGE == STAGES[0]: + STAGE = STAGES[0] + stages = [STAGE] + else: + with open(osp.join(PROJPATH, '.last_stage'), 'r') as last_stage: + STAGE = last_stage.read() + stages = STAGES[STAGES.index(STAGE):] + # For each stage since last_stage + for stage in stages: + eval('%s()' % stage) + next_stage() + def install_master(): """Start the new project""" import subprocess - management.call_command('startproject', DESTDIR) + # Need to be in the project's parent dir + os.chdir(osp.dirname(PROJPATH)) + print os.getcwd(), PROJPATH + management.call_command('startproject', osp.basename(PROJPATH)) if SYMLINKS: - os.symlink(osp.join(srcdir, 'master'), osp.join(DESTDIR, 'master')) + os.symlink(osp.join(SRCDIR, 'master'), osp.join(PROJPATH, 'master')) for file in ['urls.py']: - dest_file = osp.join(DESTDIR, file) + dest_file = osp.join(PROJPATH, file) if osp.isfile(dest_file): os.remove(dest_file) - os.symlink(osp.join(srcdir, 'custom', file), dest_file) + os.symlink(osp.join(DATAPATH, file), dest_file) else: - shutil.copytree(osp.join(srcdir, 'master'), osp.join(DESTDIR, 'master')) + shutil.copytree(osp.join(SRCDIR, 'master'), osp.join(PROJPATH, 'master')) for file in ['urls.py']: - shutil.copy(osp.join(srcdir, 'custom', file), DESTDIR) - settings = open(osp.join(DESTDIR, 'settings.py'), 'a') - master_settings = open(osp.join(srcdir, 'custom', 'merge_settings.py')) + shutil.copy(osp.join(DATAPATH, file), PROJPATH) + settings = open(osp.join(PROJPATH, 'settings.py'), 'a') + master_settings = open(osp.join(DATAPATH, 'merge_settings.py')) settings.write('\n'+master_settings.read()) settings.close() master_settings.close() - - # Install icons - icondir = osp.abspath(osp.join(srcdir, 'icons')) - installdir = osp.abspath(DESTDIR) - subprocess.check_call('cd %s; make DESTDIR=%s install' % (icondir, installdir), shell=True) + print """ +=================================== +Setup done. +Now you need to edit the database settings in %(dest)s/settings.py +and run `./setup-master.py resume %(dest)s`""" % { 'dest': RELPROJPATH } def syncdb_master(): """Initialize the database""" - from db_defaults import groups, providers, archs, releases, mirrors import copy + # Database defaults + from db_defaults import groups, providers, archs, releases, mirrors + # Django project settings + import settings + from django.core.management import setup_environ + setup_environ(settings) + from master.models import Group, Arch, Provider, Release, Mirror + + # Need to be in the project dir + os.chdir(PROJPATH) management.call_command('syncdb') # User-related defaults for group in groups: @@ -118,15 +157,31 @@ def syncdb_master(): def setup_gpg(data, gpghome): from autotua import crypt - from master import const if not osp.isdir(gpghome): os.mkdir(gpghome, 0700) print 'Creating a "sample" gpg key (expires in 1 month)' crypt.Crypto(gpghome).init_gpghome(**data) +def master_gpg(): + from master import const + from sample_data import sample_master_gpg_data + setup_gpg(sample_master_gpg_data, const.GPGHOME) + +def slave_gpg(): + from master import const + from sample_data import sample_slave_gpg_data + setup_gpg(sample_slave_gpg_data, osp.join(const.MASTER_DIR, 'sample_slave_gpghome')) + def setup_sample_slave(): + # Sample data for slave from sample_data import sample_slave + + # Django project settings + import settings + from django.core.management import setup_environ + setup_environ(settings) from master import const + from master.models import User, Group, Provider, Arch, Release, Mirror, Job, Slave slave = Slave() slave.name = sample_slave['name'] try: @@ -141,13 +196,21 @@ def setup_sample_slave(): def setup_sample_job(): from autotua import sync + # Sample data for master from sample_data import sample_job + + # Django project settings + import settings + from django.core.management import setup_environ + setup_environ(settings) + from master.models import User, Provider, Release, Job, Slave # Setup the jobtage tree in /var/tmp/autotua/jobtage sync.Syncer().sync() # Get on with the job at hand... ;) job = Job() for i in ['name', 'jobtagerev', 'atoms']: setattr(job, i, sample_job[i]) + job.name = sample_job['name'] job.maintainer = User.objects.get(username=sample_job['maintainer']['username']) job.release = Release.objects.get(name=sample_job['release'], provider=Provider.objects.get(name=sample_job['provider'])) @@ -157,61 +220,44 @@ def setup_sample_job(): job.slaves.add(Slave.objects.get(name=sample_job['slave'])) job.save() -def setup_dirs(dir): - """This must be called before doing anything else.""" - # cd to setup-master.py location - os.chdir(osp.dirname(sys.argv[0])) - # Store the current directory - # We cd and store to work around relative invocations - global srcdir - srcdir = os.getcwd() - - if not osp.isdir(dir): - os.makedirs(dir) - os.chdir(dir) - sys.path.append(os.getcwd()) - - # Add cwd+/custom - sys.path.append(osp.join(srcdir, 'custom')) - -### Start Work ### +################ +## Parse Args ## +################ if len(sys.argv) < 3: + print "Insufficient arguments:" print_help() sys.exit(1) -if management.get_version() < '0.99': - print "You need django-1.0 to use this." - sys.exit(1) - -if sys.argv[1] == 'install': - if sys.argv[2] == '--final': - where = osp.dirname(sys.argv[3].rstrip('/')) - DESTDIR = osp.basename(sys.argv[3].rstrip('/')) - elif sys.argv[1] == 'install': - where = osp.dirname(sys.argv[2].rstrip('/')) - setup_dirs(where) - install_master() - print """Setup done. -Now you need to edit the database settings in %(dest)s/settings.py -and run `./setup-master.py syncdb %(dest)s`""" % { 'dest': osp.join(where, DESTDIR) } -elif sys.argv[1] == 'syncdb': - # Setup the relevant directories - where = sys.argv[2].rstrip('/') - setup_dirs(where) - # Import stuff - import settings - from django.core.management import setup_environ - setup_environ(settings) - from master.models import User, Group, Provider, Arch, Release, Mirror, Job, Slave - from master import const - from sample_data import sample_master_gpg_data, sample_slave_gpg_data - # Start stuff - syncdb_master() - setup_gpg(sample_master_gpg_data, const.GPGHOME) - setup_sample_slave() # Should be done before job; inits User if DNE - setup_gpg(sample_slave_gpg_data, osp.join(const.MASTER_DIR, 'sample_slave_gpghome')) - setup_sample_job() - print "All done! Now you can start the master with `python manage.py runserver`" +if ACTION == 'install': + PROJPATH = osp.join(PROJPATH, PROJDIR) + STAGE = 'install_master' + if not osp.isdir(osp.dirname(PROJPATH)): + os.makedirs(osp.dirname(PROJPATH)) +elif ACTION == 'path-install': + STAGE = 'install_master' + if not osp.isdir(osp.dirname(PROJPATH)): + os.makedirs(osp.dirname(PROJPATH)) +elif ACTION == 'resume': + pass else: print_help() sys.exit(1) + +####################### +## Setup search path ## +####################### +# XXX: Document this in the help +if os.environ.has_key('PYTHONPATH'): + sys.path.insert(0, os.environ['PYTHONPATH']) +# Remove current directory from search path +sys.path = sys.path[1:] +sys.path.append(SRCDIR) +sys.path.append(DATAPATH) +sys.path.append(PROJPATH) + +#################### +## Start our work ## +#################### +atexit.register(save_current_stage) +resume_last_stage() +print "All done! Now you can start the master with `python manage.py runserver`" |