mirror of https://github.com/ConsenSys/mythril
Add support for locking config file (#1652)
* Add support for locking config file * Remove abi decode test * Fix typopull/1654/head
parent
21999f8b50
commit
5a184e8225
@ -0,0 +1,78 @@ |
||||
import os |
||||
import time |
||||
import errno |
||||
|
||||
""" |
||||
credits: https://github.com/dmfrey/FileLock |
||||
""" |
||||
|
||||
|
||||
class LockFileException(Exception): |
||||
pass |
||||
|
||||
|
||||
class LockFile(object): |
||||
""" |
||||
Locks files. |
||||
""" |
||||
|
||||
def __init__(self, file_name, timeout=100, delay=0.05): |
||||
""" |
||||
Initialises the file locker |
||||
""" |
||||
if timeout is not None and delay is None: |
||||
raise ValueError("If timeout is not None, then delay must not be None.") |
||||
self.is_locked = False |
||||
self.lockfile = os.path.join(os.getcwd(), f"{file_name}.lock") |
||||
self.file_name = file_name |
||||
self.timeout = timeout |
||||
self.delay = delay |
||||
|
||||
def acquire(self): |
||||
""" |
||||
Acquires a lock when possible. |
||||
""" |
||||
start_time = time.time() |
||||
while True: |
||||
try: |
||||
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) |
||||
self.is_locked = True |
||||
break |
||||
except OSError as e: |
||||
if e.errno != errno.EEXIST: |
||||
raise |
||||
if (time.time() - start_time) >= self.timeout: |
||||
raise FileLockException( |
||||
f"Stuck for more than {self.timeout} seconds waiting to unlock the file {self.filename}." |
||||
) |
||||
time.sleep(self.delay) |
||||
|
||||
def release(self): |
||||
""" |
||||
Releases the lock |
||||
""" |
||||
if self.is_locked: |
||||
os.close(self.fd) |
||||
os.unlink(self.lockfile) |
||||
self.is_locked = False |
||||
|
||||
def __enter__(self): |
||||
""" |
||||
Lock gets acquired at the `with` statement. |
||||
""" |
||||
if not self.is_locked: |
||||
self.acquire() |
||||
return self |
||||
|
||||
def __exit__(self, type, value, traceback): |
||||
""" |
||||
Lock get's released at the end of the `with` block |
||||
""" |
||||
if self.is_locked: |
||||
self.release() |
||||
|
||||
def __del__(self): |
||||
""" |
||||
Releases the lock during deletion. |
||||
""" |
||||
self.release() |
@ -0,0 +1,16 @@ |
||||
from subprocess import Popen, PIPE |
||||
from tests import PROJECT_DIR, TESTDATA |
||||
import json |
||||
|
||||
MYTH = str(PROJECT_DIR / "myth") |
||||
|
||||
|
||||
def test_parallel(): |
||||
|
||||
test_file = str(TESTDATA / "input_contracts" / "origin.sol") |
||||
program = f"python3 {MYTH} a {test_file} --solv 0.5.0 -o json" |
||||
processes = [Popen(program, stdout=PIPE, shell=True) for i in range(30)] |
||||
for p in processes: |
||||
out, err = p.communicate() |
||||
json_output = json.loads(out.decode("utf-8")) |
||||
assert json_output["success"] == True |
Loading…
Reference in new issue