File: tar.py

package info (click to toggle)
mpd 0.24.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,740 kB
  • sloc: cpp: 74,967; python: 1,371; xml: 628; perl: 469; java: 289; sh: 286; ansic: 235; makefile: 105
file content (20 lines) | stat: -rw-r--r-- 602 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import os, shutil, subprocess

def untar(tarball_path: str, parent_path: str, base: str,
          lazy: bool=False) -> str:
    path = os.path.join(parent_path, base)
    if lazy and os.path.isdir(path):
        return path
    try:
        shutil.rmtree(path)
    except FileNotFoundError:
        pass
    os.makedirs(parent_path, exist_ok=True)
    try:
        subprocess.check_call(['tar', 'xfC', tarball_path, parent_path])
    except FileNotFoundError:
        import tarfile
        tar = tarfile.open(tarball_path)
        tar.extractall(path=parent_path)
        tar.close()
    return path