| 1 | #!/usr/bin/perl | 
|---|
| 2 | use strict; | 
|---|
| 3 | use FindBin qw($Bin); | 
|---|
| 4 | use lib $Bin; | 
|---|
| 5 | use onserver; | 
|---|
| 6 |  | 
|---|
| 7 | setup(); | 
|---|
| 8 |  | 
|---|
| 9 | print "\nEnter the code name for your project (a valid Python package name).\n"; | 
|---|
| 10 | print "Do not use 'django' or the name of any other Python library.\n"; | 
|---|
| 11 | print "Project name: "; | 
|---|
| 12 | my $name = <STDIN>; | 
|---|
| 13 | chomp $name; | 
|---|
| 14 |  | 
|---|
| 15 | open FASTCGI, ">index.fcgi"; | 
|---|
| 16 | print FASTCGI <<EOF; | 
|---|
| 17 | #!/usr/bin/env python | 
|---|
| 18 | import sys, os, time, threading, django.utils.autoreload | 
|---|
| 19 | sys.path.insert(0, "/mit/$USER/Scripts/django/$name") | 
|---|
| 20 | os.chdir("/mit/$USER/Scripts/django/$name") | 
|---|
| 21 | os.environ['DJANGO_SETTINGS_MODULE'] = "$name.settings" | 
|---|
| 22 |  | 
|---|
| 23 | def reloader_thread(): | 
|---|
| 24 | while True: | 
|---|
| 25 | if django.utils.autoreload.code_changed(): | 
|---|
| 26 | os._exit(3) | 
|---|
| 27 | time.sleep(1) | 
|---|
| 28 | t = threading.Thread(target=reloader_thread) | 
|---|
| 29 | t.daemon = True | 
|---|
| 30 | t.start() | 
|---|
| 31 |  | 
|---|
| 32 | from django.core.servers.fastcgi import runfastcgi | 
|---|
| 33 | runfastcgi(method="threaded", daemonize="false") | 
|---|
| 34 | EOF | 
|---|
| 35 | close FASTCGI; | 
|---|
| 36 | chmod 0755, "index.fcgi"; | 
|---|
| 37 |  | 
|---|
| 38 | open HTACCESS, ">.htaccess"; | 
|---|
| 39 | print HTACCESS <<EOF; | 
|---|
| 40 | RewriteEngine On | 
|---|
| 41 |  | 
|---|
| 42 | RewriteRule ^\$ index.fcgi/ [QSA,L] | 
|---|
| 43 |  | 
|---|
| 44 | RewriteCond %{REQUEST_FILENAME} !-f | 
|---|
| 45 | RewriteCond %{REQUEST_FILENAME} !-d | 
|---|
| 46 | RewriteRule ^(.*)\$ index.fcgi/\$1 [QSA,L] | 
|---|
| 47 | EOF | 
|---|
| 48 | close HTACCESS; | 
|---|
| 49 | chmod 0777, ".htaccess"; | 
|---|
| 50 |  | 
|---|
| 51 | chdir "/mit/$USER/Scripts/django/"; | 
|---|
| 52 | system(qw{django-admin startproject}, $name)==0 or die "\nFailed to create app.\n\n"; | 
|---|
| 53 | chdir "$name/$name"; | 
|---|
| 54 |  | 
|---|
| 55 | open SETTINGS, "settings.py"; | 
|---|
| 56 | open NEWSETTINGS, ">settings.py.new"; | 
|---|
| 57 | while (<SETTINGS>) { | 
|---|
| 58 | chomp; | 
|---|
| 59 | if (/Your Name/) { | 
|---|
| 60 | $_ = "    ('$USER', '$email'),"; | 
|---|
| 61 | } elsif (/^DEBUG = /) { | 
|---|
| 62 | $_ =~ s/DEBUG/import os\n\nDEBUG/; | 
|---|
| 63 | } elsif (/'ENGINE'/) { | 
|---|
| 64 | $_ = "        'ENGINE': 'django.db.backends.mysql',"; | 
|---|
| 65 | } elsif  (/'NAME'/) { | 
|---|
| 66 | $_ = "        'NAME': '$sqldb',"; | 
|---|
| 67 | } elsif (/'USER'/) { | 
|---|
| 68 | $_ = "        'OPTIONS': {\n            'read_default_file' : os.path.expanduser('~/.my.cnf'),\n        },"; | 
|---|
| 69 | } elsif (/'PASSWORD'/) { | 
|---|
| 70 | next; | 
|---|
| 71 | } elsif (/'HOST'/) { | 
|---|
| 72 | next; | 
|---|
| 73 | } elsif (/Chicago/) { | 
|---|
| 74 | $_ =~ s/Chicago/New_York/; | 
|---|
| 75 | } elsif (/^ADMIN_MEDIA_PREFIX/) { | 
|---|
| 76 | $_ = "ADMIN_MEDIA_PREFIX = '/__scripts/django/media/'"; | 
|---|
| 77 | } elsif (/^INSTALLED_APPS/) { | 
|---|
| 78 | print NEWSETTINGS "$_\n"; | 
|---|
| 79 | while (<SETTINGS>) { | 
|---|
| 80 | if (/^\)/) { | 
|---|
| 81 | print NEWSETTINGS "    'django.contrib.admin',\n"; | 
|---|
| 82 | print NEWSETTINGS "    'django.contrib.admindocs',\n"; | 
|---|
| 83 | } | 
|---|
| 84 | print NEWSETTINGS $_; | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 | print NEWSETTINGS "$_\n"; | 
|---|
| 88 | } | 
|---|
| 89 | close NEWSETTINGS; | 
|---|
| 90 | close SETTNGS; | 
|---|
| 91 | rename "settings.py.new", "settings.py"; | 
|---|
| 92 |  | 
|---|
| 93 | open URLS, "urls.py"; | 
|---|
| 94 | open NEWURLS, ">urls.py.new"; | 
|---|
| 95 | while (<URLS>) { | 
|---|
| 96 | chomp; | 
|---|
| 97 | if (/^#.*from django\.contrib import admin/) { | 
|---|
| 98 | $_ =~ s/^# *//; | 
|---|
| 99 | } elsif (/^#.*admin.autodiscover/) { | 
|---|
| 100 | $_ =~ s/^# *//; | 
|---|
| 101 | } elsif (/^ *# *\(r\'\^admin\//) { | 
|---|
| 102 | $_ =~ s/# *//; | 
|---|
| 103 | } | 
|---|
| 104 | print NEWURLS "$_\n"; | 
|---|
| 105 | } | 
|---|
| 106 | close NEWURLS; | 
|---|
| 107 | close URLS; | 
|---|
| 108 | rename "urls.py.new", "urls.py"; | 
|---|
| 109 |  | 
|---|
| 110 | chdir ".."; | 
|---|
| 111 |  | 
|---|
| 112 | print "Initializing your project's SQL database schema...\n"; | 
|---|
| 113 | system qw{./manage.py syncdb --noinput}; | 
|---|
| 114 | print "...done\n"; | 
|---|
| 115 |  | 
|---|
| 116 | print "Creating your superuser account... "; | 
|---|
| 117 | system qw{./manage.py createsuperuser --username}, $admin_username, "--email", $email, "--noinput"; | 
|---|
| 118 | print "done\n"; | 
|---|
| 119 | print "Setting your superuser password... "; | 
|---|
| 120 | system qw{mysql -D}, "$USER+$addrlast", "-e", "UPDATE auth_user SET password=MD5(\'$admin_password\') WHERE username=\'$admin_username\'"; | 
|---|
| 121 | print "done\n"; | 
|---|
| 122 |  | 
|---|
| 123 | print "\nDjango has been installed. The setup is roughly what's described\n"; | 
|---|
| 124 | print "in the shared-hosting section of\n"; | 
|---|
| 125 | print "  http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/\n"; | 
|---|
| 126 | print "We've also enabled the admin app. You can start from the 'Creating\n"; | 
|---|
| 127 | print "models' step of the Django tutorial:\n"; | 
|---|
| 128 | print "  http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3\n\n"; | 
|---|
| 129 | print "Your project is located in:\n"; | 
|---|
| 130 | print "  /mit/$USER/Scripts/django/$name/\n"; | 
|---|
| 131 | print "To access manage.py, run 'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; | 
|---|
| 132 | press_enter; | 
|---|
| 133 |  | 
|---|
| 134 | exit 0; | 
|---|