<s> import git <EOL> import shutil <EOL> from extutils import get_filename <EOL> from template import render <EOL> import os <EOL> import tempfile <EOL> import stat <EOL> import yaml <EOL> import jsonschema <EOL> import subprocess <EOL> SSH_WRAPPER = '''<STR_LIT>''' <EOL> SCHEMA = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:url>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:args>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:url>' ] <EOL> } <EOL> def write_temp_file ( contents , suffix , executable ) : <EOL> tmpfile = tempfile . NamedTemporaryFile ( suffix = suffix , prefix = '<STR_LIT>' , delete = False ) <EOL> try : <EOL> tmpfile . write ( contents ) <EOL> if executable : <EOL> os . fchmod ( tmpfile . fileno ( ) , stat . S_IRUSR | stat . S_IWUSR | stat . S_IXUSR ) <EOL> return tmpfile . name <EOL> finally : <EOL> tmpfile . close ( ) <EOL> def _clone_repo ( url , dest , args ) : <EOL> subprocess . check_call ( '<STR_LIT>' . format ( args , url , dest ) . split ( ) ) <EOL> return git . Repo ( dest ) <EOL> def ext_git ( abs_source , dest , inputhash , loader , resources , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> contents = yaml . load ( render ( abs_source , inputhash , loader ) ) <EOL> finalfile = get_filename ( dest ) <EOL> jsonschema . validate ( contents , SCHEMA ) <EOL> url = contents [ '<STR_LIT:url>' ] <EOL> refspec = None <EOL> sshkey = None <EOL> args = '<STR_LIT>' <EOL> if '<STR_LIT>' in contents : <EOL> refspec = contents [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in contents : <EOL> sshkey = contents [ '<STR_LIT>' ] <EOL> if '<STR_LIT:args>' in contents : <EOL> args = contents [ '<STR_LIT:args>' ] <EOL> if sshkey : <EOL> key = resources [ sshkey ] ( ) <EOL> if '<STR_LIT>' in os . environ : <EOL> git_ssh = os . environ [ '<STR_LIT>' ] <EOL> reset_to_env = True <EOL> else : <EOL> git_ssh = '<STR_LIT>' <EOL> reset_to_env = False <EOL> keyfile = write_temp_file ( key , '<STR_LIT>' , False ) <EOL> wrapper = write_temp_file ( SSH_WRAPPER . format ( git_ssh , keyfile ) , '<STR_LIT>' , True ) <EOL> old_environ = os . environ . copy ( ) <EOL> try : <EOL> os . environ [ '<STR_LIT>' ] = wrapper <EOL> repo = _clone_repo ( url , finalfile , args ) <EOL> finally : <EOL> if reset_to_env : <EOL> os . environ [ '<STR_LIT>' ] = git_ssh <EOL> else : <EOL> del os . environ [ '<STR_LIT>' ] <EOL> os . remove ( keyfile ) <EOL> os . remove ( wrapper ) <EOL> else : <EOL> repo = _clone_repo ( url , finalfile , args ) <EOL> if refspec : <EOL> repo . git . checkout ( refspec ) <EOL> return finalfile </s>
