Skip to content

Commit dbdfc68

Browse files
committed
enh: add release checking on import
1 parent 5ab0212 commit dbdfc68

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

nipype/__init__.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# emacs: -*- coding: utf-8; mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2-
# vi: set fileencoding=utf-8 ft=python sts=4 ts=4 sw=4 et:
3-
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
43
import os
54

65
from info import (LONG_DESCRIPTION as __doc__,
76
URL as __url__,
87
STATUS as __status__,
98
__version__)
9+
from utils.config import config
1010

1111
# We require numpy 1.2 for our test suite. If Tester fails to import,
1212
# check the version of numpy the user has and inform them they need to
@@ -18,6 +18,7 @@
1818
else:
1919
from testing.numpytesting import Tester
2020

21+
2122
class NipypeTester(Tester):
2223
def test(self, label='fast', verbose=1, extra_argv=None,
2324
doctests=False, coverage=False):
@@ -43,7 +44,6 @@ def _test_local_install():
4344
""" Warn the user that running with nipy being
4445
imported locally is a bad idea.
4546
"""
46-
import os
4747
if os.getcwd() == os.sep.join(
4848
os.path.abspath(__file__).split(os.sep)[:-2]):
4949
import warnings
@@ -54,15 +54,49 @@ def _test_local_install():
5454

5555
# Set up package information function
5656
from pkg_info import get_pkg_info as _get_pkg_info
57-
get_info = lambda : _get_pkg_info(os.path.dirname(__file__))
57+
get_info = lambda: _get_pkg_info(os.path.dirname(__file__))
5858

5959
# Cleanup namespace
6060
del _test_local_install
6161

6262
# If this file is exec after being imported, the following lines will
6363
# fail
6464
try:
65-
del version
6665
del Tester
6766
except:
6867
pass
68+
69+
70+
def check_for_updates():
71+
from urllib import urlopen
72+
import re
73+
devdata = urlopen(('http://www.mit.edu/~satra/nipype-nightly'
74+
'/version.html')).read()
75+
try:
76+
dev_ver = re.search('Release:</th><td class="field-body">(.*)</td>\n',
77+
devdata).groups()[0]
78+
except AttributeError:
79+
dev_ver = 'unknown'
80+
81+
devdata = urlopen(('http://nipy.org/nipype/version.html')).read()
82+
try:
83+
rel_ver = re.search('Release:</th><td class="field-body">(.*)</td>\n',
84+
devdata).groups()[0]
85+
except AttributeError:
86+
rel_ver = 'unknown'
87+
print "Installed version: %s" % __version__
88+
print "Current stable version: %s" % rel_ver
89+
print "Current dev version: %s" % dev_ver
90+
91+
92+
from time import time
93+
t = time()
94+
last_check = config.get_data('last_check')
95+
if last_check is None or (t - last_check) > float(config.get('check',
96+
'interval')):
97+
try:
98+
check_for_updates()
99+
except Exception, e:
100+
print e
101+
finally:
102+
config.save_data('last_check', t)

nipype/utils/config.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
99
@author: Chris Filo Gorgolewski
1010
'''
11-
import ConfigParser, os
12-
from StringIO import StringIO
11+
12+
import ConfigParser
13+
from json import load, dump
1314
import os
15+
import shutil
16+
from StringIO import StringIO
17+
from warnings import warn
18+
19+
from ..external import portalocker
1420

1521
homedir = os.environ['HOME']
1622
default_cfg = StringIO("""
@@ -38,8 +44,28 @@
3844
stop_on_first_crash = false
3945
stop_on_first_rerun = false
4046
use_relative_paths = false
47+
48+
[check]
49+
interval = 1209600
4150
""" % (homedir, os.getcwd()))
4251

52+
"""
53+
Initialize the config object in module load
54+
"""
55+
56+
config_dir = os.path.expanduser('~/.nipype')
57+
if not os.path.exists(config_dir):
58+
os.makedirs(config_dir)
59+
old_config_file = os.path.expanduser('~/.nipype.cfg')
60+
new_config_file = os.path.join(config_dir, 'nipype.cfg')
61+
# To be deprecated in two releases
62+
if os.path.exists(old_config_file):
63+
warn("Moving old config file from: %s to %s" % (old_config_file,
64+
new_config_file))
65+
shutil.move(old_config_file, new_config_file)
66+
data_file = os.path.join(config_dir, 'nipype.json')
67+
68+
4369
class NipypeConfig(ConfigParser.ConfigParser):
4470
"""Base nipype config class
4571
"""
@@ -61,10 +87,27 @@ def set_log_dir(self, log_dir):
6187
"""
6288
config.set('logging', 'log_directory', log_dir)
6389

64-
"""
65-
Initialize the config object in module load
66-
"""
90+
def get_data(self, key):
91+
if not os.path.exists(data_file):
92+
return None
93+
with open(data_file, 'rt') as file:
94+
portalocker.lock(file, portalocker.LOCK_EX)
95+
datadict = load(file)
96+
if key in datadict:
97+
return datadict[key]
98+
return None
99+
100+
def save_data(self, key, value):
101+
datadict = {}
102+
if os.path.exists(data_file):
103+
with open(data_file, 'rt') as file:
104+
portalocker.lock(file, portalocker.LOCK_EX)
105+
datadict = load(file)
106+
with open(data_file, 'wt') as file:
107+
portalocker.lock(file, portalocker.LOCK_EX)
108+
datadict[key] = value
109+
dump(datadict, file)
67110

68111
config = NipypeConfig()
69112
config.readfp(default_cfg)
70-
config.read([os.path.expanduser('~/.nipype.cfg'), 'nipype.cfg'])
113+
config.read([new_config_file, old_config_file, 'nipype.cfg'])

0 commit comments

Comments
 (0)