~nataliabidart/ubuntu-sso-client/rename-me-too

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/__init__.py

  • Committer: Tarmac
  • Author(s): Natalia B. Bidart
  • Date: 2012-02-13 13:12:16 UTC
  • mfrom: (858.1.2 find-those-uis)
  • Revision ID: tarmac-20120213131216-2vh8eq1aeibe7vvn
- Make the UI runner use the absolute path to the UI executables (LP: #930651).

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Utility modules that may find use outside ubuntu_sso."""
18
18
 
19
19
import cgi
 
20
import os
 
21
import sys
20
22
import time
21
23
import urllib2
22
24
 
30
32
 
31
33
 
32
34
logger = setup_logging("ubuntu_sso.utils")
 
35
DATA_SUFFIX = 'data'
 
36
BIN_SUFFIX = 'bin'
 
37
 
 
38
 
 
39
def _get_dir(dir_name, dir_constant):
 
40
    """Return the absolute path to this project's 'dir_name' dir.
 
41
 
 
42
    Support symlinks, and priorize local (relative) 'dir_name' dir. If not
 
43
    found, return the value of the 'dir_constant'.
 
44
 
 
45
    """
 
46
    module = os.path.dirname(__file__)
 
47
    result = os.path.abspath(os.path.join(module, os.path.pardir,
 
48
                                          os.path.pardir, dir_name))
 
49
    logger.debug('_get_dir: trying use dir at %r (exists? %s)',
 
50
                  result, os.path.exists(result))
 
51
    if os.path.exists(result):
 
52
        logger.info('_get_dir: returning dir located at %r.', result)
 
53
        return result
 
54
 
 
55
    # otherwise, try to load 'dir_constant' from installation path
 
56
    try:
 
57
        module = sys.modules.get('ubuntu_sso.constants')
 
58
        return getattr(module, dir_constant)
 
59
    except AttributeError:
 
60
        msg = '_get_dir: can not build a valid path. Giving up. ' \
 
61
              '__file__ is %r, constants module not available.'
 
62
        logger.error(msg, __file__)
 
63
 
 
64
 
 
65
def get_project_dir():
 
66
    """Return the absolute path to this project's data/ dir.
 
67
 
 
68
    Support symlinks, and priorize local (relative) data/ dir. If not
 
69
    found, return the value of the PROJECT_DIR.
 
70
 
 
71
    """
 
72
    return _get_dir(dir_name=DATA_SUFFIX, dir_constant='PROJECT_DIR')
 
73
 
 
74
 
 
75
def get_data_file(*args):
 
76
    """Return the absolute path to 'args' within project data dir."""
 
77
    return os.path.join(get_project_dir(), *args)
 
78
 
 
79
 
 
80
def get_bin_dir():
 
81
    """Return the absolute path to this project's bin/ dir.
 
82
 
 
83
    Support symlinks, and priorize local (relative) bin/ dir. If not
 
84
    found, return the value of the BIN_DIR.
 
85
 
 
86
    """
 
87
    return _get_dir(dir_name=BIN_SUFFIX, dir_constant='BIN_DIR')
33
88
 
34
89
 
35
90
class RequestHead(urllib2.Request):