home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / lib / environ.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.8 KB  |  113 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import posixpath
  21.  
  22. from os import environ
  23. from stat import ST_MODE, S_IEXEC
  24.  
  25.  
  26. def get_variables():
  27.     return dict(environ)
  28.  
  29. def get_variable(name, default=None):
  30.     """Get the value of an environment variable name.
  31.  
  32.     Keyword arguments:
  33.     name -- name of the environment variable
  34.     """
  35.  
  36.     return environ.get(name, default)
  37.  
  38. def add_variable(name, value):
  39.     """Add or change the value of an environment variable name.
  40.  
  41.     Keyword arguments:
  42.     name -- name of the environment variable
  43.     value -- value given to the environment variable
  44.     """
  45.  
  46.     environ[name] = value
  47.  
  48. def remove_variable(name):
  49.     """Remove an environment variable name.
  50.  
  51.     Keyword arguments:
  52.     name -- name of the environment variable
  53.     """
  54.  
  55.     if name in environ:
  56.         del environ[name]
  57.  
  58. def get_paths():
  59.     paths = []
  60.     if "PATH" in environ:
  61.         paths = environ["PATH"].split(":")
  62.  
  63.     return paths
  64.  
  65. def get_path(command):
  66.     for path in get_paths():
  67.         absolute = posixpath.join(path, command)
  68.         if posixpath.exists(absolute):
  69.             mode = os.stat(absolute)[ST_MODE]
  70.             if mode & S_IEXEC:
  71.                 return absolute
  72.  
  73.     return None
  74.  
  75. def append_path(path):
  76.     """Prepend a path to the PATH environment variable if it doesn't
  77.     exist already.
  78.  
  79.     Keyword arguments:
  80.     path -- path to add
  81.     """
  82.  
  83.     environ_path = get_paths()
  84.     if path not in environ_path:
  85.         environ_path.append(path)
  86.         environ["PATH"] = ":".join(environ_path)
  87.  
  88. def prepend_path(path):
  89.     """Prepend a path to the PATH environment variable if it doesn't
  90.     exist already.
  91.  
  92.     Keyword arguments:
  93.     path -- path to add
  94.     """
  95.  
  96.     environ_path = get_paths()
  97.     if path not in environ_path:
  98.         environ_path.insert(0, path)
  99.         environ["PATH"] = ":".join(environ_path)
  100.  
  101. def remove_path(path):
  102.     """Remove a path from the PATH environment variable if it exists
  103.     already.
  104.  
  105.     Keyword arguments:
  106.     path -- path to remove
  107.     """
  108.  
  109.     environ_path = get_paths()
  110.     if path in environ_path:
  111.         environ_path.remove(path)
  112.         environ["PATH"] = ":".join(environ_path)
  113.