Skip to content

Commit

Permalink
Merge pull request #164 from JdeRobot/adding-style-check
Browse files Browse the repository at this point in the history
Adding style check
  • Loading branch information
CDAM2020 authored Sep 23, 2024
2 parents cefe0fb + 22f6475 commit ac76a04
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions manager/manager/lint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def append_rating_if_missing(self, result):

return result

def evaluate_code(self, code, exercise_id, ros_version, warnings=False):
def evaluate_code(self, code, exercise_id, ros_version, warnings=False, py_lint_source="pylint_checker.py"):
try:
code = re.sub(r'from HAL import HAL', 'from hal import HAL', code)
code = re.sub(r'from GUI import GUI', 'from gui import GUI', code)
Expand All @@ -76,9 +76,9 @@ def evaluate_code(self, code, exercise_id, ros_version, warnings=False):

command = ""
if "humble" in str(ros_version):
command = f"export PYTHONPATH=$PYTHONPATH:/RoboticsAcademy/exercises/static/exercises/{exercise_id}/python_template/ros2_humble; python3 RoboticsAcademy/src/manager/manager/lint/pylint_checker.py"
command = f"export PYTHONPATH=$PYTHONPATH:/RoboticsAcademy/exercises/static/exercises/{exercise_id}/python_template/ros2_humble; python3 RoboticsAcademy/src/manager/manager/lint/{py_lint_source}"
else:
command = f"export PYTHONPATH=$PYTHONPATH:/RoboticsAcademy/exercises/static/exercises/{exercise_id}/python_template/ros1_noetic; python3 RoboticsAcademy/src/manager/manager/lint/pylint_checker.py"
command = f"export PYTHONPATH=$PYTHONPATH:/RoboticsAcademy/exercises/static/exercises/{exercise_id}/python_template/ros1_noetic; python3 RoboticsAcademy/src/manager/manager/lint/{py_lint_source}"

ret = subprocess.run(
command,
Expand Down
32 changes: 32 additions & 0 deletions manager/manager/lint/pylint_checker_style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import tempfile
import subprocess
import os

# Read user code
code = open('user_code.py')
python_code = code.read()
code.close()

# Create temp file
code_file = tempfile.NamedTemporaryFile(delete=False)
code_file.write(python_code.encode())
code_file.seek(0)
code_file.close()

options = f"{code_file.name} --enable=similarities --disable=C0114,C0116,C0411,E0401,R0022,W0012 --max-line-length=80 --reports=y"

# Run pylint using subprocess
result = subprocess.run(['pylint'] + options.split(), capture_output=True, text=True)

# Process pylint exit
stdout = result.stdout

# Clean temp files
if os.path.exists(code_file.name):
os.remove(code_file.name)

# Replace tmp file name with user_code
output = stdout.replace(code_file.name, "user_code") # For tmp/****
output = output.replace(code_file.name.replace("/tmp/",""), "user_code") # For ****

print(output)
53 changes: 53 additions & 0 deletions manager/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class Manager:
"dest": "idle",
"before": "on_disconnect",
},
# Style check
{
"trigger": "style_check",
"source": "*",
"dest": "*",
"before": "on_style_check_application",
},
]

def __init__(self, host: str, port: int):
Expand Down Expand Up @@ -284,6 +291,52 @@ def add_frequency_control(self, code):
code = code + frequency_control_code_post
return code

def on_style_check_application(self, event):
def find_docker_console():
"""Search console in docker different of /dev/pts/0"""
pts_consoles = [f"/dev/pts/{dev}" for dev in os.listdir('/dev/pts/') if dev.isdigit()]
consoles = []
for console in pts_consoles:
if console != "/dev/pts/0":
try:
# Search if it's a console
with open(console, 'w') as f:
f.write("")
consoles.append(console)
except Exception:
# Continue searching
continue

# raise Exception("No active console other than /dev/pts/0")
return consoles

# Extract app config
app_cfg = event.kwargs.get("data", {})
try:
if app_cfg["type"] == "bt-studio":
return
except Exception:
pass

exercise_id = app_cfg["exercise_id"]
code = app_cfg["code"]

# Make code backwards compatible
code = code.replace("from GUI import GUI", "import GUI")
code = code.replace("from HAL import HAL", "import HAL")

# Create executable app
errors = self.linter.evaluate_code(code, exercise_id, self.ros_version, py_lint_source="pylint_checker_style.py")

if errors == "":
errors = "No errors found"

console_path = find_docker_console()
for i in console_path:
with open(i, 'w') as console:
console.write(errors + "\n\n")

raise Exception(errors)

def on_run_application(self, event):
def find_docker_console():
Expand Down

0 comments on commit ac76a04

Please sign in to comment.