13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program. If not, see http://www.gnu.org/licenses/
16
__all__ = ['get_context', 'join']
19
WINDOWS = sys.platform.startswith('win')
20
MAC = sys.platform.startswith('darwin')
21
LINUX = sys.platform.startswith('linux')
23
FROZEN = hasattr(sys, 'frozen')
24
FROZEN_MAC = MAC and FROZEN
25
FROZEN_WINDOWS = WINDOWS and FROZEN
27
INSTALLED_UNIX = (LINUX or MAC) and ('--unix' in sys.argv)
28
INSTALLED_WINDOWS = WINDOWS and ('--windows' in sys.argv) # not supported yet
29
SOURCE = not(FROZEN_MAC or FROZEN_WINDOWS or INSTALLED_UNIX
35
def _find(root, subfolder):
36
# look for parent folder
37
folder = os.path.join(root, '..', subfolder)
38
if os.path.isdir(folder):
40
# look in current folder
41
return os.path.join(root, 'data')
44
def _get_source_root():
45
return os.path.dirname(sys.argv[0])
49
"""This needs to be called first. It will initialize and verify
50
DATA_PATH. It will raise an exception when necessary. The DATA_PATH
51
is for images, docs, ... but not for locale.
53
#>>> init('openoffice')
25
def endswith(x, suffices):
26
for suffix in suffices:
27
if x.endswith(suffix):
35
if sys.platform.startswith('win'):
36
_CONTEXT['platform'] = 'windows'
37
elif sys.platform.startswith('darwin'):
38
_CONTEXT['platform'] = 'apple'
41
_CONTEXT['platform'] = 'linux'
43
UNIX = _CONTEXT['platform'] in ('linux', 'apple')
46
if hasattr(sys, 'frozen'):
47
_CONTEXT['distribute'] = 'frozen'
48
elif not endswith(sys.argv[0], ('.py', 'nosetests')):
49
_CONTEXT['distribute'] = 'package'
51
_CONTEXT['distribute'] = 'source'
53
_CONTEXT['context'] = (_CONTEXT['platform'], _CONTEXT['distribute'])
56
class ContextError(Exception):
61
path = os.path.abspath(os.path.join(*args))
62
if not os.path.exists(path):
63
raise ContextError(path)
67
def join_create(*args):
68
path = os.path.abspath(os.path.join(*args))
69
if not os.path.exists(path):
74
def _find_parent(path, base):
75
if base == os.path.basename(path):
78
raise ContextError('Can not find "%s" parent path.' % base)
79
return _find_parent(os.path.abspath(os.path.join(path, '..')), base)
82
def _find_source_path(base_source):
83
# __file__ may only be used to locate the source files
86
os.path.dirname(unicode(__file__, sys.getfilesystemencoding())),
92
def get_context(app_name, base_source='source', sys_prefix=None):
93
"""Finds the right paths depending on the context, which consists of the
94
platform and how it is distributed.
96
context = (platform, distribute)
97
platform = 'linux', 'apple' or 'windows'
98
distribute = 'frozen', 'package' or 'source'
100
if sys_prefix is None:
101
sys_prefix = sys.prefix
102
context = _CONTEXT.copy()
60
103
# initialize APP_NAME
104
context['app_name'] = app_name
62
105
# initialize APP_ROOT and DATA_PATH
64
# eg. phatch/data or phatch/phatch/data
65
APP_ROOT = _get_source_root()
66
DATA_PATH = os.path.join(APP_ROOT, '..', 'data')
67
if not os.path.isdir(DATA_PATH):
68
DATA_PATH = os.path.join(root, 'data')
70
# eg. /usr/share/phatch
71
APP_ROOT = _get_source_root()
72
DATA_PATH = os.path.join(sys.prefix, 'share', APP_NAME)
74
# eg. Phatch.app/Contents/Resources/
75
APP_ROOT = os.path.dirname(os.path.dirname(unicode(sys.executable,
76
sys.getfilesystemencoding())))
77
DATA_PATH = os.path.join(APP_ROOT, 'Contents', 'Resources')
79
# The same as the exe dir
80
APP_ROOT = os.path.dirname(unicode(sys.executable,
81
sys.getfilesystemencoding()))
82
DATA_PATH = os.path.join(APP_ROOT, 'data')
84
raise Exception('Sorry your platform is not yet supported.')
85
# make sure it is an absolute path
86
DATA_PATH = os.path.abspath(DATA_PATH)
88
if not os.path.exists(DATA_PATH):
89
raise Exception('Sorry, the expected data path does not exist.')
90
# verify if it is a folder
91
if not os.path.isdir(DATA_PATH):
92
raise Exception('Sorry, the expected data path is not a folder.')
98
def get_actions_path_default():
99
root = os.path.abspath(os.dirname(unicode(__file__,
100
sys.getfilesystemencoding())))
101
return os.path.join(root, 'actions')
104
def get_actions_path_frozen_mac():
105
return os.path.join(get_data_path_frozen_mac(), 'actions')
108
def get_actions_path_frozen_windows():
109
return os.path.join(get_data_path_frozen_windows, 'actions')
106
if context['distribute'] == 'source':
107
# source: eg. trunk/source
108
context['app_source_path'] = _find_source_path(base_source)
109
# data: eg. trunk/data
110
context['app_data_path'] = join(
111
context['app_source_path'], '..', 'data')
113
context['app_doc_path'] = join(
114
context['app_source_path'], '..', 'doc')
115
# locale: eg. trunk/locale
116
context['app_locale_path'] = join(
117
context['app_source_path'], '..', 'locale')
118
elif context['context'] == ('linux', 'package'):
119
# source: eg. /usr/share/phatch/source
120
context['app_source_path'] = _find_source_path(base_source)
121
# data: eg. /usr/share/phatch/data
122
context['app_data_path'] = join(sys_prefix, 'share', app_name, 'data')
123
# doc: eg. /usr/share/doc/phatch/
124
context['app_doc_path'] = join(sys_prefix, 'share', 'doc', app_name)
125
# locale: eg. /usr/share/locale/
126
context['app_locale_path'] = join(sys_prefix, 'share', 'locale')
127
#elif context['context'] == ('apple', 'frozen'):
128
# # TODO: implement this!
129
# APP_ROOT = os.path.dirname(os.path.dirname(unicode(sys.executable,
130
# sys.getfilesystemencoding())))
131
# # eg. Phatch.app/Contents/Resources/
132
# context['app_data_path'] = join(APP_ROOT, 'Contents', 'Resources')
133
#elif context['context'] == ('windows', 'frozen'):
134
# # The same as the exe dir
135
# APP_ROOT = os.path.dirname(unicode(sys.executable,
136
# sys.getfilesystemencoding()))
137
# context['app_data_path'] = os.path.join(APP_ROOT, 'data')
140
'Sorry your context%(context)s is not yet supported.' % context)
143
context['app_images_path'] = join(context['app_data_path'], 'images')
146
context['desktop_path'] = desktop.DESKTOP_PATH
147
context['user_path'] = desktop.USER_PATH
148
if context['platform'] == 'linux':
149
context['user_cache_path'] = join_create(desktop.USER_CACHE_PATH,
151
context['user_config_path'] = join_create(desktop.USER_CONFIG_PATH,
153
context['user_data_path'] = join_create(desktop.USER_DATA_PATH,
157
context['user_cache_path'] = join_create(desktop.USER_PATH, dot,
159
context['user_config_path'] = join_create(desktop.USER_PATH, dot,
161
context['user_data_path'] = join_create(desktop.USER_PATH, dot,
166
if __name__ == '__main__':
168
pprint.pprint(get_context('app', base_source='phatch'))