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