Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tarball.py
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | Raises: | ||||
ValueError when a problem occurs during unpacking | ValueError when a problem occurs during unpacking | ||||
""" | """ | ||||
try: | try: | ||||
shutil.unpack_archive(tarpath, extract_dir=dest) | shutil.unpack_archive(tarpath, extract_dir=dest) | ||||
except shutil.ReadError as e: | except shutil.ReadError as e: | ||||
raise ValueError(f"Problem during unpacking {tarpath}. Reason: {e}") | raise ValueError(f"Problem during unpacking {tarpath}. Reason: {e}") | ||||
# Fix permissions | normalize_permissions(dest) | ||||
for dirpath, _, fnames in os.walk(dest): | |||||
os.chmod(dirpath, 0o755) | |||||
def normalize_permissions(path: str): | |||||
"""Normalize the permissions of all files and directories under `path`. | |||||
This makes all subdirectories and files with the user executable bit set mode | |||||
0o0755, and all other files mode 0o0644. | |||||
Args: | |||||
path: the path under which permissions should be normalized | |||||
""" | |||||
for dirpath, _, fnames in os.walk(path): | |||||
os.chmod(dirpath, 0o0755) | |||||
for fname in fnames: | for fname in fnames: | ||||
fpath = os.path.join(dirpath, fname) | fpath = os.path.join(dirpath, fname) | ||||
if not os.path.islink(fpath): | if not os.path.islink(fpath): | ||||
fpath_exec = os.stat(fpath).st_mode & stat.S_IXUSR | is_executable = os.stat(fpath).st_mode & stat.S_IXUSR | ||||
if not fpath_exec: | forced_mode = 0o0755 if is_executable else 0o0644 | ||||
os.chmod(fpath, 0o644) | os.chmod(fpath, forced_mode) | ||||
def _ls(rootdir): | def _ls(rootdir): | ||||
"""Generator of filepath, filename from rootdir. | """Generator of filepath, filename from rootdir. | ||||
""" | """ | ||||
for dirpath, dirnames, fnames in os.walk(rootdir): | for dirpath, dirnames, fnames in os.walk(rootdir): | ||||
for fname in dirnames + fnames: | for fname in dirnames + fnames: | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |