), filter_unsupported_attributes=CommandLine.EntryPoint.Parameter( "Ignore element attributes that aren't supported; by default, unsupported attributes will generate an error" ), plugin_arg=CommandLine.EntryPoint.Parameter( "Argument passes directly to the plugin"), force=CommandLine.EntryPoint.Parameter("Force generation"), verbose=CommandLine.EntryPoint.Parameter( "Generate verbose output during generation"), ) @CommandLine.Constraints( plugin=_PluginTypeInfo, output_name=CommandLine.StringTypeInfo(), output_dir=CommandLine.DirectoryTypeInfo(ensure_exists=False, ), input=CommandLine.FilenameTypeInfo( match_any=True, arity="+", ), include=CommandLine.StringTypeInfo(arity="*", ), exclude=CommandLine.StringTypeInfo(arity="*", ), output_data_filename_prefix=CommandLine.StringTypeInfo(arity="?", ), plugin_arg=CommandLine.DictTypeInfo( require_exact_match=False, arity="*", ), output_stream=None, ) def Generate( plugin, output_name, output_dir, input,
re.compile(r"sparse_vector\<\S+\>"), re.compile(r"single_value_sparse_vector\<\S+\>"), re.compile(r"tuple<.+>"), ], ) # ---------------------------------------------------------------------- @CommandLine.EntryPoint( include=CommandLine.EntryPoint.Parameter( "Regular expression specifying the name of featurizers to include", ), exclude=CommandLine.EntryPoint.Parameter( "Regular expression specifying the name of featurizers to exclude", ), ) @CommandLine.Constraints( plugin=_PluginTypeInfo, input_filename=CommandLine.FilenameTypeInfo(), output_dir=CommandLine.DirectoryTypeInfo(ensure_exists=False, ), include=CommandLine.StringTypeInfo(arity="*", ), exclude=CommandLine.StringTypeInfo(arity="*", ), output_stream=None, ) def EntryPoint( plugin, input_filename, output_dir, include=None, exclude=None, output_stream=sys.stdout, ): """Generates content based on a configuration file according to the specified plugin"""
class CodeGenerator( AtomicInputProcessingMixin, ConditionalInvocationQueryMixin, MultipleOutputMixin, CodeGeneratorBase, ): # ---------------------------------------------------------------------- # | Types ContextCode = namedtuple("ContextCode", ["filename", "var_name"]) # ---------------------------------------------------------------------- # | Properties Name = Interface.DerivedProperty("Jinja2CodeGenerator") Description = Interface.DerivedProperty( "Processes a Jinja2 template and produces output") InputTypeInfo = Interface.DerivedProperty( CommandLine.FilenameTypeInfo( validation_expression=".+?\.jinja2(?:\..+)?", ), ) # ---------------------------------------------------------------------- # | Methods # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- @classmethod @Interface.override def _GetOptionalMetadata(cls): return [("jinja2_context", {}), ("jinja2_context_code", []), ("preserve_dir_structure", False), ("ignore_errors", False), ("debug", False)] + super( CodeGenerator, cls, )._GetOptionalMetadata() # ---------------------------------------------------------------------- @classmethod @Interface.override def _GetRequiredMetadataNames(cls): return ["output_dir"] + super(CodeGenerator, cls)._GetRequiredMetadataNames() # ---------------------------------------------------------------------- @classmethod @Interface.override def _CreateContext(cls, metadata, status_stream): jinja2_context = {} # Load the custom context defined in code for context_code in metadata["jinja2_context_code"]: dirname, basename = os.path.split(context_code) basename = os.path.splitext(basename)[0] sys.path.insert(0, dirname) with CallOnExit(lambda: sys.path.pop(0)): mod = importlib.import_module(basename) var = getattr(mod, context_code.var_name) del mod if isinstance(var, dict): for k, v in six.iteritems(var): jinja2_context[k] = v else: jinja2_context[context_code.var_name] = var del metadata["jinja2_context_code"] # Load the custom context for k, v in six.iteritems(metadata["jinja2_context"]): if len(v) == 1: jinja2_context[k] = v[0] else: jinja2_context[k] = v metadata["jinja2_context"] = jinja2_context # Calculate the hashes of the input filenames. We will use this information # during comparison to determine if an input file has changed. It appears # that this value isn't used, but it is actually used when comparing the # context of two different invocations. # ---------------------------------------------------------------------- def CalculateHash(input_filename): with open(input_filename, "rb") as f: return hashlib.sha256(f.read()).digest() # ---------------------------------------------------------------------- metadata["hashes"] = [ CalculateHash(input_filename) for input_filename in metadata["inputs"] ] # Get the output filenames if not metadata["preserve_dir_structure"]: # ---------------------------------------------------------------------- def GetBaseDir(input_filename): return '' # ---------------------------------------------------------------------- else: if len(metadata["inputs"]) == 1: common_prefix = os.path.dirname(metadata["inputs"][0]) else: common_prefix = FileSystem.GetCommonPath(*metadata["inputs"]) # ---------------------------------------------------------------------- def GetBaseDir(input_filename): return FileSystem.TrimPath(input_filename, common_prefix) # ---------------------------------------------------------------------- output_filenames = [] for input_filename in metadata["inputs"]: output_filenames.append( os.path.join( metadata["output_dir"], GetBaseDir(input_filename), '.'.join([ part for part in os.path.basename(input_filename).split(".") if part != "jinja2" ]), ), ) metadata["output_filenames"] = output_filenames return super(CodeGenerator, cls)._CreateContext(metadata, status_stream) # ---------------------------------------------------------------------- @classmethod @Interface.override def _InvokeImpl(cls, invoke_reason, context, status_stream, verbose_stream, verbose): # ---------------------------------------------------------------------- class RelativeFileSystemLoader(FileSystemLoader): # ---------------------------------------------------------------------- def __init__( self, input_filename, searchpath=None, *args, **kwargs, ): super(RelativeFileSystemLoader, self).__init__( searchpath=[os.path.dirname(input_filename)] + (searchpath or []), *args, **kwargs) # ---------------------------------------------------------------------- def get_source(self, environment, template): method = super(RelativeFileSystemLoader, self).get_source try: return method(environment, template) except exceptions.TemplateNotFound: for searchpath in reversed(self.searchpath): potential_template = os.path.normpath( os.path.join(searchpath, template).replace('/', os.path.sep)) if os.path.isfile(potential_template): dirname, basename = os.path.split( potential_template) self.searchpath.append(dirname) return method(environment, template) raise # ---------------------------------------------------------------------- with status_stream.DoneManager(display=False, ) as dm: for index, (input_filename, output_filename) in enumerate( zip( context["inputs"], context["output_filenames"], )): status_stream.write( "Processing '{}' ({} of {})...".format( input_filename, index + 1, len(context["inputs"]), ), ) with dm.stream.DoneManager( suppress_exceptions=True, ) as this_dm: try: # ---------------------------------------------------------------------- def ReadFileFilter(value): potential_filename = os.path.join( os.path.dirname(input_filename), value) if not os.path.isfile(potential_filename): return "<< '{}' was not found >>".format( potential_filename) with open(potential_filename) as f: return f.read() # ---------------------------------------------------------------------- loader = RelativeFileSystemLoader(input_filename) if context["debug"]: from jinja2 import meta env = Environment(loader=loader, ) with open(input_filename) as f: content = env.parse(f.read()) this_dm.stream.write("Variables:\n{}\n".format( "\n".join([ " - {}".format(var) for var in meta.find_undeclared_variables(content) ]))) continue if context["ignore_errors"]: undef = Undefined else: undef = StrictUndefined env = Environment( trim_blocks=True, lstrip_blocks=True, loader=loader, undefined=undef, ) env.tests["valid_file"] = lambda value: os.path.isfile( os.path.dirname(input_filename), value) env.filters[ "doubleslash"] = lambda value: value.replace( "\\", "\\\\") # Technically speaking, this isn't required as Jinja's import/include/extend functionality # superseeds this functionality. However, it remains in the name of backwards compatibility. env.filters["read_file"] = ReadFileFilter with open(input_filename) as f: template = env.from_string(f.read()) try: content = template.render( **context["jinja2_context"]) except exceptions.UndefinedError as ex: this_dm.stream.write("ERROR: {}\n".format(str(ex))) this_dm.result = -1 continue with open(output_filename, "w") as f: f.write(content) except: this_dm.result = -1 raise return dm.result
# ---------------------------------------------------------------------- _script_fullpath = CommonEnvironment.ThisFullpath() _script_dir, _script_name = os.path.split(_script_fullpath) # ---------------------------------------------------------------------- inflect = inflect_mod.engine() # ---------------------------------------------------------------------- DOXYGEN_EXTENSION = ".doxygen" DOXYGEN_EXTENSION_IGNORE = "{}-ignore".format(DOXYGEN_EXTENSION) # ---------------------------------------------------------------------- @CommandLine.EntryPoint @CommandLine.Constraints( code_dir_or_doxygen_filename=CommandLine.FilenameTypeInfo( match_any=True, ), output_dir=CommandLine.DirectoryTypeInfo(ensure_exists=False, ), output_stream=None, ) def EntryPoint( code_dir_or_doxygen_filename, output_dir, output_stream=sys.stdout, verbose=False, ): with StreamDecorator(output_stream).DoneManager( line_prefix="", prefix="\nResults: ", suffix="\n", ) as dm: # Get the doxygen files
info_items.append(item) for info_item in info_items: RemoveItem(info_item) break dm.stream.write("Installing...") with dm.stream.DoneManager() as this_dm: this_dm.result = Process.Execute(pip_command_line, this_dm.stream) return dm.result # ---------------------------------------------------------------------- @CommandLine.EntryPoint @CommandLine.Constraints( script_filename_or_dir=CommandLine.FilenameTypeInfo(match_any=True), output_stream=None, ) def Normalize( script_filename_or_dir, output_stream=sys.stdout, ): """Normalizes a script so that it can be run from any location.""" with StreamDecorator(output_stream).DoneManager( line_prefix='', prefix="\nResults: ", suffix='\n', ) as dm: if os.path.isfile(script_filename_or_dir): script_filenames = [ script_filename_or_dir, ] elif os.path.isdir(script_filename_or_dir): script_filenames = list(FileSystem.WalkFiles(script_filename_or_dir, recurse=False))
# ---------------------------------------------------------------------- sys.path.insert(0, os.path.join(_script_dir, "GeneratedCode")) with CallOnExit(lambda: sys.path.pop(0)): import HooksImplParser # <Unable to import> pylint: disable = E0401 sys.path.insert(0, os.getenv("DEVELOPMENT_ENVIRONMENT_FUNDAMENTAL")) with CallOnExit(lambda: sys.path.pop(0)): from RepositoryBootstrap import Constants from RepositoryBootstrap.Impl.ActivationData import ActivationData from RepositoryBootstrap.Impl import Utilities # ---------------------------------------------------------------------- @CommandLine.EntryPoint @CommandLine.Constraints( display_sentinel=CommandLine.StringTypeInfo(), json_filename=CommandLine.FilenameTypeInfo(), result_filename=CommandLine.FilenameTypeInfo(ensure_exists=False), output_stream=None, ) def Commit( display_sentinel, json_filename, result_filename, first=False, output_stream=sys.stdout, ): """Called prior to committing a change locally""" return _Impl( display_sentinel, json_filename, result_filename, first, output_stream,
def CreateCompileMethod(compiler_type): # ---------------------------------------------------------------------- @CommandLine.EntryPoint @CommandLine.Constraints( input=CommandLine.FilenameTypeInfo(arity='+'), output_dir=CommandLine.DirectoryTypeInfo(ensure_exists=False), output_name=CommandLine.StringTypeInfo(arity='?'), build_type=CommandLine.EnumTypeInfo([ "console", "windows", ], arity='?'), manifest_filename=CommandLine.FilenameTypeInfo(arity='?'), icon_filename=CommandLine.FilenameTypeInfo(arity='?'), path=CommandLine.DirectoryTypeInfo(arity='*'), include_module=CommandLine.StringTypeInfo(arity='*'), exclude_module=CommandLine.StringTypeInfo(arity='*'), package=CommandLine.StringTypeInfo(arity='*'), distutil_arg=CommandLine.StringTypeInfo(arity='*'), comments=CommandLine.StringTypeInfo(arity='?'), company_name=CommandLine.StringTypeInfo(arity='?'), file_description=CommandLine.StringTypeInfo(arity='?'), internal_name=CommandLine.StringTypeInfo(arity='?'), copyright=CommandLine.StringTypeInfo(arity='?'), trademark=CommandLine.StringTypeInfo(arity='?'), name=CommandLine.StringTypeInfo(arity='?'), version=CommandLine.StringTypeInfo(arity='?'), output_stream=None, ) def Compile( input, output_dir, output_name=None, build_type="console", include_tcl=False, no_optimize=False, no_bundle=False, manifest_filename=None, icon_filename=None, path=None, include_module=None, exclude_module=None, package=None, distutil_arg=None, comments=None, company_name=None, file_description=None, internal_name=None, trademark=None, copyright=None, name=None, version=None, preserve_temp_dir=False, output_stream=sys.stdout, no_verbose=False, ): """Creates an executable from one or more python files.""" if build_type == "console": build_type = DistutilsCompilerImpl.BuildType_Console elif build_type == "windows": build_type = DistutilsCompilerImpl.BuildType_Windows else: assert False, build_type return CommandLineCompile( compiler_type, input, output_stream, verbose=not no_verbose, # Generate compiler options output_dir=output_dir, # This compiler options preserve_temp_dir=preserve_temp_dir, build_type=build_type, include_tcl=include_tcl, no_optimize=no_optimize, no_bundle=no_bundle, manifest_filename=manifest_filename, icon_filename=icon_filename, paths=path, includes=include_module, excludes=exclude_module, packages=package, distutil_args=distutil_arg, output_name=output_name, comments=comments, company_name=company_name, file_description=file_description, internal_name=internal_name, copyright=copyright, trademark=trademark, name=name, version=version, ) # ---------------------------------------------------------------------- return Compile
# ---------------------------------------------------------------------- @CommandLine.EntryPoint() @CommandLine.Constraints( bin_dir=CommandLine.DirectoryTypeInfo( arity="?", ), profraw_filename=CommandLine.StringTypeInfo( arity="?", ), profdata_filename=CommandLine.StringTypeInfo( arity="?", ), executable=CommandLine.FilenameTypeInfo( arity="*", ), source_dir=CommandLine.DirectoryTypeInfo( arity="*", ), output_filename=CommandLine.StringTypeInfo( arity="?", ), output_stream=None, ) def Html( bin_dir=None, profraw_filename="default.profraw", profdata_filename="default.profdata", executable=None, source_dir=None,