make extracted files and directories readonly

This commit is contained in:
xenofem 2024-04-01 21:50:25 -04:00
parent 88f4ca66d7
commit 25e98489c0

View file

@ -6,12 +6,13 @@ import importlib_resources as resources
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
import os import os
from os.path import relpath, splitext from os.path import relpath, splitext, join
import random import random
import re import re
import readline import readline
import shutil import shutil
import sqlite3 import sqlite3
import stat
import textwrap import textwrap
import unicodedata import unicodedata
from urllib.parse import urlparse from urllib.parse import urlparse
@ -96,6 +97,9 @@ PDF_INLINE_IMAGE_REGEX = re.compile(r'(^|\s)(BI|ID|EI)($|\s)')
SUGGESTED_WORKS_COUNT = 10 SUGGESTED_WORKS_COUNT = 10
READONLY_FILE = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
READONLY_DIR = READONLY_FILE | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
debug_mode = False debug_mode = False
def debug(s): def debug(s):
if debug_mode: if debug_mode:
@ -120,6 +124,12 @@ def open_rarfile_with_encoding(path):
print(f'{path} contains filenames with unknown character encoding!') print(f'{path} contains filenames with unknown character encoding!')
exit(1) exit(1)
def readonly(path):
for parentdir, dirs, files in os.walk(path, topdown=False):
for f in files:
os.chmod(join(parentdir, f), READONLY_FILE, follow_symlinks=False)
os.chmod(parentdir, READONLY_DIR, follow_symlinks=False)
def extract(args): def extract(args):
absolute_archive_paths = set(path.resolve(strict=True) for path in args.archives) absolute_archive_paths = set(path.resolve(strict=True) for path in args.archives)
@ -136,6 +146,8 @@ def extract(args):
work_extract_path.mkdir(parents=True) work_extract_path.mkdir(parents=True)
z.extractall(path=work_extract_path) z.extractall(path=work_extract_path)
readonly(work_extract_path)
if args.remove: if args.remove:
archive_path.unlink() archive_path.unlink()
@ -154,6 +166,8 @@ def extract(args):
work_extract_path.mkdir(parents=True) work_extract_path.mkdir(parents=True)
r.extractall(path=work_extract_path) r.extractall(path=work_extract_path)
readonly(work_extract_path)
if args.remove: if args.remove:
for vol in volumes: for vol in volumes:
vol.unlink() vol.unlink()