def get_file(self, src_file, dst_file): sftp = SFTPv3Client(self.client) dst_fd = None src_fd = None try: dst_file = os.path.abspath(dst_file.replace('/', os.sep)) dst_dir = os.path.dirname(dst_file) if not os.path.exists(dst_dir): os.makedirs(dst_dir) dst_fd = FileOutputStream(dst_file) stats = sftp.stat(src_file) src_size = stats.size src_fd = sftp.openFileRO(src_file) size = 0 arraysize = SSHClient.BUFFER_SIZE data = jarray.zeros(arraysize, 'b') while True: moredata = sftp.read(src_fd, size, data, 0, arraysize) datalen = len(data) if moredata == -1: break if src_size - size < arraysize: datalen = src_size - size dst_fd.write(data, 0, datalen) size += datalen finally: if src_fd: sftp.closeFile(src_fd) if dst_fd: dst_fd.flush() dst_fd.close() sftp.close()
def getMsgRaw(fileName, rs): msgID = rs.getLong(1) size = rs.getInt(2) storageLocation = StoredMessage.stripOldStorageDir(rs.getString(3)) custID = rs.getInt(4) ts = rs.getTimestamp(5) receivedDate = None if ts is not None: receivedDate = Date(ts.getTime) msg = StoredMessage(custID, msgID, storageLocation, None, None, receivedDate, 0, 0, 0, 0, None, amPartId, size, size, None, None) if msg: # do some validation if long(msgId) != msg.getMessageId(): printQueue.append("consumer: ERROR: message ID " + str(msgId) + " not the same as " + str(msg.getMessageId())) #print "found message", msgId, "in DB for AM partition" msgStream = msg.getEncryptedCompressedContentStream() if msgStream: out = FileOutputStream(fileName) FileUtils.copyStream(msg.getEncryptedCompressedContentStream(), out) out.flush() out.close() #print "found message", msgId, "on disk in AM partition" return True else: printErrorToRemigrate(msgId, " not found on disk for AM partition.") return False else: printErrorToRemigrate(msgId, " not found in db for AM partition.") return False
def save_example(self, label, image_byte_array): output_file_name = label + "_" + str(java.lang.System.currentTimeMillis()) + ".png" save_path = File(self.dir_path, output_file_name).getCanonicalPath() fileos = FileOutputStream(save_path) for byte in image_byte_array: fileos.write(byte) fileos.flush() fileos.close()
def run(string, args=[], callback=None, callbackOnErr=False): def out (exit, call, inp, err): return { "exitCode": exit, "callbackReturn": call, "inputArray": inp, "errorArray": err } tmp = File.createTempFile('tmp', None) tmp.setExecutable(True) writer = FileOutputStream(tmp); writer.write(string) writer.flush() writer.close() try: process = Runtime.getRuntime().exec([tmp.getAbsolutePath()] + ([str(i) for i in args] or [])) process.waitFor() inp = BufferedReader(InputStreamReader(process.getInputStream())) err = BufferedReader(InputStreamReader(process.getErrorStream())) errFlag = False inputArray = [] errorArray = [] holder = inp.readLine() while holder != None: print holder inputArray += [holder] holder = inp.readLine() holder = err.readLine() while holder != None: errFlag = True errorArray += [holder] holder = err.readLine() tmp.delete() if errFlag: if callback and callbackOnErr: return out(1, callback(out(1, None, inputArray, errorArray)), inputArray, errorArray) else: return out(1, None, inputArray, errorArray) else: if callback: return out(0, callback(out(0, None, inputArray, [])), inputArray, []) else: return out(0, None, inputArray, []) except Exception as e: print str(e) tmp.delete() if callback and callbackOnErr: return out(3, callback(out(3, None, [], str(e).split("\n"))), [], str(e).split("\n")) else: return out(3, None, [], str(e).split("\n"))
def getMsgDxDirect(msgId, sl, fileName): storage = DXStorage(sl.getServer(), sl.getUserName(), sl.getPassword(), sl.getDomain(), "email", True, sl.getManagementServer()) out = FileOutputStream(fileName) response = storage.read(str(msgId), out) out.flush() out.close() if not response.isSuccessful(): printErrorToRemigrate(msgId, " from DX") return False else: return True
def getMsgDxDirect(someMsgId, sl, fileName, threadName): printQueue.add("debug("+ threadName +"): entering getMsgDxDirect() with someMsgId = " + str(someMsgId)) storage = DXStorage(sl.getServer(), sl.getUserName(), sl.getPassword(), sl.getDomain(), "email", True, sl.getManagementServer()) out = FileOutputStream(fileName) response = storage.read(str(someMsgId), out) printQueue.add("debug("+ threadName +"): dx response is " + str(response.toString()) + " with someMsgId = " + str(someMsgId)) out.flush() out.close() if not response.isSuccessful(): printErrorToRemigrate(someMsgId, " from DX") return False else: printQueue.add("debug("+ threadName +"): exiting successfully getMsgDxDirect() with someMsgId = " + str(someMsgId)) return True
def _get_file(self, source, dest): localfile = FileOutputStream(dest) tempstats = self._client.stat(source) remotefilesize = tempstats.size remotefile = self._client.openFileRO(source) size = 0 arraysize = 4096 data = jarray.zeros(arraysize, "b") while True: moredata = self._client.read(remotefile, size, data, 0, arraysize) datalen = len(data) if moredata == -1: break if remotefilesize - size < arraysize: datalen = remotefilesize - size localfile.write(data, 0, datalen) size += datalen self._client.closeFile(remotefile) localfile.flush() localfile.close()
def _get_file(self, remote_path, local_path): local_file = FileOutputStream(local_path) remote_file_size = self._client.stat(remote_path).size remote_file = self._client.openFileRO(remote_path) array_size_bytes = 4096 data = jarray.zeros(array_size_bytes, 'b') offset = 0 while True: read_bytes = self._client.read(remote_file, offset, data, 0, array_size_bytes) data_length = len(data) if read_bytes == -1: break if remote_file_size - offset < array_size_bytes: data_length = remote_file_size - offset local_file.write(data, 0, data_length) offset += data_length self._client.closeFile(remote_file) local_file.flush() local_file.close()
def _get_file(self, remote_path, local_path): local_file = FileOutputStream(local_path) remote_file_size = self._client.stat(remote_path).size remote_file = self._client.openFileRO(remote_path) array_size_bytes = 4096 data = jarray.zeros(array_size_bytes, 'b') offset = 0 while True: read_bytes = self._client.read(remote_file, offset, data, 0, array_size_bytes) data_length = len(data) if read_bytes == -1: break if remote_file_size - offset < array_size_bytes: data_length = remote_file_size - offset local_file.write(data, 0, data_length) offset += data_length self._client.closeFile(remote_file) local_file.flush() local_file.close()
def getMsgRaw(fileName, thisMsg, threadName): printQueue.add("debug("+ threadName +"): entering getMsgRaw() with thisMsg = " + str(thisMsg.getMessageId())) msgStream = None try: msgStream = thisMsg.getEncryptedCompressedContentStream() except: pass if msgStream is not None: try: printQueue.add("debug("+ threadName +"): processing stream with thisMsg = " + str(thisMsg.getMessageId())) out = FileOutputStream(fileName) FileUtils.copyStream(msgStream, out) out.flush() out.close() printQueue.add("debug("+ threadName +"): done processing stream with thisMsg = " + str(thisMsg.getMessageId())) return True except: printErrorToRemigrate(str(thisMsg.getMessageId()), " error reading file on AM partition.") return False else: printErrorToRemigrate(str(thisMsg.getMessageId()), " not found on disk for AM partition.") return False
def write_image_to_disk(self, image_path, image): os = FileOutputStream(image_path) ImageIO.write( image, "png", os ) os.flush() os.close()
bi = chart.createBufferedImage(600, 400) imp = ImagePlus('My Plot Title', bi) imp.show() #################### # Create SVG image # #################### #Could be open using inkscape, see https://inkscape.org #1- Get a DOMImplementation and create an XML document domImpl = GenericDOMImplementation.getDOMImplementation() document = domImpl.createDocument(None, 'svg', None) #2-Create an instance of the SVG Generator svgGenerator = SVGGraphics2D(document) #3-Draw the chart in the SVG generator bounds = Rectangle(600, 400) chart.draw(svgGenerator, bounds) #4-Select a folder to save the SVG dir = IJ.getDirectory('Where should the svg file be saved?') if(dir!=None): #5-Write the SVG file svgFile = File(dir + 'test.svg') outputStream = FileOutputStream(svgFile) out = OutputStreamWriter(outputStream, 'UTF-8') svgGenerator.stream(out, True) outputStream.flush() outputStream.close() print 'Saved in %s' % (os.path.join(dir,'test.svg'))
msg = 'Product ' + awipsWanPil + ' failed to be ingested and archived properly. Reason:\n' + str(e) resp.setMessage(msg) return attachedFilename = oup.getAttachedFilename() attachedFile = oup.getAttachedFile() if attachedFilename and attachedFile: # spaces will screw up the command line string attachedFilename = attachedFilename.replace(" ", "") # dealing with a java byte[] so write it out with java from java.io import File, FileOutputStream attachedFilename = createTargetFile("", OUT_DIR + '/' + attachedFilename) f = File(attachedFilename) fos = FileOutputStream(f) fos.write(attachedFile) fos.flush() fos.close() messageIdToAcknowledge = None #---------- # Check if product should be distributed over WAN via NCF #---------- wmoID = contents[0:6] splitAddr = address.split(',') for addr in splitAddr: if addr != '000': # 000 is local only _Logger.info("Addressee is " + addr) #---------- # Check if product should be sent to the NWWS for uplink #---------- if (addr.find('NWWSUP') > -1):
resp.setMessage(msg) return attachedFilename = oup.getAttachedFilename() attachedFile = oup.getAttachedFile() if attachedFilename and attachedFile: # spaces will screw up the command line string attachedFilename = attachedFilename.replace(" ", "") # dealing with a java byte[] so write it out with java from java.io import File, FileOutputStream attachedFilename = createTargetFile("", OUT_DIR + '/' + attachedFilename) f = File(attachedFilename) fos = FileOutputStream(f) fos.write(attachedFile) fos.flush() fos.close() if test: try: os.remove(awipsPathname) except EnvironmentError: pass # ignore if attachedFilename: try: os.remove(attachedFilename) except EnvironmentError: pass # ignore resp.setSendLocalSuccess(True) resp.setSendWANSuccess(True)
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # from java.lang import ClassLoader from java.io import InputStreamReader, BufferedReader, OutputStreamWriter, FileOutputStream, File import java.lang.Byte import jarray import subprocess import os import array from org.apache.commons.io import IOUtils print "We are in python" fileStream = File("output") inStream = ClassLoader.getSystemClassLoader().getResourceAsStream( "exec/go/hello") out = FileOutputStream(fileStream) IOUtils.copy(inStream, out) fileStream.setExecutable(True) out.flush() out.close() function_call = subprocess.Popen(["./output"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = function_call.stdout.read() print output # print(function_call)
def write_image_to_disk(self, image_path, image): os = FileOutputStream(image_path) ImageIO.write(image, "png", os) os.flush() os.close()