<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from itertools import chain <EOL> from pants . base . build_environment import get_buildroot <EOL> from pants . util . dirutil import safe_mkdir <EOL> from pants . contrib . go . targets . go_target import GoTarget <EOL> from pants . contrib . go . tasks . go_task import GoTask <EOL> class GoWorkspaceTask ( GoTask ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def prepare ( cls , options , round_manager ) : <EOL> super ( GoWorkspaceTask , cls ) . prepare ( options , round_manager ) <EOL> round_manager . require_data ( '<STR_LIT>' ) <EOL> def get_gopath ( self , target ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . workdir , target . id ) <EOL> def ensure_workspace ( self , target ) : <EOL> """<STR_LIT>""" <EOL> gopath = self . get_gopath ( target ) <EOL> for d in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:src>' ) : <EOL> safe_mkdir ( os . path . join ( gopath , d ) ) <EOL> required_links = set ( ) <EOL> for dep in target . closure ( ) : <EOL> if not isinstance ( dep , GoTarget ) : <EOL> continue <EOL> if self . is_remote_lib ( dep ) : <EOL> self . _symlink_remote_lib ( gopath , dep , required_links ) <EOL> else : <EOL> self . _symlink_local_src ( gopath , dep , required_links ) <EOL> self . remove_unused_links ( os . path . join ( gopath , '<STR_LIT:src>' ) , required_links ) <EOL> @ staticmethod <EOL> def remove_unused_links ( dirpath , required_links ) : <EOL> """<STR_LIT>""" <EOL> for root , dirs , files in os . walk ( dirpath ) : <EOL> for p in chain ( dirs , files ) : <EOL> p = os . path . join ( root , p ) <EOL> if os . path . islink ( p ) and p not in required_links : <EOL> os . unlink ( p ) <EOL> def _symlink_local_src ( self , gopath , go_local_src , required_links ) : <EOL> """<STR_LIT>""" <EOL> source_iter = ( os . path . join ( get_buildroot ( ) , src ) <EOL> for src in go_local_src . sources_relative_to_buildroot ( ) ) <EOL> return self . _symlink_lib ( gopath , go_local_src , source_iter , required_links ) <EOL> def _symlink_remote_lib ( self , gopath , go_remote_lib , required_links ) : <EOL> """<STR_LIT>""" <EOL> def source_iter ( ) : <EOL> remote_lib_source_dir = self . context . products . get_data ( '<STR_LIT>' ) [ go_remote_lib ] <EOL> for path in os . listdir ( remote_lib_source_dir ) : <EOL> remote_src = os . path . join ( remote_lib_source_dir , path ) <EOL> if os . path . isfile ( remote_src ) : <EOL> yield remote_src <EOL> return self . _symlink_lib ( gopath , go_remote_lib , source_iter ( ) , required_links ) <EOL> def _symlink_lib ( self , gopath , lib , source_iter , required_links ) : <EOL> src_dir = os . path . join ( gopath , '<STR_LIT:src>' , lib . import_path ) <EOL> safe_mkdir ( src_dir ) <EOL> for path in source_iter : <EOL> src_link = os . path . join ( src_dir , os . path . basename ( path ) ) <EOL> if not os . path . islink ( src_link ) : <EOL> os . symlink ( path , src_link ) <EOL> required_links . add ( src_link ) </s>
