User data folder is no longer found relative to sys.argv[0]

For source checkouts from git, the data folder is in the checkout folder. Otherwise, it is in the user's home folder.
This commit is contained in:
David Vierra 2015-09-06 08:01:31 -10:00
parent fc9c8b831c
commit bb0e7b1b9d
4 changed files with 31 additions and 21 deletions

View File

@ -5,12 +5,13 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import logging
import os
import sys
from mcedit2.util import resources
log = logging.getLogger(__name__)
# no build scripts for OS X yet. assuming py2app or py2installer will be used.
#
# store user files in source checkout folder
# store user files in source checkout folder when checked out, otherwise in Documents folder
def getUserFilesDirectory():
@ -21,10 +22,11 @@ def getUserFilesDirectory():
# OS X filenames are defined to be UTF-8 encoded.
# We internally handle filenames as unicode.
script = sys.argv[0].decode(sys.getfilesystemencoding())
folder = os.path.dirname(os.path.dirname(os.path.dirname(script))) # main script is src/mcedit/main.py, so, ../../
dataDir = os.path.join(folder, "MCEdit User Data")
if resources.isSrcCheckout():
# Source checkouts don't use the same folder as regular installs.
dataDir = os.path.join(os.path.dirname(resources.getSrcFolder()), "MCEdit User Data")
else:
dataDir = os.path.expanduser(u"~/Documents/MCEdit 2 Files")
if not os.path.exists(dataDir):
os.makedirs(dataDir)

View File

@ -5,11 +5,12 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import logging
import os
import sys
from mcedit2.util import resources
log = logging.getLogger(__name__)
# no build scripts for linux yet. no idea if frozen apps will be built for linux.
# assume always running from source checkout and put user files in source checkout.
# put user files in checkout dir when running from source checkout, otherwise ~/.mcedit2
# xxx for .whl or distro-specific distrib, put user files in ~/.mcedit2
@ -17,21 +18,15 @@ def getUserFilesDirectory():
# On Linux, the FS encoding is given by the current locale
# Linux filenames are defined to be bytestrings.
# TODO: if not os.path.exists(os.path.join(mumble_mumble, ".git")):
# We handle filenames internally as 'unicode', so decode 'sys.argv[0]'
# If a linux filename cannot be decoded with the current locale, ignore it.
# If this filename is the script filename, you lose.
try:
# assert the source checkout is not in a non-representable path...
script = sys.argv[0].decode(sys.getfilesystemencoding())
except UnicodeDecodeError:
print("Script filename %r cannot be decoded with the current locale %s! Please use sensible filenames." % (
sys.argv[0], sys.getfilesystemencoding()))
raise
folder = os.path.dirname(os.path.dirname(os.path.dirname(script))) # main script is src/mcedit/main.py, so, ../../
dataDir = os.path.join(folder, "MCEdit User Data")
if resources.isSrcCheckout():
# Source checkouts don't use the same folder as regular installs.
dataDir = os.path.join(os.path.dirname(resources.getSrcFolder()), "MCEdit User Data")
else:
dataDir = os.path.expanduser(u"~/.mcedit2")
if not os.path.exists(dataDir):
os.makedirs(dataDir)

View File

@ -28,6 +28,7 @@ def getUserFilesDirectory():
if hasattr(sys, 'frozen'):
# On Windows, filenames are UTF-16 encoded.
# Filenames are defined as UTF-16.
#
# However, sys.executable is codepage-encoded. Codepages cannot represent all possible
# filenames, so we must get the exe filename using this wide-character API.
# Wide-character APIs in pywin32 always return a `unicode`.
@ -42,10 +43,10 @@ def getUserFilesDirectory():
exe = win32api.GetModuleFileNameW(None)
assert os.path.exists(exe), "MCEdit executable %r does not exist! Something is very wrong." % exe
folder = os.path.dirname(exe)
dataDir = os.path.join(folder, "MCEdit 2 Files")
else:
folder = os.path.dirname(resources.getSrcFolder())
dataDir = os.path.join(folder, "MCEdit User Data")
if not os.path.exists(dataDir):

View File

@ -8,6 +8,12 @@ import sys
log = logging.getLogger(__name__)
def isSrcCheckout():
srcFolder = getSrcFolder()
gitPath = os.path.join(os.path.dirname(srcFolder), '.git')
return os.path.exists(gitPath)
def getSrcFolder():
"""
Find the 'src/' folder of a source checkout.
@ -27,8 +33,14 @@ def getSrcFolder():
# On Linux, it is locale-encoded and filenames are defined as bytestrings, so it is possible
# to have a filename that cannot be interpreted as unicode. If the user writes a filename
# that is not locale-encoded, he loses.
try:
# assert the source checkout is not in a non-representable path...
mod = mod.decode(sys.getfilesystemencoding())
except UnicodeDecodeError:
print("Script filename %r cannot be decoded with the current locale %s! "
"Please use sensible filenames." %
(mod, sys.getfilesystemencoding()))
raise
# Assert the source checkout is not in a non-representable path...
assert os.path.exists(mod), ("Source checkout path cannot be represented as unicode. "