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