<s> import os <EOL> import tempfile <EOL> import re <EOL> import sys <EOL> from pip . util import call_subprocess <EOL> from pip . util import display_path , rmtree <EOL> from pip . log import logger <EOL> from pip . vcs import vcs , VersionControl <EOL> from pip . download import path_to_url2 <EOL> from pip . backwardcompat import ConfigParser <EOL> class Mercurial ( VersionControl ) : <EOL> name = '<STR_LIT>' <EOL> dirname = '<STR_LIT>' <EOL> repo_name = '<STR_LIT>' <EOL> schemes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> bundle_file = '<STR_LIT>' <EOL> guide = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def parse_vcs_bundle_file ( self , content ) : <EOL> url = rev = None <EOL> for line in content . splitlines ( ) : <EOL> if not line . strip ( ) or line . strip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> url_match = re . search ( r'<STR_LIT>' , line ) <EOL> if url_match : <EOL> url = url_match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> rev_match = re . search ( r'<STR_LIT>' , line ) <EOL> if rev_match : <EOL> rev = rev_match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> if url and rev : <EOL> return url , rev <EOL> return None , None <EOL> def export ( self , location ) : <EOL> """<STR_LIT>""" <EOL> temp_dir = tempfile . mkdtemp ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . unpack ( temp_dir ) <EOL> try : <EOL> call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' , location ] , <EOL> filter_stdout = self . _filter , show_stdout = False , cwd = temp_dir ) <EOL> finally : <EOL> rmtree ( temp_dir ) <EOL> def switch ( self , dest , url , rev_options ) : <EOL> repo_config = os . path . join ( dest , self . dirname , '<STR_LIT>' ) <EOL> config = ConfigParser . SafeConfigParser ( ) <EOL> try : <EOL> config . read ( repo_config ) <EOL> config . set ( '<STR_LIT>' , '<STR_LIT:default>' , url ) <EOL> config_file = open ( repo_config , '<STR_LIT:w>' ) <EOL> config . write ( config_file ) <EOL> config_file . close ( ) <EOL> except ( OSError , ConfigParser . NoSectionError ) : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . warn ( <EOL> '<STR_LIT>' <EOL> % ( url , e ) ) <EOL> else : <EOL> call_subprocess ( [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] + rev_options , cwd = dest ) <EOL> def update ( self , dest , rev_options ) : <EOL> call_subprocess ( [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] , cwd = dest ) <EOL> call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] + rev_options , cwd = dest ) <EOL> def obtain ( self , dest ) : <EOL> url , rev = self . get_url_rev ( ) <EOL> if rev : <EOL> rev_options = [ rev ] <EOL> rev_display = '<STR_LIT>' % rev <EOL> else : <EOL> rev_options = [ ] <EOL> rev_display = '<STR_LIT>' <EOL> if self . check_destination ( dest , url , rev_options , rev_display ) : <EOL> logger . notify ( '<STR_LIT>' <EOL> % ( url , rev_display , display_path ( dest ) ) ) <EOL> call_subprocess ( [ self . cmd , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , url , dest ] ) <EOL> call_subprocess ( [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] + rev_options , cwd = dest ) <EOL> def get_url ( self , location ) : <EOL> url = call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> show_stdout = False , cwd = location ) . strip ( ) <EOL> if self . _is_local_repository ( url ) : <EOL> url = path_to_url2 ( url ) <EOL> return url . strip ( ) <EOL> def get_tag_revs ( self , location ) : <EOL> tags = call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' ] , show_stdout = False , cwd = location ) <EOL> tag_revs = [ ] <EOL> for line in tags . splitlines ( ) : <EOL> tags_match = re . search ( r'<STR_LIT>' , line ) <EOL> if tags_match : <EOL> tag = tags_match . group ( <NUM_LIT:1> ) <EOL> rev = tags_match . group ( <NUM_LIT:2> ) <EOL> if "<STR_LIT>" != tag : <EOL> tag_revs . append ( ( rev . strip ( ) , tag . strip ( ) ) ) <EOL> return dict ( tag_revs ) <EOL> def get_branch_revs ( self , location ) : <EOL> branches = call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' ] , show_stdout = False , cwd = location ) <EOL> branch_revs = [ ] <EOL> for line in branches . splitlines ( ) : <EOL> branches_match = re . search ( r'<STR_LIT>' , line ) <EOL> if branches_match : <EOL> branch = branches_match . group ( <NUM_LIT:1> ) <EOL> rev = branches_match . group ( <NUM_LIT:2> ) <EOL> if "<STR_LIT:default>" != branch : <EOL> branch_revs . append ( ( rev . strip ( ) , branch . strip ( ) ) ) <EOL> return dict ( branch_revs ) <EOL> def get_revision ( self , location ) : <EOL> current_revision = call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> show_stdout = False , cwd = location ) . strip ( ) <EOL> return current_revision <EOL> def get_revision_hash ( self , location ) : <EOL> current_rev_hash = call_subprocess ( <EOL> [ self . cmd , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> show_stdout = False , cwd = location ) . strip ( ) <EOL> return current_rev_hash <EOL> def get_src_requirement ( self , dist , location , find_tags ) : <EOL> repo = self . get_url ( location ) <EOL> if not repo . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> repo = '<STR_LIT>' + repo <EOL> egg_project_name = dist . egg_name ( ) . split ( '<STR_LIT:->' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if not repo : <EOL> return None <EOL> current_rev = self . get_revision ( location ) <EOL> current_rev_hash = self . get_revision_hash ( location ) <EOL> tag_revs = self . get_tag_revs ( location ) <EOL> branch_revs = self . get_branch_revs ( location ) <EOL> if current_rev in tag_revs : <EOL> full_egg_name = '<STR_LIT>' % ( egg_project_name , tag_revs [ current_rev ] ) <EOL> elif current_rev in branch_revs : <EOL> full_egg_name = '<STR_LIT>' % ( egg_project_name , branch_revs [ current_rev ] ) <EOL> else : <EOL> full_egg_name = '<STR_LIT>' % egg_project_name <EOL> return '<STR_LIT>' % ( repo , current_rev_hash , full_egg_name ) <EOL> vcs . register ( Mercurial ) </s>
