<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from base64 import urlsafe_b64encode <EOL> from contextlib import contextmanager <EOL> from hashlib import sha256 <EOL> from itertools import chain <EOL> from linecache import getline <EOL> from optparse import OptionParser <EOL> from os import listdir <EOL> from os . path import join , basename , splitext <EOL> import re <EOL> import shlex <EOL> from shutil import rmtree <EOL> from sys import argv , exit <EOL> from tempfile import mkdtemp <EOL> from urlparse import urlparse <EOL> from pkg_resources import require , VersionConflict , DistributionNotFound <EOL> def activate ( specifier ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for distro in require ( specifier ) : <EOL> distro . activate ( ) <EOL> except ( VersionConflict , DistributionNotFound ) : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' + specifier ) <EOL> activate ( '<STR_LIT>' ) <EOL> import pip <EOL> from pip . log import logger <EOL> from pip . req import parse_requirements <EOL> __version__ = <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> <EOL> ITS_FINE_ITS_FINE = <NUM_LIT:0> <EOL> SOMETHING_WENT_WRONG = <NUM_LIT:1> <EOL> COMMAND_LINE_ERROR = <NUM_LIT:2> <EOL> ARCHIVE_EXTENSIONS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class PipException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , error_code ) : <EOL> self . error_code = error_code <EOL> def encoded_hash ( sha ) : <EOL> """<STR_LIT>""" <EOL> return urlsafe_b64encode ( sha . digest ( ) ) . decode ( '<STR_LIT:ascii>' ) . rstrip ( '<STR_LIT:=>' ) <EOL> @ contextmanager <EOL> def ephemeral_dir ( ) : <EOL> dir = mkdtemp ( prefix = '<STR_LIT>' ) <EOL> try : <EOL> yield dir <EOL> finally : <EOL> rmtree ( dir ) <EOL> def run_pip ( initial_args ) : <EOL> """<STR_LIT>""" <EOL> status_code = pip . main ( initial_args = initial_args ) <EOL> logger . consumers = [ ] <EOL> if status_code : <EOL> raise PipException ( status_code ) <EOL> def pip_download ( req , argv , temp_path ) : <EOL> """<STR_LIT>""" <EOL> line = getline ( * requirements_path_and_line ( req ) ) <EOL> argv = ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , temp_path ] + <EOL> list ( requirement_args ( argv , want_other = True ) ) + <EOL> shlex . split ( line ) ) <EOL> old_contents = set ( listdir ( temp_path ) ) <EOL> run_pip ( argv ) <EOL> return ( set ( listdir ( temp_path ) ) - old_contents ) . pop ( ) <EOL> def pip_install_archives_from ( temp_path ) : <EOL> """<STR_LIT>""" <EOL> for filename in listdir ( temp_path ) : <EOL> archive_path = join ( temp_path , filename ) <EOL> run_pip ( [ '<STR_LIT>' , '<STR_LIT>' , archive_path ] ) <EOL> def hash_of_file ( path ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:rb>' ) as archive : <EOL> sha = sha256 ( ) <EOL> while True : <EOL> data = archive . read ( <NUM_LIT:2> ** <NUM_LIT:20> ) <EOL> if not data : <EOL> break <EOL> sha . update ( data ) <EOL> return encoded_hash ( sha ) <EOL> def is_git_sha ( text ) : <EOL> """<STR_LIT>""" <EOL> if len ( text ) in ( <NUM_LIT> , <NUM_LIT:7> ) : <EOL> try : <EOL> int ( text , <NUM_LIT:16> ) <EOL> return True <EOL> except ValueError : <EOL> pass <EOL> return False <EOL> def filename_from_url ( url ) : <EOL> parsed = urlparse ( url ) <EOL> path = parsed . path <EOL> return path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> def is_always_unsatisfied ( req ) : <EOL> """<STR_LIT>""" <EOL> url = req . url <EOL> if url : <EOL> filename = filename_from_url ( url ) <EOL> if filename . endswith ( ARCHIVE_EXTENSIONS ) : <EOL> filename , ext = splitext ( filename ) <EOL> if is_git_sha ( filename ) : <EOL> return True <EOL> return False <EOL> def version_of_download ( filename , package_name ) : <EOL> """<STR_LIT>""" <EOL> def version_of_archive ( filename , package_name ) : <EOL> for ext in ARCHIVE_EXTENSIONS : <EOL> if filename . endswith ( ext ) : <EOL> filename = filename [ : - len ( ext ) ] <EOL> break <EOL> if is_git_sha ( filename ) : <EOL> filename = package_name + '<STR_LIT:->' + filename <EOL> if not filename . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) . startswith ( package_name ) : <EOL> give_up ( filename , package_name ) <EOL> return filename [ len ( package_name ) + <NUM_LIT:1> : ] <EOL> def version_of_wheel ( filename , package_name ) : <EOL> whl_package_name , version , _rest = filename . split ( '<STR_LIT:->' , <NUM_LIT:2> ) <EOL> our_package_name = re . sub ( r'<STR_LIT>' , '<STR_LIT:_>' , package_name , re . UNICODE ) <EOL> if whl_package_name != our_package_name : <EOL> give_up ( filename , whl_package_name ) <EOL> return version <EOL> def give_up ( filename , package_name ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( filename , package_name ) ) <EOL> return ( version_of_wheel if filename . endswith ( '<STR_LIT>' ) <EOL> else version_of_archive ) ( filename , package_name ) <EOL> def requirement_args ( argv , want_paths = False , want_other = False ) : <EOL> """<STR_LIT>""" <EOL> was_r = False <EOL> for arg in argv : <EOL> if was_r : <EOL> if want_paths : <EOL> yield arg <EOL> was_r = False <EOL> elif arg in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> was_r = True <EOL> else : <EOL> if want_other : <EOL> yield arg <EOL> def requirements_path_and_line ( req ) : <EOL> """<STR_LIT>""" <EOL> path , line = ( re . match ( r'<STR_LIT>' , <EOL> req . comes_from ) . groups ( ) ) <EOL> return path , int ( line ) <EOL> def hashes_of_requirements ( requirements ) : <EOL> """<STR_LIT>""" <EOL> def hashes_above ( path , line_number ) : <EOL> """<STR_LIT>""" <EOL> for line_number in range ( line_number - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> line = getline ( path , line_number ) <EOL> if not line . startswith ( '<STR_LIT:#>' ) : <EOL> break <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> yield line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . strip ( ) <EOL> expected_hashes = { } <EOL> missing_hashes_req = [ ] <EOL> for req in requirements : <EOL> path , line_number = requirements_path_and_line ( req ) <EOL> hashes = list ( hashes_above ( path , line_number ) ) <EOL> if hashes : <EOL> hashes . reverse ( ) <EOL> expected_hashes [ req . name ] = hashes <EOL> else : <EOL> missing_hashes_req . append ( req ) <EOL> return expected_hashes , missing_hashes_req <EOL> def hash_mismatches ( expected_hash_map , downloaded_hashes ) : <EOL> """<STR_LIT>""" <EOL> for package_name , expected_hashes in expected_hash_map . items ( ) : <EOL> try : <EOL> hash_of_download = downloaded_hashes [ package_name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> if hash_of_download not in expected_hashes : <EOL> yield expected_hashes , package_name , hash_of_download <EOL> def peep_hash ( argv ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( <EOL> usage = '<STR_LIT>' , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _ , paths = parser . parse_args ( args = argv ) <EOL> if paths : <EOL> for path in paths : <EOL> print ( '<STR_LIT>' , hash_of_file ( path ) ) <EOL> return ITS_FINE_ITS_FINE <EOL> else : <EOL> parser . print_usage ( ) <EOL> return COMMAND_LINE_ERROR <EOL> class EmptyOptions ( object ) : <EOL> """<STR_LIT>""" <EOL> default_vcs = None <EOL> skip_requirements_regex = None <EOL> def peep_install ( argv ) : <EOL> """<STR_LIT>""" <EOL> req_paths = list ( requirement_args ( argv , want_paths = True ) ) <EOL> if not req_paths : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> return COMMAND_LINE_ERROR <EOL> requirements = list ( chain ( * ( parse_requirements ( path , <EOL> options = EmptyOptions ( ) ) <EOL> for path in req_paths ) ) ) <EOL> downloaded_hashes , downloaded_versions , satisfied_reqs , malformed_reqs = { } , { } , [ ] , [ ] <EOL> with ephemeral_dir ( ) as temp_path : <EOL> for req in requirements : <EOL> if not req . req or not req . req . project_name : <EOL> malformed_reqs . append ( '<STR_LIT>' % req . url ) <EOL> continue <EOL> req . check_if_exists ( ) <EOL> if req . satisfied_by and not is_always_unsatisfied ( req ) : <EOL> satisfied_reqs . append ( req ) <EOL> else : <EOL> name = req . req . project_name <EOL> archive_filename = pip_download ( req , argv , temp_path ) <EOL> downloaded_hashes [ name ] = hash_of_file ( join ( temp_path , archive_filename ) ) <EOL> downloaded_versions [ name ] = version_of_download ( archive_filename , name ) <EOL> expected_hashes , missing_hashes_reqs = hashes_of_requirements ( requirements ) <EOL> mismatches = list ( hash_mismatches ( expected_hashes , downloaded_hashes ) ) <EOL> satisfied_req_names = set ( req . name for req in satisfied_reqs ) <EOL> missing_hashes_reqs = [ req for req in missing_hashes_reqs if req . name not in satisfied_req_names ] <EOL> if mismatches or missing_hashes_reqs or malformed_reqs : <EOL> print ( ) <EOL> if mismatches : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> for expected_hashes , package_name , hash_of_download in mismatches : <EOL> hash_of_download = downloaded_hashes [ package_name ] <EOL> preamble = '<STR_LIT>' % ( <EOL> package_name , <EOL> '<STR_LIT>' if len ( expected_hashes ) > <NUM_LIT:1> else '<STR_LIT>' ) <EOL> print ( preamble , end = '<STR_LIT:U+0020>' ) <EOL> print ( ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * ( len ( preamble ) + <NUM_LIT:1> ) ) . join ( expected_hashes ) ) <EOL> print ( '<STR_LIT:U+0020>' * ( len ( preamble ) - <NUM_LIT:4> ) , '<STR_LIT>' , hash_of_download ) <EOL> if mismatches : <EOL> print ( ) <EOL> if missing_hashes_reqs : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> for req in missing_hashes_reqs : <EOL> print ( '<STR_LIT>' % downloaded_hashes [ req . name ] ) <EOL> if req . url : <EOL> line = req . url <EOL> if req . name not in filename_from_url ( req . url ) : <EOL> line = '<STR_LIT>' % ( line , req . name ) <EOL> else : <EOL> line = '<STR_LIT>' % ( req . name , downloaded_versions [ req . name ] ) <EOL> print ( line + '<STR_LIT:\n>' ) <EOL> if malformed_reqs : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT:*>' , '<STR_LIT>' . join ( malformed_reqs ) ) <EOL> if mismatches or missing_hashes_reqs or malformed_reqs : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> return SOMETHING_WENT_WRONG <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> pip_install_archives_from ( temp_path ) <EOL> if satisfied_reqs : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> for req in satisfied_reqs : <EOL> print ( '<STR_LIT:U+0020>' , req . req ) <EOL> return ITS_FINE_ITS_FINE <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> commands = { '<STR_LIT>' : peep_hash , <EOL> '<STR_LIT>' : peep_install } <EOL> try : <EOL> if len ( argv ) >= <NUM_LIT:2> and argv [ <NUM_LIT:1> ] in commands : <EOL> return commands [ argv [ <NUM_LIT:1> ] ] ( argv [ <NUM_LIT:2> : ] ) <EOL> else : <EOL> return pip . main ( ) <EOL> except PipException as exc : <EOL> return exc . error_code <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> exit ( main ( ) ) </s>
