<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import logging <EOL> import os <EOL> import sys <EOL> from pants . base . build_environment import get_buildroot <EOL> from pants . subsystem . subsystem import Subsystem <EOL> from pants . util . contextutil import open_tar , temporary_file <EOL> from pants . util . dirutil import chmod_plus_x <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ReproError ( Exception ) : <EOL> pass <EOL> class Reproducer ( Subsystem ) : <EOL> options_scope = '<STR_LIT>' <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> register ( '<STR_LIT>' , metavar = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = list , <EOL> help = '<STR_LIT>' ) <EOL> def create_repro ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . get_options ( ) . capture <EOL> if path is None : <EOL> return None <EOL> buildroot = get_buildroot ( ) <EOL> ignore = [ '<STR_LIT>' , os . path . relpath ( self . get_options ( ) . pants_distdir , buildroot ) ] <EOL> if self . get_options ( ) . ignore : <EOL> ignore . extend ( self . get_options ( ) . ignore ) <EOL> return Repro ( path , buildroot , ignore ) <EOL> class Repro ( object ) : <EOL> def __init__ ( self , path , buildroot , ignore ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . expanduser ( path ) <EOL> if os . path . realpath ( path ) . startswith ( buildroot ) : <EOL> raise ReproError ( '<STR_LIT>' ) <EOL> if not path . endswith ( '<STR_LIT>' ) and not path . endswith ( '<STR_LIT>' ) : <EOL> path += '<STR_LIT>' <EOL> if os . path . exists ( path ) : <EOL> raise ReproError ( '<STR_LIT>' . format ( path ) ) <EOL> self . _path = path <EOL> self . _buildroot = buildroot <EOL> self . _ignore = ignore <EOL> def capture ( self , run_info_dict ) : <EOL> logger . info ( '<STR_LIT>' . format ( self . _path ) ) <EOL> with open_tar ( self . _path , '<STR_LIT>' , dereference = True , compresslevel = <NUM_LIT:6> ) as tarout : <EOL> for relpath in os . listdir ( self . _buildroot ) : <EOL> if relpath not in self . _ignore : <EOL> tarout . add ( os . path . join ( self . _buildroot , relpath ) , relpath ) <EOL> with temporary_file ( ) as tmpfile : <EOL> tmpfile . write ( '<STR_LIT>' ) <EOL> for k , v in sorted ( run_info_dict . items ( ) ) : <EOL> tmpfile . write ( '<STR_LIT>' . format ( k , v ) ) <EOL> cmd_line = list ( sys . argv ) <EOL> cmd_line [ <NUM_LIT:0> ] = '<STR_LIT>' <EOL> cmd_line = [ x for x in cmd_line if not x . startswith ( '<STR_LIT>' ) ] <EOL> tmpfile . write ( "<STR_LIT:'>" + "<STR_LIT>" . join ( cmd_line ) + "<STR_LIT>" ) <EOL> tmpfile . flush ( ) <EOL> chmod_plus_x ( tmpfile . name ) <EOL> tarout . add ( tmpfile . name , '<STR_LIT>' ) <EOL> def log_location_of_repro_file ( self ) : <EOL> if not self . _path : <EOL> return <EOL> logger . info ( '<STR_LIT>' . format ( self . _path ) ) </s>
