77import os
88import re
99import sqlite3
10+ import sys
1011from filelock import FileLock
1112
1213import reframe .utility .jsonext as jsonext
@@ -52,6 +53,13 @@ def __init__(self):
5253 self .__db_file = os .path .join (
5354 osext .expandvars (runtime ().get_option ('storage/0/sqlite_db_file' ))
5455 )
56+ mode = runtime ().get_option (
57+ 'storage/0/sqlite_db_file_mode'
58+ )
59+ if not isinstance (mode , int ):
60+ self .__db_file_mode = int (mode , base = 8 )
61+ else :
62+ self .__db_file_mode = mode
5563
5664 def _db_file (self ):
5765 prefix = os .path .dirname (self .__db_file )
@@ -78,6 +86,18 @@ def _db_connect(self, *args, **kwargs):
7886 with getprofiler ().time_region ('sqlite connect' ):
7987 return sqlite3 .connect (* args , ** kwargs )
8088
89+ def _db_lock (self ):
90+ prefix = os .path .dirname (self .__db_file )
91+ if sys .version_info >= (3 , 7 ):
92+ kwargs = {'mode' : self .__db_file_mode }
93+ else :
94+ # Python 3.6 forces us to use an older filelock version that does
95+ # not support file modes. File modes where introduced in
96+ # filelock 3.10
97+ kwargs = {}
98+
99+ return FileLock (os .path .join (prefix , '.db.lock' ), ** kwargs )
100+
81101 def _db_create (self ):
82102 clsname = type (self ).__name__
83103 getlogger ().debug (
@@ -104,6 +124,8 @@ def _db_create(self):
104124 'on testcases(job_completion_time_unix)' )
105125 conn .execute ('CREATE TABLE IF NOT EXISTS metadata('
106126 'schema_version TEXT)' )
127+ # Update DB file mode
128+ os .chmod (self .__db_file , self .__db_file_mode )
107129
108130 def _db_schema_check (self ):
109131 with self ._db_connect (self .__db_file ) as conn :
@@ -164,9 +186,8 @@ def _db_store_report(self, conn, report, report_file_path):
164186 return session_uuid
165187
166188 def store (self , report , report_file = None ):
167- prefix = os .path .dirname (self .__db_file )
168189 with self ._db_connect (self ._db_file ()) as conn :
169- with FileLock ( os . path . join ( prefix , '.db.lock' ) ):
190+ with self . _db_lock ( ):
170191 return self ._db_store_report (conn , report , report_file )
171192
172193 @time_function
@@ -298,8 +319,7 @@ def fetch_session_json(self, uuid):
298319 return jsonext .loads (results [0 ][0 ]) if results else {}
299320
300321 def _do_remove (self , uuid ):
301- prefix = os .path .dirname (self .__db_file )
302- with FileLock (os .path .join (prefix , '.db.lock' )):
322+ with self ._db_lock ():
303323 with self ._db_connect (self ._db_file ()) as conn :
304324 # Enable foreign keys for delete action to have cascade effect
305325 conn .execute ('PRAGMA foreign_keys = ON' )
@@ -316,8 +336,7 @@ def _do_remove(self, uuid):
316336
317337 def _do_remove2 (self , uuid ):
318338 '''Remove a session using the RETURNING keyword'''
319- prefix = os .path .dirname (self .__db_file )
320- with FileLock (os .path .join (prefix , '.db.lock' )):
339+ with self ._db_lock ():
321340 with self ._db_connect (self ._db_file ()) as conn :
322341 # Enable foreign keys for delete action to have cascade effect
323342 conn .execute ('PRAGMA foreign_keys = ON' )
0 commit comments