| # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Common project related utilities.""" |
| |
| from __future__ import print_function |
| |
| import json |
| import os |
| |
| from chromite.lib import cros_build_lib |
| from chromite.lib import osutils |
| |
| _DEFAULT_LAYOUT_CONF = {'profile-formats': 'portage-2', |
| 'thin-manifests': 'true', |
| 'use-manifests': 'true'} |
| |
| def PrettyPrintJson(content): |
| """Dumps a nicely formatted json representation of |content|""" |
| return json.dumps(content, sort_keys=True, indent=4, separators=(',', ': ')) |
| |
| |
| def WriteLayoutConf(content, project_dir): |
| """Write metadata/layout.conf for a given project. |
| |
| Sets unset fields to a sensible default and write |content| in layout.conf in |
| the right format. |
| |
| Args: |
| content: directory containing the set fields in layout.conf. |
| project_dir: the project directory |
| """ |
| for k, v in _DEFAULT_LAYOUT_CONF.items(): |
| content.setdefault(k, v) |
| |
| content = ''.join(['%s = %s\n' % (k, v) for k, v in content.items()]) |
| osutils.WriteFile(os.path.join(project_dir, 'metadata', 'layout.conf'), |
| content, makedirs=True) |
| |
| |
| def WriteParents(parents, project_dir): |
| """Write the parent file for a given project.""" |
| osutils.WriteFile(os.path.join(project_dir, 'profiles', 'base', 'parent'), |
| '\n'.join(parents), makedirs=True) |
| |
| |
| def WriteProjectJson(content, project_directory, update_config=True): |
| """Write the project.json file. |
| |
| Write the json representation of |content| in |project_directory|. |
| If |update_config| is true, regenerate the portage configuration files in |
| this project to match the new project.json. |
| |
| Args: |
| content: project configuration as a python dict |
| project_directory: project directory |
| update_config: if True, regenerate autogenerated project files |
| """ |
| osutils.WriteFile(os.path.join(project_directory, 'project.json'), |
| PrettyPrintJson(content), |
| makedirs=True) |
| |
| if update_config: |
| GeneratePortageConfig(project_directory) |
| |
| |
| def GeneratePortageConfig(project_dir): |
| """Generate all autogenerated project files.""" |
| conf = json.loads(osutils.ReadFile(os.path.join(project_dir, |
| 'project.json'))) |
| |
| deps = [d.get('name', None) for d in conf.get('dependencies', [])] |
| if None in deps: |
| cros_build_lib.Die('Invalid dependency name') |
| |
| WriteLayoutConf({'masters': ' '.join(['portage-stable', 'chromiumos'] + deps), |
| 'repo-name': conf['name']}, |
| project_dir) |
| |
| WriteParents([m + ':base' for m in deps], project_dir) |