forked from rootpy/rootpy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserdata.py
More file actions
68 lines (56 loc) · 2.01 KB
/
Copy pathuserdata.py
File metadata and controls
68 lines (56 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2012 the rootpy developers
# distributed under the terms of the GNU General Public License
"""
This module handles creation of the user-data area
"""
from __future__ import absolute_import
import os
import sys
import tempfile
import atexit
from os.path import expanduser, expandvars, exists, isdir, join as pjoin
from platform import machine
from . import log; log = log[__name__]
from . import QROOT
from rootpy.defaults import extra_initialization
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') and
not sys.argv[0].endswith('nosetests')) or os.getenv('DEBUG', None):
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)
ARCH = "{0}-{1}".format(machine(), QROOT.gROOT.GetVersionInt())
BINARY_PATH = pjoin(DATA_ROOT, ARCH)
if not exists(BINARY_PATH):
os.makedirs(BINARY_PATH)
@extra_initialization
def show_binary_path():
log.debug("Using binary path: {0}".format(BINARY_PATH))