<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import subprocess <EOL> from collections import namedtuple <EOL> from pants . binaries . binary_util import BinaryUtil <EOL> from pants . fs . archive import TGZ <EOL> from pants . subsystem . subsystem import Subsystem <EOL> from pants . util . contextutil import temporary_dir <EOL> from pants . util . memo import memoized_property <EOL> class NodeDistribution ( object ) : <EOL> """<STR_LIT>""" <EOL> class Factory ( Subsystem ) : <EOL> options_scope = '<STR_LIT>' <EOL> @ classmethod <EOL> def subsystem_dependencies ( cls ) : <EOL> return ( BinaryUtil . Factory , ) <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( NodeDistribution . Factory , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , advanced = True , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , advanced = True , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def create ( self ) : <EOL> binary_util = BinaryUtil . Factory . create ( ) <EOL> options = self . get_options ( ) <EOL> return NodeDistribution ( binary_util , options . supportdir , options . version ) <EOL> @ classmethod <EOL> def _normalize_version ( cls , version ) : <EOL> return version if version . startswith ( '<STR_LIT:v>' ) else '<STR_LIT:v>' + version <EOL> def __init__ ( self , binary_util , relpath , version ) : <EOL> self . _binary_util = binary_util <EOL> self . _relpath = relpath <EOL> self . _version = self . _normalize_version ( version ) <EOL> @ property <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _version <EOL> @ memoized_property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> node_distribution = self . _binary_util . select_binary ( self . _relpath , self . version , '<STR_LIT>' ) <EOL> distribution_workdir = os . path . dirname ( node_distribution ) <EOL> outdir = os . path . join ( distribution_workdir , '<STR_LIT>' ) <EOL> if not os . path . exists ( outdir ) : <EOL> with temporary_dir ( root_dir = distribution_workdir ) as tmp_dist : <EOL> TGZ . extract ( node_distribution , tmp_dist ) <EOL> os . rename ( tmp_dist , outdir ) <EOL> return os . path . join ( outdir , '<STR_LIT>' ) <EOL> class Command ( namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:args>' ] ) ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def cmd ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ os . path . join ( self . bin_dir_path , self . executable ) ] + self . args <EOL> def _prepare_env ( self , kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs = kwargs . copy ( ) <EOL> env = kwargs . pop ( '<STR_LIT>' , os . environ ) . copy ( ) <EOL> env [ '<STR_LIT>' ] = ( self . bin_dir_path + os . path . pathsep + env [ '<STR_LIT>' ] <EOL> if env . get ( '<STR_LIT>' , '<STR_LIT>' ) else self . bin_dir_path ) <EOL> return env , kwargs <EOL> def run ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> env , kwargs = self . _prepare_env ( kwargs ) <EOL> return subprocess . Popen ( self . cmd , env = env , ** kwargs ) <EOL> def check_output ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> env , kwargs = self . _prepare_env ( kwargs ) <EOL> return subprocess . check_output ( self . cmd , env = env , ** kwargs ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+0020>' . join ( self . cmd ) <EOL> def node_command ( self , args = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _create_command ( '<STR_LIT>' , args ) <EOL> def npm_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> return self . _create_command ( '<STR_LIT>' , args ) <EOL> def _create_command ( self , executable , args = None ) : <EOL> return self . Command ( os . path . join ( self . path , '<STR_LIT>' ) , executable , args or [ ] ) </s>
