/
paths.py
352 lines (257 loc) · 9.79 KB
/
paths.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: iso-8859-1 -*-
# $Id$
# Snowberry: Extensible Launcher for the Doomsday Engine
#
# Copyright (C) 2004, 2005
# Jaakko Keränen <jaakko.keranen@iki.fi>
#
# This program 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.
#
# This program 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: http://www.opensource.org/
## @file paths.py File Path Services
##
## This module contains functions that can be used to determine where
## in the filesystem the various parts of the Snowberry application
## and data are located.
##
## It is assumed that the current working directory is Snowberry's
## installation directory, and also contains Doomsday.
import os, re, sys
import host, events
def isHomeless():
"""Homeless setup means that Snowberry uses the main installation
directory for user data. In Windows, the default is homeless
mode."""
noHome = '-nohome' in sys.argv
useHome = '-home' in sys.argv
# The default.
if useHome:
return False
if noHome:
return True
# The default is homeless in Windows.
return host.isWindows()
# Directory Type constants to be used with getSystemPath() and
# getUserPath().
ADDONS = 'addons'
MANIFESTS = 'manifests'
UNINSTALLED = 'uninstalled'
CONF = 'conf'
LANG = 'lang'
PLUGINS = 'plugins'
PROFILES = 'profiles'
GRAPHICS = 'graphics'
RUNTIME = 'runtime'
# List of custom addons directories.
addonPaths = []
# On the Mac, change the current directory to Resources inside the bundle.
if host.isMac():
import __main__
os.chdir(os.path.dirname(os.path.abspath(__main__.__file__)))
# This is the name of the directory under which Snowberry's files will
# be stored in the user's home directory.
if host.isMac():
SNOWBERRY_HOME_DIR = 'Snowberry'
else:
SNOWBERRY_HOME_DIR = '.snowberry'
# In a homeless setup all the user data goes in the main installation folder.
if isHomeless():
SNOWBERRY_HOME_DIR = '.'
# This is the full path of the user's Snowberry home directory.
homeDir = None
# Bundle paths. Files will be searched in these after the user path,
# but before the system path.
bundlePaths = []
# The custom paths override the default user paths.
customPaths = {}
def _createDir(path):
"""Creates the specified directory if it doesn't already exist."""
if not os.path.exists(path):
os.mkdir(path)
def _checkSnowberryHome():
"""Checks if the Snowberry home directory exists. If it doesn't, it
is created and populated with the basic files and directories."""
global homeDir
# When this is run for the first time, we'll need to figure out
# where the home directory should be.
if not homeDir:
if isHomeless():
homeLocation = os.getcwd()
else:
if host.isMac():
# Home on the Mac.
homeLocation = os.path.join(os.getenv('HOME'),
'Library/Application Support')
else:
# First see if a HOME environment variable has been defined.
homeLocation = os.getenv('HOME')
if not homeLocation:
# The failsafe.
homeLocation = os.getcwd()
# The environment variable was not defined. Let's try
# something else.
if host.isWindows():
if os.getenv('HOMEPATH'):
homeLocation = os.getenv('HOMEDRIVE') + \
os.getenv('HOMEPATH')
elif os.getenv('USERPROFILE'):
homeLocation = os.getenv('USERPROFILE')
elif os.getenv('APPDATA'):
homeLocation = os.getenv('APPDATA')
homeDir = os.path.join(homeLocation, SNOWBERRY_HOME_DIR)
homeDir = os.path.abspath(homeDir)
# The home needs to be created if it doesn't exist yet.
_createDir(homeDir)
# Create the rest of the user directories if they don't exist yet.
for p in [ADDONS, MANIFESTS, UNINSTALLED, CONF, LANG, PLUGINS, PROFILES,
GRAPHICS, RUNTIME]:
_createDir(getUserPath(p))
def isCaseSensitive():
"""Returns True if the file system is case sensitive."""
return not host.isWindows() and not host.isMac()
def addBundlePath(path):
"""Add a path to the list of paths where files will be searched
from.
@param path The bundle contents path.
"""
global bundlePaths
bundlePaths.append(path)
def setCustomPath(path, custom):
"""Set a custom path.
@param path The path to set (PROFILES, RUNTIME, CONF, etc.).
@param custom The custom path to use.
"""
customPaths[path] = custom
def getHomeDirectory():
"Returns the current user's Snowberry home directory."
return homeDir
def getSystemPath(which):
"""Returns the directory where the specified kind of data is
stored. System paths are assumed to be relative to the current
working directory."""
return os.path.abspath(which)
def getUserPath(which):
"""Returns the directory where the specified kind of data is
stored for the current user. The location of the user home
directory depends on the operating system."""
if customPaths.has_key(which):
return customPaths[which]
path = os.path.join(homeDir, which)
# The system profiles directory and the user profiles directory can't
# be the same.
if which == PROFILES and path == getSystemPath(which):
path = os.path.join(homeDir, 'user' + which)
return path
def getBundlePaths(which):
"""Returns the directories where plugin bundles have their files
of the specified kind."""
return map(lambda p: os.path.join(p, which), bundlePaths)
def listPaths(which):
"""Returns an array of search paths for the specified kind of files.
"""
return [getUserPath(which)] + getBundlePaths(which) + \
[getSystemPath(which)]
def listFiles(which):
"""Returns a list of all the files of the specified kind.
@return An array of absolute file names.
"""
files = []
for path in listPaths(which):
try:
for name in os.listdir(path):
full = os.path.join(path, name)
if full not in files:
files.append(os.path.abspath(full))
except OSError:
# The directory probably didn't exist. We don't need to
# do anything.
pass
return files
def findBitmap(name):
"""Locates a bitmap file. Files in the user's graphics directory
override the contents of the system graphics directory.
@param name Name of the bitmap without the file name extension.
@return The full path to the bitmap file.
"""
# First see if its in the user's graphics directory.
for path in listPaths(GRAPHICS):
for ext in ['jpg', 'bmp', 'png']:
fileName = os.path.join(path, name + '.' + ext)
if os.path.exists(fileName):
return fileName
# The file was not found.
return ''
def getBase(path):
"""Returns the file name sans path and extension."""
base = os.path.basename(path)
if base.find('.') < 0:
# There is no extension.
return base
# Find the last dot.
pos = base.find('.')
return base[:pos]
def hasExtension(extension, fileName):
"""Checks if the specified file name has the given extension.
@param extension The extension to look for.
@param fileName A file name.
@return True, if the extension is found.
"""
return re.search("^[^.#][^.]*\." + extension + "$",
os.path.basename(fileName).lower()) != None
def quote(fileName):
"""Return the gives path in quotes so that it may be used as a
command line option."""
return '"' + fileName.replace('"', '""') + '"'
def fixSlashes(path):
"""Replace all forward and backward slashes with the correct
slashes of the host OS."""
def addAddonPath(path):
"""Define a new custom directory for addons. Addons can be loaded
from all the custom directories. Installed addons always go to
the default addons directory.
@param path Absolute path of the new custom addons directory.
"""
if path not in addonPaths:
addonPaths.append(path)
def removeAddonPath(path):
"""Remove a custom directory for addons.
@param path Absolute path of the custom addons directory.
"""
if path in addonPaths:
addonPaths.remove(path)
def getAddonPaths():
"""Returns all the custom addon paths.
@return An array containing full paths.
"""
return addonPaths
def saveAddonPaths():
"""Write the custom addon paths to a configuration file."""
fileName = os.path.join(getUserPath(CONF), 'addon-paths.conf')
try:
f = file(fileName, 'w')
f.write('# This file is generated automatically.\n')
f.write('configure addon-path (\n')
for p in addonPaths:
f.write(' readonly: %s\n' % p)
f.write(')\n')
except:
# Paths not saved.
pass
def handleNotify(event):
"""When the <code>quit</code> notification is sent, save the
custom addon paths."""
if event.hasId('quit'):
saveAddonPaths()
## When this module is initialized, check for the home directory.
## It is created automatically if it doesn't exist yet.
_checkSnowberryHome()
events.addNotifyListener(handleNotify)