| 185 | |
| 186 | |
| 187 | class CompileFileInfo: |
| 188 | def __init__(self, compileFile, store): |
| 189 | self.file_info = {} # {file: command} |
| 190 | self.dir_info = None # {dir: set[file key]} |
| 191 | self.cmd_info = None # {cmd: set[file key]} |
| 192 | |
| 193 | # load compileFile into info |
| 194 | import json |
| 195 | |
| 196 | with open(compileFile) as f: |
| 197 | m: List[dict] = json.load(f) |
| 198 | for i in m: |
| 199 | command = i.get("command") |
| 200 | if not command: |
| 201 | continue |
| 202 | if files := i.get("files"): # batch files, eg: swift module |
| 203 | self.file_info.update((filekey(f), command) for f in files) |
| 204 | if fileLists := i.get( |
| 205 | "fileLists" |
| 206 | ): # file list store in a dedicated file |
| 207 | self.file_info.update( |
| 208 | (filekey(f), command) |
| 209 | for l in fileLists |
| 210 | if os.path.isfile(l) |
| 211 | for f in getFileArgs(l, store.setdefault("filelist", {})) |
| 212 | ) |
| 213 | if file := i.get("file"): # single file info |
| 214 | self.file_info[filekey(file)] = command |
| 215 | |
| 216 | def get(self, filename): |
| 217 | if command := self.file_info.get(filename.lower()): |
| 218 | # xcode 12 escape =, but not recognized... |
| 219 | return command.replace("\\=", "=") |
| 220 | |
| 221 | def groupby_dir(self) -> dict[str, set[str]]: |
| 222 | if self.dir_info is None: # lazy index dir and cmd |
| 223 | self.dir_info = defaultdict(set) |
| 224 | self.cmd_info = defaultdict(set) |
| 225 | for f, cmd in self.file_info.items(): |
| 226 | self.dir_info[os.path.dirname(f)].add(f) |
| 227 | self.cmd_info[cmd].add(f) |
| 228 | |
| 229 | return self.dir_info |
| 230 | |
| 231 | # hack new file into current compile file |
| 232 | # return: set of filekey for match for file. or None if new_file can't be infered |
| 233 | def new_file(self, filename): |
| 234 | # Currently only processing swift files |
| 235 | if not filename.endswith(".swift"): |
| 236 | return |
| 237 | |
| 238 | filename = os.path.realpath(filename) |
| 239 | filename_key = filename.lower() |
| 240 | if filename_key in self.file_info: |
| 241 | return {filename_key} # already handled |
| 242 | |
| 243 | dir = os.path.dirname(filename_key) |
| 244 |
no outgoing calls
no test coverage detected