aboutsummaryrefslogtreecommitdiff
blob: 896f680f5fdf5cdd155f3c030a1360a978ce0059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
# vim: set sw=4 sts=4 et :
# Copyright: 2008 Gentoo Foundation
# Author(s): Nirbheek Chauhan <nirbheek.chauhan@gmail.com>
# License: GPL-2
#
# Immortal lh!
#

import sys, os, shutil
try:
    from django.core import management
except ImportError:
    print "You need to install django-1.0 first."
    sys.exit(1)

# XXX: Document this in the help
if os.environ.has_key('PYTHONPATH'):
    sys.path.insert(0, os.environ['PYTHONPATH'])

DESTDIR = 'autotua_master'
SYMLINKS = False

def print_help():
    print \
    """\
%(file)s: Setup the django webapp for autotua
Usage:
    %(file)s install <where>
    <edit db settings>
    %(file)s syncdb <where>
    
Example:
    %(file)s install /home/me/projects/
will create `/home/me/projects/autotua_master` with all the necessary files.

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

By default, the directory will have the files copied. 
Toggle `SYMLINKS` to symlink the files instead.
    """ % {'file': sys.argv[0]}

def install_master():
    """Start the new project"""

    management.call_command('startproject', DESTDIR)
    if SYMLINKS:
        os.symlink(cwd+'/autotua', DESTDIR+'/master')
        for file in ['autotua_settings.py', 'urls.py']:
            dest_file = DESTDIR+'/'+file
            if os.path.isfile(dest_file):
                os.remove(dest_file)
            os.symlink(cwd+'/custom/'+file, dest_file)
    else:
        shutil.copytree(cwd+'/autotua', DESTDIR+'/master')
        for file in ['autotua_settings.py', 'urls.py']:
            shutil.copy(cwd+'/custom/'+file, DESTDIR)
    settings = open(DESTDIR+'/settings.py', 'a')
    settings.write('\nfrom autotua_settings import *\n')
    settings.close()

def syncdb_master():
    """Initialize the database"""
    import settings
    from django.core.management import setup_environ
    setup_environ(settings)
    from db_defaults import providers, archs, stages, releases, mirrors
    from master.models import StageProvider, Arch, Stage, Release, Mirror
    import copy

    management.call_command('syncdb')
    for provider in providers:
        provobj = StageProvider(name=provider)
        provobj.save()
        # Populate arch list
        for generic_arch in archs[provider]:
            gen_archobj = Arch(provider=provobj)
            # If (generic, (specific1, specific2))
            if isinstance(generic_arch, tuple):
                gen_archobj.generic = generic_arch[0]
                for arch in generic_arch[1]:
                    archobj = copy.copy(gen_archobj)
                    archobj.specific = arch
                    archobj.save()
            # If (specific1, specific2, specific3)
            else:
                gen_archobj.generic = generic_arch
                gen_archobj.specific = generic_arch
                gen_archobj.save()
        # Populate stage list
        for stage in stages[provider]:
            stageobj = Stage(provider=provobj)
            stageobj.name = stage
            stageobj.save()
        # Populate release list
        for release in releases[provider]:
            releaseobj = Release(provider=provobj)
            releaseobj.name = release
            releaseobj.save()
        # Populate mirror list
        obj = Mirror(owner=provobj)
        obj.structure = mirrors[provider]['structure']
        for server in mirrors[provider]['servers']:
            serverobj = copy.copy(obj)
            serverobj.server = server[0]
            serverobj.prefix = server[1]
            serverobj.save()

if len(sys.argv) < 3:
    print_help()
    sys.exit(1)

os.chdir(os.path.dirname(sys.argv[0]))
cwd = os.getcwd()
if not os.path.isdir(sys.argv[2]):
    os.makedirs(sys.argv[2])
os.chdir(sys.argv[2])

if management.get_version() < '0.99':
    print "You need django-1.0 to use this."
    sys.exit(1)

sys.path.append(os.getcwd())
sys.path.append(cwd+'/custom')

if sys.argv[1] == 'install':
    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': os.path.join(sys.argv[2], DESTDIR) }
elif sys.argv[1] == 'syncdb':
    syncdb_master()
    print "All done! Now you can start the master with `python manage.py runserver`"
else:
    print_help()
    sys.exit(1)