2011-11-08 12 views
0

Gerrit http://code.google.com/p/gerrit/フックの作成にいくつか問題があります。Python + getopt - 構文解析に関する問題

http://gerrit.googlecode.com/svn/documentation/2.2.0/config-hooks.html

私は パッチセットが作成した--change --change-urlには、コマンドラインを解析した場合--project --branch --uploader --commit --patchset

def main(): 

if (len(sys.argv) < 2): 
    showUsage() 
    exit() 

if (sys.argv[1] == 'update-projects'): 
    updateProjects() 
    exit() 

need = ['action=', 'change=', 'change-url=', 'commit=', 'project=', 'branch=', 'uploader=', 
     'patchset=', 'abandoner=', 'reason=', 'submitter=', 'comment=', 'CRVW=', 'VRIF=' , 'patchset=' , 'restorer=', 'author='] 
print sys.argv[1:] 
print '-----' 
optlist, args = getopt.getopt(sys.argv[1:], '', need) 
id = url = hash = who = comment = reason = codeReview = verified = restorer = '' 
print optlist 

for o, a in optlist: 
    if o == '--change': id = a 
    elif o == '--change-url': url = a 
    elif o == '--commit': hash = a 
    elif o == '--action': what = a 
    elif o == '--uploader': who = a 
    elif o == '--submitter': who = a 
    elif o == '--abandoner': who = a 
    elif o == '--author' : who = a 
    elif o == '--branch': branch = a 
    elif o == '--comment': comment = a 
    elif o == '--CRVW' : codeReview = a 
    elif o == '--VRIF' : verified = a 
    elif o == '--patchset' : patchset = a 
    elif o == '--restorer' : who = a 
    elif o == '--reason' : reason = a 

コマンドライン入力:

--change I87f7802d438d5640779daa9ac8196aeb3eec8c2a 
--change-url http://<hostname>:8080/308 
--project private/bar 
--branch master 
--uploader xxxxxxx-xxxxx xxxxxxx ([email protected]) 
--commit 49aae9befaf27a5fede51b498f0660199f47b899 --patchset 1 

印刷sys.argvの[1]

['--action', 'new', 
'--change','I87f7802d438d5640779daa9ac8196aeb3eec8c2a', 
'--change-url', 
'http://<hostname>:8080/308', 
'--project', 'private/bar', 
'--branch', 'master', 
'--uploader', 'xxxxxxx-xxxxx', 'xxxxxxx', '([email protected])', 
'--commit', '49aae9befaf27a5fede51b498f0660199f47b899', 
'--patchset', '1'] 

印刷optlistの

[('--action', 'new'), 
('--change', 'I87f7802d438d5640779daa9ac8196aeb3eec8c2a'), 
('--change-url', 'http://<hostname>:8080/308'), 
('--project', 'private/bar'), 
('--branch', 'master'), 
('--uploader', 'xxxxxxx-xxxxx')] 

スクリプトが

'--uploader', 'xxxxxxx-xxxxx', 'xxxxxxx', '([email protected])' 
and not 
'--uploader', 'xxxxxxx-xxxxx xxxxxxx ([email protected])' 

を生成し、なぜスクリプトが解析--commit --patchset dont'tので、私は...知らない

コメントを解析すると、すべてが動作します:

コマンドライン入力:

-change I87f7802d438d5640779daa9ac8196aeb3eec8c2a 
    --change-url http://<hostname>.intra:8080/308 
    --project private/bar 
    --branch master 
    --author xxxxxxx-xxxxx xxxxxxx ([email protected]) 
    --commit 49aae9befaf27a5fede51b498f0660199f47b899 
    --comment asdf 
    --CRVW 0 
    --VRIF 0 

印刷sys.argvの[1:]それは、すべてのコマンドラインツールのように、スペースが含まれている場合は、引数を引用符で囲む必要があります

'--action', 'comment', 
    '--change', 'I87f7802d438d5640779daa9ac8196aeb3eec8c2a', 
    '--change-url', 
    'http://<hostname>:8080/308', 
    '--project', 'private/bar', 
    '--branch', 'master', 
    '--author', 'xxxxxxx-xxxxx xxxxxxx ([email protected])', <<< That's right! 
    '--commit', '49aae9befaf27a5fede51b498f0660199f47b899', 
    '--comment', 'asdf', 
    '--CRVW', '0', 
    '--VRIF', '0'] 

答えて

1

--uploader "xxxxxxx-xxxxx xxxxxxx ([email protected])" 
+0

Gerritからこのストリームを取得しますが、Gerritでコメントを追加したときになぜこの問題が発生するのですか?それは私が理解できない問題です – Philippxp

+0

なぜやらないのですか''' .join(a)' --uploader'の引数はありますか?ジェリットも同じことをしていると思います。それほど難しいことではありません。 – neurino

+0

スクリプトが始まる前に引数を移動し、この回避策を実行します。 – Philippxp

2
オプション名として

と値がスペースで区切られている場合は、値自体にスペースが含まれている場合は、値を引用符で囲む必要があります。あなたが--uploader xxxxxxx-xxxxx xxxxxxx ([email protected])を記述する場合、それらは--uploader

0

に関連付けられていないよう

、最後の2つの文字列が実際にあなたがまたそれとしてgnu_getoptを()を使用して検討することができるライン

optlist, args = getopt.getopt(sys.argv[1:], '', need) 

からargsになってしまいますオプションと非オプションの引数を混在させることができます。 Documentation

からのgetopt()関数は、すぐに非オプションの引数はあなたがgnu_getoptを使用する場合は、オプションの残りの部分は、すなわちコミットとpathsetはまだ意志

に遭遇しているとして処理オプションを停止しますアップローダの引数に引用符がない場合でも正しく解析されます

+0

Thx。これは良いヒントですが、これはオプションではありません。 – Philippxp

関連する問題