def write(self, filename): if filename.endswith('.xml'): writer = XmlConfigWriter() writer.write(self._dict, fu.normalizeName(filename)) else: writer = PropConfigWriter() writer.write(self._dict, fu.normalizeName(filename))
def runSSHScatter(args, nprocs, outBufSize, errBufSize): #parse the args with ssh syntax parser = SSHArgsParser('scp') parser.parse(args) #parse the multi-host args if len(parser.posargs) != 2: raise SyntaxError( 'Expect 2 positional args, got %s: %s' %(len(parser.posargs), parser.posargs)) ##the first is the src path, the second is the all hosts dst path src = parser.posargs[0] dsts = parser.posargs[1] ##parse hosts if '@' in dsts: user, dsts = dsts.split('@') else: user = getpass.getuser() hostfile, r, dstdir = parseAddrString(dsts) hosts = fileToList(hostfile) src = normalizeName(src) dstdir = normalizeName(dstdir) sshCmds = [] for i in r: try: host = hosts[i] except IndexError: print 'Host file only have %s entries'%len(hosts) break command = 'scp %s %s %s@%s:%s'%(' '.join(parser.options), src, user, host, dstdir) sshCmds.append(SSHCmd(command, outBufSize, errBufSize)) runCommands(sshCmds, nprocs)
def ls(self, argv): if len(argv) > 1: print print 'ls [path]' sys.exit(-1) if len(argv) == 0: listFiles(normalizeName(os.curdir)) else: listFiles(normalizeName(argv[0]))
def parse(self, filename): retDict = {} tree = ET.parse(fu.normalizeName(filename)) root = tree.getroot() if 'configuration' != root.tag: raise ValueError('invalid root tag: ' + root.tag) for prop in root: if 'configuration' == prop.tag: retDict.update(self.parse(prop.text)) continue if 'property' != prop.tag: raise ValueError('invalid property tag: ' + prop.tag) key = None val = None for field in prop: if 'name' == field.tag: #name should not have child if len(list(field)) != 0: raise SyntaxError('name should not have child:' '%s' %ET.dump(field)) key = field.text if 'value' == field.tag: #value should not have child if len(list(field)) != 0: raise SyntaxError('value should not have child:' '%s' %ET.dump(field)) val = field.text if (key == None) or (val == None): raise SyntaxError('no key or value for prop:' '%s' %ET.dump(prop)) retDict[key] = val return retDict
def who(self, argv): if len(argv) < 1: print "ec2 who <regionName> <values or path>" sys.exit(-1) regionName = argv[0] #get values valuesOrPath = argv[1] if os.path.isfile(normalizeName(valuesOrPath)): values = fileToList(valuesOrPath) else: values = valuesOrPath.split(',') for i in range(len(values)): values[i] = values[i].strip() #get ec2 instances region = boto.ec2.get_region(regionName) conn = region.connect() resvList = conn.get_all_instances() for value in values: for resv in resvList: for instance in resv.instances: for key, val in instance.__dict__.iteritems(): if re.search(value, str(val)): print ('value=%s: id=%s, public=%s, private=%s' %(value, instance.id, instance.ip_address, instance.private_ip_address)) break
def moveFiles(srcpath, dstpath): #generate move list srcs = _getRangPathList(srcpath) #set up dst path dstpath = normalizeName(dstpath) if os.path.exists(dstpath): if os.path.isfile(dstpath): raise ValueError('%s is a file'%dstpath) else: os.mkdir(dstpath) #move for src in srcs: src = normalizeName(src) try: shutil.move(src, dstpath) except: print 'Failed to move %s to %s'%(src, dstpath)
def removeFiles(path): #generate remove list names = _getRangPathList(path) #remove for name in names: name = normalizeName(name) try: shutil.rmtree(name) except: try: os.remove(name) except: print 'Failed to remove: %s'%name
def parse(self, filename): retDict = {} f = open(fu.normalizeName(filename)) lineno = 1 for line in f: if line.startswith('#'): continue try: key, value = re.split('\s*=\s*', line.strip(), 1) except: print "PropConfigParser Parse Error. [%s] %s" %(lineno, line) continue retDict[key] = value f.close() return retDict
def write(self, theDict, filename): f = open(fu.normalizeName(filename), 'w') for key, value in theDict.iteritems(): f.write('%s = %s\n' %(key, value)) f.close()