-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentloader
executable file
·49 lines (38 loc) · 1.11 KB
/
contentloader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
# Auto-install and activate a virtualenv if possible
import autovenv
autovenv.run()
import contextlib
import fcntl
import inspect
import os
import subprocess
from utils import script_directory
from config import (
DB_NAME,
JOBS_PRE,
JOBS_POST,
)
@contextlib.contextmanager
def process_lock(name=None):
'''
Use a file lock to ensure only one process runs at a time
'''
caller = inspect.getouterframes(inspect.currentframe())[2]
caller_dir = os.path.dirname(os.path.realpath(caller.filename))
if not name:
name = os.path.splitext(os.path.basename(caller.filename))
with open(os.path.join(caller_dir, f'{name}.lock'), 'w') as f:
try:
fcntl.lockf(f, fcntl.LOCK_EX)
yield
finally:
fcntl.lockf(f, fcntl.LOCK_UN)
with script_directory():
for shell_command in JOBS_PRE:
subprocess.run(shell_command, shell=True)
with process_lock(DB_NAME):
import contentloader
contentloader.ContentLoader()
for shell_command in JOBS_POST:
subprocess.run(shell_command, shell=True)