def test_read(self): # given that there is no generic parser, emulate one like so def parser(text): result = Node() result.raw = text return result stream = StringIO('var foo = "bar";') node = io.read(parser, stream) self.assertIsNone(node.sourcepath) stream.name = 'somefile.js' node = io.read(parser, stream) self.assertEqual(node.sourcepath, 'somefile.js')
def check_all_alias_declared(self, alias, name_checker): missing = set() for modname, path in alias.items(): # look into how to throw in a preprocess hook to the # unparser to report this, to maybe save on parse time. if not exists(path): logger.warning( "alias '%s' points to '%s' but file does not exist", modname, path, ) continue with codecs.open(path, 'r', encoding='utf8') as fd: tree = io.read(parse, fd) new_missing = [ name for name in yield_module_imports(tree) if not name_checker(name) ] if new_missing: logger.info( "modname '%s' has references not in modules: %s", modname, new_missing, ) missing.update(new_missing) return missing
def readwrite(self, source): unparser = convert_dynamic_require_unparser() original = StringIO(source) original.name = 'source.js' output = StringIO() output.name = 'output.js' srcmap = StringIO() srcmap.name = 'output.js.map' io.write(unparser, io.read(es5, original), output, srcmap) return output, srcmap
def test_read_callable(self): # given that there is no generic parser, emulate one like so def parser(text): result = Node() result.raw = text return result stream = StringIO('var foo = "bar";') def close(): stream._closed = True def wrapper(): stream.close = close return stream stream = StringIO('var foo = "bar";') io.read(parser, wrapper) self.assertTrue(stream._closed)
def test_read_error(self): # given that there is no generic parser, emulate one like so def parser(text): raise ECMASyntaxError('Illegal input') def not_a_parser(text): raise Exception('Illegal input') stream = StringIO('var foo = "bar";') stream.name = 'somefile.js' with self.assertRaises(Exception) as e: io.read(parser, stream) self.assertEqual("Illegal input in 'somefile.js'", e.exception.args[0]) stream = StringIO('var foo = "bar";') with self.assertRaises(Exception) as e: io.read(not_a_parser, stream) self.assertNotEqual("Illegal input in 'somefile.js'", e.exception.args[0])
def _finalize_test_path(toolchain, spec, modname, path): """ Process the path as a test file and bring it to a finalized location if necessary. Current condition for the relocation is usage of dynamic imports within the provided ES5 source file. """ if not exists(path): # nothing to do. return path with codecs.open(path, encoding='utf8') as fd: try: tree = io.read(parse, fd) imports = yield_module_imports_nodes(tree) except Exception: # can't do anything. return path # if there are not any nodes that are not strings if not any(node for node in imports if not isinstance(node, asttypes.String)): return path # generate the new target using the toolchain instance. rel_target = toolchain.modname_source_to_target(spec, modname, path) target = join(spec[BUILD_DIR], normpath(rel_target)) target_map = target + '.map' makedirs(dirname(target)) with codecs.open(target, 'w', encoding='utf8') as ss: with codecs.open(target_map, 'w', encoding='utf8') as sm: io.write(convert_dynamic_require_unparser(), tree, ss, sm) return target
def run(inputs, output, mangle, obfuscate, pretty, source_map, indent_width, drop_semi, encoding, version): """ Not a general use method, as sys.exit is called. """ def stdin(): return ( sys.stdin if isinstance(sys.stdin, (StringIO, TextIOWrapper)) else codecs.getreader(sys.stdin.encoding or encoding)(sys.stdin) ) def stdout(): return ( sys.stdout if isinstance(sys.stdout, (StringIO, TextIOWrapper)) else codecs.getwriter(sys.stdout.encoding or encoding)(sys.stdout) ) abs_output = abspath(output) if output else output abs_source_map = abspath(source_map) if source_map else source_map input_streams = ( [partial(codecs.open, abspath(p), encoding=encoding) for p in inputs] if inputs else [stdin] ) output_stream = ( partial(codecs.open, abs_output, 'w', encoding=encoding) if abs_output else stdout ) # if source_map is flagged (from empty string, in the const), if # no further name is supplied, use the output to build the path, # if it is also not stdout. source_map_path = ( abs_output + '.map' if source_map == '' and output_stream is not stdout else abs_source_map ) if source_map and abs_source_map == abs_output: # if a source_map was specified and the absolute paths of that # and the output is equal, use the output_stream that was # already created. sourcemap_stream = output_stream else: # no source map if source_map path is actually None, and only # generate a callable if the source_map_path is not an empty # string. sourcemap_stream = None if source_map_path is None else ( partial(codecs.open, source_map_path, 'w', encoding=encoding) if source_map_path else stdout ) enabled_rules = [rules.minify(drop_semi=drop_semi or mangle)] if obfuscate or mangle: enabled_rules.append(rules.obfuscate( reserved_keywords=Lexer.keywords_dict.keys() )) if pretty: enabled_rules.append(rules.indent(indent_str=' ' * indent_width)) printer = Unparser(rules=enabled_rules) try: io.write( printer, (io.read(parse, f) for f in input_streams), output_stream, sourcemap_stream, ) except ECMASyntaxError as e: logger.error('%s', e) sys.exit(1) except (IOError, OSError) as e: logger.error('%s', e) if e.args and isinstance(e.args[0], int): sys.exit(e.args[0]) sys.exit(5) # EIO except UnicodeDecodeError as e: logger.error('read error: %s', e) sys.exit(1) except UnicodeEncodeError as e: logger.error('write error: %s', e) sys.exit(1) except KeyboardInterrupt: sys.exit(130) # no need to close any streams as they are callables and that the io # read/write functions take care of that. sys.exit(0)