Jonathan Maw pushed to branch lachlan/pickle-yaml-test-list-composite at BuildStream / buildstream
Commits:
- 
3d96ab37
by Jonathan Maw at 2018-10-05T10:30:34Z
3 changed files:
Changes:
| ... | ... | @@ -113,7 +113,13 @@ class Loader(): | 
| 113 | 113 |              profile_start(Topics.LOAD_PROJECT, target)
 | 
| 114 | 114 |              junction, name, loader = self._parse_name(target, rewritable, ticker,
 | 
| 115 | 115 |                                                        fetch_subprojects=fetch_subprojects)
 | 
| 116 | -            with YamlCache.open(self._context) as yaml_cache:
 | |
| 116 | + | |
| 117 | +            # XXX This will need to be changed to the context's top-level project if this method
 | |
| 118 | +            # is ever used for subprojects
 | |
| 119 | +            top_dir = self.project.directory
 | |
| 120 | + | |
| 121 | +            cache_file = YamlCache.get_cache_file(top_dir)
 | |
| 122 | +            with YamlCache.open(self._context, cache_file) as yaml_cache:
 | |
| 117 | 123 |                  loader._load_file(name, rewritable, ticker, fetch_subprojects, yaml_cache)
 | 
| 118 | 124 |              deps.append(Dependency(name, junction=junction))
 | 
| 119 | 125 |              profile_end(Topics.LOAD_PROJECT, target)
 | 
| ... | ... | @@ -54,8 +54,10 @@ class YamlCache(): | 
| 54 | 54 |          self._context = context
 | 
| 55 | 55 |  | 
| 56 | 56 |      # Writes the yaml cache to the specified path.
 | 
| 57 | -    def write(self):
 | |
| 58 | -        path = self._get_cache_file(self._context)
 | |
| 57 | +    #
 | |
| 58 | +    # Args:
 | |
| 59 | +    #    path (str): The path to the cache file.
 | |
| 60 | +    def write(self, path):
 | |
| 59 | 61 |          parent_dir = os.path.dirname(path)
 | 
| 60 | 62 |          os.makedirs(parent_dir, exist_ok=True)
 | 
| 61 | 63 |          with open(path, "wb") as f:
 | 
| ... | ... | @@ -139,14 +141,14 @@ class YamlCache(): | 
| 139 | 141 |      #
 | 
| 140 | 142 |      # Args:
 | 
| 141 | 143 |      #    context (Context): The context.
 | 
| 144 | +    #    cachefile (str): The path to the cache file.
 | |
| 142 | 145 |      #
 | 
| 143 | 146 |      # Returns:
 | 
| 144 | 147 |      #    (YamlCache): A YamlCache.
 | 
| 145 | 148 |      @staticmethod
 | 
| 146 | 149 |      @contextmanager
 | 
| 147 | -    def open(context):
 | |
| 150 | +    def open(context, cachefile):
 | |
| 148 | 151 |          # Try to load from disk first
 | 
| 149 | -        cachefile = YamlCache._get_cache_file(context)
 | |
| 150 | 152 |          cache = None
 | 
| 151 | 153 |          if os.path.exists(cachefile):
 | 
| 152 | 154 |              try:
 | 
| ... | ... | @@ -155,12 +157,13 @@ class YamlCache(): | 
| 155 | 157 |              except pickle.UnpicklingError as e:
 | 
| 156 | 158 |                  sys.stderr.write("Failed to load YamlCache, {}\n".format(e))
 | 
| 157 | 159 |  | 
| 160 | +        # Failed to load from disk, create a new one
 | |
| 158 | 161 |          if not cache:
 | 
| 159 | 162 |              cache = YamlCache(context)
 | 
| 160 | 163 |  | 
| 161 | 164 |          yield cache
 | 
| 162 | 165 |  | 
| 163 | -        cache.write()
 | |
| 166 | +        cache.write(cachefile)
 | |
| 164 | 167 |  | 
| 165 | 168 |      # Calculates a key for putting into the cache.
 | 
| 166 | 169 |      @staticmethod
 | 
| ... | ... | @@ -170,13 +173,7 @@ class YamlCache(): | 
| 170 | 173 |  | 
| 171 | 174 |      # Retrieves a path to the yaml cache file.
 | 
| 172 | 175 |      @staticmethod
 | 
| 173 | -    def _get_cache_file(context):
 | |
| 174 | -        try:
 | |
| 175 | -            toplevel_project = context.get_toplevel_project()
 | |
| 176 | -            top_dir = toplevel_project.directory
 | |
| 177 | -        except IndexError:
 | |
| 178 | -            # Context has no projects, fall back to current directory
 | |
| 179 | -            top_dir = os.getcwd()
 | |
| 176 | +    def get_cache_file(top_dir):
 | |
| 180 | 177 |          return os.path.join(top_dir, ".bst", YAML_CACHE_FILENAME)
 | 
| 181 | 178 |  | 
| 182 | 179 |  | 
| ... | ... | @@ -52,7 +52,8 @@ def generate_project(tmpdir, ref_storage, with_junction, name="test"): | 
| 52 | 52 |  def with_yamlcache(project_dir):
 | 
| 53 | 53 |      context = Context()
 | 
| 54 | 54 |      project = Project(project_dir, context)
 | 
| 55 | -    with YamlCache.open(context) as yamlcache:
 | |
| 55 | +    cache_file = YamlCache.get_cache_file(project_dir)
 | |
| 56 | +    with YamlCache.open(context, cache_file) as yamlcache:
 | |
| 56 | 57 |          yield yamlcache, project
 | 
| 57 | 58 |  | 
| 58 | 59 |  | 
| ... | ... | @@ -62,12 +63,12 @@ def yamlcache_key(yamlcache, in_file, copy_tree=False): | 
| 62 | 63 |      return key
 | 
| 63 | 64 |  | 
| 64 | 65 |  | 
| 65 | -def modified_file(input_file):
 | |
| 66 | +def modified_file(input_file, tmpdir):
 | |
| 66 | 67 |      with open(input_file) as f:
 | 
| 67 | 68 |          data = f.read()
 | 
| 68 | 69 |      assert 'variables' not in data
 | 
| 69 | 70 |      data += '\nvariables: {modified: True}\n'
 | 
| 70 | -    _, temppath = tempfile.mkstemp(text=True)
 | |
| 71 | +    _, temppath = tempfile.mkstemp(dir=tmpdir, text=True)
 | |
| 71 | 72 |      with open(temppath, 'w') as f:
 | 
| 72 | 73 |          f.write(data)
 | 
| 73 | 74 |  | 
| ... | ... | @@ -96,7 +97,7 @@ def test_yamlcache_used(cli, tmpdir, ref_storage, with_junction, move_project): | 
| 96 | 97 |          # *Absolutely* horrible cache corruption to check it's being used
 | 
| 97 | 98 |          # Modifying the data from the cache is fraught with danger,
 | 
| 98 | 99 |          # so instead I'll load a modified version of the original file
 | 
| 99 | -        temppath = modified_file(element_path)
 | |
| 100 | +        temppath = modified_file(element_path, str(tmpdir))
 | |
| 100 | 101 |          contents = _yaml.load(temppath, copy_tree=False, project=prj)
 | 
| 101 | 102 |          key = yamlcache_key(yc, element_path)
 | 
| 102 | 103 |          yc.put(prj, element_path, key, contents)
 | 
