|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import tarfile |
| 5 | +import tempfile |
| 6 | +import requests |
| 7 | + |
| 8 | +from sinol_make import util |
| 9 | + |
| 10 | + |
| 11 | +def check_oiejq(path = None): |
| 12 | + """ |
| 13 | + Function to check if oiejq is installed |
| 14 | + """ |
| 15 | + if sys.platform != 'linux': |
| 16 | + return False |
| 17 | + |
| 18 | + def check(path): |
| 19 | + try: |
| 20 | + p = subprocess.Popen([path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 21 | + p.wait() |
| 22 | + p.kill() |
| 23 | + return p.returncode == 0 |
| 24 | + except FileNotFoundError: |
| 25 | + return False |
| 26 | + |
| 27 | + if path is not None: |
| 28 | + return check(path) |
| 29 | + |
| 30 | + if not check(os.path.expanduser('~/.local/bin/oiejq')): |
| 31 | + return False |
| 32 | + else: |
| 33 | + return True |
| 34 | + |
| 35 | + |
| 36 | +def install_oiejq(): |
| 37 | + """ |
| 38 | + Function to install oiejq, if not installed. |
| 39 | + Returns True if successful. |
| 40 | + """ |
| 41 | + |
| 42 | + if sys.platform != 'linux': |
| 43 | + return False |
| 44 | + if check_oiejq(): |
| 45 | + return True |
| 46 | + |
| 47 | + if not os.path.exists(os.path.expanduser('~/.local/bin')): |
| 48 | + os.makedirs(os.path.expanduser('~/.local/bin'), exist_ok=True) |
| 49 | + |
| 50 | + try: |
| 51 | + request = requests.get('https://oij.edu.pl/zawodnik/srodowisko/oiejq.tar.gz') |
| 52 | + except requests.exceptions.ConnectionError: |
| 53 | + raise Exception('Couldn\'t download oiejq (https://oij.edu.pl/zawodnik/srodowisko/oiejq.tar.gz couldn\'t connect)') |
| 54 | + if request.status_code != 200: |
| 55 | + raise Exception('Couldn\'t download oiejq (https://oij.edu.pl/zawodnik/srodowisko/oiejq.tar.gz returned status code: ' + str(request.status_code) + ')') |
| 56 | + |
| 57 | + # oiejq is downloaded to a temporary directory and not to the `cache` dir, |
| 58 | + # as there is no guarantee that the current directory is the package directory. |
| 59 | + # The `cache` dir is only used for files that are part of the package and those |
| 60 | + # that the package creator might want to look into. |
| 61 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 62 | + oiejq_path = os.path.join(tmpdir, 'oiejq.tar.gz') |
| 63 | + with open(oiejq_path, 'wb') as oiejq_file: |
| 64 | + oiejq_file.write(request.content) |
| 65 | + |
| 66 | + def strip(tar): |
| 67 | + l = len('oiejq/') |
| 68 | + for member in tar.getmembers(): |
| 69 | + member.name = member.name[l:] |
| 70 | + yield member |
| 71 | + |
| 72 | + with tarfile.open(oiejq_path) as tar: |
| 73 | + tar.extractall(path=os.path.expanduser('~/.local/bin'), members=strip(tar)) |
| 74 | + os.rename(os.path.expanduser('~/.local/bin/oiejq.sh'), os.path.expanduser('~/.local/bin/oiejq')) |
| 75 | + |
| 76 | + return check_oiejq() |
| 77 | + |
| 78 | + |
| 79 | +def get_oiejq_path(): |
| 80 | + if not check_oiejq(): |
| 81 | + return None |
| 82 | + |
| 83 | + def check(path): |
| 84 | + p = subprocess.Popen([path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 85 | + p.wait() |
| 86 | + p.kill() |
| 87 | + if p.returncode == 0: |
| 88 | + return True |
| 89 | + else: |
| 90 | + return False |
| 91 | + |
| 92 | + if check(os.path.expanduser('~/.local/bin/oiejq')): |
| 93 | + return os.path.expanduser('~/.local/bin/oiejq') |
| 94 | + else: |
| 95 | + return None |
| 96 | + |
| 97 | + |
| 98 | +def check_perf_counters_enabled(): |
| 99 | + """ |
| 100 | + Checks if `kernel.perf_event_paranoid` is set to -1. |
| 101 | + :return: |
| 102 | + """ |
| 103 | + if sys.platform != 'linux' or not check_oiejq(): |
| 104 | + return |
| 105 | + |
| 106 | + oiejq = get_oiejq_path() |
| 107 | + test_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'perf_test.py') |
| 108 | + python_executable = sys.executable |
| 109 | + |
| 110 | + # subprocess.Pipe is not used, because than the code would hang on process.communicate() |
| 111 | + with tempfile.TemporaryFile() as tmpfile: |
| 112 | + process = subprocess.Popen([oiejq, python_executable, test_file], stdout=tmpfile, stderr=subprocess.DEVNULL) |
| 113 | + process.wait() |
| 114 | + tmpfile.seek(0) |
| 115 | + output = tmpfile.read().decode('utf-8') |
| 116 | + process.terminate() |
| 117 | + |
| 118 | + if output != "Test string\n": |
| 119 | + util.exit_with_error("To use the recommended tool for measuring time called oiejq, please:\n" |
| 120 | + "- execute `sudo sysctl kernel.perf_event_paranoid=-1` to make oiejq work for\n" |
| 121 | + " the current system session,\n" |
| 122 | + "- or add `kernel.perf_event_paranoid=-1` to `/etc/sysctl.conf`\n" |
| 123 | + " and reboot to permanently make oiejq work.\n" |
| 124 | + "For more details, see https://github.com/sio2project/sio2jail#running.\n") |
0 commit comments