summaryrefslogtreecommitdiffstats
path: root/site_scons/site_tools/publish.py
blob: bf6a737997c9d465244c61410318516a6e13c0f3 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python2.4
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Publish tool for SCons."""


# List of published resources.  This is a dict indexed by group name.  Each
# item in this dict is a dict indexed by resource type.  Items in that dict
# are lists of files for that resource.
__published = {}

#------------------------------------------------------------------------------


class PublishItem(object):
  """Item to be published."""

  def __init__(self, source, subdir):
    """Initialize object.

    Args:
      source: Source node.
      subdir: If not None, subdirectory to copy node into in
          ReplicatePublished().
    """
    object.__init__(self)
    self.source = source
    self.subdir = subdir

#------------------------------------------------------------------------------


def _InitializePublish(env):
  """Re-initializes published resources.

  Args:
    env: Parent environment
  """
  env=env     # Silence gpylint

  # Clear the dict of published resources
  __published.clear()


def ReplicatePublished(self, target, group_name, resource_type):
  """Replicate published resources for the group to the target directory.

  Args:
    self: Environment in which this function was called.
    target: Target directory for resources.
    group_name: Name of resource group, or a list of names of resource groups.
    resource_type: Type of resources (string), or a list of resource types.

    Uses the subdir parameter passed to Publish() when replicating source nodes
    to the target.

  Returns:
    The list of target nodes from the calls to Replicate().

  Since this is based on Replicate(), it will also use the REPLICATE_REPLACE
  variable, if it's set in the calling environment.
  """
  target_path = self.Dir(target).abspath

  dest_nodes = []
  for group in self.SubstList2(group_name):
    for resource in self.SubstList2(resource_type):
      # Get items for publish group and resource type
      items = __published.get(group, {}).get(resource, [])
      for i in items:
        if i.subdir:
          dest_nodes += self.Replicate(target_path + '/' + i.subdir, i.source)
        else:
          dest_nodes += self.Replicate(target_path, i.source)
  return dest_nodes


def GetPublished(self, group_name, resource_type):
  """Returns a list of the published resources of the specified type.

  Args:
    self: Environment in which this function was called.
    group_name: Name of resource group, or a list of names of resource groups.
    resource_type: Type of resources (string), or a list of resource types.

  Returns:
    A flattened list of the source nodes from calls to Publish() for the
        specified group and resource type.  Returns an empty list if there are
        no matching resources.
  """
  source_list = []
  for group in self.SubstList2(group_name):
    # Get items for publish group and resource type
    for resource in self.SubstList2(resource_type):
      items = __published.get(group, {}).get(resource, [])
      for i in items:
        source_list.append(i.source)

  return source_list


def Publish(self, group_name, resource_type, source, subdir=None):
  """Publishes resources for use by other scripts.

  Args:
    self: Environment in which this function was called.
    group_name: Name of resource group.
    resource_type: Type of resources (string).
    source: Source file(s) to copy.  May be a string, Node, or a list of
        mixed strings or Nodes.  Strings will be passed through env.Glob() to
        evaluate wildcards.  If a source evaluates to a directory, the entire
        directory will be recursively copied.
    subdir: Subdirectory to which the resources should be copied, relative to
        the primary directory for that resource type, if not None.
  """
  if subdir is None:
    subdir = ''         # Make string so we can append to it

  # Evaluate SCons variables in group name
  # TODO(rspangler): Should Publish() be able to take a list of group names
  # and publish the resource to all of them?
  group_name = self.subst(group_name)

  # Get list of sources
  items = []
  for source_entry in self.Flatten(source):
    if isinstance(source_entry, str):
      # Search for matches for each source entry
      # TODO(rspangler): If there are no wildcard chars in the source entry,
      # should generate an error if there were no matches?
      source_nodes = self.Glob(source_entry)
    else:
      # Source entry is already a file or directory node; no need to glob it
      source_nodes = [source_entry]
    for s in source_nodes:
      if str(s.__class__) == 'SCons.Node.FS.Dir':
        # Recursively publish all files in subdirectory.  Since glob('*')
        # doesn't match dot files, also glob('.*').
        self.Publish(group_name, resource_type,
                     [s.abspath + '/*', s.abspath + '/.*'],
                     subdir=subdir + '/' + s.name)
      else:
        items.append(PublishItem(s, subdir))

  # Publish items, if any
  if items:
    # Get publish group
    if group_name not in __published:
      __published[group_name] = {}
    group = __published[group_name]
    if resource_type not in group:
      group[resource_type] = []

    # Publish items into group
    group[resource_type] += items


def generate(env):
  # NOTE: SCons requires the use of this name, which fails gpylint.
  """SCons entry point for this tool."""

  # Defer initializing publish, but do before building SConscripts
  env.Defer(_InitializePublish)
  env.Defer('BuildEnvironmentSConscripts', after=_InitializePublish)

  env.AddMethod(GetPublished)
  env.AddMethod(Publish)
  env.AddMethod(ReplicatePublished)