Add special handling to determine default settings for portable version

The portable version will prefer to create it's settings in it's own
directory and only fallback to a user-specific path
($LOCAL_APPDATA/openslides) if this directory is not writable
This commit is contained in:
Andy Kittner 2012-08-27 21:48:46 +02:00
parent 14ba868df4
commit cc20ec3456
2 changed files with 46 additions and 2 deletions

View File

@ -23,7 +23,7 @@ static const char *site_code =
static const char *run_openslides_code = static const char *run_openslides_code =
"import openslides.main;" "import openslides.main;"
"openslides.main.main()"; "openslides.main.win32_portable_main()";
/* determine the path to the executable /* determine the path to the executable
* NOTE: Py_GetFullProgramPath() can't be used because * NOTE: Py_GetFullProgramPath() can't be used because

View File

@ -72,7 +72,7 @@ def _fs2unicode(s):
return s.decode(_fs_encoding) return s.decode(_fs_encoding)
def main(argv=None): def main(argv=None, opt_defaults = None):
if argv is None: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
@ -90,6 +90,9 @@ def main(argv=None):
parser.add_option( parser.add_option(
"--no-reload", action="store_true", help="Do not reload the development server") "--no-reload", action="store_true", help="Do not reload the development server")
if not opt_defaults is None:
parser.set_defaults(**opt_defaults)
opts, args = parser.parse_args(argv) opts, args = parser.parse_args(argv)
if args: if args:
sys.stderr.write("This command does not take arguments!\n\n") sys.stderr.write("This command does not take arguments!\n\n")
@ -249,6 +252,47 @@ def start_browser(url):
t = threading.Thread(target=f) t = threading.Thread(target=f)
t.start() t.start()
def win32_portable_main(argv=None):
"""special entry point for the win32 portable version"""
import tempfile
# NOTE: sys.executable will be the path to openslides.exe
# since it is essentially a small wrapper that embeds the
# python interpreter
portable_dir = os.path.dirname(os.path.abspath(sys.executable))
try:
fd, test_file = tempfile.mkstemp(dir = portable_dir)
except OSError:
portable_dir_writeable = False
else:
portable_dir_writeable = True
os.close(fd)
os.unlink(test_file)
if portable_dir_writeable:
default_settings = os.path.join(portable_dir, "openslides",
"openslides_personal_settings.py")
else:
import ctypes
shell32 = ctypes.WinDLL("shell32.dll")
SHGetFolderPath = shell32.SHGetFolderPathW
SHGetFolderPath.argtypes = (ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_wchar_p)
SHGetFolderPath.restype = ctypes.c_uint32
CSIDL_LOCAL_APPDATA = 0x001c
MAX_PATH = 260
buf = ctypes.create_unicode_buffer(MAX_PATH)
res = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, buf)
if res != 0:
raise Exception("Could not deterime APPDATA path")
default_settings = os.path.join(buf.value, "openslides",
"openslides_personal_settings.py")
main(argv, opt_defaults = { "settings": default_settings })
if __name__ == "__main__": if __name__ == "__main__":
main() main()