-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbox.py
34 lines (24 loc) · 945 Bytes
/
toolbox.py
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
#!/usr/bin/env python
"""A collection of useful modules. Do not import from this file. Instead, copy
the code into the main project file. This will allow the main project to be
used as a standalone program."""
import fcntl
import os
from typing import Optional, TypeVar
class Fail(Exception):
pass
def mutex():
this_script = os.path.realpath(__file__)
lockfd = os.open(this_script, os.O_RDONLY)
try:
fcntl.flock(lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
raise Fail(f"{this_script} is already running")
# From: https://github.com/facebook/pyre-check/blob/master/pyre_extensions/__init__.py#L7-L8
_T = TypeVar("_T")
def none_throws(optional: Optional[_T], message: str = "Unexpected `None`") -> _T:
"""Convert an optional to its value. Raises an `AssertionError` if the
value is `None`"""
if optional is None:
raise AssertionError(message)
return optional