[995] | 1 | #!/usr/bin/perl |
---|
| 2 | use strict; |
---|
[1222] | 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 |
---|
[1278] | 18 | import sys, os, time, threading, django.utils.autoreload |
---|
[2310] | 19 | sys.path.insert(0, "/mit/$USER/Scripts/django/$name") |
---|
[995] | 20 | os.chdir("/mit/$USER/Scripts/django/$name") |
---|
| 21 | os.environ['DJANGO_SETTINGS_MODULE'] = "$name.settings" |
---|
| 22 | |
---|
[1278] | 23 | def reloader_thread(): |
---|
[1281] | 24 | while True: |
---|
| 25 | if django.utils.autoreload.code_changed(): |
---|
| 26 | os._exit(3) |
---|
| 27 | time.sleep(1) |
---|
[1278] | 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 |
---|
[1901] | 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/"; |
---|
[1901] | 52 | system(qw{django-admin startproject}, $name)==0 or die "\nFailed to create app.\n\n"; |
---|
[2310] | 53 | chdir "$name/$name"; |
---|
[995] | 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'),"; |
---|
[2370] | 61 | } elsif (/^DEBUG = /) { |
---|
| 62 | $_ =~ s/DEBUG/import os\n\nDEBUG/; |
---|
[1775] | 63 | } elsif (/'ENGINE'/) { |
---|
| 64 | $_ = " 'ENGINE': 'django.db.backends.mysql',"; |
---|
| 65 | } elsif (/'NAME'/) { |
---|
| 66 | $_ = " 'NAME': '$sqldb',"; |
---|
| 67 | } elsif (/'USER'/) { |
---|
[2370] | 68 | $_ = " 'OPTIONS': {\n 'read_default_file' : os.path.expanduser('~/.my.cnf'),\n },"; |
---|
[1775] | 69 | } elsif (/'PASSWORD'/) { |
---|
[2370] | 70 | next; |
---|
[1775] | 71 | } elsif (/'HOST'/) { |
---|
[2370] | 72 | next; |
---|
[995] | 73 | } elsif (/Chicago/) { |
---|
| 74 | $_ =~ s/Chicago/New_York/; |
---|
[2442] | 75 | } elsif (/^ADMIN_MEDIA_PREFIX/) { |
---|
| 76 | $_ = "ADMIN_MEDIA_PREFIX = '/__scripts/django/media/'"; |
---|
[1042] | 77 | } elsif (/^INSTALLED_APPS/) { |
---|
| 78 | print NEWSETTINGS "$_\n"; |
---|
| 79 | while (<SETTINGS>) { |
---|
| 80 | if (/^\)/) { |
---|
[1281] | 81 | print NEWSETTINGS " 'django.contrib.admin',\n"; |
---|
| 82 | print NEWSETTINGS " 'django.contrib.admindocs',\n"; |
---|
[1042] | 83 | } |
---|
| 84 | print NEWSETTINGS $_; |
---|
| 85 | } |
---|
[995] | 86 | } |
---|
[1038] | 87 | print NEWSETTINGS "$_\n"; |
---|
[995] | 88 | } |
---|
| 89 | close NEWSETTINGS; |
---|
| 90 | close SETTNGS; |
---|
| 91 | rename "settings.py.new", "settings.py"; |
---|
| 92 | |
---|
[1042] | 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/^# *//; |
---|
[2442] | 101 | } elsif (/^ *# *\(r\'\^admin\//) { |
---|
[1042] | 102 | $_ =~ s/# *//; |
---|
| 103 | } |
---|
| 104 | print NEWURLS "$_\n"; |
---|
| 105 | } |
---|
| 106 | close NEWURLS; |
---|
| 107 | close URLS; |
---|
| 108 | rename "urls.py.new", "urls.py"; |
---|
| 109 | |
---|
[2310] | 110 | chdir ".."; |
---|
| 111 | |
---|
[1042] | 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 | |
---|
[1046] | 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"; |
---|
[1044] | 129 | print "Your project is located in:\n"; |
---|
[995] | 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; |
---|