def __init__(self, targets_path=None, template_root=None, targets_dict=None): """Constructor. Loads targets file. Args: targets_path: (str) Path to targets file. Defaults to './targets.json' template_root: (str) Path to template root. Defaults to '.' targets_dict: (dict) Initial data, if not supplied from a file. Raises: ValueError: if the targets file does not contain the required sections. """ self.template_root = template_root or Targets._default_template_root self.targets_path = targets_path or os.path.join(self.template_root, 'targets.json') if targets_dict: self._targets_dict = targets_dict else: self._targets_dict = json_with_comments.Loads( files.GetFileContents(self.targets_path)) # Do some basic validation that this has the required fields if 'languages' not in self._targets_dict: raise ValueError('languages not in targets.json')
def GetFeatures(self, variation): """Returns the features dictionary for a specific variation. This is the basic dictionary informaion plus any specific overrides in the per-template-tree features.json file. Args: variation: (str) A target variation name. Returns: (Features) features dictionary """ if not variation: return None template_dir = self.AbsoluteTemplateDir(variation) features = Features(template_dir, self.get(variation), variation) json_path = os.path.join(template_dir, 'features.json') try: features_json = files.GetFileContents(json_path) except files.FileDoesNotExist: # for backwards compatibility, we forgive this. # TODO(user): be stricter about this and # fix/remove any tests that fail as a result. return features features.update(json_expander.ExpandJsonTemplate( json_with_comments.Loads(features_json))) # If not specified, the releaseVersion matches the variation if not features.get('releaseVersion'): features['releaseVersion'] = variation return features
def IncludeFile(self, path, name): """Read a file from disk into the archive. Args: path: (str) path to the file. name: (str) name the file should have in the archive. """ output_stream = self.StartFile(name) output_stream.write(files.GetFileContents(path)) self.EndFile()
def DjangoRenderTemplate(template_path, context_dict): """Renders a template specified by a file path with a give values dict. Args: template_path: (str) Path to file. context_dict: (dict) The dictionary to use for template evaluation. Returns: (str) The expanded template. """ source = files.GetFileContents(template_path).decode('utf-8') return _DjangoRenderTemplateSource(source, context_dict)
def ExpandZipFile(path): """Expand a zip file found in the template tree. Args: path: Path to file, relative to top of template tree. """ full_path = os.path.join(self._template_dir, path) zip_slurp = files.GetFileContents(full_path) archive = zipfile.ZipFile(StringIO.StringIO(zip_slurp), 'r') for info in archive.infolist(): package.WriteDataAsFile( archive.read(info.filename), os.path.join(relative_path, info.filename))
def _RenderToString(template_path, context): """Renders a template specified by a file path with a give values dict. NOTE: This routine is essentially a copy of what is in django_helpers. We duplicate it here rather than call that one to avoid a mutual recursion in the strange django loading process. Args: template_path: (str) Path to file. context: (Context) A django Context. Returns: (str) The expanded template. """ source = files.GetFileContents(template_path).decode('utf-8') template = django_template.Template(source) return template.render(context)
def testGetFileContentsLocal(self): filename = os.path.join(self.tempdir, 'a') contents = files.GetFileContents(filename) self.assertEquals('a', contents)
def _LoadTemplate(self, template_path): source = files.GetFileContents(template_path).decode('utf-8') return django_template.Template(source)