Skip to content

Commit

Permalink
Create GH actions python-app.yml (#225)
Browse files Browse the repository at this point in the history
* Create GH actions python-app.yml

* Add flak8 conf, requirements.txt and correct all flake8 warnings/errors. This undoes some of the changes introduced in #221 to use `is` when comparing to `None` (https://peps.python.org/pep-0008/#programming-recommendations)
  • Loading branch information
ghantoos authored Oct 22, 2024
1 parent 1ec2895 commit cf065f0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 120
ignore = E501, W503, C901
exclude = build/
43 changes: 43 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Set up Python path
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install the lshell package
run: pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
10 changes: 4 additions & 6 deletions lshell/checkconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class CheckConfig:

def __init__(self, args, refresh=None, stdin=None, stdout=None, stderr=None):
"""Force the calling of the methods below"""
if stdin == None:
if stdin is None:
self.stdin = sys.stdin
else:
self.stdin = stdin
if stdout == None:
if stdout is None:
self.stdout = sys.stdout
else:
self.stdout = stdout
if stderr == None:
if stderr is None:
self.stderr = sys.stderr
else:
self.stderr = stderr
Expand Down Expand Up @@ -598,7 +598,7 @@ def get_config_user(self):

if os.path.isdir(self.conf["home_path"]):
# change dir to home when initially loading the configuration
if self.refresh == None:
if self.refresh is None:
os.chdir(self.conf["home_path"])
# if reloading the configuration, do not change directory
else:
Expand Down Expand Up @@ -803,9 +803,7 @@ def set_noexec(self):
self.conf["path_noexec"] = self.myeval(self.conf_raw["path_noexec"])
# if path_noexec is empty, disable LD_PRELOAD
# /!\ this feature should be used at the administrator's own risks!

if self.conf["path_noexec"] == "":

return
if not os.path.exists(self.conf["path_noexec"]):
self.log.critical(
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configparser
logging
readline
pexpect
24 changes: 18 additions & 6 deletions test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,26 @@ def test_38_script_execution_with_template(self):

template_path = f"{TOPDIR}/test/template.lsh"
test_script_path = f"{TOPDIR}/test/test.lsh"
wrapper_path = f"{TOPDIR}/bin/lshell_wrapper"

# Step 1: Create the wrapper script
with open(wrapper_path, "w") as wrapper:
wrapper.write(
f"""#!/bin/bash
exec {TOPDIR}/bin/lshell --config {TOPDIR}/etc/lshell.conf "$@"
"""
)

# Make the wrapper executable
os.chmod(wrapper_path, 0o755)

# Copy template.lsh to test.lsh
# Step 2: Copy template.lsh to test.lsh and replace the shebang
shutil.copy(template_path, test_script_path)

# Replace the placeholder in the shebang
with open(test_script_path, "r+") as f:
content = f.read()
content = content.replace("#!SHEBANG", f"#!{TOPDIR}/bin/lshell")
content = content.replace("#!SHEBANG", f"#!{wrapper_path}")
f.seek(0)
f.write(content)
f.truncate()
Expand All @@ -648,7 +660,7 @@ def test_38_script_execution_with_template(self):
self.child = pexpect.spawn(f"{test_script_path}")

# Expected output
expected_output = f"""test\r
expected_output = """test\r
*** forbidden command: dig\r
*** forbidden path: /tmp/\r
FREEDOM\r
Expand Down Expand Up @@ -681,8 +693,8 @@ def test_39_script_execution_with_template_strict(self):
with open(wrapper_path, "w") as wrapper:
wrapper.write(
f"""#!/bin/bash
exec {TOPDIR}/bin/lshell --config {TOPDIR}/etc/lshell.conf --strict 1 "$@"
"""
exec {TOPDIR}/bin/lshell --config {TOPDIR}/etc/lshell.conf --strict 1 "$@"
"""
)

# Make the wrapper executable
Expand All @@ -702,7 +714,7 @@ def test_39_script_execution_with_template_strict(self):
self.child = pexpect.spawn(f"{test_script_path}")

# Expected output
expected_output = f"""test\r
expected_output = """test\r
*** forbidden command -> "dig"\r
*** You have 1 warning(s) left, before getting kicked out.\r
This incident has been reported.\r
Expand Down

0 comments on commit cf065f0

Please sign in to comment.