2013-12-17 2 views
8

sto ottenendo il seguente errore quando si esegue lo script di seguito, posso Anyhelp di individuare qual è il problema e come superarlaTypeError: execv() arg 2 deve contenere solo stringhe

import subprocess 
import sys 
import os 

def main(): 
    to = '' 
    ssh_command = ["ssh", "-p", "29418", "review-android.quicinc.com", "gerrit", 
        "query", "--format=JSON", "--current-patch-set", 
        "--commit-message", "--files", ] 

    with open('gerrit_output.txt', 'a') as fp: 
     with open('caf_gerrits.txt','r') as f : 
      for gerrit in f : 
       print gerrit 
       result = subprocess.check_output(ssh_command + [gerrit, ]) 
       print result 
       fp.write(result) 

if __name__ == '__main__': 
main() 

errore: -

Traceback (most recent call last): 
    File "test.py", line 20, in <module> 

    File "test.py", line 15, in main 

    File "/usr/lib/python2.7/subprocess.py", line 537, in check_output 
    process = Popen(stdout=PIPE, *popenargs, **kwargs) 
    File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ 
    errread, errwrite) 
    File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child 
    raise child_exception 
TypeError: execv() arg 2 must contain only strings 

risposta

16

Il terzo elemento ssh_command è un numero intero. Deve essere una stringa.

es:

ssh_command = ["ssh", "-p", 29418, ... 
#       ^problem 

E la soluzione è semplice, basta aggiungere alcune citazioni:

ssh_command = ["ssh", "-p", "29418", ... 
#       ^Now it's a string. 
+0

grazie per le informazioni .. come risolvere il problema? – user2955256

+1

che non ha risolto il problema .. – user2955256

+1

@ user2955256 - Stesso errore? Se c'è lo stesso errore, dovresti pubblicare esattamente ciò di cui abbiamo bisogno * per riprodurre * il problema. per esempio. inserisci i contenuti esatti di 'gerrit'. Se non riusciamo a riprodurre il tuo problema, allora stiamo solo indovinando cosa sta andando storto. – mgilson

1

In primo luogo è necessario aggiungere le virgolette attorno 29418 come mgilson menzionato. In secondo luogo cerchiamo di abbattere quello che stai cercando di correre:

ssh_command = ["ssh", "-p", 29418, "review-android.company.com", "gerrit", 
       "query", "--format", "JSON", "--current-patch-set", 
       "--commit-message", "--files", ] 

Che equivale

ssh -p 29418 review-android.company.com gerrit query --format JSON --current-patch-set --commit-message --files 
(then I'm assuming you have filenames in your caf_gerrits.txt file which get appended at the end) 

Una cosa che salta fuori di me è che si può voler dire --format=JSON in questo caso gli elementi della L'array ssh_command deve essere combinato come [..., "--format=JSON", ...]. L'altra cosa che si potrebbe desiderare di fare è print result dopo la linea result= per facilitare il debug.

+0

Ho aggiornato l'ultimo codice e l'errore – user2955256

+0

Qual è il comando ssh esatto che stai cercando di realizzare? – misakm