def backupSWUtilizationConfig(Framework): # keep remote ini files BASEDIR = AgentPlatformParameters.getAgentConfigurationPath(Framework) localDiscusgeFile = File.createTempFile( "discusge" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remoteDiscusgeFile = BASEDIR + "discusge.ini" if InventoryUtils.copyRemoteFileToLocal( Framework, remoteDiscusgeFile, localDiscusgeFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_discusge_temp_file", localDiscusgeFile.getCanonicalPath()) else: Framework.reportWarning( "backup discusge.ini file in remote server failed, upgrade agent will use default configuration." ) localPluginFile = File.createTempFile( "plugin" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remotePluginFile = BASEDIR + "plugin.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remotePluginFile, localPluginFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_plugin_temp_file", localPluginFile.getCanonicalPath()) else: Framework.reportWarning( "backup discusge.ini file in remote server failed, upgrade agent will use default configuration." ) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def register_script(self): """ Registers a pig scripts with its variables substituted. raises: IOException If a temp file containing the pig script could not be created. raises: ParseException The pig script could not have all its variables substituted. todo: Refactor this processes that result in calling this method. This method gets called twice for a single assert as every method that needs the data assumes no one else has called it (even methods that call other methods that call it (assertOutput() calls get_alias() which both call this method). """ pigIStream = BufferedReader(StringReader(self.orig_pig_code)) pigOStream = StringWriter() ps = ParameterSubstitutionPreprocessor(50) # Where does 50 come from? ps.genSubstitutedFile(pigIStream, pigOStream, self.args, self.arg_files) substitutedPig = pigOStream.toString() f = File.createTempFile("tmp", "pigunit") pw = PrintWriter(f) pw.println(substitutedPig) pw.close() pigSubstitutedFile = f.getCanonicalPath() self._temp_pig_script = pigSubstitutedFile self.pig.registerScript(pigSubstitutedFile, self.alias_overrides)
def select2(self, sql, rowCallback): ''' Select rows calling a user supplied method for each row. The callback method must have a single argument -- a list of values -- one for each column. Returns the number of rows found. ''' tmpFile = File.createTempFile("SQLForceSelect", ".tmp") tmpFile.deleteOnExit(); sql = sql.lstrip() selectxPattern = re.compile(r'^SELECTX', re.IGNORECASE) if not selectxPattern.match(sql): sql = "SELECTX" + sql[6:] self._session.runCommands(sql + " OUTPUT '" \ + tmpFile.getAbsolutePath() + "'") fSelect = open(tmpFile.getAbsolutePath(), "r"); parser = make_parser() handler = self.SelectHandler(rowCallback) parser.setContentHandler(handler) parser.parse(fSelect) fSelect.close() tmpFile.delete(); return self.getenv("ROW_COUNT")
def bash(*args): """Execute in a bash shell. There are two ways to call this, first as just 'bash' this executes the text and prints the output. Secondly, bash("varname"), executes the text and sets varname equal to this output. """ if (len(args) == 3): a, b, c = args ss = b % _getNamespace(2) print ss pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) print ex.getOutput() return ex.getOutput() else: def doBash(a, b, c): ss = b % _getNamespace(2) pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) c[args[0]] = ex.getOutput() return ex.getOutput() return doBash
def __persist_model(model, model_context): """ Save the model to the specified model file name or to the archive if the file name was not specified. :param model: the model to save :param model_context: the model context :raises DiscoverException: if an error occurs while create a temporary file for the model or while adding it to the archive :raises TranslateException: if an error occurs while serializing the model or writing it to disk """ _method_name = '__persist_model' __logger.entering(class_name=_class_name, method_name=_method_name) add_to_archive = False model_file_name = model_context.get_model_file() if model_file_name is None: add_to_archive = True try: domain_name = model_context.get_domain_name() model_file = File.createTempFile(domain_name, '.yaml').getCanonicalFile() model_file_name = model_context.get_domain_name() + '.yaml' except (IllegalArgumentException, IOException), ie: ex = exception_helper.create_discover_exception( 'WLSDPLY-06008', ie.getLocalizedMessage(), error=ie) __logger.throwing(ex, class_name=_class_name, method_name=_method_name) raise ex
def rasterize(cls, dom, width): """ generated source for method rasterize """ imagePointer = [None]*1 # Rendering hints can't be set programatically, so # we override defaults with a temporary stylesheet. css = "svg {" + "shape-rendering: geometricPrecision;" + "text-rendering: geometricPrecision;" + "color-rendering: optimizeQuality;" + "image-rendering: optimizeQuality;" + "}" cssFile = File.createTempFile("batik-default-override-", ".css") FileUtils.writeStringToFile(cssFile, css) transcoderHints = TranscodingHints() transcoderHints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE) transcoderHints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation()) transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI) transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg") transcoderHints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().__str__()) transcoderHints.put(ImageTranscoder.KEY_WIDTH, float(2 * width)) transcoderHints.put(ImageTranscoder.KEY_HEIGHT, float(2 * width)) transcoderHints.put(ImageTranscoder.KEY_MAX_HEIGHT, float(2 * width)) transcoderHints.put(ImageTranscoder.KEY_MAX_WIDTH, float(2 * width)) try: t.setTranscodingHints(transcoderHints) t.transcode(input, None) except TranscoderException as ex: # Requires Java 6 ex.printStackTrace() raise IOException("Couldn't convert SVG") finally: cssFile.delete() return imagePointer[0]
def bash(*args): """Execute in a bash shell. There are two ways to call this, first as just 'bash' this executes the text and prints the output. Secondly, bash("varname"), executes the text and sets varname equal to this output. """ if (len(args)==3): a,b,c = args ss = b % _getNamespace(2) print ss pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) print ex.getOutput() return ex.getOutput() else: def doBash(a,b,c): ss = b % _getNamespace(2) pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) c[args[0]]=ex.getOutput() return ex.getOutput() return doBash
def select2(self, sql, rowCallback): ''' Select rows calling a user supplied method for each row. The callback method must have a single argument -- a list of values -- one for each column. Returns the number of rows found. ''' tmpFile = File.createTempFile("SQLForceSelect", ".tmp") tmpFile.deleteOnExit() sql = sql.lstrip() selectxPattern = re.compile(r'^SELECTX', re.IGNORECASE) if not selectxPattern.match(sql): sql = "SELECTX" + sql[6:] self._session.runCommands(sql + " OUTPUT '" \ + tmpFile.getAbsolutePath() + "'") fSelect = open(tmpFile.getAbsolutePath(), "r") parser = make_parser() handler = self.SelectHandler(rowCallback) parser.setContentHandler(handler) parser.parse(fSelect) fSelect.close() tmpFile.delete() return self.getenv("ROW_COUNT")
def popTempFiles(): global TEMP_FILES for dir in SFConstants.TEMP_DATA_DIRS: dir = File(dir) tmpf = File.createTempFile("cached" + str(os.getpid()), ".pool", dir) fileName = tmpf.getCanonicalPath() TEMP_FILES[dir.getCanonicalPath()] = fileName tmpf.deleteOnExit()
def rawPython(a,b,c): """Execute the text as a new CPython process""" tmpFile = File.createTempFile("fieldRawPython", ".py", None) writer = BufferedWriter(FileWriter(tmpFile)) writer.write(b, 0, len(b)) writer.close() com = ExecuteCommand(".", ("/System/Library/Frameworks/Python.framework/Versions/Current/bin/python", tmpFile.getAbsolutePath()),1) com.waitFor(1) print com.getOutput()
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 doBash(a,b,c): ss = b % _getNamespace(2) pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) c[args[0]]=ex.getOutput() return ex.getOutput()
def doBash(a, b, c): ss = b % _getNamespace(2) pp = File.createTempFile("field_tmpBash", "").getAbsolutePath() f = file(pp, "w") print >> f, ss f.close() ex = ExecuteCommand(".", ["/bin/bash", pp], 1) ex.waitFor(1) c[args[0]] = ex.getOutput() return ex.getOutput()
def rawPython(a, b, c): """Execute the text as a new CPython process""" tmpFile = File.createTempFile("fieldRawPython", ".py", None) writer = BufferedWriter(FileWriter(tmpFile)) writer.write(b, 0, len(b)) writer.close() com = ExecuteCommand(".", ( "/System/Library/Frameworks/Python.framework/Versions/Current/bin/python", tmpFile.getAbsolutePath()), 1) com.waitFor(1) print com.getOutput()
def backupSWUtilizationConfig(Framework): # keep remote ini files BASEDIR = AgentPlatformParameters.getAgentConfigurationPath(Framework) localDiscusgeFile = File.createTempFile("discusge" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remoteDiscusgeFile = BASEDIR + "discusge.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remoteDiscusgeFile, localDiscusgeFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_discusge_temp_file", localDiscusgeFile.getCanonicalPath()) else : Framework.reportWarning("backup discusge.ini file in remote server failed, upgrade agent will use default configuration.") localPluginFile = File.createTempFile("plugin" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remotePluginFile = BASEDIR + "plugin.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remotePluginFile, localPluginFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_plugin_temp_file", localPluginFile.getCanonicalPath()) else : Framework.reportWarning("backup discusge.ini file in remote server failed, upgrade agent will use default configuration.") Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def rasterizeLines(something = None, bounds = None, background = (0,0,0,0), scale = 1): if (not something): something = getSelf().lines pdfContext = BasePDFGraphicsContext() SimplePDFLineDrawing().installInto(pdfContext) if (bounds==None): for n in something: bounds = Rect.union(LineUtils().slowBounds(n),bounds) bounds.x-=2 bounds.y-=2 bounds.w+=4 bounds.h+=4 pdfContext.paperWidth=bounds.w pdfContext.paperHeight=bounds.h SimplePDFLineDrawing.outputTransform.z=(-bounds.x)*pdfContext.paperWidth/bounds.w SimplePDFLineDrawing.outputTransform.w=(bounds.y+bounds.h)*pdfContext.paperWidth/bounds.w SimplePDFLineDrawing.outputTransform.x=pdfContext.paperWidth/bounds.w SimplePDFLineDrawing.outputTransform.y=-pdfContext.paperWidth/bounds.w name = File.createTempFile("field", ".pdf", None) pdfContext.drawTo = name pdfContext.windowDisplayEnter() for n in something: pdfContext.submitLine(n, n.getProperties()) pdfContext.windowDisplayExit() print "rasterizing: make sure to run 'defaults write com.apple.versioner.python Prefer-32-Bit -bool yes' from terminal for this to work" print "... about to execute command %s " % ( ("/usr/bin/python", "raster.py", name.getAbsolutePath(), name.getAbsolutePath().replace(".pdf",".tif"), "1", "%s"%background[0],"%s"%background[1], "%s"%background[2], "%s"%background[3]),) ExecuteCommand(".", ("/usr/bin/python", "raster.py", name.getAbsolutePath(), name.getAbsolutePath().replace(".pdf",".tif"), "%s"%scale, "%s"%background[0],"%s"%background[1], "%s"%background[2], "%s"%background[3]), 1).waitFor() print "... loading from %s " % name.getAbsolutePath().replace(".pdf",".tif"); i = image("file://"+name.getAbsolutePath().replace(".pdf",".tif")) i.defaultPosition = Vector2(bounds.x, bounds.y) i.defaultScale = scale return i
def __index(self, emailInfo): from org.apache.lucene.index import IndexWriterConfig from org.apache.lucene.util import Version from org.apache.lucene.analysis.standard import StandardAnalyzer analyser = StandardAnalyzer(Version.LUCENE_33) conf = IndexWriterConfig(Version.LUCENE_33, analyser) from org.apache.lucene.store import FSDirectory from java.io import File storage = File.createTempFile(u'Tubelight-', '.index') storage.delete() storage.mkdir() storage.deleteOnExit() self.storage = storage.getAbsolutePath() from java.io import File self.session.setAttribute('directory', storage.getAbsolutePath()+File.separator+'mail.idx') directory = FSDirectory.open(storage) from org.apache.lucene.index import IndexWriter iw = IndexWriter(directory, conf) from us.d8u.tubelight import Configuration addr = emailInfo[Configuration.EmailAddressKey] (username, server) = addr.split('@') from java.lang import System System.setProperty("mail.imap.partialfetch", "false") urlPrefix = (("imap://%s@%s:%d/Inbox") % (username, server, int(emailInfo[Configuration.EmailPortKey]))) from javax.mail import Session session = Session.getDefaultInstance(System.getProperties(), None).getStore(h.get(Configuration.EmailProtocolKey)) session.connect(server, username, emailInfo[Configuration.EmailPasswordKey]) folder = session.getDefaultFolder() for m in folder.getMessages(): from org.apache.lucene.document import Document d = Document() subject = Field("subject", m.getSubject(), Field.Store.YES, Field.Index.ANALYZED) toSrc = u'' toSrc = [((u'%s, %s') % (toSrc, str(r))) for r in m.getAllRecipients()] to = Field("to", toSrc.substring(toSrc.indexOf(u',')), Field.Store.YES, Field.Index.ANALYZED) d.add(to) d.add(subject) iw.addDocument(d) iw.commit() self.searcher = IndexSearcher(directory)
def dom_document_from_file(f): if hasattr(f, 'name'): documentURI = os.path.abspath(f.name) else: documentURI = None bytestring = f.read() temp = File.createTempFile('someprefix', 'tmp') outputstream = FileOutputStream(temp) try: outputstream.write(bytestring) finally: outputstream.close() inputstream = FileInputStream(temp) try: dom_doc = dom_document_from_inputstream(inputstream) dom_doc.setDocumentURI(documentURI) return dom_doc finally: inputstream.close()
def run(self, target=None): """ Runs a job using the variables specified in this class. If a target isn't specified, self is used for the job. """ # did the initializer get called? if not hasattr(self, "_initialized"): raise Exception("HappyJob.__init__() must be called before HappyJob.run()") # are we in a job? global job if job is not None: raise Exception('run() cannot be called inside of a running job. Did you check if __name__=="__main__"?') # by default, we try to run ourselves if no target is given. if target is None: target = self # sanity checking: if not hasattr(target.__class__, "map"): raise Exception("Target is missing map function: " + str(target)) if (self.reducetasks > 0 or self.reducetasks is None) and not hasattr(target.__class__, "reduce"): raise Exception("Target is missing reduce function: " + str(target)) jobconf = JobConf() # set the filesystem: if self.localfs: jobconf.set("fs.default.name", "file:///") # set the script: if _scriptname is not None: localscriptname = File(_scriptname).getName() jobconf.set(HappyBase.SCRIPT_KEY, localscriptname) # check if the script asks for a local job: localjob = self.localjob if localjob: jobconf.set("mapred.job.tracker", "local") # check if that is how the tracker is configured: elif jobconf.get("mapred.job.tracker") == "local": localjob = True # set the name: jobname = self.jobname if jobname is None: jobname = localscriptname + " - " + self.__class__.__name__ jobconf.setJobName(jobname) if self.maptasks is not None: jobconf.setNumMapTasks(self.maptasks) if self.reducetasks is not None: jobconf.setNumReduceTasks(self.reducetasks) # input and output paths: if self.inputpaths is None or len(self.inputpaths) == 0: raise Exception("No inputpaths specified") if not isinstance(self.inputpaths, list): inputpaths = [self.inputpaths] else: inputpaths = self.inputpaths for inputpath in inputpaths: FileInputFormat.addInputPath(jobconf, Path(inputpath)) if self.outputpath is None: raise Exception("No outputpath specified") FileOutputFormat.setOutputPath(jobconf, Path(self.outputpath)) # input formats: if self.inputformat == "text": jobconf.setInputFormat(TextInputFormat) elif self.inputformat == "keyvalue": jobconf.setInputFormat(KeyValueTextInputFormat) elif self.inputformat == "json": jobconf.setInputFormat(JSONInputFormat) elif self.inputformat == "sequence": jobconf.setInputFormat(SequenceFileInputFormat) elif self.inputformat == "auto": jobconf.setInputFormat(TextOrSequenceInputFormat) else: jobconf.setInputFormat(Class.forName(self.inputformat)) # output formats: if self.outputformat == "text": jobconf.setOutputFormat(TextOutputFormat) elif self.outputformat == "sequence": jobconf.setOutputFormat(SequenceFileOutputFormat) elif self.outputformat == "mapdir": jobconf.setOutputFormat(Class.forName("org.apache.hadoop.mapred.MapFileOutputFormat")) else: jobconf.setOutputFormat(Class.forName(self.outputformat)) # compression output: if self.compressoutput: jobconf.set("mapred.output.compress", "true") codec = None if self.compressiontype == "zlib": codec = "org.apache.hadoop.io.compress.DefaultCodec" elif self.compressiontype == "gzip": codec = "org.apache.hadoop.io.compress.GzipCodec" elif self.compressiontype == "lzo": codec = "org.apache.hadoop.io.compress.LzoCodec" if codec is not None: jobconf.set("mapred.output.compression.codec", codec) if self.sequencetype == "BLOCK" or self.sequencetype == "RECORD": jobconf.set("mapred.output.compression.type", self.sequencetype) # set the map and reduce runners: jobconf.setMapRunnerClass(HappyMap) if hasattr(target.__class__, "combine"): jobconf.setCombinerClass(HappyCombine) jobconf.setReducerClass(HappyReduce) # configure key, value types: def getOutputType(t): if t == None or t == "text": return Text elif t == "json": return JSONWritable else: return Class.forName(t) jobconf.setOutputKeyClass(getOutputType(self.outputkey)) jobconf.setOutputValueClass(getOutputType(self.outputvalue)) jobconf.setMapOutputKeyClass(getOutputType(self.mapoutputkey)) jobconf.setMapOutputValueClass(getOutputType(self.mapoutputvalue)) # speculative execution off for now: jobconf.setSpeculativeExecution(False) # serialize this object: scriptobject = PyObjectSerializer.serialize(target) scriptobject.deleteOnExit() _log.info("Job state serialized to " + scriptobject.getAbsolutePath()) if localjob: scriptobjectPath = scriptobject.getAbsolutePath() else: scriptobjectPath = scriptobject.getName() jobconf.set(HappyBase.SCRIPT_OBJECT, scriptobjectPath) # set up the happy python path: global path if localjob: jobpythonpath = [includepath for includepath in path if not includepath.endswith(".jar")] else: jobpythonpath = [File(includepath).getName() for includepath in path if not includepath.endswith(".jar")] jobconf.set(HappyBase.PATH_KEY, ":".join(jobpythonpath)) # set up other resources: resourcefiles = self.includepaths[:] resourcefiles.append(scriptobject.getAbsolutePath()) if localjob: localIncludePaths = [includepath for includepath in resourcefiles if not includepath.endswith(".jar")] else: localIncludePaths = [File(includepath).getName() for includepath in resourcefiles if not includepath.endswith(".jar")] jobconf.set(HappyBase.RESOURCE_KEY, ":".join(localIncludePaths)) # sort all of the paths into jars and files: allpaths = path + self.includepaths includeJars = [JarUtil.findContainingPath(HappyJobRunner)] includeFiles = [scriptobject.getAbsolutePath()] for includepath in allpaths: if includepath.endswith(".jar"): includeJars.append(includepath) else: includeFiles.append(includepath) # create a jar file to ship out if it isn't a local job: if not localjob: # create a temp job jar: jobJar = File.createTempFile("happy-", ".jar") jobJar.deleteOnExit() jobJarPath = jobJar.getAbsolutePath() # package it up: JarUtil.buildJar(includeJars, includeFiles, jobJar.getAbsolutePath()) jobconf.setJar(jobJarPath) # add additional job arguments: for key, value in self.jobargs.iteritems(): jobconf.set(key, value) # run the job: finishedJob = HappyJobClient.runJob(jobconf) if not finishedJob.isSuccessful(): raise Exception("Job " + jobname + " failed") # return results: return ResultSerializer.deserialize(jobconf)
print " ** Exporting modified datastream: ", value payload = object.getPayload(value) #Check if object exist objectResult = restClient.findObjects(fedoraId, 1) if not objectResult.getObjectFields().isEmpty(): try: mimeType = payload.getContentType() formatURI = None dsLocation = None dsState = "A" #Check if datastream exist print "Getting datastream for: ", value found, foundDs = checkIfDsExist(apim, fedoraId, value, dsState) if found: print " * Modifying: '%s'" % value f = File.createTempFile("fedora-", ".xml") fOut = FileOutputStream(f) IOUtils.copy(payload.open(), fOut) payload.close() fOut.close() dsLocation = client.uploadFile(f) dsId = apim.modifyDatastreamByReference(fedoraId, foundDs.getID(), foundDs.getAltIDs(), foundDs.getLabel(), \ mimeType, foundDs.getFormatURI(), dsLocation, foundDs.getChecksumType(), foundDs.getChecksum(), \ "Updating datastream from Fascinator", True) print " * Modified: '%s'" % dsId else: print " * Adding: '%s'" % value f = File.createTempFile("fedora-", ".xml") fOut = FileOutputStream(f) IOUtils.copy(payload.open(), fOut) payload.close()
def makePDF(geometry = None, bounds = None, background = None, scale=1, filename=None, is3d=0): """ makePDF exports PLines to a PDF file. Every parameter is actually optional. X{geometry} specifies a list of PLines to draw, if it's missing, then everything on the canvas gets exported (you can set .notForExport on a line to make sure it doesn't get exported). X{bounds} specifies a Rect or a PLine that encloses all of the geometry that gets drawn. It becomes the "paper size" of the PDF that's exported. If it's ommitted then the bounds of the geometry that's going to get exported is used instead. One trick is to make a PLine with a rect, then pass that PLine into here as the parameter bounds. X{background} is an optional background color. X{filename} is an optional filename (a temp file is used if this is omitted) X{scale} increases the "dpi" of the canvas, making the paper and the drawing bigger (while keeping the line thickness the same). """ if (not geometry): geometry = sum([x.lines for x in getSelf().all if hasattr(x, "lines") and x.lines], []) else: try: geometry = sum(geometry, []) except: pass something = geometry pdfContext = BasePDFGraphicsContext() if (not is3d): ld = SimplePDFLineDrawing() ld.installInto(pdfContext) SimplePDFImageDrawing().installInto(pdfContext, ld) ctx = SimplePDFLineDrawing else: ld = SimplePDFLineDrawing_3d() ld.installInto(pdfContext) #SimplePDFImageDrawing().installInto(pdfContext, ld) ctx = SimplePDFLineDrawing_3d if (bounds==None): for n in something: b = LineUtils().fastBounds(n) if (b): b = Rect(b[0].x,b[0].y, b[1].x-b[0].x, b[1].y-b[0].y) if (b.w>0 or b.h>0): print b bounds = Rect.union(b, bounds) if (not bounds): print "makePDF error: no bounds specified" return None bounds.x-=2 bounds.y-=2 bounds.w+=4 bounds.h+=4 if (isinstance(bounds, PLine)): bounds = bounds.bounds() if (isinstance(bounds, CachedLine)): bounds = bounds.bounds2() print bounds pdfContext.paperWidth=bounds.w*scale pdfContext.paperHeight=bounds.h*scale if (background): pdfContext.getGlobalProperties().paperColor = background ctx.outputTransform.z=(-bounds.x)*pdfContext.paperWidth/bounds.w ctx.outputTransform.w=(bounds.y+bounds.h)*pdfContext.paperHeight/bounds.h ctx.outputTransform.x=pdfContext.paperWidth/bounds.w ctx.outputTransform.y=-pdfContext.paperHeight/bounds.h if (not filename): name = File.createTempFile("field", ".pdf", None) else: name = File(filename) pdfContext.drawTo = name pdfContext.windowDisplayEnter() for n in something: pdfContext.submitLine(n, n.getProperties()) pdfContext.windowDisplayExit() return name.getPath()
'.*DGC LUNZ.*', output): try: # EMC inquiry tool can be downloaded from ftp://ftp.emc.com/pub/symm3000/inquiry/ but we require an older # version for it to work. The tested version: # Inquiry utility, Version V7.3-1305 (Rev 1.0) (SIL Version V7.3.1.0 (Edit Level 1305) # copy inq to targets inq = 'inq.exe' path = coll_home + "/etc/templates/commands/extension-scripts/" + inq remotePath = os_handle.executeCommand( 'cmd.exe /C echo %TEMP%').strip() if not remotePath.endswith('\\'): remotePath = remotePath + '\\' remotePath = remotePath + inq srcInq = File(path) tmpInq = File.createTempFile( srcInq.getName() + '-' + os_handle.getSession().getHost() + '-' + str(System.currentTimeMillis()), None) if not tmpInq.exists(): tmpInq.createNewFile() source = None destination = None try: source = FileInputStream(srcInq).getChannel() destination = FileOutputStream(tmpInq).getChannel() destination.transferFrom(source, 0, source.size()) finally: if source != None: source.close() if destination != None: destination.close() try:
# Output it result = pluginsTreeToString() if uploadToWiki or compareToWiki: from fiji import MediaWikiClient client = MediaWikiClient(URL) wiki = client.sendRequest(['title', PAGE, 'action', 'edit'], None) begin = wiki.find('<textarea') begin = wiki.find('>', begin) + 1 end = wiki.find('</textarea>', begin) wiki = wiki[begin:end].replace('<', '<') if wiki != result: if compareToWiki: from fiji import SimpleExecuter from java.io import File, FileWriter file1 = File.createTempFile('PluginList', '.wiki') writer1 = FileWriter(file1) writer1.write(wiki) writer1.close() file2 = File.createTempFile('PluginList', '.wiki') writer2 = FileWriter(file2) writer2.write(result) writer2.close() diff = SimpleExecuter(['git', 'diff', '--patience', '--no-index', '--src-prefix=wiki/', '--dst-prefix=local/', file1.getAbsolutePath(), file2.getAbsolutePath()]) file1.delete() file2.delete() print diff.getOutput() else: # get username and password user = None password = None
def __processRequest(self): baseUrl = "http://%s:%s%s/%s" % (request.serverName, serverPort, contextPath, portalId) depositUrl = "%s/sword/deposit.post" % baseUrl sword = SwordSimpleServer(depositUrl) try: p = request.path.split(portalId+"/"+pageName+"/")[1] # portalPath except: p = "" if p=="post": print "\n--- post ---" c = sword.getClient() c.clearProxy() c.clearCredentials() postMsg = sword.getPostMessage(); postMsg.filetype = "application/zip" postMsg.filepath = "/home/ward/Desktop/Test.zip" depositResponse = c.postFile(postMsg) return str(depositResponse) elif p=="servicedocument": #print "\n--- servicedocument ---" sdr = sword.getServiceDocumentRequest() sdr.username = formData.get("username", "test") sdr.password = formData.get("password", "test") if formData.get("test"): depositUrl += "?test=1" sd = sword.doServiceDocument(sdr) # get a serviceDocument out = response.getPrintWriter("text/xml") out.println(str(sd)) out.close() bindings["pageName"] = "-noTemplate-" return sd elif p=="deposit.post": #print "\n--- deposit --- formData='%s'" % str(formData) inputStream = formData.getInputStream() headers = {} for x in formData.getHeaders().entrySet(): headers[x.getKey()] = x.getValue() deposit = sword.getDeposit() noOp = headers.get("X-No-Op") or "false" deposit.noOp = (noOp.lower()=="true") or \ (formData.get("test") is not None) contentDisposition = headers.get("Content-Disposition", "") filename = "" if contentDisposition!="": try: filename = contentDisposition.split("filename=")[1] deposit.filename = filename except: pass slug = headers.get("Slug") if slug is not None and slug!="": deposit.slug = slug #elif filename!="": # deposit.slug = filename deposit.username = "******" deposit.password = deposit.username try: file = File.createTempFile("tmptf", ".zip") file.deleteOnExit() fos = FileOutputStream(file.getAbsolutePath()) IOUtils.copy(inputStream, fos) fos.close() print "copied posted data to '%s'" % file.getAbsolutePath() except Exception, e: print "--- Exception - '%s'" % str(e) deposit.contentDisposition = file.getAbsolutePath() #???? deposit.file = inputStream depositResponse = sword.doDeposit(deposit) id = str(depositResponse.getEntry().id) try: print #imsPlugin = PluginManager.getTransformer("ims") jsonConfig = JsonConfig() #imsPlugin.init(jsonConfig.getSystemFile()) #harvestClient = HarvestClient(jsonConfig.getSystemFile()); storagePlugin = PluginManager.getStorage(jsonConfig.get("storage/type")) #storagePlugin.init(jsonConfig.getSystemFile()) setConfigUri = self.__getPortal().getClass().getResource("/swordRule.json").toURI() configFile = File(setConfigUri) harvestConfig = JsonConfigHelper(configFile); tFile = File.createTempFile("harvest", ".json") tFile.deleteOnExit() harvestConfig.set("configDir", configFile.getParent()) harvestConfig.set("sourceFile", file.getAbsolutePath()) harvestConfig.store(FileWriter(tFile)) zipObject = GenericDigitalObject(id) zipObject.addPayload(FilePayload(file, id)) #digitalObject = imsPlugin.transform(zipObject, file) qStorage = QueueStorage(storagePlugin, tFile) qStorage.init(jsonConfig.getSystemFile()) qStorage.addObject(zipObject) if deposit.noOp: print "-- Testing noOp='true' --" else: # deposit the content pass except Exception, e: print "---" print " -- Exception - '%s'" % str(e) print "---"
# # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS # FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS. # from java.io import File from java.net import URL from org.apache.commons.io import FileUtils from com.perfectomobile.selenium import MobileDriver driver = MobileDriver(perfectomobileServer['url'], perfectomobileServer['username'], perfectomobileServer['password']) try: file = File.createTempFile("application","maf") FileUtils.copyURLToFile(URL(applicationUrl), file, 300000, 300000) driver.uploadMedia(repositoryKey, file) for key in deviceIds: device = driver.getDevice(key) device.open() device.installApplication(repositoryKey) finally: driver.quit()
def makePDF(geometry=None, bounds=None, background=None, scale=1, filename=None, is3d=0): """ makePDF exports PLines to a PDF file. Every parameter is actually optional. X{geometry} specifies a list of PLines to draw, if it's missing, then everything on the canvas gets exported (you can set .notForExport on a line to make sure it doesn't get exported). X{bounds} specifies a Rect or a PLine that encloses all of the geometry that gets drawn. It becomes the "paper size" of the PDF that's exported. If it's ommitted then the bounds of the geometry that's going to get exported is used instead. One trick is to make a PLine with a rect, then pass that PLine into here as the parameter bounds. X{background} is an optional background color. X{filename} is an optional filename (a temp file is used if this is omitted) X{scale} increases the "dpi" of the canvas, making the paper and the drawing bigger (while keeping the line thickness the same). """ if (not geometry): geometry = sum([ x.lines for x in getSelf().all if hasattr(x, "lines") and x.lines ], []) else: try: geometry = sum(geometry, []) except: pass something = geometry pdfContext = BasePDFGraphicsContext() if (not is3d): ld = SimplePDFLineDrawing() ld.installInto(pdfContext) SimplePDFImageDrawing().installInto(pdfContext, ld) ctx = SimplePDFLineDrawing else: ld = SimplePDFLineDrawing_3d() ld.installInto(pdfContext) #SimplePDFImageDrawing().installInto(pdfContext, ld) ctx = SimplePDFLineDrawing_3d if (bounds == None): for n in something: b = LineUtils().fastBounds(n) if (b): b = Rect(b[0].x, b[0].y, b[1].x - b[0].x, b[1].y - b[0].y) if (b.w > 0 or b.h > 0): print b bounds = Rect.union(b, bounds) if (not bounds): print "makePDF error: no bounds specified" return None bounds.x -= 2 bounds.y -= 2 bounds.w += 4 bounds.h += 4 if (isinstance(bounds, PLine)): bounds = bounds.bounds() if (isinstance(bounds, CachedLine)): bounds = bounds.bounds2() print bounds pdfContext.paperWidth = bounds.w * scale pdfContext.paperHeight = bounds.h * scale if (background): pdfContext.getGlobalProperties().paperColor = background ctx.outputTransform.z = (-bounds.x) * pdfContext.paperWidth / bounds.w ctx.outputTransform.w = (bounds.y + bounds.h) * pdfContext.paperHeight / bounds.h ctx.outputTransform.x = pdfContext.paperWidth / bounds.w ctx.outputTransform.y = -pdfContext.paperHeight / bounds.h if (not filename): name = File.createTempFile("field", ".pdf", None) else: name = File(filename) pdfContext.drawTo = name pdfContext.windowDisplayEnter() for n in something: pdfContext.submitLine(n, n.getProperties()) pdfContext.windowDisplayExit() return name.getPath()
def __processRequest(self): depositUrl = "%s/sword/deposit.post" % portalPath sword = SwordSimpleServer(depositUrl) try: p = self.vc("request").path.split( self.vc("portalId") + "/" + self.vc("pageName") + "/")[1] # portalPath except: p = "" if p == "post": print "\n--- post ---" c = sword.getClient() c.clearProxy() c.clearCredentials() postMsg = sword.getPostMessage() postMsg.filetype = "application/zip" postMsg.filepath = "/home/ward/Desktop/Test.zip" depositResponse = c.postFile(postMsg) return str(depositResponse) elif p == "servicedocument": #print "\n--- servicedocument ---" sdr = sword.getServiceDocumentRequest() sdr.username = self.vc("formData").get("username", "test") sdr.password = self.vc("formData").get("password", "test") if self.vc("formData").get("test"): depositUrl += "?test=1" sd = sword.doServiceDocument(sdr) # get a serviceDocument out = self.vc("response").getPrintWriter("text/xml; charset=UTF-8") out.println(str(sd)) out.close() self.velocityContext["pageName"] = "-noTemplate-" return sd elif p == "deposit.post": #print "\n--- deposit --- formData='%s'" % str(formData) inputStream = self.vc("formData").getInputStream() headers = {} for x in self.vc("formData").getHeaders().entrySet(): headers[x.getKey()] = x.getValue() deposit = sword.getDeposit() noOp = headers.get("X-No-Op") or "false" deposit.noOp = (noOp.lower()=="true") or \ (formData.get("test") is not None) contentDisposition = headers.get("Content-Disposition", "") filename = "" if contentDisposition != "": try: filename = contentDisposition.split("filename=")[1] deposit.filename = filename except: pass slug = headers.get("Slug") if slug is not None and slug != "": deposit.slug = slug #elif filename!="": # deposit.slug = filename deposit.username = "******" deposit.password = deposit.username try: file = File.createTempFile("tmptf", ".zip") file.deleteOnExit() fos = FileOutputStream(file.getAbsolutePath()) IOUtils.copy(inputStream, fos) fos.close() print "copied posted data to '%s'" % file.getAbsolutePath() except Exception, e: print "--- Exception - '%s'" % str(e) deposit.contentDisposition = file.getAbsolutePath() #???? deposit.file = inputStream depositResponse = sword.doDeposit(deposit) id = str(depositResponse.getEntry().id) try: print #imsPlugin = PluginManager.getTransformer("ims") jsonConfig = JsonConfig() #imsPlugin.init(jsonConfig.getSystemFile()) #harvestClient = HarvestClient(jsonConfig.getSystemFile()); storagePlugin = PluginManager.getStorage( jsonConfig.get("storage/type")) #storagePlugin.init(jsonConfig.getSystemFile()) setConfigUri = self.__getPortal().getClass().getResource( "/swordRule.json").toURI() configFile = File(setConfigUri) harvestConfig = JsonConfigHelper(configFile) tFile = File.createTempFile("harvest", ".json") tFile.deleteOnExit() harvestConfig.set("configDir", configFile.getParent()) harvestConfig.set("sourceFile", file.getAbsolutePath()) harvestConfig.store(FileWriter(tFile)) zipObject = GenericDigitalObject(id) zipObject.addPayload(FilePayload(file, id)) #digitalObject = imsPlugin.transform(zipObject, file) qStorage = QueueStorage(storagePlugin, tFile) qStorage.init(jsonConfig.getSystemFile()) qStorage.addObject(zipObject) if deposit.noOp: print "-- Testing noOp='true' --" else: # deposit the content pass except Exception, e: print "---" print " -- Exception - '%s'" % str(e) print "---"