# ==================================================================================================
# Copyright 2012 Twitter, Inc.
# --------------------------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or 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.
# ==================================================================================================

import os
import shutil

from twitter.common.dirutil import safe_mkdir
from twitter.pants.goal.products import MultipleRootedProducts

from twitter.pants.tasks import Task


class PrepareResources(Task):

  def __init__(self, context):
    Task.__init__(self, context)

    self.workdir = context.config.get('prepare-resources', 'workdir')
    self.confs = context.config.getlist('prepare-resources', 'confs')
    self.context.products.require_data('exclusives_groups')

  def execute(self, targets):
    if len(targets) == 0:
      return
    def extract_resources(target):
      return target.resources if target.has_resources else ()
    all_resources_tgts = set()
    for resources_tgts in map(extract_resources, targets):
      all_resources_tgts.update(resources_tgts)

    def target_dir(resources_tgt):
      return os.path.join(self.workdir, resources_tgt.id)

    with self.invalidated(all_resources_tgts) as invalidation_check:
      invalid_targets = set()
      for vt in invalidation_check.invalid_vts:
        invalid_targets.update(vt.targets)

      for resources_tgt in invalid_targets:
        resources_dir = target_dir(resources_tgt)
        safe_mkdir(resources_dir, clean=True)
        for resource_path in resources_tgt.sources:
          basedir = os.path.dirname(resource_path)
          destdir = os.path.join(resources_dir, basedir)
          safe_mkdir(destdir)
          # TODO: Symlink instead?
          shutil.copy(os.path.join(resources_tgt.target_base, resource_path),
                      os.path.join(resources_dir, resource_path))

    resources_by_target = \
      self.context.products.get_data('resources_by_target', MultipleRootedProducts)
    egroups = self.context.products.get_data('exclusives_groups')
    group_key = egroups.get_group_key_for_target(targets[0])

    for resources_tgt in all_resources_tgts:
      resources_dir = target_dir(resources_tgt)
      target_resources = resources_by_target[resources_tgt]
      target_resources.add_rel_paths(resources_dir, resources_tgt.sources)
      for conf in self.confs:
        egroups.update_compatible_classpaths(group_key, [(conf, resources_dir)])

@@ -892,16 +892,52 @@
 ========
+%0Afrom collections import defaultdict
 %0A%0Aimport
@@ -1427,24 +1427,253 @@
 , targets):%0A
+    if self.context.products.is_required_data('resources_by_target'):%0A      self.context.products.safe_create_data('resources_by_target',%0A                                             lambda: defaultdict(MultipleRootedProducts))%0A%0A
     if len(t
@@ -2730,24 +2730,26 @@
 path))%0A%0A    
+  
 resources_by
@@ -2761,16 +2761,8 @@
 et =
- %5C%0A     
  sel
@@ -2814,34 +2814,12 @@
 get'
-, MultipleRootedProducts
 )%0A
+  
     
@@ -2884,16 +2884,18 @@
 s')%0A    
+  
 group_ke
@@ -2944,16 +2944,18 @@
 ts%5B0%5D)%0A%0A
+  
     for 
@@ -2993,24 +2993,26 @@
 tgts:%0A      
+  
 resources_di
@@ -3051,26 +3051,127 @@
     
-target_resources =
+  for conf in self.confs:%0A          egroups.update_compatible_classpaths(group_key, %5B(conf, resources_dir)%5D)%0A        if
  res
@@ -3190,24 +3190,26 @@
 rget
-%5Bresources_tgt%5D%0A
+ is not None:%0A    
     
@@ -3230,153 +3230,100 @@
 rces
-.add_rel_paths(resources_dir, resources_tgt.sources)%0A      for conf in self.confs:%0A        egroups.update_compatible_classpaths(group_key, %5B(conf
+ = resources_by_target%5Bresources_tgt%5D%0A          target_resources.add_abs_paths(resources_dir
 , re
@@ -3318,27 +3318,33 @@
 rces_dir, resources_
-dir)%5D
+tgt.sources
 )%0A

Fix resource preparation bug.