mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Update key_file generator script
- Prevent overwrite of key file if it already exists - Option to force key file overwrite if required
This commit is contained in:
parent
040f719b68
commit
443041de3a
@ -3,8 +3,11 @@
|
|||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
|
||||||
fn = 'secret_key.txt'
|
KEY_FN = 'secret_key.txt'
|
||||||
|
KEY_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
|
||||||
def generate_key():
|
def generate_key():
|
||||||
@ -15,10 +18,28 @@ def generate_key():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
# Ensure key file is placed in same directory as this script
|
parser = argparse.ArgumentParser(description='Generate Django SECRET_KEY file')
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
parser.add_argument('--output', help='Specify key file path', default=None)
|
||||||
key_file = os.path.join(path, fn)
|
parser.add_argument('--force', '-f', help='Override key file (if it exists)', action='store_true')
|
||||||
|
parser.add_argument('--dummy', '-d', help='Dummy run (display key only', action='store_true')
|
||||||
|
|
||||||
with open(key_file, 'w') as kf:
|
args = parser.parse_args()
|
||||||
kf.write(generate_key())
|
|
||||||
print('Generated SECRET_KEY to {f}'.format(f=key_file))
|
if args.output:
|
||||||
|
key_filename = args.output
|
||||||
|
else:
|
||||||
|
key_filename = os.path.join(KEY_DIR, KEY_FN)
|
||||||
|
|
||||||
|
key_data = generate_key()
|
||||||
|
|
||||||
|
if args.dummy:
|
||||||
|
print('SECRET_KEY: {k}'.format(k=key_data))
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if not args.force and os.path.exists(key_filename):
|
||||||
|
print("Key file already exists - '{f}'".format(f=key_filename))
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
with open(key_filename, 'w') as key_file:
|
||||||
|
print("Generating SECRET_KEY file - '{f}'".format(f=key_filename))
|
||||||
|
key_file.write(key_data)
|
||||||
|
Loading…
Reference in New Issue
Block a user