Skip to content

Commit 3ec55b1

Browse files
committed
Tidy up
1 parent 48209d9 commit 3ec55b1

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/common/core/main.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import logging
33
import os
4-
import pathlib
54
import sys
65
import typing
76
from tempfile import gettempdir
@@ -12,7 +11,7 @@
1211

1312
from common.core.cli import healthcheck
1413
from common.core.constants import DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME
15-
from common.core.utils import clear_directory
14+
from common.core.utils import clear_directory, make_writable_directory
1615

1716
logger = logging.getLogger(__name__)
1817

@@ -38,25 +37,13 @@ def ensure_cli_env() -> typing.Generator[None, None, None]:
3837
# TODO @khvn26 Move logging setup to here
3938

4039
# Prometheus multiproc support
41-
prom_dir = pathlib.Path(
42-
os.environ.setdefault(
43-
"PROMETHEUS_MULTIPROC_DIR",
44-
os.path.join(gettempdir(), DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME),
45-
)
40+
prom_dir = os.environ.setdefault(
41+
"PROMETHEUS_MULTIPROC_DIR",
42+
os.path.join(gettempdir(), DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME),
4643
)
47-
48-
if prom_dir.exists():
44+
if os.path.exists(prom_dir):
4945
clear_directory(prom_dir)
50-
51-
prom_dir.mkdir(parents=True, exist_ok=True)
52-
53-
# While `mkdir` sets mode=0o777 by default, this can be affected by umask resulting in
54-
# lesser permissions for other users. This step ensures the directory is writable for
55-
# all users.
56-
try:
57-
prom_dir.chmod(0o777)
58-
except Exception:
59-
pass
46+
make_writable_directory(prom_dir)
6047

6148
# Currently we don't install Flagsmith modules as a package, so we need to add
6249
# $CWD to the Python path to be able to import them

src/common/core/utils.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import os
34
import pathlib
45
import random
56
import shutil
@@ -201,16 +202,28 @@ def using_database_replica(
201202
return manager.db_manager(chosen_replica)
202203

203204

204-
def clear_directory(dir_: pathlib.Path) -> None:
205+
def clear_directory(directory_path: str) -> None:
205206
"""
206207
Safely clear a directory including all subdirectories and files.
207208
"""
208-
for p in dir_.rglob("*"):
209+
for p in pathlib.Path(directory_path).rglob("*"):
209210
try:
210211
# Ensure that the cleanup doesn't silently fail on
211212
# files and subdirs created by other users.
212213
p.chmod(0o777)
213-
except Exception: # pragma: no cover
214+
except (PermissionError, FileNotFoundError): # pragma: no cover
214215
pass
215216

216-
shutil.rmtree(dir_, ignore_errors=True)
217+
shutil.rmtree(directory_path, ignore_errors=True)
218+
219+
220+
def make_writable_directory(directory_path: str) -> None:
221+
os.makedirs(directory_path, exist_ok=True)
222+
223+
try:
224+
# While `mkdir` sets mode=0o777 by default, this can be affected by umask
225+
# resulting in lesser permissions for other users. This step ensures the
226+
# directory is writable for all users.
227+
os.chmod(directory_path, 0o777)
228+
except PermissionError:
229+
pass

0 commit comments

Comments
 (0)