为什么我的字典不长?

2024-06-09 09:14:28 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:

我已经按照要求更新了下面的代码以反映完整的文件。你知道吗


我用布料做一些工作:

#! /usr/bin/python25
import os
import subprocess
import ConfigParser

from fabric.api import env, execute, parallel, \
                       run, runs_once, task

# --- sudo consts --- #
# relative path, regardless of where we're executing from
RELATIVE_PATH = os.path.abspath(os.path.dirname(__file__))
BASE_SERVER_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.serv.cfg')
BASE_CONFIG_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.base.cfg')
# --- end consts --- #

# --- globals because why not --- #
# did we specify a server override file?
subServSpecified = False

# dict of host objects
hostObjs = {}
# --- end globals --- #

@task
def SetHosts(hostlist):
    env.hosts = list(hostlist)

@task
@parallel
def DoWork(cmd):
    hostObjs[env.host].cmdResults[cmd] = run(cmd)
    print env.host
    print cmd
    print hostObjs[env.host].cmdResults.items()
    print len(hostObjs[env.host].cmdResults)
    print "*" * 50

class Host:
    fqdn = ''

    # dict {'command string': 'result string'}
    cmdResults = {}
    desiredResults = []    

    def __init__(self, fqdn):
        self.fqdn = fqdn
        hostObjs[self.fqdn] = self

def CreateHosts(servlist, commands, desiredResults):
    for serv in servlist:
        h = Host(serv)
        h.desiredResults = list(desiredResults)

baseConfigParser = ConfigParser.SafeConfigParser()
baseConfigParser.read(BASE_CONFIG_FILE)
runlist = baseConfigParser.get('Config List', 'runlist').strip().split("\n")
print runlist

baseServConfigParser = ConfigParser.SafeConfigParser()
baseServConfigParser.read(BASE_SERVER_FILE)
servlist = baseServConfigParser.get('Server List', 'servers').strip().split("\n")
print servlist

for subDir in runlist:
    print subDir
    relativeRunPath = os.path.join(RELATIVE_PATH, 'cfg', subDir)
    subConfigParser = ConfigParser.SafeConfigParser()
    subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
    commands = subConfigParser.get('Command List', 'commands').strip().split("\n")
    if os.path.isfile(os.path.join(relativeRunPath, 'veri.serv.cfg')):
        subServSpecified = True
        subServConfigParser = ConfigParser.SafeConfigParser()
        subServConfigParser.read(os.path.join(relativeRunPath, 'veri.serv.cfg'))
        subServList = subServConfigParser.get('Server List', 'servers').strip().split("\n")
    else:
        subServList = servlist

    CreateHosts(subServList, commands, "succeeded!")
    execute(SetHosts, subServList)

    for cmd in commands:
        execute(DoWork, cmd)

我可以看到我的cmdResults字典实际上并没有变大:

my.serv.com
nc -zw1 another.serv.com 9988 | gawk '{print $7}'
[("nc -zw1 another.serv.com 9988 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************
my.serv.com
nc -zw1 another.serv.com 8050 | gawk '{print $7}'
[("nc -zw1 another.serv.com 8050 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************
my.serv.com
nc -zw1 another.serv.com 80 | gawk '{print $7}'
[("nc -zw1 another.serv.com 80 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************

上面的1出现在循环的每次迭代中,items()的打印输出总是一个条目。有人能看到我在这里遗漏了什么吗?你知道吗

输入文件示例:

//veri.cfg
[Command List]
commands:
; T
    nc -zw1 another.serv.com 9988 | gawk '{print $7}'

; P
    nc -zw1 another.serv.com 8050 | gawk '{print $7}'

; G
    nc -zw1 another.serv.com 80 | gawk '{print $7}'

//veri.serv.cfg
[Server List]
servers: 
    my.serv.com
    my.serv2.com

//veri.base.cfg
[Config List]
runlist:
    veri.acl

目录结构:

root
 -/cfg (veri.serv.cfg / veri.base.cfg)
 -/cfg/veri.acl (veri.cfg)
 -script.py

Tags: pathenvcmdcomosanothercfglist