Extracts tar file into single local directory. We fail if items in tar file have different . path: The tar file. mode: As tarfile.open(). prefix: If not None, we fail if tar file's is not . exists:
(path, mode='r:gz', prefix=None, exists='raise')
| 327 | |
| 328 | |
| 329 | def tar_extract(path, mode='r:gz', prefix=None, exists='raise'): |
| 330 | ''' |
| 331 | Extracts tar file into single local directory. |
| 332 | |
| 333 | We fail if items in tar file have different <top-directory>. |
| 334 | |
| 335 | path: |
| 336 | The tar file. |
| 337 | mode: |
| 338 | As tarfile.open(). |
| 339 | prefix: |
| 340 | If not None, we fail if tar file's <top-directory> is not <prefix>. |
| 341 | exists: |
| 342 | What to do if <top-directory> already exists: |
| 343 | 'raise': raise exception. |
| 344 | 'remove': remove existing file/directory before extracting. |
| 345 | 'return': return without extracting. |
| 346 | |
| 347 | Returns the directory name (which will be <prefix> if not None, with '/' |
| 348 | appended if not already present). |
| 349 | ''' |
| 350 | prefix_actual = tar_check( path, mode, prefix) |
| 351 | if os.path.exists( prefix_actual): |
| 352 | if exists == 'raise': |
| 353 | raise Exception( f'Path already exists: {prefix_actual!r}') |
| 354 | elif exists == 'remove': |
| 355 | remove( prefix_actual) |
| 356 | elif exists == 'return': |
| 357 | log( f'Not extracting {path} because already exists: {prefix_actual}') |
| 358 | return prefix_actual |
| 359 | else: |
| 360 | assert 0, f'Unrecognised exists={exists!r}' |
| 361 | assert not os.path.exists( prefix_actual), f'Path already exists: {prefix_actual}' |
| 362 | log( f'Extracting {path}') |
| 363 | with tarfile.open( path, mode) as t: |
| 364 | t.extractall() |
| 365 | return prefix_actual |
| 366 | |
| 367 | |
| 368 | def git_info( directory): |
no test coverage detected
searching dependent graphs…