遍历目录树
(self, top)
| 188 | return [] |
| 189 | |
| 190 | def walk(self, top): |
| 191 | """遍历目录树""" |
| 192 | try: |
| 193 | with self._temp_container() as container_id: |
| 194 | cmd = f"docker export {container_id} | tar -t" |
| 195 | result = subprocess.run(cmd, shell=True, capture_output=True, text=True) |
| 196 | |
| 197 | dir_tree = {} |
| 198 | for line in result.stdout.splitlines(): |
| 199 | line = line.strip().strip('/') |
| 200 | if not line or not line.startswith(top.strip('/')): |
| 201 | continue |
| 202 | |
| 203 | parts = line.split('/') |
| 204 | current_path = '' |
| 205 | for i, part in enumerate(parts): |
| 206 | parent_path = current_path |
| 207 | current_path = os.path.join(current_path, part) if current_path else part |
| 208 | |
| 209 | if current_path not in dir_tree: |
| 210 | dir_tree[current_path] = {'dirs': set(), 'files': set()} |
| 211 | |
| 212 | if i < len(parts) - 1: |
| 213 | dir_tree[parent_path]['dirs'].add(part) |
| 214 | else: |
| 215 | dir_tree[parent_path]['files'].add(part) |
| 216 | |
| 217 | for dirpath in sorted(dir_tree.keys()): |
| 218 | if not dirpath.startswith(top.strip('/')): |
| 219 | continue |
| 220 | |
| 221 | full_path = '/' + dirpath |
| 222 | dirs = sorted(dir_tree[dirpath]['dirs']) |
| 223 | files = sorted(dir_tree[dirpath]['files']) |
| 224 | yield full_path, dirs, files |
| 225 | |
| 226 | except Exception as e: |
| 227 | yield top, [], [] |
| 228 | |
| 229 | def stat(self, path): |
| 230 | """获取文件状态信息""" |
no test coverage detected