Add setup script

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-12 13:36:52 +02:00
parent 52456ff531
commit ba4746b7c4
1 changed files with 43 additions and 0 deletions

43
setup.py Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python3
import configparser
import os.path
import getpass
import pymysql
if os.path.isfile("config.cfg"):
print("config.cfg already exists. Please remove it before running this script.")
exit(1)
config = configparser.RawConfigParser()
config.add_section('Database')
host = input("MySQL host [localhost]: ") or "localhost"
user = input("MySQL username [gps]: ") or "gps"
pwd = getpass.getpass("MySQL password (not echoed!): ")
name = input("MySQL database name [gps]: ") or "gps"
print()
config.set('Database', 'host', host)
config.set('Database', 'user', user)
config.set('Database', 'pass', pwd)
config.set('Database', 'name', name)
sql1 = "CREATE TABLE IF NOT EXISTS tracker ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, device VARCHAR(38), lat DOUBLE, lon DOUBLE, PRIMARY KEY(ts, device) );";
sql2 = "CREATE TABLE IF NOT EXISTS users ( user VARCHAR(64) PRIMARY KEY, password VARCHAR(128), admin BOOLEAN );";
sql3 = "CREATE TABLE IF NOT EXISTS device ( device VARCHAR(38) PRIMARY KEY, passkey VARCHAR(128) );";
conn = pymysql.connect(host, user, pwd, name)
cur = conn.cursor()
for sql in sql1, sql2, sql3:
cur.execute(sql)
conn.commit()
with open('config.cfg', 'wt') as cfg:
config.write(cfg)
print("We're all done. You can now use your GPS tracker. Have fun!")