Beispiel #1
0
def run(command, server):
    output = []
    error = []
    jsch = JSch()
    session = jsch.getSession("root", server, 22)
    session.setPassword("couchbase")
    session.setConfig("StrictHostKeyChecking", "no")
    try:
        session.connect(10000)
    except JSchException:
        failed.append(server)
    try:
        _ssh_client = session.openChannel("exec")
        _ssh_client.setInputStream(None)
        _ssh_client.setErrStream(None)

        instream = _ssh_client.getInputStream()
        errstream = _ssh_client.getErrStream()
        _ssh_client.setCommand(command)
        _ssh_client.connect()
        fu1 = FileUtil.wrap(instream)
        for line in fu1.readlines():
            output.append(line)
        fu1.close()

        fu2 = FileUtil.wrap(errstream)
        for line in fu2.readlines():
            error.append(line)
        fu2.close()
        _ssh_client.disconnect()
        session.disconnect()
    except JSchException as e:
        print("JSch exception on %s: %s" % (server, str(e)))
    return output, error
Beispiel #2
0
    def __init__(self, cmd, capturestderr=0, bufsize=-1):
        """The parameter 'cmd' is the shell command to execute in a
        sub-process.  Can be either a sequence of executable
        and arguments, or a shell command.
        The 'capturestderr' flag, if true, specifies that
        the object should capture standard error output of the child process.
        The default is false.  If the 'bufsize' parameter is specified, it
        specifies the size of the I/O buffers to/from the child process.
        """
        self.process = shellexecute( cmd )
        self._tochild = self.process.getOutputStream()
        self._fromchild = self.process.getInputStream()
        if capturestderr:
            self._childerr = self.process.getErrorStream()
        else:
            self._childerr = None
        import threading
        self.childWaiterLock = threading.Lock()

        if bufsize > 0:
            self._tochild = BufferedOutputStream( self._tochild, bufsize )
            self._fromchild = BufferedInputStream( self._fromchild, bufsize )
            if self._childerr:
                self._childerr = BufferedInputStream(
                    self._childerr,
                    bufsize
                    )
                
        self.tochild = FileUtil.wrap(self._tochild)
        self.fromchild = FileUtil.wrap(self._fromchild)
        if self._childerr:
            self.childerr = FileUtil.wrap(self._childerr)
Beispiel #3
0
    def __init__(self, cmd, capturestderr=0, bufsize=-1):
        """The parameter 'cmd' is the shell command to execute in a
        sub-process.  Can be either a sequence of executable
        and arguments, or a shell command.
        The 'capturestderr' flag, if true, specifies that
        the object should capture standard error output of the child process.
        The default is false.  If the 'bufsize' parameter is specified, it
        specifies the size of the I/O buffers to/from the child process.
        """
        self.process = shellexecute(cmd)
        self._tochild = self.process.getOutputStream()
        self._fromchild = self.process.getInputStream()
        if capturestderr:
            self._childerr = self.process.getErrorStream()
        else:
            self._childerr = None
        import threading
        self.childWaiterLock = threading.Lock()

        if bufsize > 0:
            self._tochild = BufferedOutputStream(self._tochild, bufsize)
            self._fromchild = BufferedInputStream(self._fromchild, bufsize)
            if self._childerr:
                self._childerr = BufferedInputStream(self._childerr, bufsize)

        self.tochild = FileUtil.wrap(self._tochild)
        self.fromchild = FileUtil.wrap(self._fromchild)
        if self._childerr:
            self.childerr = FileUtil.wrap(self._childerr)
def run(command, session):
    output = []
    error = []
    try:
        _ssh_client = session.openChannel("exec")
        _ssh_client.setInputStream(None)
        _ssh_client.setErrStream(None)

        instream = _ssh_client.getInputStream()
        errstream = _ssh_client.getErrStream()
        _ssh_client.setCommand(command)
        _ssh_client.connect()
        fu1 = FileUtil.wrap(instream)
        for line in fu1.readlines():
            output.append(line)
        fu1.close()

        fu2 = FileUtil.wrap(errstream)
        for line in fu2.readlines():
            error.append(line)
        fu2.close()
        _ssh_client.disconnect()
    except JSchException as e:
        print("JSch exception on %s: %s" % (server, str(e)))
    return output, error
Beispiel #5
0
 def test_infinite_input(self):
     iis = InfiniteInputStream()
     f = FileUtil.wrap(iis, 'rb')
     size = 10000
     self.assertEqual(len(f.read(size)), size)
     self.assertEqual(len(f.read(size)), size)
     self.assertEqual(len(f.read(size)), size)
Beispiel #6
0
 def test_infinite_input(self):
     iis = InfiniteInputStream()
     f = FileUtil.wrap(iis, 'rb')
     size = 10000
     self.assertEqual(len(f.read(size)), size)
     self.assertEqual(len(f.read(size)), size)
     self.assertEqual(len(f.read(size)), size)
Beispiel #7
0
	def getsource(self, module):
		try:
			return inspect.getsource(module)
		except IOError:
			moduleName = module.__name__
			
			#inspect.getsource could not find the file
			#it surely means that the code is not inside a regular .py file loaded from disk by python
			#it may be a .py file loaded by a Java classloader
			#or even a .py inside a jar
			
			# go from Python module name to path and file name
			moduleName = moduleName.split('.')
			moduleName = os.path.join(*moduleName)
			moduleName += ".py"
			
			resourceURL = self.classloader.getResource(moduleName)
			if resourceURL <> None:
				protocol = resourceURL.getProtocol()
				
				if protocol == "jar":
					jarFile = resourceURL.openConnection().getJarFile()
					jarEntry = resourceURL.openConnection().getJarEntry()
					inputStream = jarFile.getInputStream(jarEntry)
			
					from org.python.core.util import FileUtil
					f = FileUtil.wrap(inputStream)
					return f.read(-1) # read until EOF
				elif protocol == "file":
					fileName = resourceURL.getFile()
					return "".join(linecache.getlines(fileName))
				else:
					raise IOError("Cannot load module %s whose resource %s is not a file nor a jar" %(moduleName, resourceURL))
			else:
				raise IOError("Cannot load module %s: not found by inspect module, and not found in java classloader" %(moduleName))
Beispiel #8
0
def getResource(path):
    if isJython():
        import org.mozilla.jydoop.PythonValue as PythonValue
        import org.python.core.util.FileUtil as FileUtil
        f = FileUtil.wrap(PythonValue().getClass().getClassLoader().getResourceAsStream(path))
    else:
        # Python case
        f = open(path, 'r')
    return f.read()
Beispiel #9
0
 def _join(self, stdout, stderr, bufsize):
     """create a stream that joins two output streams"""
     self._pipeOut = PipedOutputStream()
     joinedStream = PipedInputStream(self._pipeOut)
     self._outReader = _makeReaderThread(stdout, self._pipeOut.write,
                                         bufsize,
                                         "%s-stdout" % self.process,
                                         self._close)
     self._errReader = _makeReaderThread(stderr, self._pipeOut.write,
                                         bufsize,
                                         "%s-stderr" % self.process,
                                         self._close)
     return FileUtil.wrap(joinedStream)
Beispiel #10
0
 def _testCmds(self, _shellEnv, testCmds, whichEnv):
     """test commands (key) and compare output to expected output (value).
     this actually executes all the commands twice, testing the return
     code by calling system(), and testing some of the output by calling
     execute()
     """
     for cmd, pattern in testCmds:
         dprint("\nExecuting '%s' with %s environment" % (cmd, whichEnv))
         p = javashell.shellexecute(cmd)
         line = FileUtil.wrap(p.getInputStream()).readlines()[0]
         assert re.match( pattern, line ), \
                 "expected match for %s, got %s" % ( pattern, line )
         dprint("waiting for", cmd, "to complete")
         assert not p.waitFor(), \
                 "%s failed with %s environment" % (cmd, whichEnv)
Beispiel #11
0
 def _testCmds( self, _shellEnv, testCmds, whichEnv ):
     """test commands (key) and compare output to expected output (value).
     this actually executes all the commands twice, testing the return
     code by calling system(), and testing some of the output by calling
     execute()
     """
     for cmd, pattern in testCmds:
         dprint( "\nExecuting '%s' with %s environment" % (cmd, whichEnv))
         p = javashell.shellexecute(cmd)
         line = FileUtil.wrap(p.getInputStream()).readlines()[0]
         assert re.match( pattern, line ), \
                 "expected match for %s, got %s" % ( pattern, line )
         dprint( "waiting for", cmd, "to complete")
         assert not p.waitFor(), \
                 "%s failed with %s environment" % (cmd, whichEnv)
Beispiel #12
0
 def _join( self, stdout, stderr, bufsize ):
     """create a stream that joins two output streams"""
     self._pipeOut = PipedOutputStream()
     joinedStream = PipedInputStream( self._pipeOut )
     self._outReader = _makeReaderThread(
         stdout,
         self._pipeOut.write,
         bufsize,
         "%s-stdout" % self.process,
         self._close
         )
     self._errReader = _makeReaderThread(
         stderr,
         self._pipeOut.write,
         bufsize,
         "%s-stderr" % self.process,
         self._close
         )
     return FileUtil.wrap(joinedStream)
    else:
        props.remove(key)

print " *** Update Marc Prefix *** "

# setup namespaces
ElementTree._namespace_map["http://www.loc.gov/MARC21/slim"] = "marc"
ElementTree._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = "xsi"

# parse the marc payload and reserialize with proper namespaces
props = object.getMetadata()
try:
    #sourcePayload = object.getPayload(object.getSourceId())
    sourcePayload = object.getPayload("DS1")
    tree = ElementTree.ElementTree()
    elem = tree.parse(FileUtil.wrap(sourcePayload.open()))
    xml = ElementTree.tostring(elem)
    sourcePayload.close()

    # save to new payload
    xmlBytes = ByteArrayInputStream(StringUtil.toBytes(xml))
    
    try:
        newPayloadId = "MARC"
        props.setProperty("modified", "true");
        #Can be processing multiple payload...
        props.setProperty("payloadToDelete.1", "DS1")
        props.setProperty("payloadToExport.1", "MARC")
        fixedPayload = object.createStoredPayload(newPayloadId, xmlBytes)
    except:
        print "Fail to create new payload, '%s' is already exist" % newPayloadId
# channel.setXForwarding(true);

#channel.setInputStream(System.in);
channel.setInputStream(None);

#channel.setOutputStream(System.out);

#FileOutputStream fos=new FileOutputStream("/tmp/stderr");
#((ChannelExec)channel).setErrStream(fos);
channel.setErrStream(System.err);
instream=channel.getInputStream();

channel.connect();


fu = FileUtil.wrap(instream)

for line in fu.readlines():
print line

if channel.isClosed():
    print "exit-status: "+channel.getExitStatus()

try:
    Thread.sleep(1000)
except:
    print e

channel.disconnect();
session.disconnect();
Beispiel #15
0
 def __init__(self, stream, process, name):
     self._file = FileUtil.wrap(stream)
     self._process = process
Beispiel #16
0
        session.connect(5000)
    except JSchException:
        failed.append(server)
        continue
    try:
        _ssh_client = session.openChannel("exec")
        _ssh_client.setInputStream(None)
        _ssh_client.setErrStream(None)

        instream = _ssh_client.getInputStream()
        errstream = _ssh_client.getErrStream()
        _ssh_client.setCommand("free -m")
        _ssh_client.connect()
        output = []
        error = []
        fu = FileUtil.wrap(instream)
        for line in fu.readlines():
            output.append(line)
        fu.close()

        fu1 = FileUtil.wrap(errstream)
        for line in fu1.readlines():
            error.append(line)
        fu1.close()
        try:
            print "{}) {}:{}".format(i, server, output[1].split()[1])
        except:
            print "{}: Not a linux machine".format(server)
        i += 1
        _ssh_client.disconnect()
        session.disconnect()
Beispiel #17
0
 def __init__(self, stream, process, name):
     self._file = FileUtil.wrap(stream)
     self._process = process