-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
util.py
88 lines (67 loc) · 2.05 KB
/
util.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
#
# gdb-frontend is a easy, flexible and extensionable gui debugger
#
# https://github.com/rohanrhu/gdb-frontend
# https://oguzhaneroglu.com/projects/gdb-frontend/
#
# Licensed under GNU/GPLv3
# Copyright (C) 2019, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected]>
import os
import sys
import io
import contextlib
import threading
import config
def verbose(*text):
if not config.VERBOSE:
return
print("[GDBFrontend]", *text)
sys.stdout.flush()
def gdbPath(path):
return os.path.realpath(os.path.join(config.gdb_path, path.replace("..", "").lstrip("/")))
def appPath(path):
return os.path.realpath(os.path.join(config.app_path, path.replace("..", "").lstrip("/")))
def webFSPath(path):
return os.path.realpath(os.path.join(config.app_path, "frontend", path.replace("..", "").lstrip("/")))
def readFile(path):
fd = open(path, 'r', encoding="utf8")
content = fd.read()
fd.close()
return content
def versionString(version):
release = ""
if not version[-1].isdigit():
release = "-"+version[-1]
version = version[:-1]
return "v"+".".join([str(i) for i in version])+release
class AtomicInteger():
def __init__(self, num=0):
self.num = num
self.lock = threading.Lock()
def incr(self, diff=1):
self.lock.acquire()
self.num += diff
self.lock.release()
def decr(self, diff=1):
self.lock.acquire()
self.num -= diff
self.lock.release()
def set(self, val):
self.lock.acquire()
self.num = val
self.lock.release()
def get(self):
self.lock.acquire()
num = self.num
self.lock.release()
return num
@contextlib.contextmanager
def bufferOutput():
orig_stdout = sys.stdout
temp_stdout = io.StringIO()
sys.stdout = temp_stdout
yield temp_stdout
sys.stdout = orig_stdout
temp_stdout.seek(0)
temp_stdout = temp_stdout.read(0)