def testEnsureDirectoryExists(self): """Test that EnsureDirectoryExists fuctions correctly.""" temp_dir = tempfile.mkdtemp() try: self.assertTrue(os.path.exists(temp_dir)) # Directory does not exist, yet. full = os.path.join(temp_dir, "foo", "bar") self.assertFalse(os.path.exists(full)) # Create the directory. fileutil.EnsureDirectoryExists(full) self.assertTrue(os.path.exists(full)) # Trying to create it again does not cause an error. fileutil.EnsureDirectoryExists(full) self.assertTrue(os.path.exists(full)) # Bypass check for directory existence to tickle error handling that # occurs in response to a race. fileutil.EnsureDirectoryExists(full, always_try_to_create=True) self.assertTrue(os.path.exists(full)) finally: shutil.rmtree(temp_dir)
def _DoGenerateFiles(self): fileutil.EnsureDirectoryExists(self.output_dir) for struct in self.module.structs: self.WriteWithComment(self._GenerateStructSource(struct), '%s.java' % GetNameForElement(struct)) for union in self.module.unions: self.WriteWithComment(self._GenerateUnionSource(union), '%s.java' % GetNameForElement(union)) for enum in self.module.enums: self.WriteWithComment(self._GenerateEnumSource(enum), '%s.java' % GetNameForElement(enum)) for interface in self.module.interfaces: self.WriteWithComment(self._GenerateInterfaceSource(interface), '%s.java' % GetNameForElement(interface)) self.WriteWithComment( self._GenerateInterfaceInternalSource(interface), '%s_Internal.java' % GetNameForElement(interface)) if self.module.constants: self.WriteWithComment( self._GenerateConstantsSource(self.module), '%s.java' % GetConstantsMainEntityName(self.module))
def _Generate(args, remaining_args): if args.variant == "none": args.variant = None for idx, import_dir in enumerate(args.import_directories): tokens = import_dir.split(":") if len(tokens) >= 2: args.import_directories[idx] = RelativePath(tokens[0], tokens[1]) else: args.import_directories[idx] = RelativePath(tokens[0], args.depth) generator_modules = LoadGenerators(args.generators_string) fileutil.EnsureDirectoryExists(args.output_dir) processor = MojomProcessor(lambda filename: filename in args.filename) processor.LoadTypemaps(set(args.typemaps)) for filename in args.filename: processor.ProcessFile(args, remaining_args, generator_modules, filename) if args.depfile: assert args.depfile_target with open(args.depfile, 'w') as f: f.write('%s: %s' % (args.depfile_target, ' '.join( processor._parsed_files.keys()))) return 0
def _VerifyImportDeps(args, __): fileutil.EnsureDirectoryExists(args.gen_dir) for filename in args.filename: rel_path = RelativePath(filename, args.depth) tree = _UnpickleAST(_GetPicklePath(rel_path, args.gen_dir)) mojom_imports = set(parsed_imp.import_filename for parsed_imp in tree.import_list) # read the paths from the file f_deps = open(args.deps_file, 'r') deps_sources = set() for deps_path in f_deps: deps_path = deps_path.rstrip('\n') f_sources = open(deps_path, 'r') for source_file in f_sources: source_dir = deps_path.split(args.gen_dir + "/", 1)[1] full_source_path = os.path.dirname(source_dir) + "/" + \ source_file deps_sources.add(full_source_path.rstrip('\n')) if (not deps_sources.issuperset(mojom_imports)): print ">>> [%s] Missing dependencies for the following imports: %s" % ( \ args.filename[0], \ list(mojom_imports.difference(deps_sources))) sys.exit(1) source_filename, _ = os.path.splitext(rel_path.relative_path()) output_file = source_filename + '.v' output_file_path = os.path.join(args.gen_dir, output_file) WriteFile("", output_file_path) return 0
def _Generate(args, remaining_args): if args.variant == "none": args.variant = None for idx, import_dir in enumerate(args.import_directories): tokens = import_dir.split(":") if len(tokens) >= 2: args.import_directories[idx] = RelativePath(tokens[0], tokens[1]) else: args.import_directories[idx] = RelativePath(tokens[0], args.depth) generator_modules = LoadGenerators(args.generators_string) fileutil.EnsureDirectoryExists(args.output_dir) processor = MojomProcessor(lambda filename: filename in args.filename) processor.LoadTypemaps(set(args.typemaps)) if args.filelist: with open(args.filelist) as f: args.filename.extend(f.read().split()) for filename in args.filename: processor._GenerateModule(args, remaining_args, generator_modules, RelativePath(filename, args.depth), []) return 0
def DoGenerateFiles(self): fileutil.EnsureDirectoryExists(self.output_dir) # Keep this above the others as .GetStructs() changes the state of the # module, annotating structs with required information. for struct in self.GetStructs(): self.Write(self.GenerateStructSource(struct), '%s.java' % GetNameForElement(struct)) for union in self.module.unions: self.Write(self.GenerateUnionSource(union), '%s.java' % GetNameForElement(union)) for enum in self.module.enums: self.Write(self.GenerateEnumSource(enum), '%s.java' % GetNameForElement(enum)) for interface in self.GetInterfaces(): self.Write(self.GenerateInterfaceSource(interface), '%s.java' % GetNameForElement(interface)) self.Write(self.GenerateInterfaceInternalSource(interface), '%s_Internal.java' % GetNameForElement(interface)) if self.module.constants: self.Write(self.GenerateConstantsSource(self.module), '%s.java' % GetConstantsMainEntityName(self.module))
def GenerateFiles(self, args): elements = self.module.namespace.split('.') elements.append("%s.dart" % self.module.name) package_name = GetPackage(self.module) lib_module = self.GenerateLibModule(args) pkg_path = os.path.join("dart-pkg", package_name, "lib", *elements) self.Write(lib_module, pkg_path) gen_path = os.path.join("dart-gen", package_name, "lib", *elements) full_gen_path = os.path.join(self.output_dir, gen_path) self.Write(lib_module, gen_path) link = self.MatchMojomFilePath("%s.dart" % self.module.name) full_link_path = os.path.join(self.output_dir, link) if os.path.exists(full_link_path): os.unlink(full_link_path) fileutil.EnsureDirectoryExists(os.path.dirname(full_link_path)) try: if sys.platform == "win32": shutil.copy(full_gen_path, full_link_path) else: os.symlink(full_gen_path, full_link_path) except OSError as e: # Errno 17 is file already exists. If the link fails because file already # exists assume another instance of this script tried to create the same # file and continue on. if e.errno != 17: raise e
def GenerateFiles(self, unparsed_args): parser = argparse.ArgumentParser() parser.add_argument('--libcamera_generate_core_header', action='store_true') parser.add_argument('--libcamera_generate_core_serializer', action='store_true') parser.add_argument('--libcamera_generate_header', action='store_true') parser.add_argument('--libcamera_generate_serializer', action='store_true') parser.add_argument('--libcamera_generate_proxy_cpp', action='store_true') parser.add_argument('--libcamera_generate_proxy_h', action='store_true') parser.add_argument('--libcamera_generate_proxy_worker', action='store_true') parser.add_argument('--libcamera_output_path') args = parser.parse_args(unparsed_args) if not args.libcamera_generate_core_header and \ not args.libcamera_generate_core_serializer: ValidateNamespace(self.module.mojom_namespace) ValidateInterfaces(self.module.interfaces) self.module_name = ModuleClassName(self.module) fileutil.EnsureDirectoryExists(os.path.dirname(args.libcamera_output_path)) gen_funcs = [ [args.libcamera_generate_core_header, self._GenerateCoreHeader], [args.libcamera_generate_core_serializer, self._GenerateCoreSerializer], [args.libcamera_generate_header, self._GenerateDataHeader], [args.libcamera_generate_serializer, self._GenerateSerializer], [args.libcamera_generate_proxy_cpp, self._GenerateProxyCpp], [args.libcamera_generate_proxy_h, self._GenerateProxyHeader], [args.libcamera_generate_proxy_worker, self._GenerateProxyWorker], ] for pair in gen_funcs: if pair[0]: self.Write(pair[1](), args.libcamera_output_path)
def ZipContentInto(root, zip_filename): fileutil.EnsureDirectoryExists(os.path.dirname(zip_filename)) with zipfile.ZipFile(zip_filename, 'w') as zip_file: for dirname, _, files in os.walk(root): for filename in files: path = os.path.join(dirname, filename) path_in_archive = os.path.relpath(path, root) zip_file.write(path, path_in_archive)
def WriteFile(contents, full_path): # Make sure the containing directory exists. full_dir = os.path.dirname(full_path) fileutil.EnsureDirectoryExists(full_dir) # Dump the data to disk. with open(full_path, "w+") as f: f.write(contents)
def _PickleAST(ast, output_file): full_dir = os.path.dirname(output_file) fileutil.EnsureDirectoryExists(full_dir) try: WriteFile(cPickle.dumps(ast), output_file) except (IOError, cPickle.PicklingError) as e: print "%s: Error: %s" % (output_file, str(e)) sys.exit(1)
def _Parse(args, _): fileutil.EnsureDirectoryExists(args.output_dir) if args.filelist: with open(args.filelist) as f: args.filename.extend(f.read().split()) for filename in args.filename: _ParseFile(args, RelativePath(filename, args.depth)) return 0
def _Generate(args, remaining_args): if args.variant == "none": args.variant = None generator_modules = LoadGenerators(args.generators_string) fileutil.EnsureDirectoryExists(args.output_dir) processor = MojomProcessor(lambda filename: filename in args.filename) processor.LoadTypemaps(set(args.typemaps)) for filename in args.filename: processor.ProcessFile(args, remaining_args, generator_modules, filename) return 0
def WriteFile(contents, full_path): # If |contents| is same with the file content, we skip updating. if os.path.isfile(full_path): with open(full_path, 'rb') as destination_file: if destination_file.read() == contents: return # Make sure the containing directory exists. full_dir = os.path.dirname(full_path) fileutil.EnsureDirectoryExists(full_dir) # Dump the data to disk. with open(full_path, "wb") as f: f.write(contents)
def main(): args = parse_args() mojom_module = args.mojom_module[0] output_dir = args.output_dir[0] excluded = args.exclude[0] if args.exclude else "" generator_module = importlib.import_module('mojom_objc_generator') bytecode_path = os.path.join(output_dir, "objc_templates_bytecode") fileutil.EnsureDirectoryExists(bytecode_path) template_expander.PrecompileTemplates({"objc": generator_module}, bytecode_path) generator = generator_module.Generator(None) generator.bytecode_path = bytecode_path generator.excludedTypes = excluded.split(',') with open(mojom_module, 'rb') as f: generator.module = Module.Load(f) generator.GenerateFiles(output_dir)
def main(): parser = argparse.ArgumentParser( description="Generate bindings from mojom files.") parser.add_argument("filename", nargs="+", help="mojom input file") parser.add_argument("-d", "--depth", dest="depth", default=".", help="depth from source root") parser.add_argument("-o", "--output_dir", dest="output_dir", default=".", help="output directory for generated files") parser.add_argument("-g", "--generators", dest="generators_string", metavar="GENERATORS", default="c++,javascript,java", help="comma-separated list of generators") parser.add_argument("--debug_print_intermediate", action="store_true", help="print the intermediate representation") parser.add_argument("-I", dest="import_directories", action="append", metavar="directory", default=[], help="add a directory to be searched for import files") parser.add_argument("--use_bundled_pylibs", action="store_true", help="use Python modules bundled in the SDK") (args, remaining_args) = parser.parse_known_args() generator_modules = LoadGenerators(args.generators_string) fileutil.EnsureDirectoryExists(args.output_dir) processor = MojomProcessor(lambda filename: filename in args.filename) for filename in args.filename: processor.ProcessFile(args, remaining_args, generator_modules, filename) return 0
def WriteFile(contents, full_path): # If |contents| is same with the file content, we skip updating. if not isinstance(contents, bytes): data = contents.encode('utf8') else: data = contents if os.path.isfile(full_path): with open(full_path, 'rb') as destination_file: if destination_file.read() == data: return # Make sure the containing directory exists. full_dir = os.path.dirname(full_path) fileutil.EnsureDirectoryExists(full_dir) # Dump the data to disk. with open(full_path, 'wb') as f: f.write(data)
def GenerateFiles(self, args): elements = self.module.namespace.split('.') elements.append("%s.dart" % self.module.name) lib_module = self.GenerateLibModule(args) # List of packages with checked in bindings. # TODO(johnmccutchan): Stop generating bindings as part of build system # and then remove this. packages_with_checked_in_bindings = [ '_mojo_for_test_only', 'mojo', 'mojo_apptest', 'mojo_services', 'mojo_sdk', 'mojom', ] package_name = GetPackage(self.module) if not (package_name in packages_with_checked_in_bindings): pkg_path = os.path.join("dart-pkg", package_name, "lib", *elements) self.Write(lib_module, pkg_path) gen_path = os.path.join("dart-gen", package_name, "lib", *elements) full_gen_path = os.path.join(self.output_dir, gen_path) self.Write(lib_module, gen_path) link = self.MatchMojomFilePath("%s.dart" % self.module.name) full_link_path = os.path.join(self.output_dir, link) if os.path.exists(full_link_path): os.unlink(full_link_path) fileutil.EnsureDirectoryExists(os.path.dirname(full_link_path)) try: if sys.platform == "win32": shutil.copy(full_gen_path, full_link_path) else: os.symlink(full_gen_path, full_link_path) except OSError as e: # Errno 17 is file already exists. If the link fails because file already # exists assume another instance of this script tried to create the same # file and continue on. if e.errno != 17: raise e
def main(): global ast_root_dir args = parse_args() mojom_module = args.mojom_module[0] module_include_path = args.module_include_path[0] output_dir = args.output_dir[0] ast_root_dir = os.path.dirname(mojom_module) generator_module = importlib.import_module('mojom_objc_generator') bytecode_path = os.path.join(output_dir, "objc_templates_bytecode") fileutil.EnsureDirectoryExists(bytecode_path) template_expander.PrecompileTemplates({"objc": generator_module}, bytecode_path) generator = generator_module.Generator(None) generator.bytecode_path = bytecode_path generator.module_include_path = module_include_path with open(mojom_module, 'rb') as f: generator.module = Module.Load(f) generator.GenerateFiles(output_dir)
def _VerifyImportDeps(args, __): fileutil.EnsureDirectoryExists(args.gen_dir) if args.filelist: with open(args.filelist) as f: args.filename.extend(f.read().split()) for filename in args.filename: rel_path = RelativePath(filename, args.depth) tree = _UnpickleAST(_GetPicklePath(rel_path, args.gen_dir)) mojom_imports = set(parsed_imp.import_filename for parsed_imp in tree.import_list) sources = set() target_prefix = args.deps_file.split(".deps_sources_list")[0] sources = GetSourcesList(target_prefix, sources, args.gen_dir) if (not sources.issuperset(mojom_imports)): target_name = target_prefix.rsplit("/", 1)[1] target_prefix_without_gen_dir = target_prefix.split( args.gen_dir + "/", 1)[1] full_target_name = "//" + target_prefix_without_gen_dir.rsplit( "/", 1)[0] + ":" + target_name print(">>> File \"%s\"" % filename) print(">>> from target \"%s\"" % full_target_name) print( ">>> is missing dependencies for the following imports:\n%s" % list(mojom_imports.difference(sources))) sys.exit(1) source_filename, _ = os.path.splitext(rel_path.relative_path()) output_file = source_filename + '.v' output_file_path = os.path.join(args.gen_dir, output_file) WriteFile(b"", output_file_path) return 0
def main(): global ast_root_dir args = parse_args() pickled_ast = args.pickled_ast[0] module_include_path = args.module_include_path[0] mojom_file = args.mojom_file[0] output_dir = args.output_dir[0] ast_root_dir = os.path.dirname(pickled_ast) tree = _UnpickleAST(pickled_ast) generator_module = importlib.import_module('mojom_objc_generator') bytecode_path = os.path.join(output_dir, "objc_templates_bytecode") fileutil.EnsureDirectoryExists(bytecode_path) template_expander.PrecompileTemplates({"objc": generator_module}, bytecode_path) generator = generator_module.Generator(None) generator.bytecode_path = bytecode_path generator.module_include_path = module_include_path generator.module = _GenerateModule(tree, mojom_file) generator.GenerateFiles(output_dir)
class Generator(generator.Generator): dart_filters = { 'array_expected_length': GetArrayExpectedLength, 'array': GetArrayKind, 'decode_method': DecodeMethod, 'default_value': DartDefaultValue, 'encode_method': EncodeMethod, 'is_imported_kind': IsImportedKind, 'is_array_kind': mojom.IsArrayKind, 'is_map_kind': mojom.IsMapKind, 'is_numerical_kind': mojom.IsNumericalKind, 'is_any_handle_kind': mojom.IsAnyHandleKind, 'is_string_kind': mojom.IsStringKind, 'is_nullable_kind': mojom.IsNullableKind, 'is_pointer_array_kind': IsPointerArrayKind, 'is_enum_array_kind': IsEnumArrayKind, 'is_struct_kind': mojom.IsStructKind, 'is_union_kind': mojom.IsUnionKind, 'is_enum_kind': mojom.IsEnumKind, 'is_interface_kind': mojom.IsInterfaceKind, 'is_interface_request_kind': mojom.IsInterfaceRequestKind, 'dart_true_false': GetDartTrueFalse, 'dart_type': DartDeclType, 'name': GetNameForElement, 'interface_response_name': GetInterfaceResponseName, 'dot_to_underscore': DotToUnderscore, 'is_cloneable_kind': mojom.IsCloneableKind, 'upper_camel': UpperCamelCase, 'lower_camel': CamelCase, 'raise': RaiseHelper, } # If set to True, then mojom type information will be generated. should_gen_mojom_types = False def GetParameters(self, args): package = self.module.name.split('.')[0] # True if handles are used anywhere in the mojom. has_handles = any( not mojom.IsCloneableKind(kind) for kind in (self.GetStructs() + self.GetStructsFromMethods() + self.GetUnions())) parameters = { "namespace": self.module.namespace, "imports": self.GetImports(args), "kinds": self.module.kinds, "enums": self.module.enums, "module": resolver.ResolveConstants(self.module, ExpressionToText), "structs": self.GetStructs() + self.GetStructsFromMethods(), "unions": self.GetUnions(), "interfaces": self.GetInterfaces(), "imported_interfaces": self.GetImportedInterfaces(), "imported_from": self.ImportedFrom(), "typepkg": '%s.' % _mojom_types_pkg_short, "descpkg": '%s.' % _service_describer_pkg_short, "mojom_types_import": 'import \'%s\' as %s;' % \ (_mojom_types_pkg, _mojom_types_pkg_short), "service_describer_import": 'import \'%s\' as %s;' % \ (_service_describer_pkg, _service_describer_pkg_short), "has_handles": has_handles, } # If this is the mojom types package, clear the import-related params. if package == _mojom_types_pkg_short: parameters["typepkg"] = "" parameters["mojom_types_import"] = "" # If this is the service describer package, clear the import-related params. if package == _service_describer_pkg_short: parameters["descpkg"] = "" parameters["service_describer_import"] = "" # If no interfaces were defined, the service describer import isn't needed. if len(self.module.interfaces) == 0: parameters["service_describer_import"] = "" return parameters def GetGlobals(self): return { 'should_gen_mojom_types': self.should_gen_mojom_types, } @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) def GenerateLibModule(self, args): return self.GetParameters(args) def GenerateFiles(self, args): self.should_gen_mojom_types = "--generate_type_info" in args elements = self.module.namespace.split('.') elements.append("%s.dart" % self.module.name) lib_module = self.GenerateLibModule(args) # List of packages with checked in bindings. # TODO(johnmccutchan): Stop generating bindings as part of build system # and then remove this. packages_with_checked_in_bindings = [ '_mojo_for_test_only', 'mojo', 'mojo_apptest', 'mojo_services', 'mojo_sdk', 'mojom', ] package_name = GetPackage(self.module) if not (package_name in packages_with_checked_in_bindings): pkg_path = os.path.join("dart-pkg", package_name, "lib", *elements) self.Write(lib_module, pkg_path) gen_path = os.path.join("dart-gen", package_name, "lib", *elements) full_gen_path = os.path.join(self.output_dir, gen_path) self.Write(lib_module, gen_path) link = self.MatchMojomFilePath("%s.dart" % self.module.name) full_link_path = os.path.join(self.output_dir, link) try: os.unlink(full_link_path) except OSError, exc: # If the file does not exist, ignore the error. if errno.ENOENT == exc.errno: pass else: raise fileutil.EnsureDirectoryExists(os.path.dirname(full_link_path)) try: if sys.platform == "win32": shutil.copy(full_gen_path, full_link_path) else: os.symlink(full_gen_path, full_link_path) except OSError as e: # Errno 17 is file already exists. If the link fails because file already # exists assume another instance of this script tried to create the same # file and continue on. if e.errno != 17: raise e
def _Parse(args, _): fileutil.EnsureDirectoryExists(args.output_dir) for filename in args.filename: _ParseFile(args, RelativePath(filename, args.depth)) return 0
class Generator(generator.Generator): dart_filters = { 'array_expected_length': GetArrayExpectedLength, 'array': GetArrayKind, 'decode_method': DecodeMethod, 'default_value': DartDefaultValue, 'encode_method': EncodeMethod, 'is_map_kind': mojom.IsMapKind, 'is_nullable_kind': mojom.IsNullableKind, 'is_pointer_array_kind': IsPointerArrayKind, 'is_enum_array_kind': IsEnumArrayKind, 'is_struct_kind': mojom.IsStructKind, 'is_union_kind': mojom.IsUnionKind, 'is_enum_kind': mojom.IsEnumKind, 'dart_true_false': GetDartTrueFalse, 'dart_type': DartDeclType, 'name': GetNameForElement, 'interface_response_name': GetInterfaceResponseName, 'dot_to_underscore': DotToUnderscore, 'is_cloneable_kind': mojom.IsCloneableKind, } def GetParameters(self, args): return { "namespace": self.module.namespace, "imports": self.GetImports(args), "kinds": self.module.kinds, "enums": self.module.enums, "module": resolver.ResolveConstants(self.module, ExpressionToText), "structs": self.GetStructs() + self.GetStructsFromMethods(), "unions": self.GetUnions(), "interfaces": self.GetInterfaces(), "imported_interfaces": self.GetImportedInterfaces(), "imported_from": self.ImportedFrom(), } @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) def GenerateLibModule(self, args): return self.GetParameters(args) def GenerateFiles(self, args): elements = self.module.namespace.split('.') elements.append("%s.dart" % self.module.name) lib_module = self.GenerateLibModule(args) # List of packages with checked in bindings. # TODO(johnmccutchan): Stop generating bindings as part of build system # and then remove this. packages_with_checked_in_bindings = [ '_mojo_for_test_only', 'mojo', 'mojo_apptest', 'mojo_services', 'mojo_sdk', 'mojom', ] package_name = GetPackage(self.module) if not (package_name in packages_with_checked_in_bindings): pkg_path = os.path.join("dart-pkg", package_name, "lib", *elements) self.Write(lib_module, pkg_path) gen_path = os.path.join("dart-gen", package_name, "lib", *elements) full_gen_path = os.path.join(self.output_dir, gen_path) self.Write(lib_module, gen_path) link = self.MatchMojomFilePath("%s.dart" % self.module.name) full_link_path = os.path.join(self.output_dir, link) try: os.unlink(full_link_path) except OSError, exc: # If the file does not exist, ignore the error. if errno.ENOENT == exc.errno: pass else: raise fileutil.EnsureDirectoryExists(os.path.dirname(full_link_path)) try: if sys.platform == "win32": shutil.copy(full_gen_path, full_link_path) else: os.symlink(full_gen_path, full_link_path) except OSError as e: # Errno 17 is file already exists. If the link fails because file already # exists assume another instance of this script tried to create the same # file and continue on. if e.errno != 17: raise e