| OLD | NEW |
| (Empty) |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import os | |
| 6 | |
| 7 from fetcher.dependency import Dependency | |
| 8 from fetcher.mojom_directory import MojomDirectory | |
| 9 from fetcher.mojom_file import MojomFile | |
| 10 from mojom.parse.parser import Parse | |
| 11 | |
| 12 | |
| 13 class Repository(object): | |
| 14 """Repository represents a code repository on the local disc.""" | |
| 15 def __init__(self, root_dir, external_dir): | |
| 16 """root_dir represents the root of the repository; | |
| 17 external_dir is the relative path of the external directory within the | |
| 18 repository (so, relative to root_dir) | |
| 19 """ | |
| 20 self._root_dir = os.path.normpath(root_dir) | |
| 21 self._external_dir = external_dir | |
| 22 | |
| 23 def get_repo_root_directory(self): | |
| 24 return self._root_dir | |
| 25 | |
| 26 def get_external_directory(self): | |
| 27 return os.path.join(self._root_dir, self._external_dir) | |
| 28 | |
| 29 def get_external_suffix(self): | |
| 30 return self._external_dir | |
| 31 | |
| 32 def _os_walk(self, root_directory): | |
| 33 # This method is included for dependency injection | |
| 34 return os.walk(root_directory) | |
| 35 | |
| 36 def _open(self, filename): | |
| 37 # This method is included for dependency injection | |
| 38 return open(filename) | |
| 39 | |
| 40 def _get_all_mojom_in_directory(self, root_directory): | |
| 41 mojoms = [] | |
| 42 for dirname, _, files in self._os_walk(root_directory): | |
| 43 for f in files: | |
| 44 if f.endswith(".mojom"): | |
| 45 mojoms.append(os.path.join(dirname,f)) | |
| 46 return mojoms | |
| 47 | |
| 48 def _resolve_dependencies(self, dependencies, mojoms): | |
| 49 """Resolve dependencies between discovered mojoms, so we know which are the | |
| 50 missing ones.""" | |
| 51 missing = [] | |
| 52 for dependency in dependencies: | |
| 53 found = False | |
| 54 for search_path in dependency.get_search_path_for_dependency(): | |
| 55 if os.path.normpath( | |
| 56 os.path.join(search_path, | |
| 57 dependency.get_imported())) in mojoms: | |
| 58 found = True | |
| 59 break | |
| 60 if not found: | |
| 61 missing.append(dependency) | |
| 62 return missing | |
| 63 | |
| 64 def get_missing_dependencies(self): | |
| 65 """get_missing_dependencies returns a set of dependencies that are required | |
| 66 by mojoms in this repository but not available. | |
| 67 """ | |
| 68 # Update the list of available mojoms in this repository. | |
| 69 mojoms = set(self._get_all_mojom_in_directory(self._root_dir)) | |
| 70 | |
| 71 # Find all declared dependencies | |
| 72 needed_deps = set([]) | |
| 73 for mojom in mojoms: | |
| 74 with self._open(mojom) as f: | |
| 75 source = f.read() | |
| 76 tree = Parse(source, mojom) | |
| 77 for dep in tree.import_list: | |
| 78 needed_deps.add(Dependency(self, dep.filename, dep.import_filename)) | |
| 79 | |
| 80 missing_deps = self._resolve_dependencies(needed_deps, mojoms) | |
| 81 | |
| 82 return missing_deps | |
| 83 | |
| 84 def get_external_urls(self): | |
| 85 """Get all external mojom files in this repository, by urls (without | |
| 86 scheme).""" | |
| 87 mojoms = set(self._get_all_mojom_in_directory( | |
| 88 self.get_external_directory())) | |
| 89 urls = [] | |
| 90 for mojom in mojoms: | |
| 91 urls.append(os.path.relpath(mojom, self.get_external_directory())) | |
| 92 return urls | |
| 93 | |
| 94 def get_all_external_mojom_directories(self): | |
| 95 """Get all external directories populated with their mojom files.""" | |
| 96 mojoms = self._get_all_mojom_in_directory(self.get_external_directory()) | |
| 97 directories = {} | |
| 98 for mojom_path in mojoms: | |
| 99 directory_path = os.path.dirname(mojom_path) | |
| 100 directory = directories.setdefault( | |
| 101 directory_path, MojomDirectory(directory_path)) | |
| 102 with self._open(mojom_path) as f: | |
| 103 source = f.read() | |
| 104 tree = Parse(source, mojom_path) | |
| 105 mojom = MojomFile(self, mojom_path) | |
| 106 directory.add_mojom(mojom) | |
| 107 for dep in tree.import_list: | |
| 108 mojom.add_dependency(dep.import_filename) | |
| 109 return directories.values() | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| OLD | NEW |