forked from gyger/OpenFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_directory.py
75 lines (60 loc) · 2.76 KB
/
main_directory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# main_directory.py
#
# Get the main directory of optical filters with a hack in the case
# the software is compiled with py2exe or py2app. This file is similar
# to the script proposed by Thomas Heller (http://www.py2exe.org/
# index.cgi/HowToDetermineIfRunningFromExe)
#
# Copyright (c) 2005,2007,2014 Stephane Larouche
#
# This file is part of OpenFilters.
#
# OpenFilters is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# OpenFilters is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import os, sys
import platform
########################################################################
# #
# main_is_frozen #
# #
########################################################################
def main_is_frozen():
"""Determine if the software runs from a py2exe compiled executable"""
return hasattr(sys, "frozen")
########################################################################
# #
# get_main_directory #
# #
########################################################################
def get_main_directory():
"""Get the directory from where the software is runned
This function returns the directory where the software is located,
whether the software is interperted using Python or running from an
executable compiled with py2exe. In the case of an executable, it
returns the directory where this executable is located. In the case
of interpretation, it returns the directory where this file is
located; this way, it is possible to call OpenFilters from another
directory and still have its path."""
if main_is_frozen():
system = platform.system()
if system == "Windows":
pathname = os.path.dirname(sys.executable)
elif system == "Darwin":
pathname = os.environ["RESOURCEPATH"]
else:
raise ValueError("Unknown operating system.")
else:
pathname = os.path.dirname(__file__)
return os.path.abspath(pathname)