查找并稍后删除zombie informatica对象

时间:2014-04-23 20:10:26

标签: informatica

是否有一种简单的方法来识别和清理未使用的信息工件?

上下文:在其中一个项目中,有很多僵尸会话/映射等,创作者早已不复存在。

我想执行以下操作:

  1. 列出/删除与工作流程无关的所有会话。
  2. 列出/删除任何会话/ wf中未使用的所有映射。
  3. 列出/删除未在任何映射中使用的所有源/目标。
  4. 列出/删除过去一年未运行的所有工作流程。
  5. 有人提到过使用:Designer>工具>查询。我无法用给出的选项表达上述1/2/3/4,是否有人可以解释一下?

    注意:

    • 我不是一个接一个地找点击查找依赖项。
    • 我不是要将整个工厂下载为xml和搜索依赖项 一个接一个

1 个答案:

答案 0 :(得分:0)

由于使用PowerCenter本身并不容易实现,我试图想出一些简单的工具来解决它。有关完整说明和下载链接,请转到this page

您可以在下方找到评论中发布的代码:Find and later delete zombie informatica objects

随意使用,分享和改进:)任何代码审查也将非常感激。

import subprocess
import os
from subprocess import *
import platform
import sys
import getpass
import configparser


#global variables declarations
currentDir=''
pmrepPath=''
domainFile=''


def connect_to_repo(Repository,Domain,User,Host,Port, UserSecurityDomain):
        #password = raw_input("Enter password for Repository: " + Repository + ", User: " + User)
        password = getpass.getpass()
        print "\nConnecting..."
        if Domain != '':
            RepoCommand="pmrep connect -r "+Repository+" -d "+Domain+" -n "+User + " -x " + password #+" -X DOMAIN_PWD"
        else:
            RepoCommand="pmrep connect -r "+Repository+" -n "+User + " -x " + password + " -h " + Host + " -o " + Port
        if UserSecurityDomain != '':
            RepoCommand += " -s " + UserSecurityDomain
        RepoCommand=RepoCommand.rstrip()
        p=subprocess.Popen(RepoCommand,stderr=subprocess.PIPE,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
        out,err=p.communicate()
        if p.returncode or err:
                print "Connection Failed"
                print err.strip()
                print out.strip()
                sys.stdout.flush()
                sys.stdin.flush()
        else:
                print "Connection Successful"
                sys.stdout.flush()
                sys.stdin.flush()
        return p.returncode

def execute_pmrep_command(command, output_file_name, start_line, end_line, line_prefix):
        if len(line_prefix)>0: line_prefix+=' '
        out=open(output_file_name,'a')
        return_code=subprocess.Popen(command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
        output,error=return_code.communicate()
        for line in output.split('\r\n')[start_line:end_line]:
            out.writelines(line_prefix + line + '\n')

        out.close()
        return

def check_platform():
        global domainFile
        global currentDir
        global pmrepPath
        global platForm
        platForm=platform.system()
        print "Platform recognized : "+platForm
##        if not os.getenv('INFA_HOME', 'C:\\Informatica\\9.5.1'):
##                print "INFA_HOME env_variable not set in your "+platForm+" platform."
##                print "Please set INFA_HOME and continue."
##                raw_input()
##                sys.exit(0)
        if not os.getenv('INFA_DOMAINS_FILE'):
                print "INFA_DOMAINS_FILE env_variable not set in your "+platForm+" platform."
                print "Please set INFA_DOMAINS_FILE and continue."
                raw_input()
                sys.exit(0)
##        elif not os.getenv('DOMAIN_PWD', 'vic'):
##                print "DOMAIN_PWD env variable not set in your "+platForm+" platform."
##                print "Please set DOMAIN_PWD and continue."
##                raw_input()
##                sys.exit(0)

##        else:
##                if platForm == 'Windows':
##                        pmrepPath=os.getenv('INFA_HOME').strip()+"\clients\PowerCenterClient\client\\bin"
##                elif platForm == 'Linux':
##                        pmrepPath=os.getenv('INFA_HOME').strip()+"/server/bin"
##                currentDir=os.getcwd()
##                domainFile=os.getenv('INFA_DOMAINS_FILE','C:\\Informatica\\9.5.1\\domains.infa').strip()


config = configparser.RawConfigParser()
config.optionxform = lambda option: option
config.read('InfaRepo_ListUnusedObjects.cfg')

infaDir = config.get('Common', 'infaDir').strip()
Repository = config.get('Common', 'Repository').strip()
Domain = config.get('Common', 'Domain').strip()
Host = config.get('Common', 'Host').strip()
Port = config.get('Common', 'Port').strip()
Folder = config.get('Common', 'Folder').strip()
User = config.get('Common', 'User').strip()
UserSecurityDomain = config.get('Common', 'UserSecurityDomain').strip()
objectTypeList = config.get('Common', 'objectTypeList').split(',')

if Domain != '':
    print 'Domain provided, will be used to connect.'
else:
    print 'Domain not provided, Host and Port will be used to connect.'

for i in range(len(objectTypeList)):
    objectTypeList[i]=objectTypeList[i].strip()

currentDir=os.getcwd()
outputDir=currentDir+'\\UnusedObjectsReport'

###objectTypeList = ['mapplet', 'mapping', 'session', 'source', 'target', 'worklet']
##
##objectTypeList = ['target']

pmrepPath=infaDir.strip()+"\clients\PowerCenterClient\client\\bin"

os.chdir(pmrepPath)

outFile = outputDir + "\ListOfUnusedObjects.txt"
if not os.path.exists(os.path.dirname(outFile)):
        os.makedirs(os.path.dirname(outFile))
print 'Output file: ' + outFile
open(outFile,'w').writelines("Domain :  "+Domain+"\nRepository : "+Repository+"\nUserName : "+User+"\n")
open(outFile,'a').writelines("***************************"+"\n")
open(outFile,'a').writelines("LIST OF UNUSED OBJECTS:\n")

outBatchFile = outputDir + "\DeleteUnusedObjects.bat"
tempDir = outputDir + "\\temp"
if not os.path.exists(outputDir):
        os.makedirs(outputDir)
if not os.path.exists(tempDir):
        os.makedirs(tempDir)

for tempFile in os.listdir(tempDir):
    os.remove(os.path.join(tempDir, tempFile))

print 'Output batch file: ' + outBatchFile

if Domain != '':
    RepoCommand="pmrep connect -r "+Repository+" -d "+Domain+" -n "+User
else:
    RepoCommand="pmrep connect -r "+Repository+" -n "+User + " -h " + Host + " -o " + Port
if UserSecurityDomain != '':
    RepoCommand += " -s " + UserSecurityDomain

open(outBatchFile,'w').writelines(pmrepPath+"\\"+RepoCommand+"\n")

objectTypeCounter=0

return_code=connect_to_repo(Repository,Domain,User,Host,Port,UserSecurityDomain)

objDepDict={}

error = False
errorList = []

#check if repository connection is successfull
if return_code==0:
    for objectType in objectTypeList:
        objectTypeCounter+=1
        print "Step {0} of {1}: {2}".format(objectTypeCounter, len(objectTypeList), objectType)

        objectFile = tempDir + "\\" + objectType + ".txt"
        open(objectFile,'w').writelines("")

        objectDepFile = tempDir + "\\" + objectType + "_dep.txt"
        open(objectDepFile,'w').writelines("")
        execute_pmrep_command("pmrep listobjects -f " + Folder + " -o " + objectType, objectFile, 8, -4, '')

        objectList=open(objectFile).readlines()

        objectCounter=0

        if len(objectList) == 0:
            print '\tNo {0}s found'.format(objectType)

        elif objectList[0][:3] == ' [[':
            error=True
            for line in objectList:
                errorList += [line.replace('\n','')]
            break


        for line in objectList:
                objectCounter+=1
                fields=line.split(' ')
                if len(fields) == 2:
                    objectType=fields[0]
                    objectName=fields[1][:-1]
                else:
                    objectType=fields[0]
                    objectName=fields[2][:-1]
                    #if the object is non-reusable, it obviously is in some workflow, so skipp it
                    if fields[1] == 'non-reusable':
                        print "\t{0} {1} of {2}: {3} is not a reusable {4} - skipping".format(objectType, objectCounter, len(objectList), objectName, objectType)
                        continue

                command = "pmrep listobjectdependencies -f " + Folder + " -n " + objectName + " -o " + objectType + " -p parents"
                #print "Getting object dependencies for " + objectType + " " + objectName

                print "\t{0} {1} of {2}: {3}".format(objectType, objectCounter, len(objectList), objectName)

                execute_pmrep_command(command, objectDepFile, 8, -6, objectName)

        #find unused objects

        for fileLine in open(objectDepFile,'r').readlines():
            line = fileLine.split(' ')
            if len(line) == 3:
                Name = line[0]
                ParentType = line[1]
                ParentName = line[2]
            else:
                Name = line[0]
                ParentType = line[1]
                ParentName = line[3]
            try:
                objDepDict[objectType + ': ' + Name]+=[ParentType + ' ' + ParentName]
            except:
                objDepDict[objectType + ': ' + Name]=[ParentType + ' ' + ParentName]


    found = False
    for objectKey in objDepDict.iterkeys():
        objectType, objName = objectKey.replace(' ','').split(':')
        if len(objDepDict[objectKey]) <= 1:
            if not found:
                print '\n'
                print 'Following unused objects have been found:'
                found = True
            print '\t{0}: {1}'.format(objectType, objName)
            open(outFile,'a').writelines('{0}: {1}\n'.format(objectType, objName))
            open(outBatchFile,'a').writelines("rem {0}\\pmrep deleteobject -f {1} -o {2} -n {3}\n".format(pmrepPath, Folder, objectType, objName))

    execute_pmrep_command('pmrep cleanup', 'log.txt', 0, 0, '')

    open(outBatchFile,'a').writelines(pmrepPath+'\\pmrep cleanup' + '\n')

    if not error:
        if not found:
            print 'No unused objects found.'
        print '\nDone.'
        print 'Output file: ' + outFile
        print 'Output batch file: ' + outBatchFile
    else:
        print 'Following errors occured:'
        for e in errorList:
            print '\t', e
#wait for key press
print '\nHit Enter to quit...'
raw_input()
#End of program