def take_action(self, parsed_args): self.log.debug("take_action(%s)" % parsed_args) # NOTE(jk0): Since create() takes kwargs, it's easiest to just make a # copy of parsed_args and remove what we don't need. args = vars(parsed_args) args = dict(filter(lambda x: x[1] is not None, args.items())) args.pop("columns") args.pop("formatter") args.pop("prefix") args.pop("variables") args["properties"] = {} for _property in args.pop("property"): key, value = _property.split("=", 1) args["properties"][key] = value if "location" not in args and "copy_from" not in args: if "file" in args: args["data"] = open(args.pop("file"), "rb") else: args["data"] = None if sys.stdin.isatty() is not True: if msvcrt: msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) args["data"] = sys.stdin image_client = self.app.client_manager.image data = image_client.images.create(**args)._info.copy() return zip(*sorted(data.iteritems()))
def sanitize_open(filename, open_mode): """Try to open the given filename, and slightly tweak it if this fails. Attempts to open the given filename. If this fails, it tries to change the filename slightly, step by step, until it's either able to open it or it fails and raises a final exception, like the standard open() function. It returns the tuple (stream, definitive_file_name). """ try: if filename == u'-': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) return (sys.stdout.buffer if hasattr(sys.stdout, 'buffer') else sys.stdout, filename) stream = open(encodeFilename(filename), open_mode) return (stream, filename) except (IOError, OSError) as err: if err.errno in (errno.EACCES,): raise # In case of error, try to remove win32 forbidden chars alt_filename = os.path.join( re.sub(u'[/<>:"\\|\\\\?\\*]', u'#', path_part) for path_part in os.path.split(filename) ) if alt_filename == filename: raise else: # An exception here should be caught in the caller stream = open(encodeFilename(filename), open_mode) return (stream, alt_filename)
def subprocess_main(): """Run loop inside the subprocess.""" global logging_setup logging_setup = False if _on_windows(): # On windows, both STDIN and STDOUT get opened as text mode. This # can causes all kinds of weirdress when reading from our pipes. # (See #17804). Change the mode to binary for both streams. import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) # unset stdin and stdout so that we don't accidentally print to them stdin = sys.stdin stdout = sys.stdout sys.stdout = sys.stdin = None # initialize things try: handler = _subprocess_setup(stdin, stdout) except Exception, e: # error reading our initial messages. Try to log a warning, then # quit. send_subprocess_error_for_exception() _finish_subprocess_message_stream(stdout) raise # reraise so that miro_helper.py returns a non-zero exit code
def handleRequest(self, fin=None, fout=None, env=None): if fin==None: fin = sys.stdin if fout==None: fout = sys.stdout if env == None: env = os.environ try: contLen=int(env['CONTENT_LENGTH']) data = fin.read(contLen) except Exception: data = "" resultData = ServiceHandler.handleRequest(self, data) response = "Content-Type: text/plain\n" response += "Content-Length: %d\n\n" % len(resultData) response += resultData #on windows all \n are converted to \r\n if stdout is a terminal and is not set to binary mode :( #this will then cause an incorrect Content-length. #I have only experienced this problem with apache on Win so far. if sys.platform == "win32": try: import msvcrt msvcrt.setmode(fout.fileno(), os.O_BINARY) except: pass #put out the response fout.write(response) fout.flush()
def get_data_file(args): if args.file: return open(args.file, 'rb') else: # distinguish cases where: # (1) stdin is not valid (as in cron jobs): # glance ... <&- # (2) image data is provided through standard input: # glance ... < /tmp/file or cat /tmp/file | glance ... # (3) no image data provided: # glance ... try: os.fstat(0) except OSError: # (1) stdin is not valid (closed...) return None if not sys.stdin.isatty(): # (2) image data is provided through standard input image = sys.stdin if hasattr(sys.stdin, 'buffer'): image = sys.stdin.buffer if msvcrt: msvcrt.setmode(image.fileno(), os.O_BINARY) return image else: # (3) no image data provided return None
def do_stdout(): qmsg('Output to STDOUT requested') if g.stdin_tty: if no_tty: die(2,'Printing {} to screen is not allowed'.format(desc)) if (ask_tty and not opt.quiet) or binary: confirm_or_raise('','output {} to screen'.format(desc)) else: try: of = os.readlink('/proc/{}/fd/1'.format(os.getpid())) # Linux except: of = None # Windows if of: if of[:5] == 'pipe:': if no_tty: die(2,'Writing {} to pipe is not allowed'.format(desc)) if ask_tty and not opt.quiet: confirm_or_raise('','output {} to pipe'.format(desc)) msg('') of2,pd = os.path.relpath(of),os.path.pardir msg("Redirecting output to file '{}'".format((of2,of)[of2[:len(pd)] == pd])) else: msg('Redirecting output to file') if binary and g.platform == 'win': import msvcrt msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY) sys.stdout.write(data.decode() if issubclass(type(data),bytes) else data)
def __init__(self, proto): """ Start talking to standard IO with the given protocol. Also, put it stdin/stdout/stderr into binary mode. """ from twisted.internet import reactor for stdfd in range(0, 1, 2): msvcrt.setmode(stdfd, os.O_BINARY) _pollingfile._PollingTimer.__init__(self, reactor) self.proto = proto hstdin = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE) hstdout = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE) self.stdin = _pollingfile._PollableReadPipe( hstdin, self.dataReceived, self.readConnectionLost) self.stdout = _pollingfile._PollableWritePipe( hstdout, self.writeConnectionLost) self._addPollableResource(self.stdin) self._addPollableResource(self.stdout) self.proto.makeConnection(self)
def main(): # Signal handlers to trap signals. signal.signal(signal.SIGINT, _signal_handler) signal.signal(signal.SIGTERM, _signal_handler) if sys.platform != "win32": signal.signal(signal.SIGHUP, _signal_handler) mentos = Mentos() if sys.platform == "win32": # disable CRLF import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) else: # close fd's inherited from the ruby parent import resource maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if maxfd == resource.RLIM_INFINITY: maxfd = 65536 for fd in range(3, maxfd): try: os.close(fd) except: pass mentos.start()
def sanitize_open(filename, open_mode): """Try to open the given filename, and slightly tweak it if this fails. Attempts to open the given filename. If this fails, it tries to change the filename slightly, step by step, until it's either able to open it or it fails and raises a final exception, like the standard open() function. It returns the tuple (stream, definitive_file_name). """ try: if filename == u'-': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) return (sys.stdout, filename) stream = open(encodeFilename(filename), open_mode) return (stream, filename) except (IOError, OSError), err: # In case of error, try to remove win32 forbidden chars filename = re.sub(ur'[/<>:"\|\?\*]', u'#', filename) # An exception here should be caught in the caller stream = open(encodeFilename(filename), open_mode) return (stream, filename)
def main(args=sys.argv[1:]): parser = optparse.OptionParser(usage=__doc__.strip()) parser.add_option( "--factory", help="Full python path to the image factory class to " "create the image with. You can use the following shortcuts to the " "built-in image factory classes: {0}.".format( ", ".join(sorted(default_factories.keys())))) parser.add_option( "--optimize", type=int, help="Optimize the data by looking for chunks " "of at least this many characters that could use a more efficient " "encoding method. Use 0 to turn off chunk optimization.") parser.add_option( "--error-correction", type='choice', choices=sorted(error_correction.keys()), default='M', help="The error correction level to use. Choices are L (7%), " "M (15%, default), Q (25%), and H (30%).") opts, args = parser.parse_args(args) qr = qrcode.QRCode( error_correction=error_correction[opts.error_correction]) if opts.factory: module = default_factories.get(opts.factory, opts.factory) if '.' not in module: parser.error("The image factory is not a full python path") module, name = module.rsplit('.', 1) imp = __import__(module, {}, [], [name]) image_factory = getattr(imp, name) else: image_factory = None if args: data = args[0] else: # Use sys.stdin.buffer if available (Python 3) avoiding # UnicodeDecodeErrors. stdin_buffer = getattr(sys.stdin, 'buffer', sys.stdin) data = stdin_buffer.read() if opts.optimize is None: qr.add_data(data) else: qr.add_data(data, optimize=opts.optimize) if image_factory is None and os.isatty(sys.stdout.fileno()): qr.print_ascii(tty=True) return img = qr.make_image(image_factory=image_factory) sys.stdout.flush() # Use sys.stdout.buffer if available (Python 3), avoiding # UnicodeDecodeErrors. stdout_buffer = getattr(sys.stdout, 'buffer', None) if not stdout_buffer: if sys.platform == 'win32': # pragma: no cover import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) stdout_buffer = sys.stdout img.save(stdout_buffer)
def main(): optparser = optparse.OptionParser( usage="\n\tapitrace pickle <trace> | %prog [options]") optparser.add_option( '-p', '--profile', action="store_true", dest="profile", default=False, help="profile call parsing") optparser.add_option( '-v', '--verbose', action="store_true", dest="verbose", default=False, help="dump calls to stdout") (options, args) = optparser.parse_args(sys.argv[1:]) if args: optparser.error('unexpected arguments') # Change stdin to binary mode try: import msvcrt except ImportError: pass else: import os msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) startTime = time.time() parser = Counter(sys.stdin.buffer, options.verbose) parser.parse() stopTime = time.time() duration = stopTime - startTime if options.profile: sys.stderr.write('Processed %u calls in %.03f secs, at %u calls/sec\n' % (parser.numCalls, duration, parser.numCalls/duration))
def main(argv): if len(argv) < 3: print __doc__ sys.exit(-1) if sys.platform == "win32": import msvcrt, os msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) # 01/13/05 note: my experience is you cannot pipe UTF16 output from a shell # to a file even with os.O_BINARY setting. Must write to file directly. option = argv[1] filename = argv[2] fp = file(filename,'rb') if option == '-d': meta = {} result = test_distill(fp, sys.stdout, meta) print >>sys.stderr print >>sys.stderr, '-'*72 print >>sys.stderr, 'Meta:', pprint.pprint(meta, sys.stderr) print >>sys.stderr, 'Result:', result else: print __doc__ sys.exit(-1) fp.close()
def Translate(filenameInput, commandPython, options): if filenameInput == '': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) fIn = sys.stdin else: fIn = open(filenameInput, 'rb') if options.output == '': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) fOut = sys.stdout else: fOut = open(options.output, 'wb') if options.script != '': execfile(options.script, globals()) if options.fullread: fOut.write(eval(commandPython)(fIn.read())) else: Transform(fIn, fOut, commandPython) if fIn != sys.stdin: fIn.close() if fOut != sys.stdout: fOut.close()
def enviarStout(rutaarchivo): ruta,nombre=os.path.split(rutaarchivo) ruta,temp=os.path.split(registro.LeerReg("Trabajo")) ruta+="\\"+temp+"\\stdout\\" # sys.stdout.write(nombre + "\n" + ruta + "\n\n") if not os.path.isfile(os.path.join(ruta,nombre)): sys.stdout.write("Error No existe archivo:"+nombre) GuardarLog ("Error No exite stdout") sys.exit(0) return 0 try: msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) arch=open(os.path.join(ruta,nombre),"rb",0) sys.stdout.write(arch.read()) #print(datos) #datos=arch.read(1)) #while len(datos)>0: # sys.stdout.write(datos) # datos=arch.read(1) except: sys.stdout.write("error") GuardarLog ("Error: lecutra:" + ruta+nombre) finally: arch.close()
def run(self): msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) stdin = sys.stdin stdout = os.fdopen(sys.stdin.fileno(), 'w', 0) conn = Connection(stdin, stdout, self) conn.run()
def get_binary_stdio(stream): """ Return the specified standard input, output or errors stream as a 'raw' buffer object suitable for reading/writing binary data from/to it. """ assert stream in ['stdin', 'stdout', 'stderr'], "invalid stream name" stdio = getattr(sys, stream) if sys.version_info[0] < 3: if sys.platform == 'win32': # set I/O stream binary flag on python2.x (Windows) runtime = platform.python_implementation() if runtime == "PyPy": # the msvcrt trick doesn't work in pypy, so I use fdopen mode = "rb" if stream == "stdin" else "wb" stdio = os.fdopen(stdio.fileno(), mode, 0) else: # this works with CPython -- untested on other implementations import msvcrt msvcrt.setmode(stdio.fileno(), os.O_BINARY) return stdio else: # get 'buffer' attribute to read/write binary data on python3.x if hasattr(stdio, 'buffer'): return stdio.buffer else: orig_stdio = getattr(sys, "__%s__" % stream) return orig_stdio.buffer
def upload_mode(): try: import msvcrt msvcrt.setmode(0, os.O_BINARY) msvcrt.setmode(1, os.O_BINARY) except ImportError: pass
def h2b(): # workaround to keep Windows from munging binary data # http://stackoverflow.com/a/2374443 if sys.platform == "win32": import os, msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) # we now return you to your regularly scheduled program lines = sys.stdin.readlines() lineno = 1 parity = 0 for l in lines: l = l.strip() if len(l) % 3 != 0: raise Exception("line %d: incomplete word" % lineno) words = [l[i:i+3] for i in range(0, len(l), 3)] for w in words[:-1]: i = wordToByte(w) if i == 256: raise Exception("line %d: unknown word %s" % (lineno, w)) parity ^= i sys.stdout.write(chr(i)) for w in [words[-1],]: i = wordToByte(w) if i == 256: raise Exception("line %d: unknown word %s" % (lineno, w)) if (parity != i): raise Exception("line %d: parity error" % lineno) parity = 0 lineno += 1
def main(): parser = optparse.OptionParser() parser.add_option('-i', help='Input SWF file', metavar='FILE', dest='input') parser.add_option('-o', help='Output basename, a lone hyphen for stdout', metavar='FILE', dest='output') parser.add_option('-s', help='Stream ID', metavar='ID', type=int, dest='stream_id') parser.add_option('-f', help='Output format (PNG, JPG, rgb)', metavar='FORMAT', dest='format', default='png') options, args = parser.parse_args() if options.input is None: parser.error('Missing input file') if options.stream_id is None: list_stream(options.input) elif options.output is None: parser.error('Missing output file') else: # set stdout to binary in windows try: import os, msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) except: pass extract_stream(options.input, options.stream_id, options.output, options.format)
def _set_data_field(fields, args): if 'location' not in fields and 'copy_from' not in fields: if args.file: fields['data'] = open(args.file, 'rb') else: # distinguish cases where: # (1) stdin is not valid (as in cron jobs): # glance ... <&- # (2) image data is provided through standard input: # glance ... < /tmp/file or cat /tmp/file | glance ... # (3) no image data provided: # glance ... try: os.fstat(0) except OSError: # (1) stdin is not valid (closed...) fields['data'] = None return if not sys.stdin.isatty(): # (2) image data is provided through standard input if msvcrt: msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) fields['data'] = sys.stdin else: # (3) no image data provided fields['data'] = None
def handle_request(self, fin=None, fout=None, env=None): if fin is None: fin = sys.stdin if fout is None: fout = sys.stdout if env == None: env = os.environ try: content_length = int(env['CONTENT_LENGTH']) data = fin.read(content_length) except (KeyError, ValueError): data = fin.read() result = super(CGIServiceHandler, self).handle_request(data) response = 'Content-Type: application/json\n' response += 'Content-Length: %d\n\n' % len(result) response += result #on windows all \n are converted to \r\n if stdout is a terminal and is not set to binary mode :( #this will then cause an incorrect Content-length. #I have only experienced this problem with apache on Win so far. if sys.platform == 'win32': try: import msvcrt msvcrt.setmode(fout.fileno(), os.O_BINARY) except: pass #put out the response fout.write(response) fout.flush()
def unhexdump(self,infile:str): "decode hexdump from file (use '-' for stdin) (warning: outputs binary data)" if g.platform == 'win': import msvcrt msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY) hexdata = get_data_from_file(infile,dash=True,quiet=True) return decode_pretty_hexdump(hexdata)
def upload(filenm, path): try: # Windows needs stdio set for binary mode. import msvcrt msvcrt.setmode (0, os.O_BINARY) # stdin = 0 msvcrt.setmode (1, os.O_BINARY) # stdout = 1 except ImportError: pass fileitem = filenm root_path = path # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) f = open(root_path + fn, 'wb', 10000) # Read the file in chunks for chunk in fbuffer(fileitem.file): f.write(chunk) f.close() return 'The file "' + fn + '" was uploaded successfully' else: return 'No file was uploaded'
def __init__(self, f_in, f_out): if sys.platform == 'win32': import msvcrt msvcrt.setmode(f_in.fileno(), os.O_BINARY) msvcrt.setmode(f_out.fileno(), os.O_BINARY) self.f_in = f_in self.f_out = f_out
def listen(self): stdout = sys.stdout # Mute stdout. Nobody should actually be able to write to it, # because stdout is used for IPC. sys.stdout = open(os.devnull, 'w') stdin = sys.stdin if sys.version_info[0] > 2: stdout = stdout.buffer stdin = stdin.buffer # Python 2 opens streams in text mode on Windows. Set stdout and stdin # to binary mode. elif sys.platform == 'win32': import msvcrt msvcrt.setmode(stdout.fileno(), os.O_BINARY) msvcrt.setmode(stdin.fileno(), os.O_BINARY) while True: try: payload = pickle_load(stdin) except EOFError: # It looks like the parent process closed. # Don't make a big fuss here and just exit. exit(0) try: result = False, None, self._run(*payload) except Exception as e: result = True, traceback.format_exc(), e pickle_dump(result, stdout, self._pickle_protocol)
def main_plugin(): '''Main function when invoked as a protoc plugin.''' import sys if sys.platform == "win32": import os, msvcrt # Set stdin and stdout to binary mode msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) data = sys.stdin.read() request = plugin_pb2.CodeGeneratorRequest.FromString(data) import shlex args = shlex.split(request.parameter) options, dummy = optparser.parse_args(args) Globals.verbose_options = options.verbose response = plugin_pb2.CodeGeneratorResponse() for filename in request.file_to_generate: for fdesc in request.proto_file: if fdesc.name == filename: results = process_file(filename, fdesc, options) f = response.file.add() f.name = results['headername'] f.content = results['headerdata'] f = response.file.add() f.name = results['sourcename'] f.content = results['sourcedata'] sys.stdout.write(response.SerializeToString())
def stdout_to_binary(): """ Ensure that stdout is in binary mode on windows """ if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
def __init__(self, infile, outfile): if sys.platform == 'win32': import msvcrt msvcrt.setmode(infile.fileno(), os.O_BINARY) msvcrt.setmode(outfile.fileno(), os.O_BINARY) self.outfile, self.infile = infile, outfile self.readable = self.writeable = True
def EncodeAndWriteToStdout(s, encoding='utf-8'): """Encode the given string and emit to stdout. The string may contain non-ascii characters. This is a problem when stdout is redirected, because then Python doesn't know the encoding and we may get a UnicodeEncodeError. Arguments: s: (string) The string to encode. encoding: (string) The encoding of the string. """ if PY3: sys.stdout.buffer.write(s.encode(encoding)) elif sys.platform == 'win32': # On python 2 and Windows universal newline transformation will be in # effect on stdout. Python 2 will not let us avoid the easily because # it happens based on whether the file handle is opened in O_BINARY or # O_TEXT state. However we can tell Windows itself to change the current # mode, and python 2 will follow suit. However we must take care to change # the mode on the actual external stdout not just the current sys.stdout # which may have been monkey-patched inside the python environment. import msvcrt # pylint: disable=g-import-not-at-top if sys.__stdout__ is sys.stdout: msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) sys.stdout.write(s.encode(encoding)) else: sys.stdout.write(s.encode(encoding))
def _build_lint_target(path, config_dict): # type: (Path, Dict[str, Any]) -> AbstractLintTarget if path == _stdin_symbol: stdin_alt_path = get_config_value(config_dict, ['cmdargs', 'stdin_display_name']) # NOTE: In Python 3, sys.stdin is a string not bytes. Then we can get bytes by sys.stdin.buffer. # But in Python 2, sys.stdin.buffer is not defined. But we can get bytes by sys.stdin directly. is_python_3 = hasattr(sys.stdin, 'buffer') if is_python_3: lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin.buffer ) else: # NOTE: Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n # SEE: https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin/38939320#38939320 if sys.platform == 'win32': # set sys.stdin to binary mode import os, msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin ) return CachedLintTarget(lint_target) else: lint_target = LintTargetFile(path) return CachedLintTarget(lint_target)
raise getopt.GetoptError('Too many arguments') except getopt.GetoptError, msg: txt = 'ERROR: ' + str( msg) # that's required to get not-so-dumb result from 2to3 tool print(txt) print(usage) sys.exit(2) fin = args[0] if not os.path.isfile(fin): txt = "ERROR: File not found: %s" % fin # that's required to get not-so-dumb result from 2to3 tool print(txt) sys.exit(1) if len(args) == 2: fout = args[1] else: # write to stdout fout = sys.stdout # force binary mode for stdout on Windows if os.name == 'nt': f_fileno = getattr(sys.stdout, 'fileno', None) if f_fileno: fileno = f_fileno() if fileno >= 0: import msvcrt msvcrt.setmode(fileno, os.O_BINARY) sys.exit(hex2src(fin, fout, start, end, size, pad))
# Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Script used to test subprocess2.""" import optparse import os import sys import time if sys.platform == 'win32': # Annoying, make sure the output is not translated on Windows. # pylint: disable=no-member,import-error import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) parser = optparse.OptionParser() parser.add_option( '--fail', dest='return_value', action='store_const', default=0, const=64) parser.add_option( '--crlf', action='store_const', const='\r\n', dest='eol', default='\n') parser.add_option( '--cr', action='store_const', const='\r', dest='eol') parser.add_option('--stdout', action='store_true') parser.add_option('--stderr', action='store_true')
def run_all(): """Transforms the source infiles to a binary outfile. Returns a shell-style exit code: 1 if there were errors, 0 if there were no errors. """ Err.count = 0 Tamagotchi.process(CmdLine.infiles) z = Frontend.parse(CmdLine.infiles) env = Environment.Environment() m = Passes.ExpandMacros() i = Passes.InitLabels() l_basic = Passes.UpdateLabels() l = Passes.FixPoint("label update", [l_basic], lambda: not l_basic.changed) # The instruction selector is a bunch of fixpoints, and which # passes run depends on the command line options a bit. c_basic = Passes.Collapse() c = Passes.FixPoint("instruction selection 1", [l, c_basic], lambda: not c_basic.changed) if CmdLine.enable_branch_extend: b = Passes.ExtendBranches() instruction_select = Passes.FixPoint("instruction selection 2", [c, b], lambda: not b.changed) else: instruction_select = c a = Passes.Assembler() passes = [] passes.append(Passes.DefineMacros()) passes.append(Passes.FixPoint("macro expansion", [m], lambda: not m.changed)) passes.append(Passes.FixPoint("label initialization", [i], lambda: not i.changed)) passes.extend([Passes.CircularityCheck(), Passes.CheckExprs(), Passes.EasyModes()]) passes.append(instruction_select) passes.extend([Passes.NormalizeModes(), Passes.UpdateLabels(), a]) for p in passes: p.go(z, env) if Err.count == 0: try: outfile = CmdLine.outfile if outfile == '-': output = sys.stdout if sys.platform == "win32": # We can't dump our binary in text mode; that would be # disastrous. So, we'll do some platform-specific # things here to force our stdout to binary mode. import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) elif outfile is None: output = file('bin', 'wb') else: output = file(outfile, 'wb') f = open("template.txt", "rb") t = f.read() head = t[:0x40000] if (len("".join(map(chr, a.output))) > 0x400): print "too large" return 1 tail = t[(0x40000 + len("".join(map(chr, a.output)))):] output.write(head + "".join(map(chr, a.output)) + tail) output.flush() if outfile != '-': output.close() return 0 except IOError: print>>sys.stderr, "Could not write to " + outfile return 1 else: Err.report() return 1
def take_action(self, parsed_args): image_client = self.app.client_manager.image if getattr(parsed_args, 'owner', None) is not None: self.log.warning( _('The --owner option is deprecated, ' 'please use --project instead.')) kwargs = {} copy_attrs = ('name', 'owner', 'min_disk', 'min_ram', 'properties', 'container_format', 'disk_format', 'size', 'store', 'location', 'copy_from', 'volume', 'force', 'checksum') for attr in copy_attrs: if attr in parsed_args: val = getattr(parsed_args, attr, None) if val: # Only include a value in kwargs for attributes that are # actually present on the command line kwargs[attr] = val # Special case project option back to API attribute name 'owner' val = getattr(parsed_args, 'project', None) if val: kwargs['owner'] = val # Handle exclusive booleans with care # Avoid including attributes in kwargs if an option is not # present on the command line. These exclusive booleans are not # a single value for the pair of options because the default must be # to do nothing when no options are present as opposed to always # setting a default. if parsed_args.protected: kwargs['protected'] = True if parsed_args.unprotected: kwargs['protected'] = False if parsed_args.public: kwargs['is_public'] = True if parsed_args.private: kwargs['is_public'] = False # Wrap the call to catch exceptions in order to close files try: image = utils.find_resource( image_client.images, parsed_args.image, ) if not parsed_args.location and not parsed_args.copy_from: if parsed_args.volume: volume_client = self.app.client_manager.volume source_volume = utils.find_resource( volume_client.volumes, parsed_args.volume, ) volume_client.volumes.upload_to_image( source_volume.id, parsed_args.force, parsed_args.image, (parsed_args.container_format if parsed_args.container_format else image.container_format), (parsed_args.disk_format if parsed_args.disk_format else image.disk_format), ) elif parsed_args.file: # Send an open file handle to glanceclient so it will # do a chunked transfer kwargs["data"] = io.open(parsed_args.file, "rb") else: # Read file from stdin if sys.stdin.isatty() is not True: if parsed_args.stdin: if msvcrt: msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) # Send an open file handle to glanceclient so it # will do a chunked transfer kwargs["data"] = sys.stdin else: self.log.warning('Use --stdin to enable read image' ' data from standard input') if image.properties and parsed_args.properties: image.properties.update(kwargs['properties']) kwargs['properties'] = image.properties if not kwargs: self.log.warning('no arguments specified') return image = image_client.images.update(image.id, **kwargs) finally: # Clean up open files - make sure data isn't a string if ('data' in kwargs and hasattr(kwargs['data'], 'close') and kwargs['data'] != sys.stdin): kwargs['data'].close()
import cgi import sqlite3 import datetime import os from http.cookies import * from datetime import timedelta, date import msvcrt try: con = sqlite3.connect("F:\Tom\pic.db") cur = con.cursor() form = cgi.FieldStorage() data = [] msvcrt.setmode(0, os.O_BINARY) msvcrt.setmode(1, os.O_BINARY) print("Content-Type:text/html\n\n") def save_uploaded_file(form_field, upload_dir): """This saves a file uploaded by an HTML form. The form_field is the name of the file input field from the form. For example, the following form_field would be "file_1": <input name="file_1" type="file"> The upload_dir is the directory where the file will be written. If no file was uploaded or if the field does not exist then this does nothing. """ if not form[form_field]: return fileitem = form[form_field] if not fileitem.file: return fout = file(os.path.join(upload_dir, fileitem.filename), 'wb')
def ifWIN32SetBinary(io): if sys.platform == 'win32': import msvcrt, os msvcrt.setmode(io.fileno(), os.O_BINARY)
def Unhexdump(infile): if g.platform == 'win': import msvcrt msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY) sys.stdout.write(decode_pretty_hexdump( get_data_from_file(infile,dash=True,silent=True)))
def set_binary_mode(stream): if sys.platform == 'win32': import os import msvcrt msvcrt.setmode(stream.fileno(), os.O_BINARY)
def _make_binary_on_windows(fileno): """Win32 mangles \r\n to \n and that breaks streams. See bug lp:505078.""" if sys.platform == "win32": import msvcrt msvcrt.setmode(fileno, os.O_BINARY)
# unable to read file log(traceback.format_exc()) pass if __name__ == '__main__': # TODO: Pull this from an env var handler_name = os.getenv('WSGI_HANDLER', 'django.core.handlers.wsgi.WSGIHandler') module, callable = handler_name.rsplit('.', 1) handler = getattr(__import__(module, fromlist=[callable]), callable)() stdout = sys.stdin.fileno() try: import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) except ImportError: pass update_environment() _REQUESTS = {} while True: try: record = read_fastcgi_record(sys.stdin) if record: record.params['wsgi.input'] = cStringIO.StringIO( record.params['wsgi.input']) def start_response(status, headers, exc_info=None):
def main(): lost_comments = False written_count = skipped_count = duplicate_count = 0 broken_count = variation_count = 0 parser = argparse.ArgumentParser(description=DESCRIPTION) parser.add_argument('-v', '--version', action='version', version=utils.TITLE) parser.add_argument('--quiet', action='store_true', help='Do not print out informational messages') parser.add_argument('--sort-records', action='store_true', help='Order simulation data records by OID') parser.add_argument( '--ignore-broken-records', action='store_true', help='Drop malformed simulation data records rather than bailing out') parser.add_argument('--deduplicate-records', action='store_true', help='Drop duplicate simulation data records') parser.add_argument( '--escaped-strings', action='store_true', help='Produce Python-style escaped strings (e.g. "\x00") in ' 'simulation data values rather than hexify such non-ASCII ' 'values') parser.add_argument( '--start-object', metavar='<MIB::Object|OID>', type=_parse_mib_object, help='Drop all simulation data records prior to this OID specified ' 'as MIB object (MIB::Object) or OID (1.3.6.)') parser.add_argument( '--stop-object', metavar='<MIB::Object|OID>', type=functools.partial(_parse_mib_object, last=True), help='Drop all simulation data records after this OID specified ' 'as MIB object (MIB::Object) or OID (1.3.6.)') parser.add_argument( '--mib-source', dest='mib_sources', metavar='<URI|PATH>', action='append', type=str, help='One or more URIs pointing to a collection of ASN.1 MIB files.' 'Optional "@mib@" token gets replaced with desired MIB module ' 'name during MIB search.') parser.add_argument( '--source-record-type', choices=RECORD_TYPES, default='snmprec', help='Treat input as simulation data of this record type') parser.add_argument( '--destination-record-type', choices=RECORD_TYPES, default='snmprec', help='Produce simulation data with record of this type') parser.add_argument('--input-file', dest='input_files', metavar='<FILE>', action='append', type=str, required=True, help='SNMP simulation data file to read records from') parser.add_argument('--output-file', metavar='<FILE>', type=str, help='SNMP simulation data file to write records to') args = parser.parse_args() if not args.mib_source: args.mib_source = ['http://mibs.snmplabs.com/asn1/@mib@'] args.input_files = [ RECORD_TYPES[args.source_record_type].open(x) for x in args.input_files ] if args.output_file and args.output_file != '-': ext = os.path.extsep + RECORD_TYPES[args.destination_record_type].ext if not args.output_file.endswith(ext): args.output_file += ext args.output_file = RECORD_TYPES[args.destination_record_type].open( args.output_file, 'wb') else: args.output_file = sys.stdout if sys.version_info >= (3, 0, 0): # binary mode write args.output_file = sys.stdout.buffer elif sys.platform == "win32": import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) if not args.input_files: args.input_files.append(sys.stdin) if (isinstance(args.start_oid, rfc1902.ObjectIdentity) or isinstance(args.stop_oid, rfc1902.ObjectIdentity)): mib_builder = builder.MibBuilder() mib_view_controller = view.MibViewController(mib_builder) compiler.addMibCompiler(mib_builder, sources=args.mib_sources) try: if isinstance(args.start_oid, rfc1902.ObjectIdentity): args.start_oid.resolveWithMib(mib_view_controller) if isinstance(args.stop_oid, rfc1902.ObjectIdentity): args.stop_oid.resolveWithMib(mib_view_controller) except PySnmpError as exc: sys.stderr.write('ERROR: %s\r\n' % exc) return 1 records_list = [] for input_file in args.input_files: if not args.quiet: sys.stderr.write( '# Input file #%s, processing records from %s till ' '%s\r\n' % (args.input_files.index(input_file), args.start_oid or 'the beginning', args.stop_oid or 'the end')) line_no = 0 while True: line, rec_line_no, _ = get_record(input_file, line_no) if not line: break if rec_line_no != line_no + 1: if not args.quiet: sys.stderr.write( '# Losing comment at lines %s..%s (input file #' '%s)\r\n' % (line_no + 1, rec_line_no - 1, args.input_files.index(input_file))) line_no = rec_line_no lost_comments += 1 backdoor = {} try: oid, value = RECORD_TYPES[args.source_record_type].evaluate( line, backdoor=backdoor) except error.SnmpsimError as exc: if args.ignore_broken_records: if not args.quiet: sys.stderr.write('# Skipping broken record <%s>: ' '%s\r\n' % (line, exc)) broken_count += 1 continue else: if not args.quiet: sys.stderr.write('ERROR: broken record <%s>: ' '%s\r\n' % (line, exc)) return 1 if (args.start_oid and args.start_oid > oid or args.stop_oid and args.stop_oid < oid): skipped_count += 1 continue records_list.append((oid, value, backdoor)) if args.sort_records: records_list.sort(key=lambda x: x[0]) unique_indices = set() for record in records_list: if args.deduplicate_records: if record[0] in unique_indices: if not args.quiet: sys.stderr.write('# Skipping duplicate record ' '<%s>\r\n' % record[0]) duplicate_count += 1 continue else: unique_indices.add(record[0]) try: args.output_file.write( RECORD_TYPES[args.destination_record_type].format( record[0], record[1], backdoor=record[2], nohex=args.escaped_strings)) except Exception as exc: sys.stderr.write('ERROR: record not written: %s\r\n' % exc) break written_count += 1 if record[2]: variation_count += 1 if not args.quiet: sys.stderr.write( '# Records: written %s, filtered out %s, de-duplicated %s, ignored ' '%s, broken %s, variated %s\r\n' % (written_count, skipped_count, duplicate_count, lost_comments, broken_count, variation_count)) args.output_file.flush() args.output_file.close() return 0
#!/usr/bin/env python import cgi, os import cgitb; cgitb.enable() try: # Windows needs stdio set for binary mode. import msvcrt msvcrt.setmode (0, os.O_BINARY) # stdin = 0 msvcrt.setmode (1, os.O_BINARY) # stdout = 1 except ImportError: pass form = cgi.FieldStorage() # A nested FieldStorage instance holds the file fileitem = form['file'] # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) open('files/' + fn, 'wb').write(fileitem.file.read()) message = 'The file "' + fn + '" was uploaded successfully' else: message = 'No file was uploaded' print """\ Content-Type: text/html\n <html><body> <p>%s</p>
def __enter__(self): if sys.platform == "win32": import msvcrt self.previous_mode = msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) return sys.stdout
def connect(hostspec, password=None, namespace=None): """Returns Connection to pythonshare server at hostspec. Parameters: hostspec (string): Syntax: [socket://][PASSWORD@]HOST[:PORT][/NAMESPACE] shell://[SHELLCOMMAND] The default scheme is socket. Examples: hostname equals socket://hostname:8089 host:port equals socket://host:port host/namespace equals socket://host:8089/namespace hostspec can also be a key in PYTHONSHARE_HOSTSPECS environment variable (a Python dictionary that maps shorthands to real hostspecs). password (string, optional): use password to log in to the server. Overrides hostspec password. namespace (string, optional): send code to the namespace on server by default. Overrides hostspec namespace. """ if hostspec in _PYTHONSHARE_HOSTSPECS: hostspec = _PYTHONSHARE_HOSTSPECS[hostspec] if not "://" in hostspec: hostspec = "socket://" + hostspec scheme, netloc, path, _, _ = _urlparse.urlsplit(hostspec) kwargs = {} if namespace != None: kwargs["namespace"] = namespace if scheme == "socket": # Parse URL if "@" in netloc: userinfo, hostport = netloc.split("@", 1) else: userinfo, hostport = "", netloc if ":" in userinfo: userinfo_user, userinfo_password = userinfo.split(":", 1) else: userinfo_user, userinfo_password = userinfo, None if ":" in hostport: host, port = hostport.split(":") else: host, port = hostport, default_port # If userinfo has been given, authenticate using it. # Allow forms # socket://password@host:port # socket://dontcare:password@host:port if password == None and userinfo: if userinfo_password: password = userinfo_password else: password = userinfo if not "namespace" in kwargs and path.replace("/", "", 1): kwargs["namespace"] = path.replace("/", "", 1) rv = client.Connection(host, int(port), password=password, **kwargs) elif scheme == "shell": p = subprocess.Popen(hostspec[len("shell://"):], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) if os.name == "nt": import msvcrt msvcrt.setmode(p.stdin.fileno(), os.O_BINARY) msvcrt.setmode(p.stdout.fileno(), os.O_BINARY) rv = client.Connection(p.stdout, p.stdin, **kwargs) else: raise ValueError('invalid URI "%s"' % (hostspec,)) rv.hostspec = hostspec return rv
# which these translations are suppressed. # " # On Windows, change mode of stdin and stdout to _O_BINARY, i.e. untranslated # mode. This translation might be a convenient auto-correction in many # situations. However, in this program, I want precise control over item # separation in stdin and stdout. So I prefer not to let Windows implicitly mess # with the byte streams. In untranslated mode, the program's test suite can # largely be the same on Windows and Unix. In translated mode the specification # of item separation in input and output unnecessarily complicated. if WINDOWS: for stream in (sys.stdout, sys.stdin): # ValueError: redirected Stdin is pseudofile, has no fileno() # is possible. Seen when py.test imports the package. try: if sys.version < '3': msvcrt.setmode(stream.fileno(), os.O_BINARY) else: msvcrt.setmode(stream.buffer.fileno(), os.O_BINARY) except ValueError as e: log.error("Could not set mode to O_BINARY on %s: %s", stream, e) # To be populated by argparse from cmdline arguments. options = None def main(): parse_options() if options.verbose == 1: log.setLevel(logging.INFO) elif options.verbose == 2: log.setLevel(logging.DEBUG)
def __exit__(self, type, value, traceback): if sys.platform == "win32": import msvcrt msvcrt.setmode(sys.stdout.fileno(), self.previous_mode)
def EMLDump(emlfilename, options): FixPipe() if emlfilename == '': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) data = sys.stdin.read() elif emlfilename.lower().endswith('.zip'): oZipfile = zipfile.ZipFile(emlfilename, 'r') oZipContent = oZipfile.open(oZipfile.infolist()[0], 'r', C2BIP3(MALWARE_PASSWORD)) data = oZipContent.read() oZipContent.close() oZipfile.close() else: data = File2String(emlfilename) global decoders decoders = [] LoadDecoders(options.decoders, True) if options.yara != None: if not 'yara' in sys.modules: print('Error: option yara requires the YARA Python module.') return rules = YARACompile(options.yara) counter = 0 for line in data.splitlines(): if ContainsField(line): break counter += 1 if not options.header and not options.filter and options.select == '' and counter != 0: if counter == 1: print('Warning: the first line does not contain a field.') else: print('Warning: the first %d lines do not contain a field.' % counter) if not options.filter and options.select == '': # for line in data.splitlines(True): # if len(line) > 100: # print('Warning: contains lines longer than 100 characters.') # break warningPrinted = False for line in data.splitlines(True): if line == '\r\n': break if not StartsWithWhitespace(line) and not ContainsField( line) and not warningPrinted: print( 'Warning: the first block contains lines that are not a field.' ) warningPrinted = True if options.header or options.filter: data = ''.join(data.splitlines(True)[counter:]) if options.filter: # data = ''.join([line for line in data.splitlines(True) if len(line) <= 100]) temp = [] firstBlock = True for line in data.splitlines(True): if not firstBlock: temp.append(line) if firstBlock and (StartsWithWhitespace(line) or ContainsField(line)): temp.append(line) if firstBlock and line == '\r\n': firstBlock = False temp.append(line) data = ''.join(temp) oEML = email.message_from_string(data) if options.select == '': if options.yara == None: counter = 1 for oPart in oEML.walk(): data = oPart.get_payload(decode=True) if data == None: lengthString = ' ' else: lengthString = '%7d' % len(data) extrainfo = GenerateExtraInfo( options.extra, counter, IFF(oPart.is_multipart(), 'M', ''), oPart.get_content_type(), data) line = '%d: %s %s %s' % ( counter, IFF(oPart.is_multipart(), 'M', ' '), lengthString, oPart.get_content_type()) if options.extra.startswith('!'): line = '' line += extrainfo print(line) counter += 1 else: counter = 1 for oPart in oEML.walk(): data = oPart.get_payload(decode=True) if data != None: oDecoders = [cIdentity(data, None)] for cDecoder in decoders: try: oDecoder = cDecoder(data, options.decoderoptions) oDecoders.append(oDecoder) except Exception as e: print('Error instantiating decoder: %s' % cDecoder.name) if options.verbose: raise e return for oDecoder in oDecoders: while oDecoder.Available(): for result in rules.match(data=oDecoder.Decode()): lengthString = '%7d' % len(data) decoderName = oDecoder.Name() if decoderName != '': decoderName = ' (%s)' % decoderName print('%d: %s %s %-20s %s %s%s' % (counter, IFF(oPart.is_multipart(), 'M', ' '), lengthString, oPart.get_content_type(), result.namespace, result.rule, decoderName)) if options.yarastrings: for stringdata in result.strings: print(' %06x %s %s %s' % (stringdata[0], stringdata[1], binascii.hexlify(stringdata[2]), repr(stringdata[2]))) counter += 1 else: if options.dump: DumpFunction = lambda x: x if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) elif options.hexdump: DumpFunction = HexDump else: DumpFunction = HexAsciiDump counter = 1 for oPart in oEML.walk(): if IsNumeric(options.select) and counter == int( options.select) or not IsNumeric( options.select) and oPart.get_content_type( ) == options.select: if not oPart.is_multipart(): StdoutWriteChunked( DumpFunction( CutData(oPart.get_payload(decode=True), options.cut))) else: print('Warning: you selected a multipart stream') break counter += 1
def Translate(filenameInput, commandPython, options): if filenameInput == '': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) fIn = sys.stdin else: decoded = FilenameCheckHash(filenameInput) if decoded == '': fIn = open(filenameInput, 'rb') elif decoded == None: print('Error parsing filename: ' + filenameInput) return else: fIn = StringIO(decoded) if options.secondbytestream != '': decoded = FilenameCheckHash(options.secondbytestream) if decoded == '': fIn2 = open(options.secondbytestream, 'rb') elif decoded == None: print('Error parsing filename: ' + options.secondbytestream) return else: fIn2 = StringIO(decoded) else: fIn2 = None if options.output == '': if sys.platform == 'win32': import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) fOut = sys.stdout else: fOut = open(options.output, 'wb') if options.script != '': execfile(options.script, globals()) if options.execute != '': exec(options.execute, globals()) if options.fullread: Output(fOut, eval(commandPython)(fIn.read())) elif options.regex != '' or options.filterregex != '': content = fIn.read() if options.regex != '': Output(fOut, re.sub(options.regex, eval(commandPython), content)) else: Output( fOut, re.sub( options.filterregex, eval(commandPython), ''.join([ x.group() for x in re.finditer(options.filterregex, content) ]))) else: Transform(fIn, fIn2, fOut, commandPython) if fIn != sys.stdin: fIn.close() if fIn2 != None: fIn2.close() if fOut != sys.stdout: fOut.close()
def setbinary(fd): # When run without console, pipes may expose invalid # fileno(), usually set to -1. fno = getattr(fd, 'fileno', None) if fno is not None and fno() >= 0: msvcrt.setmode(fno(), os.O_BINARY)
def main(): initialized = False log('wfastcgi.py %s started' % __version__) log('Python version: %s' % sys.version) try: fcgi_stream = sys.stdin.detach( ) if sys.version_info[0] >= 3 else sys.stdin try: import msvcrt msvcrt.setmode(fcgi_stream.fileno(), os.O_BINARY) except ImportError: pass while True: record = read_fastcgi_record(fcgi_stream) if not record: continue errors = sys.stderr = sys.__stderr__ = record.params[ 'wsgi.errors'] = StringIO() output = sys.stdout = sys.__stdout__ = StringIO() with handle_response(fcgi_stream, record, output.getvalue, errors.getvalue) as response: if not initialized: log('wfastcgi.py %s initializing' % __version__) os.chdir(response.physical_path) sys.path[0] = '.' # Initialization errors should be treated as fatal. response.fatal_errors = True response.error_message = 'Error occurred while reading WSGI handler' env, handler = read_wsgi_handler(response.physical_path) response.error_message = 'Error occurred starting file watcher' start_file_watcher(response.physical_path, env.get('WSGI_RESTART_FILE_REGEX')) # Enable debugging if possible. Default to local-only, but # allow a web.config to override where we listen ptvsd_secret = env.get('WSGI_PTVSD_SECRET') if ptvsd_secret: ptvsd_address = (env.get('WSGI_PTVSD_ADDRESS') or 'localhost:5678').split(':', 2) try: ptvsd_port = int(ptvsd_address[1]) except LookupError: ptvsd_port = 5678 except ValueError: log('"%s" is not a valid port number for debugging' % ptvsd_address[1]) ptvsd_port = 0 if ptvsd_address[0] and ptvsd_port: try: import ptvsd except ImportError: log('unable to import ptvsd to enable debugging' ) else: addr = ptvsd_address[0], ptvsd_port ptvsd.enable_attach(secret=ptvsd_secret, address=addr) log('debugging enabled on %s:%s' % addr) response.error_message = '' response.fatal_errors = False log('wfastcgi.py %s initialized' % __version__) initialized = True os.environ.update(env) # SCRIPT_NAME + PATH_INFO is supposed to be the full path # (http://www.python.org/dev/peps/pep-0333/) but by default # (http://msdn.microsoft.com/en-us/library/ms525840(v=vs.90).aspx) # IIS is sending us the full URL in PATH_INFO, so we need to # clear the script name here if 'AllowPathInfoForScriptMappings' not in os.environ: record.params['SCRIPT_NAME'] = '' record.params['wsgi.script_name'] = wsgi_encode('') # correct SCRIPT_NAME and PATH_INFO if we are told what our SCRIPT_NAME should be if 'SCRIPT_NAME' in os.environ and record.params[ 'PATH_INFO'].lower().startswith( os.environ['SCRIPT_NAME'].lower()): record.params['SCRIPT_NAME'] = os.environ['SCRIPT_NAME'] record.params['PATH_INFO'] = record.params['PATH_INFO'][ len(record.params['SCRIPT_NAME']):] record.params['wsgi.script_name'] = wsgi_encode( record.params['SCRIPT_NAME']) record.params['wsgi.path_info'] = wsgi_encode( record.params['PATH_INFO']) # Send each part of the response to FCGI_STDOUT. # Exceptions raised in the handler will be logged by the context # manager and we will then wait for the next record. result = handler(record.params, response.start) try: for part in result: if part: response.send(FCGI_STDOUT, part) finally: if hasattr(result, 'close'): result.close() except _ExitException: pass except Exception: maybe_log('Unhandled exception in wfastcgi.py: ' + traceback.format_exc()) except BaseException: maybe_log('Unhandled exception in wfastcgi.py: ' + traceback.format_exc()) raise finally: run_exit_tasks() maybe_log('wfastcgi.py %s closed' % __version__)
def effect(self): svg_file = self.args[-1] ttmp_orig = self.document.getroot() docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi')) if docname is None: docname = self.args[-1] doc_scale = self.getDocumentScale() res_scale = eval(self.options.resolution) / 96.0 scale = doc_scale * res_scale pageHeight = self.uutounit(self.unittouu(self.getDocumentHeight()), "px") pageWidth = self.uutounit(self.unittouu(self.getDocumentWidth()), "px") # Create os temp dir (to store exported pngs and Gimp log file) self.tmp_dir = tempfile.mkdtemp() # Guides hGuides = [] vGuides = [] if self.options.saveGuides: # Grab all guide tags in the namedview tag guideXpath = "sodipodi:namedview/sodipodi:guide" for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS): ori = guideNode.get('orientation') if ori == '0,1': # This is a horizontal guide pos = self.uutounit(float(guideNode.get('position').split(',')[1]), "px") * doc_scale # GIMP doesn't like guides that are outside of the image if pos > 0 and pos < pageHeight: # The origin is at the top in GIMP land hGuides.append(str(int(round((pageHeight - pos) * res_scale)))) elif ori == '1,0': # This is a vertical guide pos = self.uutounit(float(guideNode.get('position').split(',')[0]), "px") * doc_scale # GIMP doesn't like guides that are outside of the image if pos > 0 and pos < pageWidth: vGuides.append(str(int(round(pos * res_scale)))) hGList = ' '.join(hGuides) vGList = ' '.join(vGuides) # Grid gridSpacingFunc = '' gridOriginFunc = '' # GIMP only allows one rectangular grid gridXpath = "sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]" if (self.options.saveGrid and self.document.xpath(gridXpath, namespaces=inkex.NSS)): gridNode = self.xpathSingle(gridXpath) if gridNode != None: # These attributes could be nonexistent spacingX = gridNode.get('spacingx') if spacingX == None: spacingX = 1 else: spacingX = self.uutounit(float(spacingX),"px") * scale spacingY = gridNode.get('spacingy') if spacingY == None: spacingY = 1 else: spacingY = self.uutounit(float(spacingY),"px") * scale originX = gridNode.get('originx') if originX == None: originX = 0 else: originX = self.uutounit(float(originX),"px") * scale originY = gridNode.get('originy') if originY == None: originY = 0 else: originY = self.uutounit(float(originY),"px") * doc_scale offsetY = pageHeight % (self.uutounit(float(spacingY),"px") * doc_scale) originY = (pageHeight - originY) * res_scale gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (int(round(float(spacingX))), int(round(float(spacingY)))) gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (int(round(float(originX))), int(round(float(originY)))) # Layers area = '--export-area-page' opacity = '--export-background-opacity=' resolution = '--export-dpi=' + self.options.resolution if self.options.layerBackground: opacity += "1" else: opacity += "0" pngs = [] names = [] self.valid = 0 path = "/svg:svg/*[name()='g' or @style][@id]" for node in self.document.xpath(path, namespaces=inkex.NSS): if len(node) > 0: # Get rid of empty layers self.valid = 1 id = node.get('id') if node.get("{" + inkex.NSS["inkscape"] + "}label"): name = node.get("{" + inkex.NSS["inkscape"] + "}label") else: name = id filename = os.path.join(self.tmp_dir, "%s.png" % id) command = "inkscape -i \"%s\" -j %s %s -e \"%s\" %s %s" % (id, area, opacity, filename, svg_file, resolution) p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr stdin = p.stdin f.read() f.close() err.close() stdin.close() if return_code != 0: self.clear_tmp() raise GimpXCFInkscapeNotInstalled if os.name == 'nt': filename = filename.replace("\\", "/") pngs.append(filename) names.append(name) if (self.valid == 0): self.clear_tmp() inkex.errormsg(_('This extension requires at least one non empty layer.')) else: filelist = '"%s"' % '" "'.join(pngs) namelist = '"%s"' % '" "'.join(names) xcf = os.path.join(self.tmp_dir, "%s.xcf" % docname) if os.name == 'nt': xcf = xcf.replace("\\", "/") script_fu = """ (tracing 1) (define (png-to-layer img png_filename layer_name) (let* ( (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename))) (png_layer (car (gimp-image-get-active-layer png))) (xcf_layer (car (gimp-layer-new-from-drawable png_layer img))) ) (gimp-image-add-layer img xcf_layer -1) (gimp-drawable-set-name xcf_layer layer_name) ) ) (let* ( (img (car (gimp-image-new 200 200 RGB))) ) (gimp-image-set-resolution img %s %s) (gimp-image-undo-disable img) (for-each (lambda (names) (png-to-layer img (car names) (cdr names)) ) (map cons '(%s) '(%s)) ) (gimp-image-resize-to-layers img) (for-each (lambda (hGuide) (gimp-image-add-hguide img hGuide) ) '(%s) ) (for-each (lambda (vGuide) (gimp-image-add-vguide img vGuide) ) '(%s) ) %s %s (gimp-image-undo-enable img) (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s")) (gimp-quit 0) """ % (self.options.resolution, self.options.resolution, filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf) junk = os.path.join(self.tmp_dir, 'junk_from_gimp.txt') command = 'gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) f = p.stdin out = p.stdout err = p.stderr f.write(script_fu.encode('utf-8')) return_code = p.wait() if p.returncode != 0: self.clear_tmp() raise GimpXCFGimpNotInstalled f.close() err.close() out.close() # Uncomment these lines to see the output from gimp #err = open(junk, 'r') #inkex.debug(err.read()) #err.close() try: x = open(xcf, 'rb') except: self.clear_tmp() raise GimpXCFScriptFuError if os.name == 'nt': try: import msvcrt msvcrt.setmode(1, os.O_BINARY) except: pass try: sys.stdout.write(x.read()) finally: x.close() self.clear_tmp()
csv.field_size_limit( 10485760 ) # The default value is 128KB; upping to 10MB. See SPL-12117 for background on this issue if sys.platform == 'win32': # Work around the fact that on Windows '\n' is mapped to '\r\n'. The typical solution is to simply open files in # binary mode, but stdout is already open, thus this hack. 'CPython' and 'PyPy' work differently. We assume that # all other Python implementations are compatible with 'CPython'. This might or might not be a valid assumption. from platform import python_implementation implementation = python_implementation() fileno = sys.stdout.fileno() if implementation == 'PyPy': sys.stdout = os.fdopen(fileno, 'wb', 0) else: from msvcrt import setmode setmode(fileno, os.O_BINARY) class CommandLineParser(object): """ Parses the arguments to a search command. A search command line is described by the following syntax. **Syntax**:: command = command-name *[wsp option] *[wsp [dquote] field-name [dquote]] command-name = alpha *( alpha / digit ) option = option-name [wsp] "=" [wsp] option-value option-name = alpha *( alpha / digit / "_" ) option-value = word / quoted-string word = 1*( %01-%08 / %0B / %0C / %0E-1F / %21 / %23-%FF ) ; Any character but DQUOTE and WSP
def usage(): print >> sys.stderr, """\ import: lyrics -i [-f lyricfile] audiofile export: lyrics -e [-f lyricfile] audiofile remove: lyrics -x audiofile """ sys.exit(2) # Turn off textmode stdio, otherwise CR CR LF is written if sys.platform == "win32": from os import O_BINARY import msvcrt for fd in (sys.stdin, sys.stdout, sys.stderr): msvcrt.setmode(fd.fileno(), O_BINARY) def write_id3(file, lyrics): tag = mutagen.mp3.MP3(file) atom = u"USLT::'eng'" if lyrics is None: if atom in tag: del tag[atom] else: tag[atom] = mutagen.id3.USLT() tag[atom].text = lyrics tag[atom].encoding = 1 tag[atom].lang = "eng" tag[atom].desc = u"" if verbose:
def take_action(self, parsed_args): self.log.debug("take_action(%s)", parsed_args) image_client = self.app.client_manager.image # Build an attribute dict from the parsed args, only include # attributes that were actually set on the command line kwargs = {} copy_attrs = ('name', 'id', 'store', 'container_format', 'disk_format', 'owner', 'size', 'min_disk', 'min_ram', 'localtion', 'copy_from', 'volume', 'force', 'checksum', 'properties') for attr in copy_attrs: if attr in parsed_args: val = getattr(parsed_args, attr, None) if val: # Only include a value in kwargs for attributes that are # actually present on the command line kwargs[attr] = val # Handle exclusive booleans with care # Avoid including attributes in kwargs if an option is not # present on the command line. These exclusive booleans are not # a single value for the pair of options because the default must be # to do nothing when no options are present as opposed to always # setting a default. if parsed_args.protected: kwargs['protected'] = True if parsed_args.unprotected: kwargs['protected'] = False if parsed_args.public: kwargs['is_public'] = True if parsed_args.private: kwargs['is_public'] = False if not parsed_args.location and not parsed_args.copy_from: if parsed_args.volume: volume_client = self.app.client_manager.volume source_volume = utils.find_resource( volume_client.volumes, parsed_args.volume, ) response, body = volume_client.volumes.upload_to_image( source_volume.id, parsed_args.force, parsed_args.name, parsed_args.container_format, parsed_args.disk_format, ) info = body['os-volume_upload_image'] elif parsed_args.file: # Send an open file handle to glanceclient so it will # do a chunked transfer kwargs["data"] = open(parsed_args.file, "rb") else: # Read file from stdin kwargs["data"] = None if sys.stdin.isatty() is not True: if msvcrt: msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) # Send an open file handle to glanceclient so it will # do a chunked transfer kwargs["data"] = sys.stdin try: image = utils.find_resource( image_client.images, parsed_args.name, ) # Preserve previous properties if any are being set now if image.properties: if parsed_args.properties: image.properties.update(kwargs['properties']) kwargs['properties'] = image.properties except exceptions.CommandError: if not parsed_args.volume: # This is normal for a create or reserve (create w/o an image) # But skip for create from volume image = image_client.images.create(**kwargs) else: # Update an existing reservation # If an image is specified via --file, --location or # --copy-from let the API handle it image = image_client.images.update(image.id, **kwargs) info = {} info.update(image._info) return zip(*sorted(six.iteritems(info)))
def _open(self): if self.filename: self.fd = open(self.filename, "wb") if is_win32: msvcrt.setmode(self.fd.fileno(), os.O_BINARY)
def parse_args(self): (self.options, self.args) = None, [] # parse_args may terminate... (self.options, self.args) = self.parser.parse_args() self.debug = self.options.debug self.verbose = self.options.verbose if self.verbose > 3: level = logging.DEBUG elif self.verbose > 2: level = logging.INFO else: level = logging.WARN logging.basicConfig(level=level) if self.verbose > 1: # debug infos sys.stderr.write("Debug is %s\n" % self.options.debug) sys.stderr.write("Verbosity level set to %d\n" % self.options.verbose) #~ sys.stderr.write("logging module level set to %s\n" % (level,)) sys.stderr.write("Python version: %s\n" % sys.version) if self.options.input_format is not None and self.options.input_format not in memory.load_formats: self.parser.error('Input format %s not supported.' % (self.options.input_format)) if self.options.output_format not in memory.save_formats: self.parser.error('Output format %s not supported.' % (self.options.output_format)) # sanity check of options if self.options.do_run is not None and self.options.do_reset: if self.verbose: sys.stderr.write( "Warning: option --reset ignored as --go is specified!\n") self.options.do_reset = False if self.options.upload_list and self.options.do_reset: if self.verbose: sys.stderr.write( "Warning: option --reset ignored as --upload is specified!\n" ) self.options.do_reset = False if self.options.upload_list and self.options.do_wait: if self.verbose: sys.stderr.write( "Warning: option --wait ignored as --upload is specified!\n" ) self.options.do_wait = False # create a list of functions an arguments if self.options.do_mass_erase: self.add_action(self.mass_erase) if self.options.do_main_erase: self.add_action(self.main_erase) if self.options.do_erase_by_file: self.add_action(self.erase_by_file) if self.options.do_info_erase: self.add_action(self.erase_infomem) for a in self.options.erase_list: try: adr, adr2 = parseAddressRange(a) if adr2 is not None: while adr <= adr2: if not (0x1000 <= adr <= 0xffffff): self.parser.error( "Start address for --erase is not within Flash memory: 0x%04x" % (adr, )) elif adr < 0x1100: modulo = 64 # F2xx XXX: on F1xx/F4xx are segments erased twice elif adr < 0x1200: modulo = 256 else: modulo = 512 adr = adr - (adr % modulo) self.add_action(self.erase, adr) adr = adr + modulo else: self.add_action(self.erase, adr) except ValueError as e: self.parser.error("--erase: %s" % e) default_action = True if self.options.do_erase_check: self.add_action(self.erase_check_by_file) default_action = False if self.options.do_program: self.add_action(self.program_file) default_action = False if self.options.do_verify: self.add_action(self.verify_by_file) default_action = False if self.options.do_upload_by_file: self.add_action(self.upload_by_file) default_action = False # as default action (no other given by user), program if a file is given if default_action and self.args: self.add_action(self.program_file) for a in self.options.upload_list: try: start, end = parseAddressRange(a) if end is None: end = start + 15 self.add_action(self.upload, start, end) except ValueError as e: self.parser.error("--upload: %s" % e) if self.options.do_reset: self.add_action(self.reset) if self.options.upload_list or self.options.do_upload_by_file: self.upload_data = memory.Memory() if self.options.do_run: self.add_action(self.execute, self.options.do_run) else: # XXX reset otherwise, independently of -r option. imitate old behavior self.add_action(self.reset) # prepare output if self.options.output is None: self.output = sys.stdout if sys.platform == "win32": # ensure that the console is in binary mode import os, msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) else: self.output = open(self.options.output, 'wb') # prepare data to download / load files self.download_data = memory.Memory() # prepare downloaded data for filename in self.args: if filename == '-': data = memory.load('<stdin>', sys.stdin, format=self.options.input_format or "titext") else: data = memory.load(filename, format=self.options.input_format) self.download_data.merge(data)
def main(): parser = argparse.ArgumentParser(description=DESCRIPTION) parser.add_argument('-v', '--version', action='version', version=utils.TITLE) parser.add_argument('--quiet', action='store_true', help='Do not print out informational messages') parser.add_argument( '--debug', choices=debug.flagMap, action='append', type=str, default=[], help='Enable one or more categories of SNMP debugging.') parser.add_argument('--row-hint', dest='row_hint', action='store_true', help='Hint for MIBs Type') parser.add_argument( '--mib-source', dest='mib_sources', metavar='<URI|PATH>', action='append', type=str, default=['http://mibs.snmplabs.com/asn1/@mib@'], help='One or more URIs pointing to a collection of ASN.1 MIB files.' 'Optional "@mib@" token gets replaced with desired MIB module ' 'name during MIB search.') parser.add_argument('--mib-module', dest='mib_modules', action='append', type=str, required=True, help='MIB module to generate simulation data from') parser.add_argument( '--start-object', metavar='<MIB::Object|OID>', type=_parse_mib_object, help='Drop all simulation data records prior to this OID specified ' 'as MIB object (MIB::Object) or OID (1.3.6.)') parser.add_argument( '--stop-object', metavar='<MIB::Object|OID>', type=functools.partial(_parse_mib_object, last=True), help='Drop all simulation data records after this OID specified ' 'as MIB object (MIB::Object) or OID (1.3.6.)') parser.add_argument('--manual-values', action='store_true', help='Fill all managed objects values interactively') parser.add_argument( '--automatic-values', type=int, default=5000, help='Probe for suitable managed object value this many times ' 'prior to failing over to manual value specification') parser.add_argument( '--table-size', type=int, default=10, help='Generate SNMP conceptual tables with this many rows') parser.add_argument( '--destination-record-type', choices=RECORD_TYPES, default='snmprec', help='Produce simulation data with record of this type') parser.add_argument('--output-file', metavar='<FILE>', type=str, help='SNMP simulation data file to write records to') parser.add_argument('--string-pool', metavar='<words>', action='append', help='Words to use for simulated string values') parser.add_argument( '--string-pool-file', metavar='<FILE>', type=str, help='File containing the words for simulating SNMP string values') parser.add_argument( '--integer32-range', metavar='<min,max>', type=_parse_range, default=(0, 32), help='Range of values used to populate simulated Integer32 values') parser.add_argument( '--unsigned-range', metavar='<min,max>', type=_parse_range, default=(0, 65535), help='Range of values used to populate simulated Unsigned values') parser.add_argument( '--counter-range', metavar='<min,max>', type=_parse_range, default=(0, 0xffffffff), help='Range of values used to populate simulated Counter values') parser.add_argument( '--counter64-range', metavar='<min,max>', type=_parse_range, default=(0, 0xffffffffffffffff), help='Range of values used to populate simulated Counter64 values') parser.add_argument( '--gauge-range', metavar='<min,max>', type=_parse_range, default=(0, 0xffffffff), help='Range of values used to populate simulated Gauge values') parser.add_argument( '--timeticks-range', metavar='<min,max>', type=_parse_range, default=(0, 0xffffffff), help='Range of values used to populate simulated Timeticks values') args = parser.parse_args() if args.debug: debug.setLogger(debug.Debug(*args.debug)) if args.manual_values: args.automatic_values = 0 if args.string_pool_file: with open(args.string_pool_file) as fl: args.string_pool = fl.read().split() elif args.string_pool: args.string_pool = [ 'Jaded', 'zombies', 'acted', 'quaintly', 'but', 'kept', 'driving', 'their', 'oxen', 'forward' ] if args.output_file: ext = os.path.extsep + RECORD_TYPES[args.destination_record_type].ext if not args.output_file.endswith(ext): args.output_file += ext args.output_file = RECORD_TYPES[args.destination_record_type].open( args.output_file, 'wb') else: args.output_file = sys.stdout if sys.version_info >= (3, 0, 0): # binary mode write args.output_file = sys.stdout.buffer elif sys.platform == "win32": import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) def get_value(syntax, hint='', automatic_values=args.automatic_values): make_guess = args.automatic_values val = None while True: if make_guess: if isinstance(syntax, rfc1902.IpAddress): val = '.'.join( [str(random.randrange(1, 256)) for x in range(4)]) elif isinstance(syntax, rfc1902.TimeTicks): val = random.randrange(args.timeticks_range[0], args.timeticks_range[1]) elif isinstance(syntax, rfc1902.Gauge32): val = random.randrange(args.gauge_range[0], args.gauge_range[1]) elif isinstance(syntax, rfc1902.Counter32): val = random.randrange(args.counter_range[0], args.counter_range[1]) elif isinstance(syntax, rfc1902.Integer32): val = random.randrange(args.integer32_range[0], args.integer32_range[1]) elif isinstance(syntax, rfc1902.Unsigned32): val = random.randrange(args.unsigned_range[0], args.unsigned_range[1]) elif isinstance(syntax, rfc1902.Counter64): val = random.randrange(args.counter64_range[0], args.counter64_range[1]) elif isinstance(syntax, univ.OctetString): maxWords = 10 val = ' '.join([ args.string_pool[random.randrange( 0, len(args.string_pool))] for i in range(random.randrange(1, maxWords)) ]) elif isinstance(syntax, univ.ObjectIdentifier): val = '.'.join(['1', '3', '6', '1', '3'] + [ '%d' % random.randrange(0, 255) for x in range(random.randrange(0, 10)) ]) elif isinstance(syntax, rfc1902.Bits): val = [ random.randrange(0, 256) for x in range(random.randrange(0, 9)) ] else: val = '?' # remove value enumeration try: if syntax.tagSet == rfc1902.Integer32.tagSet: return rfc1902.Integer32(syntax.clone(val)) if syntax.tagSet == rfc1902.Unsigned32.tagSet: return rfc1902.Unsigned32(syntax.clone(val)) if syntax.tagSet == rfc1902.Bits.tagSet: return rfc1902.OctetString(syntax.clone(val)) return syntax.clone(val) except PyAsn1Error as exc: if make_guess == 1: sys.stderr.write( '*** Inconsistent value: %s\r\n*** See constraints and ' 'suggest a better one for:\r\n' % exc) if make_guess: make_guess -= 1 continue sys.stderr.write('%s# Value [\'%s\'] ? ' % ( hint, (val is None and '<none>' or val), )) sys.stderr.flush() line = sys.stdin.readline().strip() if line: if line[:2] == '0x': if line[:4] == '0x0x': line = line[2:] elif isinstance(syntax, univ.OctetString): val = syntax.clone(hexValue=line[2:]) else: val = int(line[2:], 16) else: val = line data_file_handler = snmprec.SnmprecRecord() mib_builder = builder.MibBuilder() # Load MIB tree foundation classes (MibScalar, MibTable, MibTableRow, MibTableColumn) = mib_builder.importSymbols('SNMPv2-SMI', 'MibScalar', 'MibTable', 'MibTableRow', 'MibTableColumn') mib_view_controller = view.MibViewController(mib_builder) compiler.addMibCompiler(mib_builder, sources=args.mib_sources) try: if isinstance(args.start_object, ObjectIdentity): args.start_object.resolveWithMib(mib_view_controller) if isinstance(args.stop_object, ObjectIdentity): args.stop_object.resolveWithMib(mib_view_controller) except error.PySnmpError as exc: sys.stderr.write('ERROR: %s\r\n' % exc) return 1 output = [] # MIBs walk for modName in args.mib_modules: if not args.quiet: sys.stderr.write( '# MIB module: %s, from %s till ' '%s\r\n' % (modName, args.start_object or 'the beginning', args.stop_object or 'the end')) try: oid = ObjectIdentity(modName).resolveWithMib(mib_view_controller) except error.PySnmpError as exc: sys.stderr.write('ERROR: failed on MIB %s: ' '%s\r\n' % (modName, exc)) return 1 hint = row_hint = '' row_oid = None suffix = () this_table_size = 0 while True: try: oid, label, _ = mib_view_controller.getNextNodeName(oid) except error.NoSuchObjectError: break if row_oid and not row_oid.isPrefixOf(oid): this_table_size += 1 if args.automatic_values: if this_table_size < args.table_size: oid = tuple(row_oid) if not args.quiet: sys.stderr.write( '# Synthesizing row #%d of table %s\r\n' % (this_table_size, row_oid)) else: if not args.quiet: sys.stderr.write( '# Finished table %s (%d rows)\r\n' % (row_oid, this_table_size)) row_oid = None else: while True: sys.stderr.write( '# Synthesize row #%d for table %s (y/n)? ' % (this_table_size, row_oid)) sys.stderr.flush() line = sys.stdin.readline().strip() if line: if line[0] in ('y', 'Y'): oid = tuple(row_oid) break elif line[0] in ('n', 'N'): if not args.quiet: sys.stderr.write( '# Finished table %s (%d rows)\r\n' % (row_oid, this_table_size)) row_oid = None break if args.start_object and oid < args.start_object: continue # skip on premature OID if args.stop_object and oid > args.stop_object: break # stop on out of range condition mib_name, sym_name, _ = mib_view_controller.getNodeLocation(oid) node, = mib_builder.importSymbols(mib_name, sym_name) if isinstance(node, MibTable): hint = '# Table %s::%s\r\n' % (mib_name, sym_name) if not args.quiet: sys.stderr.write( '# Starting table %s::%s (%s)\r\n' % (mib_name, sym_name, univ.ObjectIdentifier(oid))) continue elif isinstance(node, MibTableRow): row_indices = {} suffix = () row_hint = hint + '# Row %s::%s\r\n' % (mib_name, sym_name) for (implied_flag, idx_mod_name, idx_sym_name) in node.getIndexNames(): idxNode, = mib_builder.importSymbols( idx_mod_name, idx_sym_name) row_hint += '# Index %s::%s (type %s)\r\n' % ( idx_mod_name, idx_sym_name, idxNode.syntax.__class__.__name__) row_indices[idxNode.name] = get_value( idxNode.syntax, not args.quiet and row_hint or '') suffix = suffix + node.getAsName(row_indices[idxNode.name], implied_flag) if not row_indices: if not args.quiet: sys.stderr.write( '# WARNING: %s::%s table has no index!\r\n' % (mib_name, sym_name)) if row_oid is None: this_table_size = 0 row_oid = univ.ObjectIdentifier(oid) continue elif isinstance(node, MibTableColumn): oid = node.name if oid in row_indices: val = row_indices[oid] else: hint = '' if not args.quiet: hint += row_hint hint += ('# Column %s::%s (type' ' %s)\r\n' % (mib_name, sym_name, node.syntax.__class__.__name__)) val = get_value(node.syntax, hint) elif isinstance(node, MibScalar): hint = '' if not args.row_hint: hint += ( '# Scalar %s::%s (type %s)' '\r\n' % (mib_name, sym_name, node.syntax.__class__.__name__)) oid = node.name suffix = (0, ) val = get_value(node.syntax, hint) else: hint = '' continue output.append((oid + suffix, val)) output.sort(key=lambda x: univ.ObjectIdentifier(x[0])) unique = set() for oid, val in output: if oid in unique: if not args.quiet: sys.stderr.write('# Dropping duplicate OID %s\r\n' % (univ.ObjectIdentifier(oid), )) else: try: args.output_file.write(data_file_handler.format(oid, val)) except SnmpsimError as exc: sys.stderr.write('ERROR: %s\r\n' % (exc, )) else: unique.add(oid) if not args.quiet: sys.stderr.write('# End of %s, %s OID(s) dumped\r\n' % (modName, len(unique))) args.output_file.flush() args.output_file.close() return 0
def setconsmode(flag): for fh in (sys.stdin, sys.stdout, sys.stderr): msvcrt.setmode(fh.fileno(), flag)
def main(args=None): if args is None: args = sys.argv[1:] from pkg_resources import get_distribution version = get_distribution('qrcode').version parser = optparse.OptionParser(usage=__doc__.strip(), version=version) parser.add_option( "--factory", help="Full python path to the image factory class to " "create the image with. You can use the following shortcuts to the " "built-in image factory classes: {0}.".format(", ".join( sorted(default_factories.keys())))) parser.add_option( "--optimize", type=int, help="Optimize the data by looking for chunks " "of at least this many characters that could use a more efficient " "encoding method. Use 0 to turn off chunk optimization.") parser.add_option( "--error-correction", type='choice', choices=sorted(error_correction.keys()), default='M', help="The error correction level to use. Choices are L (7%), " "M (15%, default), Q (25%), and H (30%).") opts, args = parser.parse_args(args) qr = qrcode.QRCode( error_correction=error_correction[opts.error_correction]) if opts.factory: module = default_factories.get(opts.factory, opts.factory) if '.' not in module: parser.error("The image factory is not a full python path") module, name = module.rsplit('.', 1) imp = __import__(module, {}, [], [name]) image_factory = getattr(imp, name) else: image_factory = None if args: data = args[0] else: # Use sys.stdin.buffer if available (Python 3) avoiding # UnicodeDecodeErrors. stdin_buffer = getattr(sys.stdin, 'buffer', sys.stdin) data = stdin_buffer.read() if opts.optimize is None: qr.add_data(data) else: qr.add_data(data, optimize=opts.optimize) if image_factory is None and os.isatty(sys.stdout.fileno()): qr.print_ascii(tty=True) return img = qr.make_image(image_factory=image_factory) sys.stdout.flush() # Use sys.stdout.buffer if available (Python 3), avoiding # UnicodeDecodeErrors. stdout_buffer = getattr(sys.stdout, 'buffer', None) if not stdout_buffer: if sys.platform == 'win32': # pragma: no cover import msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) stdout_buffer = sys.stdout img.save(stdout_buffer)
from __future__ import absolute_import import sys import os import six import platform ostype = platform.uname()[0] if ostype == 'Windows': if six.PY3: sys.stdout = sys.stdout.buffer else: try: import msvcrt msvcrt.setmode(1, os.O_BINARY) # @UndefinedVariable msvcrt.setmode(2, os.O_BINARY) # @UndefinedVariable except: pass else: if six.PY3: sys.stdout = sys.stdout.buffer if __name__ == "__main__": try: sys.path.append(os.path.abspath(os.path.join('.', 'parallelp', 'pp'))) from parallelp.pp.ppworker import _WorkerProcess wp = _WorkerProcess() wp.run() except Exception as exc: pass