Skip to content

Commit 668bb95

Browse files
author
Mike Cayanan
committed
Update database path to support both PyPI and editable installs
- Check if package-relative data directory exists and is writable - Use package-relative path for editable installs (backwards compatible) - Fall back to runtime directory for PyPI installs - Use os.environ.get('HOME') instead of ~ for better portability
1 parent 636fc5d commit 668bb95

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

mozart/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ def resource_not_found(e):
9999

100100
# TODO: may remove this (and any code related to User models and authentication) once SSO is integrated
101101
# set database config
102-
dbdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data"))
102+
# For PyPI installs, use runtime directory; for editable installs, use package-relative path
103+
package_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data"))
104+
if os.path.exists(package_data_dir) and os.access(package_data_dir, os.W_OK):
105+
# Editable install with writable data directory
106+
dbdir = package_data_dir
107+
else:
108+
# PyPI install or non-writable package location - use runtime directory
109+
home = os.environ.get('HOME', os.path.expanduser('~'))
110+
dbdir = os.path.join(home, 'mozart', 'data')
103111
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(dbdir, "app.db")
104112
db = SQLAlchemy(app)
105113

0 commit comments

Comments
 (0)