<s> import os <EOL> import os . path as op <EOL> import shutil <EOL> import tarfile <EOL> import stat <EOL> import sys <EOL> from . . import __version__ as mne_version <EOL> from . . utils import get_config , set_config , _fetch_file , logger , warn <EOL> from . . externals . six import string_types <EOL> from . . externals . six . moves import input <EOL> _data_path_doc = """<STR_LIT>""" <EOL> _version_doc = """<STR_LIT>""" <EOL> _bst_license_text = """<STR_LIT>""" <EOL> def _dataset_version ( path , name ) : <EOL> """<STR_LIT>""" <EOL> ver_fname = op . join ( path , '<STR_LIT>' ) <EOL> if op . exists ( ver_fname ) : <EOL> with open ( ver_fname , '<STR_LIT:r>' ) as fid : <EOL> version = fid . readline ( ) . strip ( ) <EOL> else : <EOL> version = '<STR_LIT>' if name == '<STR_LIT>' else '<STR_LIT>' <EOL> return version <EOL> def _get_path ( path , key , name ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> def_path = op . realpath ( op . join ( op . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT:..>' , <EOL> '<STR_LIT>' ) ) <EOL> if get_config ( key ) is None : <EOL> key = '<STR_LIT>' <EOL> path = get_config ( key , def_path ) <EOL> if not op . exists ( path ) or not os . access ( path , os . W_OK ) : <EOL> try : <EOL> os . mkdir ( path ) <EOL> except OSError : <EOL> try : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> path = op . join ( op . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> if not op . exists ( path ) : <EOL> logger . info ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> os . mkdir ( path ) <EOL> except OSError : <EOL> raise OSError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( path ) ) <EOL> if not isinstance ( path , string_types ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return path <EOL> def _do_path_update ( path , update_path , key , name ) : <EOL> """<STR_LIT>""" <EOL> path = op . abspath ( path ) <EOL> if update_path is None : <EOL> if get_config ( key , '<STR_LIT>' ) != path : <EOL> update_path = True <EOL> if '<STR_LIT>' in sys . argv : <EOL> answer = '<STR_LIT:y>' <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( path , name ) ) <EOL> answer = input ( msg ) <EOL> if answer . lower ( ) == '<STR_LIT:n>' : <EOL> update_path = False <EOL> else : <EOL> update_path = False <EOL> if update_path is True : <EOL> set_config ( key , path ) <EOL> return path <EOL> def _data_path ( path = None , force_update = False , update_path = True , download = True , <EOL> name = None , check_version = False , return_version = False , <EOL> archive_name = None ) : <EOL> """<STR_LIT>""" <EOL> key = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } [ name ] <EOL> path = _get_path ( path , key , name ) <EOL> releases = dict ( testing = '<STR_LIT>' , misc = '<STR_LIT>' ) <EOL> archive_names = dict ( <EOL> misc = '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> sample = '<STR_LIT>' , <EOL> somato = '<STR_LIT>' , <EOL> spm = '<STR_LIT>' , <EOL> testing = '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> fake = '<STR_LIT>' , <EOL> ) <EOL> if archive_name is not None : <EOL> archive_names . update ( archive_name ) <EOL> folder_names = dict ( <EOL> brainstorm = '<STR_LIT>' , <EOL> fake = '<STR_LIT:foo>' , <EOL> misc = '<STR_LIT>' , <EOL> sample = '<STR_LIT>' , <EOL> somato = '<STR_LIT>' , <EOL> spm = '<STR_LIT>' , <EOL> testing = '<STR_LIT>' , <EOL> ) <EOL> urls = dict ( <EOL> brainstorm = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> fake = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> misc = '<STR_LIT>' <EOL> '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> sample = "<STR_LIT>" , <EOL> somato = '<STR_LIT>' , <EOL> spm = '<STR_LIT>' , <EOL> testing = '<STR_LIT>' <EOL> '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> ) <EOL> hashes = dict ( <EOL> brainstorm = None , <EOL> fake = '<STR_LIT>' , <EOL> misc = '<STR_LIT>' , <EOL> sample = '<STR_LIT>' , <EOL> somato = '<STR_LIT>' , <EOL> spm = '<STR_LIT>' , <EOL> testing = '<STR_LIT>' , <EOL> ) <EOL> folder_origs = dict ( <EOL> misc = '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> testing = '<STR_LIT>' % releases [ '<STR_LIT>' ] , <EOL> ) <EOL> folder_name = folder_names [ name ] <EOL> archive_name = archive_names [ name ] <EOL> hash_ = hashes [ name ] <EOL> url = urls [ name ] <EOL> folder_orig = folder_origs . get ( name , None ) <EOL> if '<STR_LIT:%s>' in url : <EOL> url = url % archive_name <EOL> folder_path = op . join ( path , folder_name ) <EOL> if name == '<STR_LIT>' : <EOL> extract_path = folder_path <EOL> folder_path = op . join ( folder_path , archive_names [ name ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) <EOL> rm_archive = False <EOL> martinos_path = '<STR_LIT>' + archive_name <EOL> neurospin_path = '<STR_LIT>' + archive_name <EOL> if not op . exists ( folder_path ) and not download : <EOL> return '<STR_LIT>' <EOL> if not op . exists ( folder_path ) or force_update : <EOL> if name == '<STR_LIT>' : <EOL> if '<STR_LIT>' in sys . argv : <EOL> answer = '<STR_LIT:y>' <EOL> else : <EOL> answer = input ( '<STR_LIT>' % _bst_license_text ) <EOL> if answer . lower ( ) != '<STR_LIT:y>' : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( archive_name , path ) ) <EOL> if op . exists ( martinos_path ) : <EOL> archive_name = martinos_path <EOL> elif op . exists ( neurospin_path ) : <EOL> archive_name = neurospin_path <EOL> else : <EOL> archive_name = op . join ( path , archive_name ) <EOL> rm_archive = True <EOL> fetch_archive = True <EOL> if op . exists ( archive_name ) : <EOL> msg = ( '<STR_LIT>' ) <EOL> answer = input ( msg ) <EOL> if answer . lower ( ) == '<STR_LIT:y>' : <EOL> os . remove ( archive_name ) <EOL> else : <EOL> fetch_archive = False <EOL> if fetch_archive : <EOL> _fetch_file ( url , archive_name , print_destination = False , <EOL> hash_ = hash_ ) <EOL> if op . exists ( folder_path ) : <EOL> def onerror ( func , path , exc_info ) : <EOL> """<STR_LIT>""" <EOL> do = False <EOL> if not os . access ( path , os . W_OK ) : <EOL> perm = os . stat ( path ) . st_mode | stat . S_IWUSR <EOL> os . chmod ( path , perm ) <EOL> do = True <EOL> if not os . access ( op . dirname ( path ) , os . W_OK ) : <EOL> dir_perm = ( os . stat ( op . dirname ( path ) ) . st_mode | <EOL> stat . S_IWUSR ) <EOL> os . chmod ( op . dirname ( path ) , dir_perm ) <EOL> do = True <EOL> if do : <EOL> func ( path ) <EOL> else : <EOL> raise <EOL> shutil . rmtree ( folder_path , onerror = onerror ) <EOL> logger . info ( '<STR_LIT>' % archive_name ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> for ext in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> if name != '<STR_LIT>' : <EOL> extract_path = path <EOL> tf = tarfile . open ( archive_name , '<STR_LIT>' % ext ) <EOL> tf . extractall ( path = extract_path ) <EOL> tf . close ( ) <EOL> break <EOL> except tarfile . ReadError as err : <EOL> logger . info ( '<STR_LIT>' % ( archive_name , err ) ) <EOL> if folder_orig is not None : <EOL> shutil . move ( op . join ( path , folder_orig ) , folder_path ) <EOL> if rm_archive : <EOL> os . remove ( archive_name ) <EOL> path = _do_path_update ( path , update_path , key , name ) <EOL> path = op . join ( path , folder_name ) <EOL> data_version = _dataset_version ( path , name ) <EOL> try : <EOL> from distutils . version import LooseVersion as LV <EOL> except : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % name ) <EOL> else : <EOL> if check_version and LV ( data_version ) < LV ( mne_version . strip ( '<STR_LIT>' ) ) : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> name = name , current = data_version , newest = mne_version ) ) <EOL> return ( path , data_version ) if return_version else path <EOL> def _get_version ( name ) : <EOL> """<STR_LIT>""" <EOL> if not has_dataset ( name ) : <EOL> return None <EOL> return _data_path ( name = name , return_version = True ) [ <NUM_LIT:1> ] <EOL> def has_dataset ( name ) : <EOL> """<STR_LIT>""" <EOL> endswith = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } [ name ] <EOL> archive_name = None <EOL> if name == '<STR_LIT>' : <EOL> archive_name = dict ( brainstorm = '<STR_LIT>' ) <EOL> dp = _data_path ( download = False , name = name , check_version = False , <EOL> archive_name = archive_name ) <EOL> return dp . endswith ( endswith ) </s>
