| 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 README, ">README.txt"; | 
|---|
| 39 | print README <<EOF; | 
|---|
| 40 | This directory contains index.fcgi, a script that serves up your Django site. | 
|---|
| 41 |  | 
|---|
| 42 | To modify your Django project access the files in | 
|---|
| 43 | /mit/$user/Scripts/django/$name | 
|---|
| 44 |  | 
|---|
| 45 | Files placed in this directory will be served directly to users without | 
|---|
| 46 | being processed by Django. | 
|---|
| 47 |  | 
|---|
| 48 | Static files live in the "static" subdirectory; you should not add things | 
|---|
| 49 | there directly but instead place them with the relevant application as you | 
|---|
| 50 | normally would, then run "python manage.py collectstatic" from the above | 
|---|
| 51 | directory; see <https://docs.djangoproject.com/en/1.5/howto/static-files/>. | 
|---|
| 52 |  | 
|---|
| 53 | -- Scripts Team 2013-06-28 | 
|---|
| 54 | EOF | 
|---|
| 55 | close README; | 
|---|
| 56 | chmod 0555, "README.txt"; | 
|---|
| 57 |  | 
|---|
| 58 | open HTACCESS, ">.htaccess"; | 
|---|
| 59 | print HTACCESS <<EOF; | 
|---|
| 60 | RewriteEngine On | 
|---|
| 61 |  | 
|---|
| 62 | RewriteRule ^\$ index.fcgi/ [QSA,L] | 
|---|
| 63 |  | 
|---|
| 64 | RewriteCond %{REQUEST_FILENAME} !-f | 
|---|
| 65 | RewriteCond %{REQUEST_FILENAME} !-d | 
|---|
| 66 | RewriteRule ^(.*)\$ index.fcgi/\$1 [QSA,L] | 
|---|
| 67 | EOF | 
|---|
| 68 | close HTACCESS; | 
|---|
| 69 | chmod 0777, ".htaccess"; | 
|---|
| 70 |  | 
|---|
| 71 | chdir "/mit/$USER/Scripts/django/"; | 
|---|
| 72 | system(qw{django-admin startproject}, $name)==0 or die "\nFailed to create app.\n\n"; | 
|---|
| 73 | chdir "$name/$name"; | 
|---|
| 74 |  | 
|---|
| 75 | open SETTINGS, "settings.py"; | 
|---|
| 76 | open NEWSETTINGS, ">settings.py.new"; | 
|---|
| 77 | while (<SETTINGS>) { | 
|---|
| 78 | chomp; | 
|---|
| 79 | if (/Your Name/) { | 
|---|
| 80 | $_ = "    ('$USER', '$email'),"; | 
|---|
| 81 | } elsif (/^DEBUG = /) { | 
|---|
| 82 | $_ =~ s/DEBUG/import os\n\nDEBUG/; | 
|---|
| 83 | } elsif (/'ENGINE'/) { | 
|---|
| 84 | $_ = "        'ENGINE': 'django.db.backends.mysql',"; | 
|---|
| 85 | } elsif  (/'NAME'/) { | 
|---|
| 86 | $_ = "        'NAME': '$sqldb',"; | 
|---|
| 87 | } elsif (/'USER'/) { | 
|---|
| 88 | $_ = "        'OPTIONS': {\n            'read_default_file' : os.path.expanduser('~/.my.cnf'),\n        },"; | 
|---|
| 89 | } elsif (/'PASSWORD'/) { | 
|---|
| 90 | next; | 
|---|
| 91 | } elsif (/'HOST'/) { | 
|---|
| 92 | next; | 
|---|
| 93 | } elsif (/Chicago/) { | 
|---|
| 94 | $_ =~ s/Chicago/New_York/; | 
|---|
| 95 | } elsif (/^STATIC_URL/) { | 
|---|
| 96 | $_ = "STATIC_URL = '//$USER.$server/$name/static/'"; | 
|---|
| 97 | } elsif (/^STATIC_ROOT/) { | 
|---|
| 98 | $_ = "STATIC_ROOT = '/mit/$USER/web_scripts/$addrend/static/'"; | 
|---|
| 99 | } elsif (/^INSTALLED_APPS/) { | 
|---|
| 100 | print NEWSETTINGS "$_\n"; | 
|---|
| 101 | while (<SETTINGS>) { | 
|---|
| 102 | if (/^\)/) { | 
|---|
| 103 | print NEWSETTINGS "    'django.contrib.admin',\n"; | 
|---|
| 104 | print NEWSETTINGS "    'django.contrib.admindocs',\n"; | 
|---|
| 105 | } | 
|---|
| 106 | print NEWSETTINGS $_; | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 | print NEWSETTINGS "$_\n"; | 
|---|
| 110 | } | 
|---|
| 111 | close NEWSETTINGS; | 
|---|
| 112 | close SETTNGS; | 
|---|
| 113 | rename "settings.py.new", "settings.py"; | 
|---|
| 114 |  | 
|---|
| 115 | open URLS, "urls.py"; | 
|---|
| 116 | open NEWURLS, ">urls.py.new"; | 
|---|
| 117 | while (<URLS>) { | 
|---|
| 118 | chomp; | 
|---|
| 119 | if (/^#.*from django\.contrib import admin/) { | 
|---|
| 120 | $_ =~ s/^# *//; | 
|---|
| 121 | } elsif (/^#.*admin.autodiscover/) { | 
|---|
| 122 | $_ =~ s/^# *//; | 
|---|
| 123 | } elsif (/^ *# url\(r\'\^admin\//) { | 
|---|
| 124 | $_ =~ s/# *//; | 
|---|
| 125 | } | 
|---|
| 126 | print NEWURLS "$_\n"; | 
|---|
| 127 | } | 
|---|
| 128 | close NEWURLS; | 
|---|
| 129 | close URLS; | 
|---|
| 130 | rename "urls.py.new", "urls.py"; | 
|---|
| 131 |  | 
|---|
| 132 | chdir ".."; | 
|---|
| 133 |  | 
|---|
| 134 | system(qw{python manage.py collectstatic --noinput}) == 0 or die "\nFailed to collect static files.\n\n"; | 
|---|
| 135 |  | 
|---|
| 136 | print "Initializing your project's SQL database schema...\n"; | 
|---|
| 137 | system qw{./manage.py syncdb --noinput}; | 
|---|
| 138 | print "...done\n"; | 
|---|
| 139 |  | 
|---|
| 140 | print "Creating your superuser account... "; | 
|---|
| 141 | system qw{./manage.py createsuperuser --username}, $admin_username, "--email", $email, "--noinput"; | 
|---|
| 142 | print "done\n"; | 
|---|
| 143 | print "Setting your superuser password... "; | 
|---|
| 144 | system qw{mysql -D}, "$USER+$addrlast", "-e", "UPDATE auth_user SET password=MD5(\'$admin_password\') WHERE username=\'$admin_username\'"; | 
|---|
| 145 | print "done\n"; | 
|---|
| 146 |  | 
|---|
| 147 | print "\nDjango has been installed. The setup is roughly what's described\n"; | 
|---|
| 148 | print "in the shared-hosting section of\n"; | 
|---|
| 149 | print "  http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/\n"; | 
|---|
| 150 | print "We've also enabled the admin app. You can start from the 'Creating\n"; | 
|---|
| 151 | print "models' step of the Django tutorial:\n"; | 
|---|
| 152 | print "  http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3\n\n"; | 
|---|
| 153 | print "Your project is located in:\n"; | 
|---|
| 154 | print "  /mit/$USER/Scripts/django/$name/\n"; | 
|---|
| 155 | print "To access manage.py, run 'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; | 
|---|
| 156 | press_enter; | 
|---|
| 157 |  | 
|---|
| 158 | exit 0; | 
|---|