<s> from biicode . common . output_stream import Color <EOL> import urllib <EOL> import os <EOL> import fnmatch <EOL> from collections import defaultdict , namedtuple <EOL> from biicode . client . checkout . snapshotbuilder import compute_deps_files <EOL> from biicode . client . setups . setup_tools import decompress <EOL> def _download ( out ) : <EOL> def internal_download ( url , filename ) : <EOL> def dl_progress_callback_cmd ( count , block_size , total_size ) : <EOL> if count % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> if count % <NUM_LIT> == <NUM_LIT:0> : <EOL> out . write ( '<STR_LIT>' % ( <NUM_LIT:100> * min ( count * block_size , total_size ) / total_size ) ) <EOL> else : <EOL> out . write ( '<STR_LIT:.>' ) <EOL> if not os . path . exists ( filename ) : <EOL> out . writeln ( '<STR_LIT>' % filename , Color . CYAN ) <EOL> out . writeln ( '<STR_LIT>' % url , Color . CYAN ) <EOL> urllib . urlretrieve ( url , filename , reporthook = dl_progress_callback_cmd ) <EOL> return internal_download <EOL> BiiHookPaths = namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class BiiHook ( object ) : <EOL> def __init__ ( self , name , block_name , is_dep , filenames , unresolved , hive_disk_image , out , <EOL> env_folder ) : <EOL> self . paths = BiiHookPaths ( blocks = hive_disk_image . paths . blocks , <EOL> cmake = hive_disk_image . paths . cmake , <EOL> build = hive_disk_image . paths . build , <EOL> bin = hive_disk_image . paths . bin , <EOL> deps = hive_disk_image . paths . deps , <EOL> lib = hive_disk_image . paths . lib ) <EOL> self . name = name <EOL> self . project_folder = hive_disk_image . paths . project_root <EOL> self . block = block_name <EOL> self . is_dependency = is_dep <EOL> if is_dep : <EOL> self . block_folder = os . path . join ( hive_disk_image . paths . deps , block_name ) <EOL> else : <EOL> self . block_folder = os . path . join ( hive_disk_image . paths . blocks , block_name ) <EOL> self . settings = hive_disk_image . settings <EOL> self . out = out <EOL> self . download = _download ( out ) <EOL> self . decompress = decompress <EOL> self . files = filenames <EOL> self . unresolved = unresolved <EOL> self . environment_folder = env_folder <EOL> cmake_file_path = hive_disk_image . paths . cmake_path_file <EOL> if os . path . exists ( cmake_file_path ) : <EOL> with open ( cmake_file_path , "<STR_LIT:r>" ) as f : <EOL> cmake_path = f . read ( ) . strip ( ) <EOL> os . environ [ "<STR_LIT>" ] += os . pathsep + cmake_path <EOL> def _paths_represent ( self ) : <EOL> return '<STR_LIT:\n>' . join ( [ "<STR_LIT>" % self . paths . blocks , <EOL> "<STR_LIT>" % self . paths . cmake , <EOL> "<STR_LIT>" % self . paths . build , <EOL> "<STR_LIT>" % self . paths . bin , <EOL> "<STR_LIT>" % self . paths . deps , <EOL> "<STR_LIT>" % self . paths . lib ] ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:\n>' . join ( [ "<STR_LIT>" % self . project_folder , <EOL> "<STR_LIT>" % self . block , <EOL> "<STR_LIT>" % self . is_dependency , <EOL> "<STR_LIT>" % self . block_folder , <EOL> "<STR_LIT>" % self . environment_folder , <EOL> "<STR_LIT>" % self . _paths_represent ( ) , <EOL> "<STR_LIT>" % self . settings , <EOL> "<STR_LIT>" % self . files , <EOL> "<STR_LIT>" % self . unresolved ] ) <EOL> def execute ( self , code ) : <EOL> aux = { '<STR_LIT>' : self } <EOL> try : <EOL> exec code in aux <EOL> except Exception as e : <EOL> self . out . error ( "<STR_LIT>" % self . name ) <EOL> self . out . error ( "<STR_LIT:%s>" % e ) <EOL> def handle_hooks ( stage , hive_holder , closure , bii ) : <EOL> """<STR_LIT>""" <EOL> hook_pattern = "<STR_LIT>" % stage <EOL> levels = hive_holder . hive_dependencies . version_graph . get_levels ( ) <EOL> list_blocks = [ bv . block_name for level in levels for bv in level ] <EOL> _process_deps_hooks ( closure , bii , hook_pattern , list_blocks ) <EOL> _process_blocks_hooks ( hive_holder , bii , hook_pattern , list_blocks ) <EOL> def _process_blocks_hooks ( hive_holder , bii , hook_pattern , list_blocks ) : <EOL> for block_name in list_blocks : <EOL> try : <EOL> block_holder = hive_holder [ block_name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> file_names = block_holder . cell_names <EOL> for resource in block_holder . simple_resources : <EOL> name = resource . name <EOL> if fnmatch . fnmatch ( name . cell_name , hook_pattern ) : <EOL> code = resource . content . load . load <EOL> unresolved = block_holder . unresolved ( ) <EOL> hook = BiiHook ( name , block_holder . block_name , False , file_names , unresolved , <EOL> bii . hive_disk_image , bii . user_io . out , <EOL> bii . bii_paths . user_bii_home ) <EOL> hook . execute ( code ) <EOL> def _process_deps_hooks ( closure , bii , hook_pattern , list_blocks ) : <EOL> if closure is not None : <EOL> files = compute_deps_files ( closure ) <EOL> block_files = defaultdict ( list ) <EOL> for bcn in files : <EOL> block_files [ bcn . block_name ] . append ( bcn . cell_name ) <EOL> for block_name in list_blocks : <EOL> try : <EOL> bfiles = block_files [ block_name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> for name in bfiles : <EOL> if fnmatch . fnmatch ( name , hook_pattern ) : <EOL> code = files [ block_name + name ] <EOL> unresolved = [ ] <EOL> hook = BiiHook ( name , block_name , True , bfiles , unresolved , <EOL> bii . hive_disk_image , bii . user_io . out , <EOL> bii . bii_paths . user_bii_home ) <EOL> hook . execute ( code ) </s>
