aboutsummaryrefslogtreecommitdiff
path: root/brunch/lib/core/product.py
blob: 1e64e7841405a5031bc45cb84057fb5153aa5d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Product Managment"""

import os
import shutil

from core import config
from core import product_templates
from core import util

class ProductCreatorError(Exception):
  pass

class ProductCreator(object):
  def __init__(self, name, vendor, device):
    """Initializes the ProductCreators internal properties"""
    self._name = name
    self._config = None
    self._device = device
    self._vendor = vendor

  def exists(self):
    """Returns true if a subdirectory with the product name exists"""
    try:
      return os.path.exists(self._name)
    except OSError:
      return false

  def create(self):
    """Creates a subdirectory for the product and creates templates"""
    self._create_empty()

    # Populate the envsetup.sh file.
    env = Environment(self._config, self._name)
    env.envsetup()

  def _init_config(self):
    # Populate the configuration store
    self._config = config.ProductFileStore(os.path.abspath(self._name))
    self._config.name = self._name
    self._config.brand = 'Brillo'
    self._config.manufacturer = self._vendor
    self._config.manufacturer = self._vendor
    self._config.device = self._device
    self._config.bdk.buildtype = 'userdebug'
    self._config.bdk.version = util.GetBDKVersion()
    self._config.copy_files = """
# Format:
# path-in-product-dir:path-to-install-in-device
# E.g., weaved.conf:system/etc/weaved/weaved.conf
    """
    self._config.brillo.product_id = \
      'developer-boards:brillo-starter-board-<REPLACE ME>'
    self._config.brillo.crash_server = 'https://clients2.google.com/bc/report'
    # Building without java isn't completely supported yet.
    # b/25281898
    self._config.bdk.java = '1'

  def _create_common(self):
    paths = [ self._name, os.path.join(self._name, 'out'), \
              os.path.join(self._name, 'src') ]
    for path in paths:
      if not os.path.exists(path):
        os.makedirs(path)
    self._init_config()

    with open(os.path.join(self._name, 'AndroidProducts.mk'), 'w') as f:
      f.write(product_templates.ANDROIDPRODUCTS_MK.substitute(
        self._config.dict()))

  def _create_empty(self):
    """Creates a subdirectory for the product and creates templates"""
    self._create_common()
    with open(os.path.join(self._name, ('%s.mk' % self._name)), 'w') as f:
      f.write(product_templates.PRODUCT_MK.substitute(self._config.dict()))


class Environment(object):
  """This class is used to generate a shell environment meant to streamline
     the use of the BDK while keeping it familiar to Android device developers.

     Note: None of the templates other than ENVSETUP should contain single
           quotes ('). If they do, it will break during sourcing of envsetup.sh.
  """

  def __init__(self, config, product_dir=util.GetProductDir()):
    self._config = config
    self._product_dir = product_dir

  def banner(self):
    """Return the banner to be printed after envsetup.sh is sourced"""
    bdk_path = util.GetBDKPath()
    bdk_version = util.GetBDKVersion()
    bdk_warning = ''
    # TODO(wad) implement this.
    if self._config.bdk.version != bdk_version:
      bdk_warning = 'BDK versions differ.'
    return product_templates.BDK_BANNER.substitute(
       self._config.dict(),
       bdk_version=bdk_version,
       bdk_path=bdk_path,
       bdk_warning=bdk_warning)

  def exports(self):
    """Export shell environment variables for calling dev tools"""
    bdk_path = util.GetBDKPath()
    return product_templates.ENV_EXPORTS.substitute(
      self._config.dict(),
      bdk_path=bdk_path,
      product_path=self._product_dir,
      brunch_path=os.path.join(bdk_path, 'tools', 'bdk', 'brunch'),
      target_out=os.path.join(self._product_dir, 'out',
      'out-%s' % (self._config.device),
      'target', 'product', self._config.device)
    )

  def aliases(self):
    """Wrap brunch commands with shell functions"""
    return product_templates.ENV_ALIASES.substitute(
      self._config.dict(), product_path=self._product_dir)

  def environment(self):
    """Returns a complete shell environment for product development"""
    return self.banner() + self.exports() + self.aliases()

  def envsetup(self):
    """Generates a file which will run brunch product envsetup"""
    bdk_path = util.GetBDKPath()
    bdk_version = util.GetBDKVersion()
    with open(os.path.join(self._product_dir, 'envsetup.sh'), 'w') as f:
      f.write(product_templates.ENVSETUP.substitute(
        self._config.dict(), bdk_path=bdk_path,
        product_path=self._product_dir))