| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 """Publish tool for SCons.""" |
| 32 |
| 33 |
| 34 __published = {} # List of published resources |
| 35 |
| 36 #------------------------------------------------------------------------------ |
| 37 |
| 38 |
| 39 class PublishItem(object): |
| 40 """Item to be published.""" |
| 41 |
| 42 def __init__(self, source, subdir): |
| 43 """Initialize object. |
| 44 |
| 45 Args: |
| 46 source: Source node. |
| 47 subdir: If not None, subdirectory to copy node into in |
| 48 ReplicatePublished(). |
| 49 """ |
| 50 object.__init__(self) |
| 51 self.source = source |
| 52 self.subdir = subdir |
| 53 |
| 54 #------------------------------------------------------------------------------ |
| 55 |
| 56 |
| 57 def _InitializePublish(self): |
| 58 """Re-initializes published resources. |
| 59 |
| 60 Args: |
| 61 self: Parent environment |
| 62 """ |
| 63 self=self # Silence gpylint |
| 64 |
| 65 # Clear the dict of published resources |
| 66 __published.clear() |
| 67 |
| 68 |
| 69 def ReplicatePublished(self, target, group_name, resource_type): |
| 70 """Replicate published resources for the group to the target directory. |
| 71 |
| 72 Args: |
| 73 self: Environment in which this function was called. |
| 74 target: Target directory for resources. |
| 75 group_name: Name of resource group, or a list of names of resource groups. |
| 76 resource_type: Type of resources (string), or a list of resource types. |
| 77 |
| 78 Uses the subdir parameter passed to Publish() when replicating source nodes |
| 79 to the target. |
| 80 |
| 81 Returns: |
| 82 The list of target nodes from the calls to Replicate(). |
| 83 |
| 84 Since this is based on Replicate(), it will also use the REPLICATE_REPLACE |
| 85 variable, if it's set in the calling environment. |
| 86 """ |
| 87 target_path = self.Dir(target).abspath |
| 88 |
| 89 dest_nodes = [] |
| 90 for group in self.Flatten(group_name): |
| 91 for resource in self.Flatten(resource_type): |
| 92 # Get items for publish group and resource type |
| 93 items = __published.get(self.subst(group), {}).get(resource, []) |
| 94 for i in items: |
| 95 if i.subdir: |
| 96 dest_nodes += self.Replicate(target_path + '/' + i.subdir, i.source) |
| 97 else: |
| 98 dest_nodes += self.Replicate(target_path, i.source) |
| 99 return dest_nodes |
| 100 |
| 101 |
| 102 def GetPublished(self, group_name, resource_type): |
| 103 """Returns a list of the published resources of the specified type. |
| 104 |
| 105 Args: |
| 106 self: Environment in which this function was called. |
| 107 group_name: Name of resource group, or a list of names of resource groups. |
| 108 resource_type: Type of resources (string), or a list of resource types. |
| 109 |
| 110 Returns: |
| 111 A flattened list of the source nodes from calls to Publish() for the |
| 112 specified group and resource type. Returns an empty list if there are |
| 113 no matching resources. |
| 114 """ |
| 115 source_list = [] |
| 116 for group in self.Flatten(group_name): |
| 117 # Get items for publish group and resource type |
| 118 for resource in self.Flatten(resource_type): |
| 119 items = __published.get(self.subst(group), {}).get(resource, []) |
| 120 for i in items: |
| 121 source_list.append(i.source) |
| 122 |
| 123 return source_list |
| 124 |
| 125 |
| 126 def Publish(self, group_name, resource_type, source, subdir=None): |
| 127 """Publishes resources for use by other scripts. |
| 128 |
| 129 Args: |
| 130 self: Environment in which this function was called. |
| 131 group_name: Name of resource group. |
| 132 resource_type: Type of resources (string). |
| 133 source: Source file(s) to copy. May be a string, Node, or a list of |
| 134 mixed strings or Nodes. Strings will be passed through env.Glob() to |
| 135 evaluate wildcards. If a source evaluates to a directory, the entire |
| 136 directory will be recursively copied. |
| 137 subdir: Subdirectory to which the resources should be copied, relative to |
| 138 the primary directory for that resource type, if not None. |
| 139 """ |
| 140 if subdir is None: |
| 141 subdir = '' # Make string so we can append to it |
| 142 |
| 143 # Evaluate SCons variables in group name |
| 144 group_name = self.subst(group_name) |
| 145 |
| 146 # Get list of sources |
| 147 items = [] |
| 148 for source_entry in self.Flatten(source): |
| 149 if type(source_entry) == str: |
| 150 # Search for matches for each source entry |
| 151 source_nodes = self.Glob(source_entry) |
| 152 else: |
| 153 # Source entry is already a file or directory node; no need to glob it |
| 154 source_nodes = [source_entry] |
| 155 for s in source_nodes: |
| 156 if str(s.__class__) == 'SCons.Node.FS.Dir': |
| 157 # Recursively publish all files in subdirectory. Since glob('*') |
| 158 # doesn't match dot files, also glob('.*'). |
| 159 self.Publish(group_name, resource_type, |
| 160 [s.abspath + '/*', s.abspath + '/.*'], |
| 161 subdir=subdir + '/' + s.name) |
| 162 else: |
| 163 items.append(PublishItem(s, subdir)) |
| 164 |
| 165 # Publish items, if any |
| 166 if items: |
| 167 # Get publish group |
| 168 if group_name not in __published: |
| 169 __published[group_name] = {} |
| 170 group = __published[group_name] |
| 171 if resource_type not in group: |
| 172 group[resource_type] = [] |
| 173 |
| 174 # Publish items into group |
| 175 group[resource_type] += items |
| 176 |
| 177 |
| 178 def generate(env): |
| 179 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 180 """SCons entry point for this tool.""" |
| 181 |
| 182 env.AddMethod(_InitializePublish) |
| 183 env.AddMethod(GetPublished) |
| 184 env.AddMethod(Publish) |
| 185 env.AddMethod(ReplicatePublished) |
| OLD | NEW |