| 1 |
#!/bin/sh |
| 2 |
# -*- python -*- |
| 3 |
|
| 4 |
################################################################################ |
| 5 |
# This file is python 2/3 bilingual. |
| 6 |
# The line """:" starts a comment in python and is a no-op in shell. |
| 7 |
""":" |
| 8 |
# Shell code to find and run a suitable python interpreter. |
| 9 |
for cmd in python3 python python2; do |
| 10 |
command -v > /dev/null $cmd && exec $cmd $0 "$@" |
| 11 |
done |
| 12 |
|
| 13 |
echo "Error: Could not find a valid python interpreter --> exiting!" >&2 |
| 14 |
exit 2 |
| 15 |
":""" # this line ends the python comment and is a no-op in shell. |
| 16 |
################################################################################ |
| 17 |
|
| 18 |
|
| 19 |
from __future__ import print_function |
| 20 |
import os, sys, re, getpass, base64, traceback |
| 21 |
try: |
| 22 |
import configparser |
| 23 |
except: |
| 24 |
import ConfigParser as configparser |
| 25 |
|
| 26 |
try: |
| 27 |
input = raw_input |
| 28 |
except: |
| 29 |
pass |
| 30 |
|
| 31 |
dirNm, execName = os.path.split(sys.argv[0]) |
| 32 |
sys.path.insert(1,os.path.abspath(os.path.join(dirNm, "../libexec"))) |
| 33 |
sys.path.insert(1,os.path.realpath(os.path.join(dirNm, "../site"))) |
| 34 |
|
| 35 |
import argparse |
| 36 |
|
| 37 |
class CmdLineOptions(object): |
| 38 |
def __init__(self): |
| 39 |
pass |
| 40 |
|
| 41 |
def execute(self): |
| 42 |
parser = argparse.ArgumentParser() |
| 43 |
parser.add_argument("--dbhost", dest='dbhost', action="store", help="db host") |
| 44 |
parser.add_argument("--dbuser", dest='dbuser', action="store", help="db user") |
| 45 |
parser.add_argument("--passwd", dest='passwd', action="store", help="password") |
| 46 |
parser.add_argument("--dbname", dest='dbname', action="store", help="name of db") |
| 47 |
|
| 48 |
args = parser.parse_args() |
| 49 |
|
| 50 |
return args |
| 51 |
class CreateConf(object): |
| 52 |
def __init__(self, args): |
| 53 |
self.__host = args.dbhost |
| 54 |
self.__user = args.dbuser |
| 55 |
self.__passwd = args.passwd |
| 56 |
self.__db = args.dbname |
| 57 |
|
| 58 |
def __readFromUser(self): |
| 59 |
if (not self.__host): self.__host = input("Database host: ") |
| 60 |
if (not self.__user): self.__user = input("Database user: ") |
| 61 |
if (not self.__passwd): self.__passwd = getpass.getpass("Database pass: ") |
| 62 |
if (not self.__db): self.__db = input("Database name: ") |
| 63 |
|
| 64 |
def __writeConfig(self): |
| 65 |
config=configparser.ConfigParser() |
| 66 |
config.add_section("MYSQL") |
| 67 |
config.set("MYSQL","HOST",self.__host) |
| 68 |
config.set("MYSQL","USER",self.__user) |
| 69 |
config.set("MYSQL","PASSWD",base64.b64encode(self.__passwd.encode()).decode()) |
| 70 |
config.set("MYSQL","DB",self.__db) |
| 71 |
|
| 72 |
fn = self.__db + "_db.conf" |
| 73 |
|
| 74 |
f = open(fn,"w") |
| 75 |
config.write(f) |
| 76 |
f.close() |
| 77 |
|
| 78 |
def create(self): |
| 79 |
|
| 80 |
self.__readFromUser() |
| 81 |
self.__writeConfig() |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def main(): |
| 86 |
args = CmdLineOptions().execute() |
| 87 |
createConf = CreateConf(args) |
| 88 |
createConf.create() |
| 89 |
|
| 90 |
|
| 91 |
if ( __name__ == '__main__'): main() |