1 |
|
# Copyright 2008 Dan Smith <dsmith@danplanet.com>
|
|
1 |
# Copyright 2008, 2013 Dan Smith <dsmith@danplanet.com>
|
2 |
2 |
#
|
3 |
3 |
# This program is free software: you can redistribute it and/or modify
|
4 |
4 |
# it under the terms of the GNU General Public License as published by
|
... | ... | |
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/>.
|
15 |
15 |
|
|
16 |
import errno
|
16 |
17 |
import os
|
17 |
18 |
import sys
|
18 |
19 |
import glob
|
... | ... | |
49 |
50 |
except:
|
50 |
51 |
comports = win32_comports_bruteforce
|
51 |
52 |
|
52 |
|
def _find_me():
|
53 |
|
return sys.modules["chirp.platform"].__file__
|
54 |
|
|
55 |
53 |
def natural_sorted(l):
|
56 |
54 |
convert = lambda text: int(text) if text.isdigit() else text.lower()
|
57 |
55 |
natural_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
|
... | ... | |
63 |
61 |
def __init__(self, basepath):
|
64 |
62 |
self._base = basepath
|
65 |
63 |
self._last_dir = self.default_dir()
|
|
64 |
self._prefix = None
|
|
65 |
self._locale_path = None
|
|
66 |
self._share_path = None
|
|
67 |
self._stock_configs_path = None
|
|
68 |
self._pixmaps_path = None
|
|
69 |
# check if we are packaged with PyToExe, PyToApp
|
|
70 |
self._frozen = hasattr(sys, "frozen")
|
|
71 |
# check if we are we installed or running from a source tree
|
|
72 |
self._running_from_source = \
|
|
73 |
"site-packages" not in os.path.abspath(__file__)
|
|
74 |
|
|
75 |
def init_dirs(self):
|
|
76 |
# calculate the locations of runtime directories
|
|
77 |
# relative to our install location (self._prefix).
|
|
78 |
# These are also different if we're running from a
|
|
79 |
# source tree or we are packaged with PyToExe / PyToApp
|
|
80 |
# This function should be called by derived classes after
|
|
81 |
# self._prefix is set
|
|
82 |
|
|
83 |
# TODO are these the same across platforms ?
|
|
84 |
|
|
85 |
if self._running_from_source:
|
|
86 |
# running from the source tree
|
|
87 |
self._share_path = os.path.abspath(os.path.join(
|
|
88 |
self._prefix, "share"))
|
|
89 |
self._locale_path = os.path.abspath(os.path.join(
|
|
90 |
self._prefix, "locale"))
|
|
91 |
self._stock_configs_path = os.path.abspath(os.path.join(
|
|
92 |
self._prefix, "stock_configs"))
|
|
93 |
self._pixmaps_path = self._share_path
|
|
94 |
self._schema_path = self._prefix
|
|
95 |
elif self._frozen:
|
|
96 |
# PytoExe, PyToApp
|
|
97 |
self._share_path = os.path.abspath(os.path.join(
|
|
98 |
self._prefix, "share"))
|
|
99 |
self._locale_path = os.path.abspath(os.path.join(
|
|
100 |
self._prefix, "locale"))
|
|
101 |
self._stock_configs_path = os.path.abspath(os.path.join(
|
|
102 |
self._prefix, "stock_configs"))
|
|
103 |
self._pixmaps_path = self._share_path
|
|
104 |
self._schema_path = self._prefix
|
|
105 |
else:
|
|
106 |
# python site-packages installation
|
|
107 |
self._share_path = os.path.abspath(os.path.join(
|
|
108 |
self._prefix, "share", "chirp"))
|
|
109 |
self._locale_path = os.path.abspath(os.path.join(
|
|
110 |
self._share_path, "locale"))
|
|
111 |
self._stock_configs_path = os.path.abspath(os.path.join(
|
|
112 |
self._share_path, "stock_configs"))
|
|
113 |
self._pixmaps_path = os.path.abspath(os.path.join(
|
|
114 |
self._prefix, "share", "pixmaps"))
|
|
115 |
self._schema_path = os.path.abspath(os.path.join(
|
|
116 |
self._prefix, "share", "chirp"))
|
|
117 |
|
|
118 |
if not os.path.exists(self._share_path):
|
|
119 |
raise IOError(errno.ENOENT,
|
|
120 |
"The directory %s does not exist." % self._share_path)
|
|
121 |
|
|
122 |
if not os.path.exists(self._locale_path):
|
|
123 |
raise IOError(errno.ENOENT,
|
|
124 |
"The directory %s does not exist." % self._locale_path)
|
|
125 |
|
|
126 |
if not os.path.exists(self._stock_configs_path):
|
|
127 |
raise IOError(errno.ENOENT,
|
|
128 |
"The directory %s does not exist." % self._stock_configs_path)
|
|
129 |
|
|
130 |
if not os.path.exists(self._pixmaps_path):
|
|
131 |
raise IOError(errno.ENOENT,
|
|
132 |
"The directory %s does not exist." % self._pixmaps_path)
|
|
133 |
|
|
134 |
if not os.path.exists(self._schema_path):
|
|
135 |
raise IOError(errno.ENOENT,
|
|
136 |
"The directory %s does not exist." % self._schema_path)
|
66 |
137 |
|
67 |
138 |
def get_last_dir(self):
|
68 |
139 |
"""Return the last directory used"""
|
... | ... | |
98 |
169 |
return os.path.join(self.config_dir(),
|
99 |
170 |
self.filter_filename(filename))
|
100 |
171 |
|
|
172 |
def executable_path(self):
|
|
173 |
"""Return the base of the installation"""
|
|
174 |
return self._prefix
|
|
175 |
|
|
176 |
def locale_dir(self):
|
|
177 |
"""Return the locale directory for the installation"""
|
|
178 |
return self._locale_path
|
|
179 |
|
|
180 |
def share_dir(self):
|
|
181 |
"""Return the shared files directory for the installation"""
|
|
182 |
return self._share_path
|
|
183 |
|
|
184 |
def stock_configs_dir(self):
|
|
185 |
"""Return the stock configs directory for the installation"""
|
|
186 |
return self._stock_configs_path
|
|
187 |
|
|
188 |
def pixmaps_dir(self):
|
|
189 |
"""Return the pixelmaps directory for the installation"""
|
|
190 |
return self._pixmaps_path
|
|
191 |
|
|
192 |
def schema_dir(self):
|
|
193 |
"""Return the xml schema directory for the installation"""
|
|
194 |
return self._schema_path
|
|
195 |
|
101 |
196 |
def open_text_file(self, path):
|
102 |
197 |
"""Spawn the necessary program to open a text file at @path"""
|
103 |
198 |
raise NotImplementedError("The base class can't do that")
|
... | ... | |
215 |
310 |
"""Return a string that describes the OS/platform version"""
|
216 |
311 |
return "Unknown Operating System"
|
217 |
312 |
|
218 |
|
def executable_path(self):
|
219 |
|
"""Return a full path to the program executable"""
|
220 |
|
def we_are_frozen():
|
221 |
|
return hasattr(sys, "frozen")
|
|
313 |
def _get_prefix(self):
|
|
314 |
"""Returns the python prefix
|
222 |
315 |
|
223 |
|
if we_are_frozen():
|
224 |
|
# Win32, find the directory of the executable
|
225 |
|
return os.path.dirname(unicode(sys.executable,
|
226 |
|
sys.getfilesystemencoding()))
|
|
316 |
this is the equivalent of 'python-config --prefix' , accounting
|
|
317 |
for any virtual environment, and accounting for running from source
|
|
318 |
|
|
319 |
"""
|
|
320 |
# find our current location
|
|
321 |
if self._running_from_source:
|
|
322 |
# running from source tree, only two levels up
|
|
323 |
return os.path.abspath(os.path.join(__file__,
|
|
324 |
"..", ".."))
|
227 |
325 |
else:
|
228 |
|
# UNIX: Find the parent directory of this module
|
229 |
|
return os.path.dirname(os.path.abspath(os.path.join(_find_me(),
|
230 |
|
"..")))
|
|
326 |
# we're 'installed', so prefix is five levels up
|
|
327 |
return os.path.abspath(os.path.join(__file__,
|
|
328 |
"..", "..", "..", "..", ".."))
|
|
329 |
|
231 |
330 |
|
232 |
331 |
def _unix_editor():
|
233 |
332 |
macos_textedit = "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
|
... | ... | |
249 |
348 |
|
250 |
349 |
Platform.__init__(self, basepath)
|
251 |
350 |
|
252 |
|
# This is a hack that needs to be properly fixed by importing the
|
253 |
|
# latest changes to this module from d-rats. In the interest of
|
254 |
|
# time, however, I'll throw it here
|
|
351 |
# set the installation prefix
|
|
352 |
# see http://stackoverflow.com/questions/1511461/py2exe-and-the-file-system
|
|
353 |
# py2app:
|
|
354 |
# Notes on how to find stuff on MAC, by an expert (Bob Ippolito):
|
|
355 |
# http://mail.python.org/pipermail/pythonmac-sig/2004-November/012121.html
|
|
356 |
# TODO verify this logic for OSX
|
|
357 |
if self._frozen:
|
|
358 |
self._prefix = os.environ['RESOURCEPATH']
|
|
359 |
else:
|
|
360 |
self._prefix = self._get_prefix()
|
|
361 |
if not os.path.exists(self._prefix):
|
|
362 |
raise IOError(errno.ENOENT, " The directory %s does not exist." % self._prefix)
|
|
363 |
|
|
364 |
self.init_dirs()
|
|
365 |
|
|
366 |
# This is a hack that needs to be properly fixed by importing the
|
|
367 |
# latest changes to this module from d-rats. In the interest of
|
|
368 |
# time, however, I'll throw it here
|
255 |
369 |
if sys.platform == "darwin":
|
256 |
370 |
if not os.environ.has_key("DISPLAY"):
|
257 |
371 |
print "Forcing DISPLAY for MacOS"
|
... | ... | |
302 |
416 |
|
303 |
417 |
return ver
|
304 |
418 |
|
|
419 |
|
305 |
420 |
class Win32Platform(Platform):
|
306 |
421 |
"""A platform module suitable for Windows systems"""
|
307 |
422 |
def __init__(self, basepath=None):
|
... | ... | |
316 |
431 |
|
317 |
432 |
Platform.__init__(self, basepath)
|
318 |
433 |
|
|
434 |
# set the installation prefix
|
|
435 |
if self._frozen:
|
|
436 |
# Win32, find the directory of the executable
|
|
437 |
self._prefix = os.path.dirname(unicode(sys.executable,
|
|
438 |
sys.getfilesystemencoding()))
|
|
439 |
else:
|
|
440 |
self._prefix = self._get_prefix()
|
|
441 |
|
|
442 |
self.init_dirs()
|
|
443 |
|
319 |
444 |
def default_dir(self):
|
320 |
445 |
return os.path.abspath(os.path.join(os.getenv("USERPROFILE"),
|
321 |
446 |
"Desktop"))
|
... | ... | |
431 |
556 |
return PLATFORM
|
432 |
557 |
|
433 |
558 |
def _do_test():
|
|
559 |
|
434 |
560 |
__pform = get_platform()
|
435 |
561 |
|
436 |
562 |
print "Config dir: %s" % __pform.config_dir()
|
... | ... | |
438 |
564 |
print "Log file (foo): %s" % __pform.log_file("foo")
|
439 |
565 |
print "Serial ports: %s" % __pform.list_serial_ports()
|
440 |
566 |
print "OS Version: %s" % __pform.os_version_string()
|
|
567 |
|
|
568 |
print "Frozen : %s" % __pform._frozen
|
|
569 |
print "Running from Source : %s" % __pform._running_from_source
|
|
570 |
|
|
571 |
print "Executable path : %s" % __pform.executable_path()
|
|
572 |
print "Share directory : %s" % __pform.share_dir()
|
|
573 |
print "Locale directory : %s" % __pform.locale_dir()
|
|
574 |
print "Stock configs directory : %s" % __pform.stock_configs_dir()
|
|
575 |
print "pixmaps directory : %s" % __pform.pixmaps_dir()
|
|
576 |
print "schema directory : %s" % __pform.schema_dir()
|
|
577 |
|
441 |
578 |
#__pform.open_text_file("d-rats.py")
|
442 |
579 |
|
443 |
580 |
#print "Open file: %s" % __pform.gui_open_file()
|