[4] | 1 | ############################################################################ |
---|
| 2 | # |
---|
| 3 | # SAGE LAUNCHER - A GUI for launching SAGE and all related components |
---|
| 4 | # |
---|
| 5 | # Copyright (C) 2010 Electronic Visualization Laboratory, |
---|
| 6 | # University of Illinois at Chicago |
---|
| 7 | # |
---|
| 8 | # All rights reserved. |
---|
| 9 | # |
---|
| 10 | # Redistribution and use in source and binary forms, with or without |
---|
| 11 | # modification, are permitted provided that the following conditions are met: |
---|
| 12 | # |
---|
| 13 | # * Redistributions of source code must retain the above copyright |
---|
| 14 | # notice, this list of conditions and the following disclaimer. |
---|
| 15 | # * Redistributions in binary form must reproduce the above |
---|
| 16 | # copyright notice, this list of conditions and the following disclaimer |
---|
| 17 | # in the documentation and/or other materials provided with the distribution. |
---|
| 18 | # * Neither the name of the University of Illinois at Chicago nor |
---|
| 19 | # the names of its contributors may be used to endorse or promote |
---|
| 20 | # products derived from this software without specific prior written permission. |
---|
| 21 | # |
---|
| 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 26 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 27 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 28 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 29 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 30 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 31 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 32 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 33 | # |
---|
| 34 | # Direct questions, comments etc about SAGE UI to www.evl.uic.edu/cavern/forum |
---|
| 35 | # |
---|
| 36 | # Author: Ratko Jagodic |
---|
| 37 | # |
---|
| 38 | ############################################################################ |
---|
| 39 | |
---|
| 40 | import os.path, os, sys |
---|
| 41 | import traceback as tb |
---|
| 42 | |
---|
| 43 | opj = os.path.join |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | CONFIG_DIR = ".sageConfig" |
---|
| 47 | USER_DIR = os.path.expanduser("~") |
---|
| 48 | USER_CONFIG_DIR = opj( USER_DIR, CONFIG_DIR ) |
---|
| 49 | SAGE_DIR = os.path.realpath(os.environ["SAGE_DIRECTORY"]) # where sage is located |
---|
| 50 | DEFAULT_CONFIG_DIR = opj( SAGE_DIR, CONFIG_DIR.lstrip(".") ) |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | # first make the user config dir if it doesnt exist |
---|
| 54 | if not os.path.isdir(USER_CONFIG_DIR): |
---|
| 55 | os.makedirs(USER_CONFIG_DIR) |
---|
| 56 | os.makedirs( opj(USER_CONFIG_DIR, "applications", "pid") ) |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | # searches two paths: ~/.sageConfig and $SAGE_DIRECTORY/sageConfig |
---|
| 60 | def getPath(*args): |
---|
| 61 | fileName = "" |
---|
| 62 | for a in args: |
---|
| 63 | fileName = opj(fileName, a) |
---|
| 64 | |
---|
| 65 | userPath = opj( USER_CONFIG_DIR, fileName ) |
---|
| 66 | defaultPath = opj( DEFAULT_CONFIG_DIR, fileName ) |
---|
| 67 | currentPath = opj( os.getcwd(), args[len(args)-1]) # last argument |
---|
| 68 | |
---|
| 69 | if os.path.isfile( currentPath ): |
---|
| 70 | return currentPath |
---|
| 71 | elif os.path.isfile( userPath ): |
---|
| 72 | return userPath |
---|
| 73 | else: |
---|
| 74 | return defaultPath |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | # to get a path to a file in the user's home config directory |
---|
| 78 | def getUserPath(*args): |
---|
| 79 | p = USER_CONFIG_DIR |
---|
| 80 | for a in args: |
---|
| 81 | p = opj(p, a) |
---|
| 82 | |
---|
| 83 | # make the directory... if it doesnt exist |
---|
| 84 | # this will make the directory even if a file is passed in with some directories before it |
---|
| 85 | if not os.path.exists(p): |
---|
| 86 | try: |
---|
| 87 | d, ext = os.path.splitext(p) |
---|
| 88 | if ext == "": # it's a directory |
---|
| 89 | d = os.path.splitext(p)[0] |
---|
| 90 | else: # it's a file |
---|
| 91 | d = os.path.dirname(p) |
---|
| 92 | if not os.path.exists(d): os.makedirs(d) |
---|
| 93 | |
---|
| 94 | except: |
---|
| 95 | print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])) |
---|
| 96 | |
---|
| 97 | return p |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | # to get a path to a file in the default config directory |
---|
| 102 | def getDefaultPath(*args): |
---|
| 103 | p = DEFAULT_CONFIG_DIR |
---|
| 104 | for a in args: |
---|
| 105 | p = opj(p, a) |
---|
| 106 | return p |
---|