create ~/.config if /tmp is readonly

This commit is contained in:
ed 2021-12-06 02:02:44 +01:00
parent b8945ae233
commit ece08b8179

View file

@ -25,26 +25,30 @@ ANYWIN = WINDOWS or sys.platform in ["msys"]
MACOS = platform.system() == "Darwin" MACOS = platform.system() == "Darwin"
def get_unix_home(): def get_unixdir():
try: paths = [
v = os.environ["XDG_CONFIG_HOME"] (os.environ.get, "XDG_CONFIG_HOME"),
if not v: (os.path.expanduser, "~/.config"),
raise Exception() (unicode, "/tmp"),
ret = os.path.normpath(v) ]
os.listdir(ret) for chk in [os.listdir, os.mkdir]:
return ret for pf, pa in paths:
except: try:
pass p = pf(pa)
if not p or p.startswith("~"):
continue
try: p = os.path.normpath(p)
v = os.path.expanduser("~/.config") chk(p)
if v.startswith("~"): p = os.path.join(p, "copyparty")
raise Exception() if not os.path.isdir(p):
ret = os.path.normpath(v) os.mkdir(p)
os.listdir(ret)
return ret return p
except: except:
return "/tmp" pass
raise Exception("could not find a writable path for config")
class EnvParams(object): class EnvParams(object):
@ -59,7 +63,7 @@ class EnvParams(object):
elif sys.platform == "darwin": elif sys.platform == "darwin":
self.cfg = os.path.expanduser("~/Library/Preferences/copyparty") self.cfg = os.path.expanduser("~/Library/Preferences/copyparty")
else: else:
self.cfg = get_unix_home() + "/copyparty" self.cfg = get_unixdir()
self.cfg = self.cfg.replace("\\", "/") self.cfg = self.cfg.replace("\\", "/")
try: try: