<s> """<STR_LIT>""" <EOL> from errno import ENOENT <EOL> from stat import S_IFDIR , S_IFREG , S_IFLNK <EOL> from sys import argv , exit <EOL> import re <EOL> import time <EOL> import os <EOL> import argparse <EOL> from fuse import FUSE , FuseOSError , Operations , LoggingMixIn , fuse_get_context <EOL> from bioblend import galaxy <EOL> CACHE_TIME = <NUM_LIT:3> <EOL> def path_type ( path ) : <EOL> parts = filter ( lambda x : len ( x ) > <NUM_LIT:0> , path . split ( '<STR_LIT:/>' ) ) <EOL> if path == '<STR_LIT:/>' : <EOL> return ( '<STR_LIT:root>' , dict ( ) ) <EOL> elif path == '<STR_LIT>' : <EOL> return ( '<STR_LIT>' , dict ( ) ) <EOL> elif len ( parts ) == <NUM_LIT:2> and parts [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return ( '<STR_LIT>' , dict ( h_name = unesc_filename ( parts [ <NUM_LIT:1> ] ) ) ) <EOL> elif len ( parts ) == <NUM_LIT:3> and parts [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return ( '<STR_LIT:data>' , dict ( h_name = unesc_filename ( parts [ <NUM_LIT:1> ] ) , ds_name = unesc_filename ( parts [ <NUM_LIT:2> ] ) ) ) <EOL> print "<STR_LIT>" % path <EOL> return ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def esc_filename ( fname ) : <EOL> def esc ( m ) : <EOL> c = m . group ( <NUM_LIT:0> ) <EOL> if c == '<STR_LIT:%>' : <EOL> return '<STR_LIT>' <EOL> elif c == '<STR_LIT:/>' : <EOL> return '<STR_LIT>' <EOL> return re . sub ( r'<STR_LIT>' , esc , fname ) <EOL> def unesc_filename ( fname ) : <EOL> def unesc ( m ) : <EOL> str = m . group ( <NUM_LIT:0> ) <EOL> if str == '<STR_LIT>' : <EOL> return '<STR_LIT:%>' <EOL> elif str == '<STR_LIT>' : <EOL> return '<STR_LIT:/>' <EOL> return re . sub ( r'<STR_LIT>' , unesc , fname ) <EOL> def parse_name_with_id ( fname ) : <EOL> m = re . match ( r"<STR_LIT>" , fname ) <EOL> if m is not None : <EOL> return ( m . group ( '<STR_LIT:name>' ) , m . group ( '<STR_LIT:id>' ) ) <EOL> else : <EOL> return ( fname , '<STR_LIT>' ) <EOL> class Context ( LoggingMixIn , Operations ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , api_key ) : <EOL> self . gi = galaxy . GalaxyInstance ( url = '<STR_LIT>' , key = api_key ) <EOL> self . datasets_cache = { } <EOL> self . histories_cache = { '<STR_LIT:time>' : None , '<STR_LIT>' : None } <EOL> def getattr ( self , path , fh = None ) : <EOL> ( typ , kw ) = path_type ( path ) <EOL> now = time . time ( ) <EOL> if typ == '<STR_LIT:root>' or typ == '<STR_LIT>' : <EOL> st = dict ( st_mode = ( S_IFDIR | <NUM_LIT:0> <NUM_LIT> ) , st_nlink = <NUM_LIT:2> ) <EOL> st [ '<STR_LIT>' ] = st [ '<STR_LIT>' ] = st [ '<STR_LIT>' ] = now <EOL> elif typ == '<STR_LIT>' : <EOL> st = dict ( st_mode = ( S_IFDIR | <NUM_LIT:0> <NUM_LIT> ) , st_nlink = <NUM_LIT:2> ) <EOL> st [ '<STR_LIT>' ] = st [ '<STR_LIT>' ] = st [ '<STR_LIT>' ] = now <EOL> elif typ == '<STR_LIT:data>' : <EOL> d = self . _dataset ( kw ) <EOL> t = time . mktime ( time . strptime ( d [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> fname = esc_filename ( d . get ( '<STR_LIT>' , d [ '<STR_LIT>' ] ) ) <EOL> st = dict ( st_mode = ( S_IFLNK | <NUM_LIT:0> <NUM_LIT> ) , st_nlink = <NUM_LIT:1> , <EOL> st_size = len ( fname ) , st_ctime = t , st_mtime = t , <EOL> st_atime = t ) <EOL> else : <EOL> raise FuseOSError ( ENOENT ) <EOL> return st <EOL> def readlink ( self , path ) : <EOL> ( typ , kw ) = path_type ( path ) <EOL> if typ == '<STR_LIT:data>' : <EOL> d = self . _dataset ( kw ) <EOL> return d . get ( '<STR_LIT>' , d [ '<STR_LIT>' ] ) <EOL> raise FuseOSError ( ENOENT ) <EOL> def read ( self , path , size , offset , fh ) : <EOL> raise RuntimeError ( '<STR_LIT>' % path ) <EOL> def _histories ( self ) : <EOL> cache = self . histories_cache <EOL> now = time . time ( ) <EOL> if cache [ '<STR_LIT>' ] is None or now - cache [ '<STR_LIT:time>' ] > CACHE_TIME : <EOL> cache [ '<STR_LIT:time>' ] = now <EOL> cache [ '<STR_LIT>' ] = self . gi . histories . get_histories ( ) <EOL> return cache [ '<STR_LIT>' ] <EOL> def _history ( self , h_name ) : <EOL> ( fixed_name , hist_id ) = parse_name_with_id ( h_name ) <EOL> h = filter ( lambda x : x [ '<STR_LIT:name>' ] == fixed_name , self . _histories ( ) ) <EOL> if len ( h ) == <NUM_LIT:0> : <EOL> raise FuseOSError ( ENOENT ) <EOL> if len ( h ) > <NUM_LIT:1> : <EOL> h = filter ( lambda x : x [ '<STR_LIT:id>' ] == hist_id , self . _histories ( ) ) <EOL> if len ( h ) == <NUM_LIT:0> : <EOL> raise FuseOSError ( ENOENT ) <EOL> if len ( h ) > <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> return h [ <NUM_LIT:0> ] <EOL> return h [ <NUM_LIT:0> ] <EOL> def _datasets ( self , h ) : <EOL> id = h [ '<STR_LIT:id>' ] <EOL> cache = self . datasets_cache <EOL> now = time . time ( ) <EOL> if id not in cache or now - cache [ id ] [ '<STR_LIT:time>' ] > CACHE_TIME : <EOL> cache [ id ] = { '<STR_LIT:time>' : now , <EOL> '<STR_LIT>' : self . gi . histories . show_history ( id , contents = True , details = '<STR_LIT:all>' ) } <EOL> return cache [ id ] [ '<STR_LIT>' ] <EOL> def _dataset ( self , kw ) : <EOL> h = self . _history ( kw [ '<STR_LIT>' ] ) <EOL> ds = self . _datasets ( h ) <EOL> ( d_name , d_id ) = parse_name_with_id ( kw [ '<STR_LIT>' ] ) <EOL> d = filter ( lambda x : x [ '<STR_LIT:name>' ] == d_name , ds ) <EOL> if len ( d ) == <NUM_LIT:0> : <EOL> raise FuseOSError ( ENOENT ) <EOL> if len ( d ) > <NUM_LIT:1> : <EOL> d = filter ( lambda x : x [ '<STR_LIT:name>' ] == d_name and x [ '<STR_LIT:id>' ] == d_id , ds ) <EOL> if len ( d ) == <NUM_LIT:0> : <EOL> raise FuseOSError ( ENOENT ) <EOL> if len ( d ) > <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> return d [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' not in d [ <NUM_LIT:0> ] and '<STR_LIT>' not in d [ <NUM_LIT:0> ] : <EOL> print "<STR_LIT>" <EOL> raise FuseOSError ( ENOENT ) <EOL> return d [ <NUM_LIT:0> ] <EOL> def readdir ( self , path , fh ) : <EOL> ( typ , kw ) = path_type ( path ) <EOL> if typ == '<STR_LIT:root>' : <EOL> return [ '<STR_LIT:.>' , '<STR_LIT:..>' , '<STR_LIT>' ] <EOL> elif typ == '<STR_LIT>' : <EOL> hl = self . _histories ( ) <EOL> hist_count = { } <EOL> for h in hl : <EOL> try : <EOL> hist_count [ h [ '<STR_LIT:name>' ] ] += <NUM_LIT:1> <EOL> except : <EOL> hist_count [ h [ '<STR_LIT:name>' ] ] = <NUM_LIT:1> <EOL> results = [ '<STR_LIT:.>' , '<STR_LIT:..>' ] <EOL> for h in hl : <EOL> if h [ '<STR_LIT:name>' ] in hist_count and hist_count [ h [ '<STR_LIT:name>' ] ] > <NUM_LIT:1> : <EOL> results . append ( esc_filename ( h [ '<STR_LIT:name>' ] + '<STR_LIT:->' + h [ '<STR_LIT:id>' ] ) ) <EOL> else : <EOL> results . append ( esc_filename ( h [ '<STR_LIT:name>' ] ) ) <EOL> return results <EOL> elif typ == '<STR_LIT>' : <EOL> h = self . _history ( kw [ '<STR_LIT>' ] ) <EOL> ds = self . _datasets ( h ) <EOL> d_count = { } <EOL> for d in ds : <EOL> try : <EOL> d_count [ d [ '<STR_LIT:name>' ] ] += <NUM_LIT:1> <EOL> except : <EOL> d_count [ d [ '<STR_LIT:name>' ] ] = <NUM_LIT:1> <EOL> results = [ '<STR_LIT:.>' , '<STR_LIT:..>' ] <EOL> for d in ds : <EOL> if d [ '<STR_LIT:name>' ] in d_count and d_count [ d [ '<STR_LIT:name>' ] ] > <NUM_LIT:1> : <EOL> results . append ( esc_filename ( d [ '<STR_LIT:name>' ] + '<STR_LIT:->' + d [ '<STR_LIT:id>' ] ) ) <EOL> else : <EOL> results . append ( esc_filename ( d [ '<STR_LIT:name>' ] ) ) <EOL> return results <EOL> access = None <EOL> flush = None <EOL> getxattr = None <EOL> listxattr = None <EOL> open = None <EOL> opendir = None <EOL> release = None <EOL> releasedir = None <EOL> statfs = None <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if not os . path . exists ( args . mountpoint ) : <EOL> os . makedirs ( args . mountpoint ) <EOL> fuse = FUSE ( Context ( args . apikey ) , <EOL> args . mountpoint , <EOL> foreground = True , <EOL> ro = True ) </s>
