Skip to content

Commit 48bee26

Browse files
committed
data_dir Permissions
- Check for permission when creating the cache directory/file. - Add username as part of the default data_dir
1 parent 2553832 commit 48bee26

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pyard/db.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
# > http://www.fsf.org/licensing/licenses/lgpl.html
2121
# > http://www.opensource.org/licenses/lgpl-license.php
2222
#
23+
import os
2324
import pathlib
2425
import sqlite3
26+
import sys
2527
from typing import Tuple, Dict, Set, List
2628

2729
from .mappings import ARSMapping, CodeMappings, AlleleGroups
@@ -67,8 +69,25 @@ def create_db_connection(data_dir, imgt_version, ro=False):
6769
if not pathlib.Path(data_dir).exists():
6870
pathlib.Path(data_dir).mkdir(parents=True, exist_ok=True)
6971

70-
if not pathlib.Path(db_filename).exists():
71-
print(f"Creating {db_filename} as cache.")
72+
# Check for permission to read/write
73+
if pathlib.Path(db_filename).exists():
74+
# Check file is readable
75+
if not os.access(db_filename, os.R_OK):
76+
print(
77+
f"Permission Error reading {db_filename}"
78+
"Please specify accessible --data-dir."
79+
)
80+
sys.exit(1)
81+
else:
82+
# Check directory is writeable
83+
if os.access(data_dir, os.W_OK):
84+
print(f"Creating {db_filename} as cache.")
85+
else:
86+
print(
87+
f"Directory {data_dir} is not writeable."
88+
"Please specify accessible --data-dir."
89+
)
90+
sys.exit(1)
7291

7392
# Open the database for read/write
7493
file_uri = f"file:{db_filename}"

pyard/misc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# > http://www.fsf.org/licensing/licenses/lgpl.html
2020
# > http://www.opensource.org/licenses/lgpl-license.php
2121
#
22+
import os
2223
import pathlib
2324
import tempfile
2425
from typing import List
@@ -142,7 +143,8 @@ def get_imgt_version(imgt_version):
142143

143144

144145
def get_default_db_directory():
145-
return pathlib.Path(tempfile.gettempdir()) / "pyard"
146+
username = os.getlogin()
147+
return pathlib.Path(tempfile.gettempdir()) / f"pyard-{username}"
146148

147149

148150
def validate_reduction_type(ars_type):

0 commit comments

Comments
 (0)