-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith-virtualenv.sh
115 lines (93 loc) · 3.68 KB
/
with-virtualenv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# This script will automate (and document) the set-up for a django project with
# virtualenv. You must define the variables in line 5 to 13.
#
# You also get 2 python scripts that insert and replace lines, respectively! I
# could not figure out how to properly escape for sed.
# variables
project_dir=test
envname=test-env
project_name=test1
python_executable_path=~/$project_dir/$envname/bin/python
app_name=dummy
AppName=Dummy
script_location=~/bin
port=3000 # port number for running the dev server
# python-skripts
mkdir $script_location || true
cat <<HEK>> $script_location/insert-line.py
# -*- coding: utf-8 -*-
import sys, codecs, fileinput
# parse command line arguments
if len(sys.argv) != 4:
print("\nusage: \n\targument 1: text to search\n\targument 2: text to insert\n\targument 3: file path\n")
raise SystemExit
else:
TEXT_TO_SEARCH = codecs.escape_decode(bytes(sys.argv[1], "utf-8"))[0].decode("utf-8")
TEXT_TO_INSERT = codecs.escape_decode(bytes(sys.argv[2], "utf-8"))[0].decode("utf-8")
FILE_PATH = codecs.escape_decode(bytes(sys.argv[3], "utf-8"))[0].decode("utf-8")
for line in fileinput.FileInput(FILE_PATH,inplace=1):
if TEXT_TO_SEARCH in line:
line=line.replace(line,line + TEXT_TO_INSERT + '\n')
print(line, end=''),
HEK
cat <<HEK>> $script_location/replace-line.py
# -*- coding: utf-8 -*-
import sys, codecs, fileinput
# parse command line arguments
if len(sys.argv) != 4:
print("\nusage: \n\targument 1: text to search\n\targument 2: text to insert\n\targument 3: file path\n")
raise SystemExit
else:
TEXT_TO_SEARCH = codecs.escape_decode(bytes(sys.argv[1], "utf-8"))[0].decode("utf-8")
TEXT_TO_INSERT = codecs.escape_decode(bytes(sys.argv[2], "utf-8"))[0].decode("utf-8")
FILE_PATH = codecs.escape_decode(bytes(sys.argv[3], "utf-8"))[0].decode("utf-8")
for line in fileinput.FileInput(FILE_PATH,inplace=1):
if TEXT_TO_SEARCH in line:
line=line.replace(line, TEXT_TO_INSERT + '\n')
print(line, end=''),
HEK
chmod +x $script_location/insert-line.py
chmod +x $script_location/replace-line.py
# pip-installation
sudo apt-get -y install python3-pip
sudo pip3 install --upgrade pip
sudo pip3 install setuptools
mkdir $project_dir
cd $project_dir
# virtualenv
sudo pip3 install virtualenv
virtualenv -p python3 $envname
source $envname/bin/activate # exit with 'deactivate'
## check wether python and pip are executed from virtualenv
# python # invoke python3 shell
# import sys; print(sys.executable); exit()
# which pip; which python
pip install django pyyaml pytz # djangorestframework etc
django-admin.py startproject $project_name .
python manage.py startapp $app_name
# define app in settings.py
search="\'django.contrib.staticfiles\',"
insert="\t\'$app_name.apps.${AppName}Config\',"
file=~/$project_dir/$project_name/settings.py
python ${script_location}/insert-line.py $search $insert $file
# define host names
# #ALLOWED_HOSTS = ['rpi', '192.168.2.106', 'localhost']
search="ALLOWED_HOSTS\x20=\x20["
insert="ALLOWED_HOSTS\x20=\x20[\n\t\'localhost\',\n\t\'0.0.0.0\'\n]"
file=~/$project_dir/$project_name/settings.py
python ${script_location}/replace-line.py $search $insert $file
# # define time zone
# TIME_ZONE = 'Europe/Berlin'
search="TIME_ZONE"
insert="TIME_ZONE\x20=\x20\'Europe/Berlin\'"
file=~/$project_dir/$project_name/settings.py
python $script_location/replace-line.py $search $insert $file
# create urls.py, model, view, template
# migration / create database and put model into database:
python manage.py makemigrations $app_name
python manage.py migrate
#source ~/$project_dir/$envname/bin/activate
cd ~/$project_dir && python manage.py runserver 0.0.0.0:$port
echo "Reached end of script!"
exit 0