Skip to content

Commit

Permalink
Shared system utils
Browse files Browse the repository at this point in the history
  • Loading branch information
L1ghtmann authored and 0cyn committed Jan 13, 2024
1 parent 28cba47 commit c431d6c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 69 deletions.
45 changes: 2 additions & 43 deletions src/dragon/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,9 @@
'''

import os, sys, yaml, subprocess, socket
import os, sys, yaml, socket
from shared.util import dbstate, dbwarn, dberror


def system(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
# print(proc.returncode)
return proc.returncode # , std_out, std_err


def system_with_output(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
return proc.returncode, std_out, std_err


def system_pipe_output(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
process = subprocess.Popen(cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)

while True:
realtime_output = process.stdout.readline()
realtime_err = process.stderr.readline()

if realtime_output == '' and realtime_err == '' and process.poll() is not None:
break

if realtime_output:
print(realtime_output.strip(), flush=True)
if realtime_err:
print(realtime_err.strip(), flush=True, file=sys.stderr)

from shared.util import system, system_with_output, system_pipe_output

class DeviceShell:
@staticmethod
Expand Down
15 changes: 2 additions & 13 deletions src/dragon/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python3

import shutil, subprocess, sys, os, timeit, yaml
import shutil, sys, os, timeit, yaml
from math import sin, cos, radians
from shared.util import system

TestDict = yaml.safe_load(open(os.environ['DRAGON_ROOT_DIR'] + '/internal/tests.yml'))
projects = TestDict['ProjectTests']
Expand All @@ -16,18 +17,6 @@ def bench():
return product



def system(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
# print(proc.returncode)
return proc.returncode # , std_out, std_err


def main():
tests = {

Expand Down
15 changes: 2 additions & 13 deletions src/dragongen/toolchain.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import os, os.path
import subprocess


def system_with_output(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
return proc.returncode, std_out, std_err

import os
from shared.util import system_with_output

class Toolchain:
def __init__(self):
Expand Down
43 changes: 43 additions & 0 deletions src/shared/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from enum import IntEnum
import sys
import subprocess


class OutputColors(IntEnum):
Red = 31
Expand Down Expand Up @@ -28,3 +30,44 @@ def dbwarn(tool_name, msg):

def dberror(tool_name, msg):
dprintline(OutputColors.Red, tool_name, OutputColors.White, OutputWeight.Bold, False, msg)


def system(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
# print(proc.returncode)
return proc.returncode # , std_out, std_err


def system_with_output(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
proc = subprocess.Popen("" + cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)
std_out, std_err = proc.communicate()
return proc.returncode, std_out, std_err


def system_pipe_output(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
process = subprocess.Popen(cmd,
stdout=stdout,
stderr=stderr,
shell=True,
universal_newlines=True)

while True:
realtime_output = process.stdout.readline()
realtime_err = process.stderr.readline()

if realtime_output == '' and realtime_err == '' and process.poll() is not None:
break

if realtime_output:
print(realtime_output.strip(), flush=True)
if realtime_err:
print(realtime_err.strip(), flush=True, file=sys.stderr)

0 comments on commit c431d6c

Please sign in to comment.