<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import errno <EOL> import fnmatch <EOL> import glob <EOL> import hashlib <EOL> import logging <EOL> import os <EOL> import shutil <EOL> from datetime import datetime <EOL> from salt . exceptions import FileserverConfigError <EOL> VALID_BRANCH_METHODS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> PER_REMOTE_OVERRIDES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:root>' ) <EOL> import salt . ext . six as six <EOL> try : <EOL> import hglib <EOL> HAS_HG = True <EOL> except ImportError : <EOL> HAS_HG = False <EOL> import salt . utils <EOL> import salt . utils . url <EOL> import salt . fileserver <EOL> from salt . utils . event import tagify <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if __virtualname__ not in __opts__ [ '<STR_LIT>' ] : <EOL> return False <EOL> if not HAS_HG : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return False <EOL> if __opts__ [ '<STR_LIT>' ] not in VALID_BRANCH_METHODS : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> . format ( __opts__ [ '<STR_LIT>' ] , VALID_BRANCH_METHODS ) <EOL> ) <EOL> return False <EOL> return __virtualname__ <EOL> def _all_branches ( repo ) : <EOL> '''<STR_LIT>''' <EOL> return repo . branches ( ) <EOL> def _get_branch ( repo , name ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return [ x for x in _all_branches ( repo ) if x [ <NUM_LIT:0> ] == name ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return False <EOL> def _all_bookmarks ( repo ) : <EOL> '''<STR_LIT>''' <EOL> return repo . bookmarks ( ) [ <NUM_LIT:0> ] <EOL> def _get_bookmark ( repo , name ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return [ x for x in _all_bookmarks ( repo ) if x [ <NUM_LIT:0> ] == name ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return False <EOL> def _all_tags ( repo ) : <EOL> '''<STR_LIT>''' <EOL> return [ x for x in repo . tags ( ) if x [ <NUM_LIT:0> ] != '<STR_LIT>' ] <EOL> def _get_tag ( repo , name ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return [ x for x in _all_tags ( repo ) if x [ <NUM_LIT:0> ] == name ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return False <EOL> def _get_ref ( repo , name ) : <EOL> '''<STR_LIT>''' <EOL> if name == '<STR_LIT>' : <EOL> name = repo [ '<STR_LIT>' ] <EOL> if name == repo [ '<STR_LIT>' ] or name in envs ( ) : <EOL> if repo [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return _get_branch ( repo [ '<STR_LIT>' ] , name ) or _get_tag ( repo [ '<STR_LIT>' ] , name ) <EOL> elif repo [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return _get_bookmark ( repo [ '<STR_LIT>' ] , name ) or _get_tag ( repo [ '<STR_LIT>' ] , name ) <EOL> elif repo [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return _get_branch ( repo [ '<STR_LIT>' ] , name ) or _get_bookmark ( repo [ '<STR_LIT>' ] , name ) or _get_tag ( repo [ '<STR_LIT>' ] , name ) <EOL> return False <EOL> def _failhard ( ) : <EOL> '''<STR_LIT>''' <EOL> raise FileserverConfigError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> def init ( ) : <EOL> '''<STR_LIT>''' <EOL> bp_ = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> new_remote = False <EOL> repos = [ ] <EOL> per_remote_defaults = { } <EOL> for param in PER_REMOTE_OVERRIDES : <EOL> per_remote_defaults [ param ] = six . text_type ( __opts__ [ '<STR_LIT>' . format ( param ) ] ) <EOL> for remote in __opts__ [ '<STR_LIT>' ] : <EOL> repo_conf = copy . deepcopy ( per_remote_defaults ) <EOL> if isinstance ( remote , dict ) : <EOL> repo_url = next ( iter ( remote ) ) <EOL> per_remote_conf = dict ( <EOL> [ ( key , six . text_type ( val ) ) for key , val in <EOL> six . iteritems ( salt . utils . repack_dictlist ( remote [ repo_url ] ) ) ] <EOL> ) <EOL> if not per_remote_conf : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( repo_url ) <EOL> ) <EOL> _failhard ( ) <EOL> branch_method = per_remote_conf . get ( '<STR_LIT>' , <EOL> per_remote_defaults [ '<STR_LIT>' ] ) <EOL> if branch_method not in VALID_BRANCH_METHODS : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( branch_method , repo_url , <EOL> '<STR_LIT:U+002CU+0020>' . join ( VALID_BRANCH_METHODS ) ) <EOL> ) <EOL> _failhard ( ) <EOL> per_remote_errors = False <EOL> for param in ( x for x in per_remote_conf <EOL> if x not in PER_REMOTE_OVERRIDES ) : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> param , repo_url , '<STR_LIT:U+002CU+0020>' . join ( PER_REMOTE_OVERRIDES ) <EOL> ) <EOL> ) <EOL> per_remote_errors = True <EOL> if per_remote_errors : <EOL> _failhard ( ) <EOL> repo_conf . update ( per_remote_conf ) <EOL> else : <EOL> repo_url = remote <EOL> if not isinstance ( repo_url , six . string_types ) : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repo_url ) <EOL> ) <EOL> _failhard ( ) <EOL> try : <EOL> repo_conf [ '<STR_LIT>' ] = salt . utils . url . strip_proto ( <EOL> repo_conf [ '<STR_LIT>' ] <EOL> ) <EOL> except TypeError : <EOL> pass <EOL> hash_type = getattr ( hashlib , __opts__ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> repo_hash = hash_type ( repo_url ) . hexdigest ( ) <EOL> rp_ = os . path . join ( bp_ , repo_hash ) <EOL> if not os . path . isdir ( rp_ ) : <EOL> os . makedirs ( rp_ ) <EOL> if not os . listdir ( rp_ ) : <EOL> hglib . init ( rp_ ) <EOL> new_remote = True <EOL> try : <EOL> repo = hglib . open ( rp_ ) <EOL> except hglib . error . ServerError : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( rp_ , repo_url ) <EOL> ) <EOL> _failhard ( ) <EOL> except Exception as exc : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( exc , repo_url ) <EOL> ) <EOL> _failhard ( ) <EOL> try : <EOL> refs = repo . config ( names = '<STR_LIT>' ) <EOL> except hglib . error . CommandError : <EOL> refs = None <EOL> if not refs : <EOL> hgconfpath = os . path . join ( rp_ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with salt . utils . fopen ( hgconfpath , '<STR_LIT>' ) as hgconfig : <EOL> hgconfig . write ( '<STR_LIT>' ) <EOL> hgconfig . write ( '<STR_LIT>' . format ( repo_url ) ) <EOL> repo_conf . update ( { <EOL> '<STR_LIT>' : repo , <EOL> '<STR_LIT:url>' : repo_url , <EOL> '<STR_LIT>' : repo_hash , <EOL> '<STR_LIT>' : rp_ , <EOL> '<STR_LIT>' : os . path . join ( __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( repo_hash ) ) <EOL> } ) <EOL> repos . append ( repo_conf ) <EOL> repo . close ( ) <EOL> if new_remote : <EOL> remote_map = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> try : <EOL> with salt . utils . fopen ( remote_map , '<STR_LIT>' ) as fp_ : <EOL> timestamp = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> fp_ . write ( '<STR_LIT>' . format ( timestamp ) ) <EOL> for repo in repos : <EOL> fp_ . write ( '<STR_LIT>' . format ( repo [ '<STR_LIT>' ] , repo [ '<STR_LIT:url>' ] ) ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> log . info ( '<STR_LIT>' . format ( remote_map ) ) <EOL> return repos <EOL> def _clear_old_remotes ( ) : <EOL> '''<STR_LIT>''' <EOL> bp_ = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> try : <EOL> cachedir_ls = os . listdir ( bp_ ) <EOL> except OSError : <EOL> cachedir_ls = [ ] <EOL> repos = init ( ) <EOL> for repo in repos : <EOL> try : <EOL> cachedir_ls . remove ( repo [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> pass <EOL> to_remove = [ ] <EOL> for item in cachedir_ls : <EOL> if item in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> path = os . path . join ( bp_ , item ) <EOL> if os . path . isdir ( path ) : <EOL> to_remove . append ( path ) <EOL> failed = [ ] <EOL> if to_remove : <EOL> for rdir in to_remove : <EOL> try : <EOL> shutil . rmtree ( rdir ) <EOL> except OSError as exc : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> . format ( rdir , exc ) <EOL> ) <EOL> failed . append ( rdir ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' . format ( rdir ) ) <EOL> for fdir in failed : <EOL> to_remove . remove ( fdir ) <EOL> return bool ( to_remove ) , repos <EOL> def clear_cache ( ) : <EOL> '''<STR_LIT>''' <EOL> fsb_cachedir = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> list_cachedir = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> errors = [ ] <EOL> for rdir in ( fsb_cachedir , list_cachedir ) : <EOL> if os . path . exists ( rdir ) : <EOL> try : <EOL> shutil . rmtree ( rdir ) <EOL> except OSError as exc : <EOL> errors . append ( '<STR_LIT>' . format ( rdir , exc ) ) <EOL> return errors <EOL> def clear_lock ( remote = None ) : <EOL> '''<STR_LIT>''' <EOL> def _do_clear_lock ( repo ) : <EOL> def _add_error ( errlist , repo , exc ) : <EOL> msg = ( '<STR_LIT>' <EOL> . format ( repo [ '<STR_LIT:url>' ] , repo [ '<STR_LIT>' ] , exc ) ) <EOL> log . debug ( msg ) <EOL> errlist . append ( msg ) <EOL> success = [ ] <EOL> failed = [ ] <EOL> if os . path . exists ( repo [ '<STR_LIT>' ] ) : <EOL> try : <EOL> os . remove ( repo [ '<STR_LIT>' ] ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EISDIR : <EOL> try : <EOL> shutil . rmtree ( repo [ '<STR_LIT>' ] ) <EOL> except OSError as exc : <EOL> _add_error ( failed , repo , exc ) <EOL> else : <EOL> _add_error ( failed , repo , exc ) <EOL> else : <EOL> msg = '<STR_LIT>' . format ( repo [ '<STR_LIT:url>' ] ) <EOL> log . debug ( msg ) <EOL> success . append ( msg ) <EOL> return success , failed <EOL> if isinstance ( remote , dict ) : <EOL> return _do_clear_lock ( remote ) <EOL> cleared = [ ] <EOL> errors = [ ] <EOL> for repo in init ( ) : <EOL> if remote : <EOL> try : <EOL> if not fnmatch . fnmatch ( repo [ '<STR_LIT:url>' ] , remote ) : <EOL> continue <EOL> except TypeError : <EOL> if not fnmatch . fnmatch ( repo [ '<STR_LIT:url>' ] , six . text_type ( remote ) ) : <EOL> continue <EOL> success , failed = _do_clear_lock ( repo ) <EOL> cleared . extend ( success ) <EOL> errors . extend ( failed ) <EOL> return cleared , errors <EOL> def lock ( remote = None ) : <EOL> '''<STR_LIT>''' <EOL> def _do_lock ( repo ) : <EOL> success = [ ] <EOL> failed = [ ] <EOL> if not os . path . exists ( repo [ '<STR_LIT>' ] ) : <EOL> try : <EOL> with salt . utils . fopen ( repo [ '<STR_LIT>' ] , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( '<STR_LIT>' ) <EOL> except ( IOError , OSError ) as exc : <EOL> msg = ( '<STR_LIT>' <EOL> . format ( repo [ '<STR_LIT:url>' ] , repo [ '<STR_LIT>' ] , exc ) ) <EOL> log . debug ( msg ) <EOL> failed . append ( msg ) <EOL> else : <EOL> msg = '<STR_LIT>' . format ( repo [ '<STR_LIT:url>' ] ) <EOL> log . debug ( msg ) <EOL> success . append ( msg ) <EOL> return success , failed <EOL> if isinstance ( remote , dict ) : <EOL> return _do_lock ( remote ) <EOL> locked = [ ] <EOL> errors = [ ] <EOL> for repo in init ( ) : <EOL> if remote : <EOL> try : <EOL> if not fnmatch . fnmatch ( repo [ '<STR_LIT:url>' ] , remote ) : <EOL> continue <EOL> except TypeError : <EOL> if not fnmatch . fnmatch ( repo [ '<STR_LIT:url>' ] , six . text_type ( remote ) ) : <EOL> continue <EOL> success , failed = _do_lock ( repo ) <EOL> locked . extend ( success ) <EOL> errors . extend ( failed ) <EOL> return locked , errors <EOL> def update ( ) : <EOL> '''<STR_LIT>''' <EOL> data = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> data [ '<STR_LIT>' ] , repos = _clear_old_remotes ( ) <EOL> for repo in repos : <EOL> if os . path . exists ( repo [ '<STR_LIT>' ] ) : <EOL> log . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( repo [ '<STR_LIT:url>' ] , repo [ '<STR_LIT>' ] ) <EOL> ) <EOL> continue <EOL> _ , errors = lock ( repo ) <EOL> if errors : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repo [ '<STR_LIT:url>' ] ) ) <EOL> continue <EOL> log . debug ( '<STR_LIT>' . format ( repo [ '<STR_LIT:url>' ] ) ) <EOL> repo [ '<STR_LIT>' ] . open ( ) <EOL> curtip = repo [ '<STR_LIT>' ] . tip ( ) <EOL> try : <EOL> repo [ '<STR_LIT>' ] . pull ( ) <EOL> except Exception as exc : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> . format ( exc , repo [ '<STR_LIT:url>' ] ) , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> else : <EOL> newtip = repo [ '<STR_LIT>' ] . tip ( ) <EOL> if curtip [ <NUM_LIT:1> ] != newtip [ <NUM_LIT:1> ] : <EOL> data [ '<STR_LIT>' ] = True <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> clear_lock ( repo ) <EOL> env_cache = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> if data . get ( '<STR_LIT>' , False ) is True or not os . path . isfile ( env_cache ) : <EOL> env_cachedir = os . path . dirname ( env_cache ) <EOL> if not os . path . exists ( env_cachedir ) : <EOL> os . makedirs ( env_cachedir ) <EOL> new_envs = envs ( ignore_cache = True ) <EOL> serial = salt . payload . Serial ( __opts__ ) <EOL> with salt . utils . fopen ( env_cache , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( serial . dumps ( new_envs ) ) <EOL> log . trace ( '<STR_LIT>' . format ( env_cache ) ) <EOL> if __opts__ . get ( '<STR_LIT>' , False ) : <EOL> event = salt . utils . event . get_event ( <EOL> '<STR_LIT>' , <EOL> __opts__ [ '<STR_LIT>' ] , <EOL> __opts__ [ '<STR_LIT>' ] , <EOL> opts = __opts__ , <EOL> listen = False ) <EOL> event . fire_event ( data , tagify ( [ '<STR_LIT>' , '<STR_LIT>' ] , prefix = '<STR_LIT>' ) ) <EOL> try : <EOL> salt . fileserver . reap_fileserver_cache_dir ( <EOL> os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) , <EOL> find_file <EOL> ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def _env_is_exposed ( env ) : <EOL> '''<STR_LIT>''' <EOL> return salt . utils . check_whitelist_blacklist ( <EOL> env , <EOL> whitelist = __opts__ [ '<STR_LIT>' ] , <EOL> blacklist = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> def envs ( ignore_cache = False ) : <EOL> '''<STR_LIT>''' <EOL> if not ignore_cache : <EOL> env_cache = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> cache_match = salt . fileserver . check_env_cache ( __opts__ , env_cache ) <EOL> if cache_match is not None : <EOL> return cache_match <EOL> ret = set ( ) <EOL> for repo in init ( ) : <EOL> repo [ '<STR_LIT>' ] . open ( ) <EOL> if repo [ '<STR_LIT>' ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for branch in _all_branches ( repo [ '<STR_LIT>' ] ) : <EOL> branch_name = branch [ <NUM_LIT:0> ] <EOL> if branch_name == repo [ '<STR_LIT>' ] : <EOL> branch_name = '<STR_LIT>' <EOL> ret . add ( branch_name ) <EOL> if repo [ '<STR_LIT>' ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for bookmark in _all_bookmarks ( repo [ '<STR_LIT>' ] ) : <EOL> bookmark_name = bookmark [ <NUM_LIT:0> ] <EOL> if bookmark_name == repo [ '<STR_LIT>' ] : <EOL> bookmark_name = '<STR_LIT>' <EOL> ret . add ( bookmark_name ) <EOL> ret . update ( [ x [ <NUM_LIT:0> ] for x in _all_tags ( repo [ '<STR_LIT>' ] ) ] ) <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> return [ x for x in sorted ( ret ) if _env_is_exposed ( x ) ] <EOL> def find_file ( path , tgt_env = '<STR_LIT>' , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> fnd = { '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if os . path . isabs ( path ) or tgt_env not in envs ( ) : <EOL> return fnd <EOL> dest = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' , tgt_env , path ) <EOL> hashes_glob = os . path . join ( __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> tgt_env , <EOL> '<STR_LIT>' . format ( path ) ) <EOL> blobshadest = os . path . join ( __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> tgt_env , <EOL> '<STR_LIT>' . format ( path ) ) <EOL> lk_fn = os . path . join ( __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> tgt_env , <EOL> '<STR_LIT>' . format ( path ) ) <EOL> destdir = os . path . dirname ( dest ) <EOL> hashdir = os . path . dirname ( blobshadest ) <EOL> if not os . path . isdir ( destdir ) : <EOL> try : <EOL> os . makedirs ( destdir ) <EOL> except OSError : <EOL> os . remove ( destdir ) <EOL> os . makedirs ( destdir ) <EOL> if not os . path . isdir ( hashdir ) : <EOL> try : <EOL> os . makedirs ( hashdir ) <EOL> except OSError : <EOL> os . remove ( hashdir ) <EOL> os . makedirs ( hashdir ) <EOL> for repo in init ( ) : <EOL> if repo [ '<STR_LIT>' ] and not path . startswith ( repo [ '<STR_LIT>' ] + os . path . sep ) : <EOL> continue <EOL> repo_path = path [ len ( repo [ '<STR_LIT>' ] ) : ] . lstrip ( os . path . sep ) <EOL> if repo [ '<STR_LIT:root>' ] : <EOL> repo_path = os . path . join ( repo [ '<STR_LIT:root>' ] , repo_path ) <EOL> repo [ '<STR_LIT>' ] . open ( ) <EOL> ref = _get_ref ( repo , tgt_env ) <EOL> if not ref : <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> continue <EOL> salt . fileserver . wait_lock ( lk_fn , dest ) <EOL> if os . path . isfile ( blobshadest ) and os . path . isfile ( dest ) : <EOL> with salt . utils . fopen ( blobshadest , '<STR_LIT:r>' ) as fp_ : <EOL> sha = fp_ . read ( ) <EOL> if sha == ref [ <NUM_LIT:2> ] : <EOL> fnd [ '<STR_LIT>' ] = path <EOL> fnd [ '<STR_LIT:path>' ] = dest <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> return fnd <EOL> try : <EOL> repo [ '<STR_LIT>' ] . cat ( <EOL> [ '<STR_LIT>' . format ( repo_path ) ] , rev = ref [ <NUM_LIT:2> ] , output = dest <EOL> ) <EOL> except hglib . error . CommandError : <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> continue <EOL> with salt . utils . fopen ( lk_fn , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( '<STR_LIT>' ) <EOL> for filename in glob . glob ( hashes_glob ) : <EOL> try : <EOL> os . remove ( filename ) <EOL> except Exception : <EOL> pass <EOL> with salt . utils . fopen ( blobshadest , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( ref [ <NUM_LIT:2> ] ) <EOL> try : <EOL> os . remove ( lk_fn ) <EOL> except ( OSError , IOError ) : <EOL> pass <EOL> fnd [ '<STR_LIT>' ] = path <EOL> fnd [ '<STR_LIT:path>' ] = dest <EOL> fnd [ '<STR_LIT>' ] = list ( os . stat ( dest ) ) <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> return fnd <EOL> return fnd <EOL> def serve_file ( load , fnd ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in load : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> load . pop ( '<STR_LIT>' ) <EOL> ret = { '<STR_LIT:data>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if not all ( x in load for x in ( '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> return ret <EOL> if not fnd [ '<STR_LIT:path>' ] : <EOL> return ret <EOL> ret [ '<STR_LIT>' ] = fnd [ '<STR_LIT>' ] <EOL> gzip = load . get ( '<STR_LIT>' , None ) <EOL> with salt . utils . fopen ( fnd [ '<STR_LIT:path>' ] , '<STR_LIT:rb>' ) as fp_ : <EOL> fp_ . seek ( load [ '<STR_LIT>' ] ) <EOL> data = fp_ . read ( __opts__ [ '<STR_LIT>' ] ) <EOL> if gzip and data : <EOL> data = salt . utils . gzip_util . compress ( data , gzip ) <EOL> ret [ '<STR_LIT>' ] = gzip <EOL> ret [ '<STR_LIT:data>' ] = data <EOL> return ret <EOL> def file_hash ( load , fnd ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in load : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> load . pop ( '<STR_LIT>' ) <EOL> if not all ( x in load for x in ( '<STR_LIT:path>' , '<STR_LIT>' ) ) : <EOL> return '<STR_LIT>' <EOL> ret = { '<STR_LIT>' : __opts__ [ '<STR_LIT>' ] } <EOL> relpath = fnd [ '<STR_LIT>' ] <EOL> path = fnd [ '<STR_LIT:path>' ] <EOL> hashdest = os . path . join ( __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> load [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' . format ( relpath , <EOL> __opts__ [ '<STR_LIT>' ] ) ) <EOL> if not os . path . isfile ( hashdest ) : <EOL> ret [ '<STR_LIT>' ] = salt . utils . get_hash ( path , __opts__ [ '<STR_LIT>' ] ) <EOL> with salt . utils . fopen ( hashdest , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( ret [ '<STR_LIT>' ] ) <EOL> return ret <EOL> else : <EOL> with salt . utils . fopen ( hashdest , '<STR_LIT:rb>' ) as fp_ : <EOL> ret [ '<STR_LIT>' ] = fp_ . read ( ) <EOL> return ret <EOL> def _file_lists ( load , form ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in load : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> load . pop ( '<STR_LIT>' ) <EOL> list_cachedir = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> if not os . path . isdir ( list_cachedir ) : <EOL> try : <EOL> os . makedirs ( list_cachedir ) <EOL> except os . error : <EOL> log . critical ( '<STR_LIT>' . format ( list_cachedir ) ) <EOL> return [ ] <EOL> list_cache = os . path . join ( list_cachedir , '<STR_LIT>' . format ( load [ '<STR_LIT>' ] ) ) <EOL> w_lock = os . path . join ( list_cachedir , '<STR_LIT>' . format ( load [ '<STR_LIT>' ] ) ) <EOL> cache_match , refresh_cache , save_cache = salt . fileserver . check_file_list_cache ( <EOL> __opts__ , form , list_cache , w_lock <EOL> ) <EOL> if cache_match is not None : <EOL> return cache_match <EOL> if refresh_cache : <EOL> ret = { } <EOL> ret [ '<STR_LIT>' ] = _get_file_list ( load ) <EOL> ret [ '<STR_LIT>' ] = _get_dir_list ( load ) <EOL> if save_cache : <EOL> salt . fileserver . write_file_list_cache ( <EOL> __opts__ , ret , list_cache , w_lock <EOL> ) <EOL> return ret . get ( form , [ ] ) <EOL> return [ ] <EOL> def file_list ( load ) : <EOL> '''<STR_LIT>''' <EOL> return _file_lists ( load , '<STR_LIT>' ) <EOL> def _get_file_list ( load ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in load : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> load . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in load or load [ '<STR_LIT>' ] not in envs ( ) : <EOL> return [ ] <EOL> ret = set ( ) <EOL> for repo in init ( ) : <EOL> repo [ '<STR_LIT>' ] . open ( ) <EOL> ref = _get_ref ( repo , load [ '<STR_LIT>' ] ) <EOL> if ref : <EOL> manifest = repo [ '<STR_LIT>' ] . manifest ( rev = ref [ <NUM_LIT:1> ] ) <EOL> for tup in manifest : <EOL> relpath = os . path . relpath ( tup [ <NUM_LIT:4> ] , repo [ '<STR_LIT:root>' ] ) <EOL> if not relpath . startswith ( '<STR_LIT>' ) : <EOL> ret . add ( os . path . join ( repo [ '<STR_LIT>' ] , relpath ) ) <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> return sorted ( ret ) <EOL> def file_list_emptydirs ( load ) : <EOL> '''<STR_LIT>''' <EOL> return [ ] <EOL> def dir_list ( load ) : <EOL> '''<STR_LIT>''' <EOL> return _file_lists ( load , '<STR_LIT>' ) <EOL> def _get_dir_list ( load ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in load : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> load . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in load or load [ '<STR_LIT>' ] not in envs ( ) : <EOL> return [ ] <EOL> ret = set ( ) <EOL> for repo in init ( ) : <EOL> repo [ '<STR_LIT>' ] . open ( ) <EOL> ref = _get_ref ( repo , load [ '<STR_LIT>' ] ) <EOL> if ref : <EOL> manifest = repo [ '<STR_LIT>' ] . manifest ( rev = ref [ <NUM_LIT:1> ] ) <EOL> for tup in manifest : <EOL> filepath = tup [ <NUM_LIT:4> ] <EOL> split = filepath . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> while len ( split ) > <NUM_LIT:1> : <EOL> relpath = os . path . relpath ( split [ <NUM_LIT:0> ] , repo [ '<STR_LIT:root>' ] ) <EOL> if relpath != '<STR_LIT:.>' : <EOL> if not relpath . startswith ( '<STR_LIT>' ) : <EOL> ret . add ( os . path . join ( repo [ '<STR_LIT>' ] , relpath ) ) <EOL> split = split [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> repo [ '<STR_LIT>' ] . close ( ) <EOL> if repo [ '<STR_LIT>' ] : <EOL> ret . add ( repo [ '<STR_LIT>' ] ) <EOL> return sorted ( ret ) </s>
