import os
import tempfile
import atexit


DATA_ROOT = None
if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
    DATA_ROOT = os.getenv('ROOTPY_DATA')
    if DATA_ROOT is None:
        DATA_ROOT = os.path.expanduser('~/.rootpy')
    else:
        DATA_ROOT = os.path.expandvars(os.path.expanduser(DATA_ROOT))
    # check if expanduser failed:
    if DATA_ROOT.startswith('~'):
        DATA_ROOT = None
    elif not os.path.exists(DATA_ROOT):
        os.mkdir(DATA_ROOT)
    elif not os.path.isdir(DATA_ROOT):
        # A file at DATA_ROOT already exists
        DATA_ROOT = None

__is_tmp = False
if DATA_ROOT is None:
    print "Warning: placing user data in /tmp.\n" \
          "Make sure ~/.rootpy or $ROOTPY_DATA\n" \
          "is a writable directory so that I don't need to\n" \
          "recreate all user data each time"
    DATA_ROOT = tempfile.mkdtemp()
    __is_tmp = True


@atexit.register
def __cleanup():
    if __is_tmp:
        import shutil
        shutil.rmtree(DATA_ROOT)

import os
import tempfile
import atexit

from os.path import expanduser, expandvars, exists, isdir


if "XDG_CONFIG_HOME" not in os.environ:
    os.environ["XDG_CONFIG_HOME"] = expanduser('~/.config')
if "XDG_CACHE_HOME" not in os.environ:
    os.environ["XDG_CACHE_HOME"] = expanduser('~/.cache')

def ensure_directory(variable, default):
    path = os.getenv(variable)
    if path is None:
        path = expandvars(default)
    else:
        path = expandvars(expanduser(path))
        
    # check if expanduser failed:
    if path.startswith('~'):
        path = None
    elif not exists(path):
        os.makedirs(path)
    elif not isdir(path):
        # A file at path already exists
        path = None
    return path

DATA_ROOT = CONFIG_ROOT = None
if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
    DATA_ROOT = ensure_directory('ROOTPY_DATA', '${XDG_CACHE_HOME}/rootpy')
    CONFIG_ROOT = ensure_directory('ROOTPY_CONFIG', '${XDG_CONFIG_HOME}/rootpy')


if DATA_ROOT is None:
    log.info("Placing user data in /tmp.")
    log.warning("Make sure '~/.cache/rootpy' or $ROOTPY_DATA is a writable "
                "directory so that it isn't necessary to recreate all user data"
                " each time")
    
    DATA_ROOT = tempfile.mkdtemp()

    @atexit.register
    def __cleanup():
        import shutil
        shutil.rmtree(DATA_ROOT)


Use XDG base directory specificiation
Use XDG base directory specificiation

  import os
  import tempfile
  import atexit
  
+ from os.path import expanduser, expandvars, exists, isdir
- 
- DATA_ROOT = None
- if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
-     DATA_ROOT = os.getenv('ROOTPY_DATA')
-     if DATA_ROOT is None:
-         DATA_ROOT = os.path.expanduser('~/.rootpy')
-     else:
-         DATA_ROOT = os.path.expandvars(os.path.expanduser(DATA_ROOT))
-     # check if expanduser failed:
-     if DATA_ROOT.startswith('~'):
-         DATA_ROOT = None
-     elif not os.path.exists(DATA_ROOT):
-         os.mkdir(DATA_ROOT)
-     elif not os.path.isdir(DATA_ROOT):
-         # A file at DATA_ROOT already exists
-         DATA_ROOT = None
- 
- __is_tmp = False
- if DATA_ROOT is None:
-     print "Warning: placing user data in /tmp.\n" \
-           "Make sure ~/.rootpy or $ROOTPY_DATA\n" \
-           "is a writable directory so that I don't need to\n" \
-           "recreate all user data each time"
-     DATA_ROOT = tempfile.mkdtemp()
-     __is_tmp = True
  
  
+ if "XDG_CONFIG_HOME" not in os.environ:
+     os.environ["XDG_CONFIG_HOME"] = expanduser('~/.config')
+ if "XDG_CACHE_HOME" not in os.environ:
+     os.environ["XDG_CACHE_HOME"] = expanduser('~/.cache')
+ 
+ def ensure_directory(variable, default):
+     path = os.getenv(variable)
+     if path is None:
+         path = expandvars(default)
+     else:
+         path = expandvars(expanduser(path))
+         
+     # check if expanduser failed:
+     if path.startswith('~'):
+         path = None
+     elif not exists(path):
+         os.makedirs(path)
+     elif not isdir(path):
+         # A file at path already exists
+         path = None
+     return path
+ 
+ DATA_ROOT = CONFIG_ROOT = None
+ if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
+     DATA_ROOT = ensure_directory('ROOTPY_DATA', '${XDG_CACHE_HOME}/rootpy')
+     CONFIG_ROOT = ensure_directory('ROOTPY_CONFIG', '${XDG_CONFIG_HOME}/rootpy')
+ 
+ 
+ if DATA_ROOT is None:
+     log.info("Placing user data in /tmp.")
+     log.warning("Make sure '~/.cache/rootpy' or $ROOTPY_DATA is a writable "
+                 "directory so that it isn't necessary to recreate all user data"
+                 " each time")
+     
+     DATA_ROOT = tempfile.mkdtemp()
+ 
- @atexit.register
+     @atexit.register
- def __cleanup():
+     def __cleanup():
-     if __is_tmp:
          import shutil
          shutil.rmtree(DATA_ROOT)
  
+ 
Use XDG base directory specificiation
## Code Before:
import os
import tempfile
import atexit


DATA_ROOT = None
if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
    DATA_ROOT = os.getenv('ROOTPY_DATA')
    if DATA_ROOT is None:
        DATA_ROOT = os.path.expanduser('~/.rootpy')
    else:
        DATA_ROOT = os.path.expandvars(os.path.expanduser(DATA_ROOT))
    # check if expanduser failed:
    if DATA_ROOT.startswith('~'):
        DATA_ROOT = None
    elif not os.path.exists(DATA_ROOT):
        os.mkdir(DATA_ROOT)
    elif not os.path.isdir(DATA_ROOT):
        # A file at DATA_ROOT already exists
        DATA_ROOT = None

__is_tmp = False
if DATA_ROOT is None:
    print "Warning: placing user data in /tmp.\n" \
          "Make sure ~/.rootpy or $ROOTPY_DATA\n" \
          "is a writable directory so that I don't need to\n" \
          "recreate all user data each time"
    DATA_ROOT = tempfile.mkdtemp()
    __is_tmp = True


@atexit.register
def __cleanup():
    if __is_tmp:
        import shutil
        shutil.rmtree(DATA_ROOT)

## Instruction:
Use XDG base directory specificiation
## Code After:
import os
import tempfile
import atexit

from os.path import expanduser, expandvars, exists, isdir


if "XDG_CONFIG_HOME" not in os.environ:
    os.environ["XDG_CONFIG_HOME"] = expanduser('~/.config')
if "XDG_CACHE_HOME" not in os.environ:
    os.environ["XDG_CACHE_HOME"] = expanduser('~/.cache')

def ensure_directory(variable, default):
    path = os.getenv(variable)
    if path is None:
        path = expandvars(default)
    else:
        path = expandvars(expanduser(path))
        
    # check if expanduser failed:
    if path.startswith('~'):
        path = None
    elif not exists(path):
        os.makedirs(path)
    elif not isdir(path):
        # A file at path already exists
        path = None
    return path

DATA_ROOT = CONFIG_ROOT = None
if os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true'):
    DATA_ROOT = ensure_directory('ROOTPY_DATA', '${XDG_CACHE_HOME}/rootpy')
    CONFIG_ROOT = ensure_directory('ROOTPY_CONFIG', '${XDG_CONFIG_HOME}/rootpy')


if DATA_ROOT is None:
    log.info("Placing user data in /tmp.")
    log.warning("Make sure '~/.cache/rootpy' or $ROOTPY_DATA is a writable "
                "directory so that it isn't necessary to recreate all user data"
                " each time")
    
    DATA_ROOT = tempfile.mkdtemp()

    @atexit.register
    def __cleanup():
        import shutil
        shutil.rmtree(DATA_ROOT)

