Browse Source

store DB in XDG_DATA_HOME, if possible

master
nervuri 2 months ago
parent
commit
0ad4687186
  1. 35
      trust-seeker

35
trust-seeker

@ -19,6 +19,26 @@ import cryptography.hazmat.primitives.serialization
import idna # punycode
import pycurl
# Set the database path here, or it will be created in the default location.
dbpath = ''
if not dbpath:
if os.path.exists('trust-seeker.db'):
# If there's a trust-seeker.db in the current working directory, use it.
dbpath = '.'
elif 'XDG_DATA_HOME' in os.environ:
dbpath = os.environ['XDG_DATA_HOME'] + '/trust-seeker'
elif os.path.isdir(os.path.expanduser('~/.local/share')):
dbpath = '~/.local/share/trust-seeker'
else: # Save database in the same directory as the executable.
pathname = os.path.dirname(sys.argv[0])
basepath = os.path.abspath(os.path.realpath(pathname))
dbpath = basepath
dbpath = os.path.expanduser(dbpath)
os.makedirs(dbpath, exist_ok=True)
dbpath += '/trust-seeker.db'
remote_protocol = '' # command line by default
# CGI
@ -41,11 +61,6 @@ pin_status: dict = {
'rejected': -1,
}
# Get path to database.
pathname = os.path.dirname(sys.argv[0])
basepath = os.path.abspath(os.path.realpath(pathname))
dbpath = basepath + '/trust-seeker.db'
# Print to stderr (https://stackoverflow.com/a/14981125).
def eprint(*args, **kwargs):
@ -188,9 +203,10 @@ def create_database():
if os.path.isfile(dbpath):
return # DB already created
if not os.access(basepath, os.W_OK):
respond("No write permission for \"" + basepath + "\".\n\
Can't create database.", 'internal_error')
dbdir = os.path.expanduser(os.path.dirname(dbpath))
if not os.access(dbdir, os.W_OK):
respond('No write permission for "' + dbdir + '".'\
+ os.linesep + "Can't create database.", 'internal_error')
connection = sqlite3.connect(dbpath)
cursor = connection.cursor()
@ -665,12 +681,13 @@ def ask(trust_seeker_url: str, host: str, fingerprint: str = '', timeout: int =
def main(argv: list):
create_database()
if remote_protocol:
# CGI
command: dict = check_cgi_input()
else:
# Not CGI
create_database()
command: dict = check_input(argv)
if 'lookup' in command['actions']:

Loading…
Cancel
Save