<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import itertools <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> from twitter . common . collections import OrderedSet <EOL> from pants . backend . codegen . targets . python_thrift_library import PythonThriftLibrary <EOL> from pants . backend . python . code_generator import CodeGenerator <EOL> from pants . base . build_environment import get_buildroot <EOL> from pants . util . dirutil import safe_walk <EOL> from pants . util . memo import memoized_property <EOL> class PythonThriftBuilder ( CodeGenerator ) : <EOL> """<STR_LIT>""" <EOL> class UnknownPlatformException ( CodeGenerator . Error ) : <EOL> def __init__ ( self , platform ) : <EOL> super ( PythonThriftBuilder . UnknownPlatformException , self ) . __init__ ( <EOL> '<STR_LIT>' . format ( str ( platform ) ) ) <EOL> def __init__ ( self , thrift_binary_factory , workdir , target , root_dir , target_suffix = None ) : <EOL> super ( PythonThriftBuilder , self ) . __init__ ( workdir , target , root_dir , <EOL> target_suffix = target_suffix ) <EOL> self . _thrift_binary_factory = thrift_binary_factory <EOL> @ property <EOL> def install_requires ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ memoized_property <EOL> def _thrift_binary ( self ) : <EOL> return self . _thrift_binary_factory ( ) . path <EOL> def run_thrifts ( self ) : <EOL> """<STR_LIT>""" <EOL> bases = OrderedSet ( ) <EOL> def collect_bases ( target ) : <EOL> if isinstance ( target , PythonThriftLibrary ) : <EOL> bases . add ( os . path . join ( get_buildroot ( ) , target . target_base ) ) <EOL> self . target . walk ( collect_bases ) <EOL> for source in self . target . payload . sources . relative_to_buildroot ( ) : <EOL> if not self . _run_thrift ( bases , os . path . join ( get_buildroot ( ) , source ) ) : <EOL> raise PythonThriftBuilder . CodeGenerationException ( <EOL> "<STR_LIT>" . format ( source ) ) <EOL> def _run_thrift ( self , bases , source ) : <EOL> include_paths = list ( itertools . chain . from_iterable ( ( '<STR_LIT>' , base ) for base in bases ) ) <EOL> args = [ <EOL> self . _thrift_binary , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . codegen_root ] + include_paths + [ source ] <EOL> po = subprocess . Popen ( args , cwd = self . chroot . path ( ) ) <EOL> rv = po . wait ( ) <EOL> if rv != <NUM_LIT:0> : <EOL> comm = po . communicate ( ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> print ( comm [ <NUM_LIT:0> ] , file = sys . stderr ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> print ( comm [ <NUM_LIT:1> ] , file = sys . stderr ) <EOL> return rv == <NUM_LIT:0> <EOL> @ property <EOL> def package_dir ( self ) : <EOL> return "<STR_LIT>" <EOL> def generate ( self ) : <EOL> self . run_thrifts ( ) <EOL> for root , _ , files in safe_walk ( os . path . normpath ( self . package_root ) ) : <EOL> reldir = os . path . relpath ( root , self . package_root ) <EOL> if reldir == '<STR_LIT:.>' : <EOL> continue <EOL> if '<STR_LIT>' not in files : <EOL> continue <EOL> init_py_abspath = os . path . join ( root , '<STR_LIT>' ) <EOL> module_path = self . path_to_module ( reldir ) <EOL> self . created_packages . add ( module_path ) <EOL> if os . path . getsize ( init_py_abspath ) == <NUM_LIT:0> : <EOL> with open ( init_py_abspath , '<STR_LIT:wb>' ) as f : <EOL> f . write ( b"<STR_LIT>" ) <EOL> self . created_namespace_packages . add ( module_path ) <EOL> else : <EOL> pass <EOL> if not self . created_packages : <EOL> raise self . CodeGenerationException ( '<STR_LIT>' . format ( self . target ) ) </s>
