2012-02-07 11 views
5

Tengo un script que automatiza el autor vuelve a escribir en varios repositorios de git.Imprimir el comando ejecutado para el subproceso de Python. Popen

def filter_history(old, new, name, repoPath): 

command = """--env-filter ' 
     an="$GIT_AUTHOR_NAME" 
     am="$GIT_AUTHOR_EMAIL" 
     cn="$GIT_COMMITTER_NAME" 
     cm="$GIT_COMMITTER_EMAIL" 

     if [[ "$GIT_COMMITTER_NAME" = "|old|" ]] 
     then 
      cn="|name|" 
      cm="|new|" 
     fi 

     if [[ "$GIT_AUTHOR_NAME" = "|old|" ]] 
     then 
      an="|name|" 
      am="|new|" 
     fi 

     export GIT_AUTHOR_NAME="$an" 
     export GIT_AUTHOR_EMAIL="$am" 
     export GIT_COMMITTER_NAME="$cn" 
     export GIT_COMMITTER_EMAIL="$cm" 
' 
""" 

#DO string replace 
command = command.replace("|old|", old) 
command = command.replace("|new|", new) 
command = command.replace("|name|", name) 

print "git filter-branch -f " + command 

process = subprocess.Popen(['git filter-branch -f', command],cwd=os.path.dirname(repoPath), shell=True) 
process.wait() 

El comando se ejecuta correctamente, pero me dice que nada ha cambiado en el historial de repositorio. Sin embargo, si tomo el comando que se imprime (que debería ser lo que se está ejecutando), colóquelo en un script de shell y ejecútelo, ya que cambiará el historial. Creo que el comando de alguna manera no se está ejecutando correctamente. ¿Hay alguna forma de ver exactamente qué comando está ejecutando el módulo de subproceso?

Respuesta

5

Cuando usa shell = True, subprocess.Popen espera una cadena como primer argumento. Es mejor no usar shell = True si puede evitarlo, ya que puede ser security risk (see the Warning.

Cuando omite shell = True, o use shell = False, subprocess.Popen espera una lista de argumentos. Puede generar esa lista de argumentos desde una cadena usando shlex.split:

import shlex 
import subprocess 

def filter_history(old, new, name, repoPath): 
    """Change author info 
    """ 
    # http://help.github.com/change-author-info/ 
    # http://stackoverflow.com/a/3880493/190597 
    command = """git filter-branch -f --env-filter ' 
     an="$GIT_AUTHOR_NAME" 
     am="$GIT_AUTHOR_EMAIL" 
     cn="$GIT_COMMITTER_NAME" 
     cm="$GIT_COMMITTER_EMAIL" 

     if [[ "$GIT_COMMITTER_NAME" = "{old}" ]] 
     then 
      cn="{name}" 
      cm="{new}" 
     fi 

     if [[ "$GIT_AUTHOR_NAME" = "{old}" ]] 
     then 
      an="{name}" 
      am="{new}" 
     fi 

     export GIT_AUTHOR_NAME="$an" 
     export GIT_AUTHOR_EMAIL="$am" 
     export GIT_COMMITTER_NAME="$cn" 
     export GIT_COMMITTER_EMAIL="$cm" 
     ' 
     """.format(old = old, new = new, name = name) 

    process = subprocess.Popen(
     shlex.split(command), 
     cwd = os.path.dirname(repoPath)) 
    process.communicate() 
Cuestiones relacionadas