-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathset_secret_key.py
More file actions
52 lines (41 loc) · 1.58 KB
/
set_secret_key.py
File metadata and controls
52 lines (41 loc) · 1.58 KB
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
import os
import re
import sys
import codecs
from random import choice
from tempfile import NamedTemporaryFile
from shutil import copy
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Set SECRET_KEY of Patchman Application.'
def add_arguments(self, parser):
parser.add_argument(
'--key', help=(
'The SECRET_KEY to be used by Patchman. If not set, a random '
'key of length 50 will be created.'))
@staticmethod
def get_random_key():
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return ''.join([choice(chars) for i in range(50)])
def handle(self, *args, **options):
secret_key = options.get('key', self.get_random_key())
if sys.prefix == '/usr':
conf_path = '/etc/patchman'
else:
conf_path = os.path.join(sys.prefix, 'etc/patchman')
# if conf_path doesn't exist, try ./etc/patchman
if not os.path.isdir(conf_path):
conf_path = './etc/patchman'
local_settings = os.path.join(conf_path, 'local_settings.py')
settings_contents = codecs.open(
local_settings, 'r', encoding='utf-8').read()
settings_contents = re.sub(
r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents)
f = NamedTemporaryFile(delete=False)
temp = f.name
f.close()
fh = codecs.open(temp, 'w+b', encoding='utf-8')
fh.write(settings_contents)
fh.close()
copy(temp, local_settings)
os.remove(temp)