<s> from __future__ import division <EOL> import os <EOL> import pickle <EOL> import platform <EOL> import subprocess <EOL> import sys <EOL> import warnings <EOL> from vistrails . core . modules . vistrails_module import Module , ModuleError <EOL> from . import configuration <EOL> REPROUNZIP_VISTRAILS_INTERFACE_VERSION = '<STR_LIT:1>' <EOL> class Experiment ( object ) : <EOL> def __init__ ( self , path , unpacker ) : <EOL> self . path = path <EOL> self . unpacker = unpacker <EOL> class Directory ( Module ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> _output_ports = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def compute ( self ) : <EOL> path = self . get_input ( '<STR_LIT>' ) . name <EOL> if not os . path . exists ( path ) : <EOL> self . setup_experiment ( ) <EOL> if ( not os . path . exists ( os . path . join ( path , '<STR_LIT>' ) ) or <EOL> not os . path . exists ( os . path . join ( path , '<STR_LIT>' ) ) ) : <EOL> raise ModuleError ( self , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> with open ( os . path . join ( path , '<STR_LIT>' ) , '<STR_LIT:rb>' ) as fp : <EOL> unpacked_info = pickle . load ( fp ) <EOL> unpacker = unpacked_info [ '<STR_LIT>' ] <EOL> self . set_output ( '<STR_LIT>' , Experiment ( path , unpacker ) ) <EOL> def setup_experiment ( self ) : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> class UnpackDirectory ( Directory ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def setup_experiment ( self ) : <EOL> raise NotImplementedError <EOL> class Run ( Module ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT>' , Directory ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> _output_ports = [ ( '<STR_LIT>' , Directory ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def __init__ ( self ) : <EOL> Module . __init__ ( self ) <EOL> self . input_ports_order = [ ] <EOL> self . output_ports_order = [ ] <EOL> def transfer_attrs ( self , module ) : <EOL> Module . transfer_attrs ( self , module ) <EOL> self . input_ports_order = [ p . name for p in module . input_port_specs ] <EOL> self . output_ports_order = [ p . name for p in module . output_port_specs <EOL> if p . name in module . connected_output_ports ] <EOL> def compute ( self ) : <EOL> experiment = self . get_input ( '<STR_LIT>' ) <EOL> if configuration . check ( '<STR_LIT>' ) : <EOL> python = configuration . reprounzip_python <EOL> else : <EOL> python = '<STR_LIT>' <EOL> environ = dict ( os . environ ) <EOL> removed = [ ] <EOL> for bad_var in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if bad_var in os . environ : <EOL> removed . append ( bad_var ) <EOL> del environ [ bad_var ] <EOL> if removed : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % '<STR_LIT:U+0020>' . join ( removed ) ) <EOL> if ( platform . system ( ) == '<STR_LIT>' and <EOL> '<STR_LIT>' not in environ [ '<STR_LIT>' ] . split ( os . pathsep ) ) : <EOL> environ [ '<STR_LIT>' ] += os . pathsep + '<STR_LIT>' <EOL> stdout = self . interpreter . filePool . create_file ( prefix = '<STR_LIT>' , <EOL> suffix = '<STR_LIT>' ) <EOL> stderr = self . interpreter . filePool . create_file ( prefix = '<STR_LIT>' , <EOL> suffix = '<STR_LIT>' ) <EOL> args = [ python , '<STR_LIT>' , '<STR_LIT>' , <EOL> REPROUNZIP_VISTRAILS_INTERFACE_VERSION , <EOL> experiment . unpacker , experiment . path , <EOL> '<STR_LIT>' % self . get_input ( '<STR_LIT>' ) ] <EOL> for name in self . input_ports_order : <EOL> if self . has_input ( name ) : <EOL> args . append ( '<STR_LIT>' ) <EOL> args . append ( '<STR_LIT>' % ( name , self . get_input ( name ) . name ) ) <EOL> output_ports = [ ] <EOL> for name in self . output_ports_order : <EOL> f = self . interpreter . filePool . create_file ( prefix = '<STR_LIT>' ) <EOL> args . append ( '<STR_LIT>' ) <EOL> args . append ( '<STR_LIT>' % ( name , f . name ) ) <EOL> output_ports . append ( ( name , f ) ) <EOL> with open ( stdout . name , '<STR_LIT:wb>' ) as stdout_fp : <EOL> with open ( stderr . name , '<STR_LIT:wb>' ) as stderr_fp : <EOL> proc = subprocess . Popen ( args , <EOL> stdout = stdout_fp , stderr = stderr_fp , <EOL> env = environ ) <EOL> proc . wait ( ) <EOL> with open ( stderr . name , '<STR_LIT:rb>' ) as stderr_fp : <EOL> while True : <EOL> chunk = stderr_fp . read ( <NUM_LIT> ) <EOL> if not chunk : <EOL> break <EOL> sys . stderr . write ( chunk ) <EOL> if proc . returncode != <NUM_LIT:0> : <EOL> raise ModuleError ( self , <EOL> "<STR_LIT>" % proc . returncode ) <EOL> for name , file in output_ports : <EOL> self . set_output ( name , file ) <EOL> self . set_output ( '<STR_LIT>' , experiment ) <EOL> self . set_output ( '<STR_LIT>' , stdout ) <EOL> self . set_output ( '<STR_LIT>' , stderr ) <EOL> _modules = [ Directory , Run ] </s>
