Clone of official tools

Revision:
14:ee1b877e6839
Parent:
13:ab47a20b66f0
Child:
16:a6285a7e5cc6
--- a/targets.py	Thu Jul 14 20:21:19 2016 +0100
+++ b/targets.py	Thu Jul 14 20:37:27 2016 +0100
@@ -51,9 +51,9 @@
 caches = {}
 def cached(func):
     def wrapper(*args, **kwargs):
-        if not caches.has_key((func.__name__, args)):
-            caches[(func.__name__, args)] = func(*args, **kwargs)
-        return caches[(func.__name__, args)]
+        if not caches.has_key(func):
+            caches[func] = func(*args, **kwargs)
+        return caches[func]
     return wrapper
 
 class Target:
@@ -61,24 +61,17 @@
     # need to be computed differently than regular attributes
     __cumulative_attributes = ['extra_labels', 'macros', 'device_has', 'features']
 
+    # {target_name: target_instance} map for all the targets in the system
+    __target_map = {}
+
     # List of targets that were added dynamically using "add_py_targets" (see below)
     __py_targets = set()
 
-    # Location of the 'targets.json' file
-    __targets_json_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'hal', 'targets.json')
-
     # Load the description of JSON target data
     @staticmethod
     @cached
     def get_json_target_data():
-        return json_file_to_dict(Target.__targets_json_location)
-
-    # Set the location of the targets.json file
-    @staticmethod
-    def set_targets_json_location(location):
-        Target.__targets_json_location = location
-        # Invalidate caches, since the location of the JSON file changed
-        caches.clear()
+        return json_file_to_dict(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'targets.json'))
 
     # Get the members of this module using Python's "inspect" module
     @staticmethod
@@ -210,9 +203,10 @@
 
     # Return the target instance starting from the target name
     @staticmethod
-    @cached
     def get_target(name):
-        return Target(name)
+        if not Target.__target_map.has_key(name):
+            Target.__target_map[name] = Target(name)
+        return Target.__target_map[name]
 
     def __init__(self, name):
         self.name = name
@@ -423,15 +417,3 @@
         for detect_code in target.detect_code:
             result[detect_code] = target.name
     return result
-
-# Sets the location of the JSON file that contains the targets
-def set_targets_json_location(location):
-    # First instruct Target about the new location
-    Target.set_targets_json_location(location)
-    # Then re-initialize TARGETS, TARGET_MAP and TARGET_NAMES
-    # The re-initialization does not create new variables, it keeps the old ones instead
-    # This ensures compatibility with code that does "from tools.targets import TARGET_NAMES"
-    TARGETS[:] = [Target.get_target(name) for name, value in Target.get_json_target_data().items() if value.get("public", True)]
-    TARGET_MAP.clear()
-    TARGET_MAP.update(dict([(t.name, t) for t in TARGETS]))
-    TARGET_NAMES[:] = TARGET_MAP.keys()