pygps/setup.py

44 lines
1.3 KiB
Python
Raw Permalink Normal View History

2017-10-12 11:36:52 +00:00
#!/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, alt DOUBLE, PRIMARY KEY(ts, device) );";
2017-10-12 11:36:52 +00:00
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!")