| [995] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | use strict; | 
|---|
| [1223] | 3 | use FindBin qw($Bin); | 
|---|
|  | 4 | use lib $Bin; | 
|---|
| [995] | 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 | 
|---|
| [1414] | 18 | import sys, os, time, threading, django.utils.autoreload | 
|---|
| [995] | 19 | sys.path.insert(0, "/mit/$USER/Scripts/django") | 
|---|
|  | 20 | os.chdir("/mit/$USER/Scripts/django/$name") | 
|---|
|  | 21 | os.environ['DJANGO_SETTINGS_MODULE'] = "$name.settings" | 
|---|
|  | 22 |  | 
|---|
| [1414] | 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 |  | 
|---|
| [995] | 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 | 
|---|
| [1806] | 41 |  | 
|---|
|  | 42 | RewriteRule ^\$ index.fcgi/ [QSA,L] | 
|---|
|  | 43 |  | 
|---|
| [995] | 44 | RewriteCond %{REQUEST_FILENAME} !-f | 
|---|
|  | 45 | RewriteCond %{REQUEST_FILENAME} !-d | 
|---|
| [1036] | 46 | RewriteRule ^(.*)\$ index.fcgi/\$1 [QSA,L] | 
|---|
| [995] | 47 | EOF | 
|---|
|  | 48 | close HTACCESS; | 
|---|
|  | 49 | chmod 0777, ".htaccess"; | 
|---|
|  | 50 |  | 
|---|
|  | 51 | chdir "/mit/$USER/Scripts/django/"; | 
|---|
| [1837] | 52 | system(qw{django-admin startproject}, $name)==0 or die "\nFailed to create app.\n\n"; | 
|---|
| [995] | 53 | chdir "$name"; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | open SETTINGS, "settings.py"; | 
|---|
| [1038] | 56 | open NEWSETTINGS, ">settings.py.new"; | 
|---|
| [995] | 57 | while (<SETTINGS>) { | 
|---|
|  | 58 | chomp; | 
|---|
|  | 59 | if (/Your Name/) { | 
|---|
| [1038] | 60 | $_ = "    ('$USER', '$email'),"; | 
|---|
| [1774] | 61 | } elsif (/'ENGINE'/) { | 
|---|
|  | 62 | $_ = "        'ENGINE': 'django.db.backends.mysql',"; | 
|---|
|  | 63 | } elsif  (/'NAME'/) { | 
|---|
|  | 64 | $_ = "        'NAME': '$sqldb',"; | 
|---|
|  | 65 | } elsif (/'USER'/) { | 
|---|
|  | 66 | $_ = "        'USER': '$sqluser',"; | 
|---|
|  | 67 | } elsif (/'PASSWORD'/) { | 
|---|
|  | 68 | $_ = "        'PASSWORD': '$sqlpass',"; | 
|---|
|  | 69 | } elsif (/'HOST'/) { | 
|---|
|  | 70 | $_ = "        'HOST': '$sqlhost',"; | 
|---|
| [995] | 71 | } elsif (/Chicago/) { | 
|---|
|  | 72 | $_ =~ s/Chicago/New_York/; | 
|---|
| [1042] | 73 | } elsif (/^ADMIN_MEDIA_PREFIX/) { | 
|---|
|  | 74 | $_ = "ADMIN_MEDIA_PREFIX = '/__scripts/django/media/'"; | 
|---|
|  | 75 | } elsif (/^INSTALLED_APPS/) { | 
|---|
|  | 76 | print NEWSETTINGS "$_\n"; | 
|---|
|  | 77 | while (<SETTINGS>) { | 
|---|
|  | 78 | if (/^\)/) { | 
|---|
| [1414] | 79 | print NEWSETTINGS "    'django.contrib.admin',\n"; | 
|---|
|  | 80 | print NEWSETTINGS "    'django.contrib.admindocs',\n"; | 
|---|
| [1042] | 81 | } | 
|---|
|  | 82 | print NEWSETTINGS $_; | 
|---|
|  | 83 | } | 
|---|
| [995] | 84 | } | 
|---|
| [1038] | 85 | print NEWSETTINGS "$_\n"; | 
|---|
| [995] | 86 | } | 
|---|
|  | 87 | close NEWSETTINGS; | 
|---|
|  | 88 | close SETTNGS; | 
|---|
|  | 89 | rename "settings.py.new", "settings.py"; | 
|---|
|  | 90 |  | 
|---|
| [1042] | 91 | open URLS, "urls.py"; | 
|---|
|  | 92 | open NEWURLS, ">urls.py.new"; | 
|---|
|  | 93 | while (<URLS>) { | 
|---|
|  | 94 | chomp; | 
|---|
|  | 95 | if (/^#.*from django\.contrib import admin/) { | 
|---|
|  | 96 | $_ =~ s/^# *//; | 
|---|
|  | 97 | } elsif (/^#.*admin.autodiscover/) { | 
|---|
|  | 98 | $_ =~ s/^# *//; | 
|---|
|  | 99 | } elsif (/^ *# *\(r\'\^admin\//) { | 
|---|
|  | 100 | $_ =~ s/# *//; | 
|---|
|  | 101 | } | 
|---|
|  | 102 | print NEWURLS "$_\n"; | 
|---|
|  | 103 | } | 
|---|
|  | 104 | close NEWURLS; | 
|---|
|  | 105 | close URLS; | 
|---|
|  | 106 | rename "urls.py.new", "urls.py"; | 
|---|
|  | 107 |  | 
|---|
|  | 108 | print "Initializing your project's SQL database schema...\n"; | 
|---|
|  | 109 | system qw{./manage.py syncdb --noinput}; | 
|---|
|  | 110 | print "...done\n"; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | print "Creating your superuser account... "; | 
|---|
|  | 113 | system qw{./manage.py createsuperuser --username}, $admin_username, "--email", $email, "--noinput"; | 
|---|
|  | 114 | print "done\n"; | 
|---|
|  | 115 | print "Setting your superuser password... "; | 
|---|
|  | 116 | system qw{mysql -D}, "$USER+$addrlast", "-e", "UPDATE auth_user SET password=MD5(\'$admin_password\') WHERE username=\'$admin_username\'"; | 
|---|
|  | 117 | print "done\n"; | 
|---|
|  | 118 |  | 
|---|
| [1046] | 119 | print "\nDjango has been installed. The setup is roughly what's described\n"; | 
|---|
|  | 120 | print "in the shared-hosting section of\n"; | 
|---|
|  | 121 | print "  http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/\n"; | 
|---|
|  | 122 | print "We've also enabled the admin app. You can start from the 'Creating\n"; | 
|---|
|  | 123 | print "models' step of the Django tutorial:\n"; | 
|---|
|  | 124 | print "  http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3\n\n"; | 
|---|
| [1044] | 125 | print "Your project is located in:\n"; | 
|---|
| [995] | 126 | print "  /mit/$USER/Scripts/django/$name/\n"; | 
|---|
|  | 127 | print "To access manage.py, run 'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; | 
|---|
|  | 128 | press_enter; | 
|---|
|  | 129 |  | 
|---|
|  | 130 | exit 0; | 
|---|