diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..5f78199 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,29 @@ +before_script: + - "echo $CI_BUILD_ID" + - "echo $CI_BUILD_REF_NAME" + +stages: + - test +# - release + +tests: + stage: test + script: + - "python scripts/pylint_wrapper.py -s source -s tests" + - "nosetests --with-coverage --cover-package=data_buffer --cover-package=engine --cover-package=gate --cover-package=light_sensor --cover-package=power_sensor --cover-xml" + - "nosetests --with-xunit tests/unittests/" + - "sonar-runner" + +#releases: +# stage: release +# script: +# - "python scripts/create_release_script.py" +# - "cd source" +# - "python release.py sdist" +# - "cd .." +# - "scripts/deploy_release.sh" +# only: +# - /^[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$/ +# except: +# - branches + diff --git a/.pydevproject b/.pydevproject index 0ebadbb..170cdc9 100644 --- a/.pydevproject +++ b/.pydevproject @@ -1,7 +1,9 @@ -/${PROJECT_DIR_NAME}/src +/${PROJECT_DIR_NAME}/source +/${PROJECT_DIR_NAME}/scripts +/${PROJECT_DIR_NAME}/tests python 2.7 Default diff --git a/pylintrc b/.pylintrc similarity index 100% rename from pylintrc rename to .pylintrc diff --git a/scripts/pylint_wrapper.py b/scripts/pylint_wrapper.py new file mode 100644 index 0000000..a92c426 --- /dev/null +++ b/scripts/pylint_wrapper.py @@ -0,0 +1,25 @@ +''' +Created on Feb 11, 2017 + +@author: tkl +''' +import os +import sys +import getopt + +def main(argv): + options, _ = getopt.getopt(argv, "s:", ["source="]) + source_list = [] + for opt, args in options: + if opt in ("-s", "--source"): + source_list.append(args) + + source_str = "" + for source in source_list: + source_str += source + " " + + os.system("pylint " + source_str + " -r n --msg-template=\"{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}\" > pylint.txt") + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..4494272 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,11 @@ +sonar.projectKey=chickenhouse:python +sonar.projectName=chickenhouse:python +sonar.projectVersion=1.0 +sonar.host.url=http://sonarqube:9000 +sonar.sources=source +sonar.tests=tests/unittests +sonar.language=py +sonar.sourceEncoding=UTF-8 +sonar.python.xunit.reportPath=nosetests.xml +sonar.python.coverage.reportPath=coverage.xml +sonar.python.pylint.reportPath=pylint.txt diff --git a/src/__init__.py b/source/__init__.py similarity index 100% rename from src/__init__.py rename to source/__init__.py diff --git a/src/data_buffer/__init__.py b/source/data_buffer/__init__.py similarity index 90% rename from src/data_buffer/__init__.py rename to source/data_buffer/__init__.py index f78c802..ace1a24 100644 --- a/src/data_buffer/__init__.py +++ b/source/data_buffer/__init__.py @@ -1,4 +1,3 @@ -import mymath class DataBuffer(object): def __init__(self, length): @@ -16,7 +15,7 @@ class DataBuffer(object): def average(self): if len(self.__data) != self.__max: return None - return mymath.mean(self.__data) + return sum(self.__data) / len(self.__data) def length(self): return len(self.__data) diff --git a/src/data_buffer/test/__init__.py b/source/data_buffer/test/__init__.py similarity index 100% rename from src/data_buffer/test/__init__.py rename to source/data_buffer/test/__init__.py diff --git a/src/engine/__init__.py b/source/engine/__init__.py similarity index 100% rename from src/engine/__init__.py rename to source/engine/__init__.py diff --git a/src/gate/__init__.py b/source/gate/__init__.py similarity index 100% rename from src/gate/__init__.py rename to source/gate/__init__.py diff --git a/src/close.py b/source/gate/close.py similarity index 100% rename from src/close.py rename to source/gate/close.py diff --git a/src/gate/gate_handler.py b/source/gate/gate_handler.py similarity index 100% rename from src/gate/gate_handler.py rename to source/gate/gate_handler.py diff --git a/src/gate/gate_state.py b/source/gate/gate_state.py similarity index 100% rename from src/gate/gate_state.py rename to source/gate/gate_state.py diff --git a/src/light.py b/source/gate/light.py similarity index 100% rename from src/light.py rename to source/gate/light.py diff --git a/src/open.py b/source/gate/open.py similarity index 100% rename from src/open.py rename to source/gate/open.py diff --git a/src/gate_guard.py b/source/gate_guard.py similarity index 100% rename from src/gate_guard.py rename to source/gate_guard.py diff --git a/src/light_sensor/__init__.py b/source/light_sensor/__init__.py similarity index 100% rename from src/light_sensor/__init__.py rename to source/light_sensor/__init__.py diff --git a/src/light_sensor/light_sensor.py b/source/light_sensor/light_sensor.py similarity index 100% rename from src/light_sensor/light_sensor.py rename to source/light_sensor/light_sensor.py diff --git a/src/power_sensor/__init__.py b/source/power_sensor/__init__.py similarity index 100% rename from src/power_sensor/__init__.py rename to source/power_sensor/__init__.py diff --git a/src/communiate/__init__.py b/src/communiate/__init__.py deleted file mode 100644 index 77070f3..0000000 --- a/src/communiate/__init__.py +++ /dev/null @@ -1,54 +0,0 @@ -''' -Created on Dec 19, 2016 - -@author: klaehn -''' -import paho.mqtt.client as mqtt_client - -class Mqtt(object): - ''' Wrapper class for mqtt communication ''' - def __init__(self, hostname, port=1883, keepalive=60, qos=2, retain=True): - #pylint: disable=too-many-arguments - self.__hostname = hostname - self.__port = port - self.__keepalive = keepalive - self.__client = mqtt_client.Client() - self.__is_connected = False - self.__qos = qos - self.__retain = retain - - def connect(self): - ''' Connect to mqtt broker ''' - if not self.__is_connected: - res = self.__client.connect(self.__hostname, self.__port, \ - self.__keepalive) - if res != mqtt_client.MQTT_ERR_SUCCESS: - return False - res = self.__client.loop_start() - if res != mqtt_client.MQTT_ERR_SUCCESS: - return False - self.__is_connected = True - return True - return False - - def disconnect(self): - ''' Disconnect from mqtt broker ''' - if self.__is_connected: - self.__client.loop_stop() - self.__client.disconnect() - self.__is_connected = False - return True - return False - - def transmit(self, topic, payload): - ''' Transmit to subscriber via broker''' - was_connected = True - if not self.__is_connected: - was_connected = False - self.connect() - result = self.__client.publish(topic, payload, self.__qos, self.__retain) - if not was_connected: - self.disconnect() - if result == 0: - return True - return False diff --git a/src/gpio/__init__.py b/src/gpio/__init__.py deleted file mode 100644 index bf38b9e..0000000 --- a/src/gpio/__init__.py +++ /dev/null @@ -1,32 +0,0 @@ -from os.path import islink, isfile - -class Gpio: - DIRECTION_OUT = "out" - DIRECTION_IN = "in" - - def __init__(self, pin): - self.pin = pin - - def export(self): - if not islink("/sys/class/gpio/gpio" + str(self.pin)): - f = open("/sys/class/gpio/export", "w") - f.write(str(self.pin)) - f.close() - - def unexport(self): - if islink("/sys/class/gpio/gpio" + str(self.pin)): - f = open("/sys/class/gpio/unexport", "w") - f.write(str(self.pin)) - f.close() - - def direction(self, direction = DIRECTION_OUT): - if isfile("/sys/class/gpio/gpio" + str(self.pin) + "/direction"): - f = open("/sys/class/gpio/gpio" + str(self.pin) + "/direction", "w") - f.write(direction) - f.close() - - def set(self, value = 0): - if isfile("/sys/class/gpio/gpio" + str(self.pin) + "/value"): - f = open("/sys/class/gpio/gpio" + str(self.pin) + "/value", "w") - f.write(str(value)) - f.close() diff --git a/src/mymath/__init__.py b/src/mymath/__init__.py deleted file mode 100644 index f3168d5..0000000 --- a/src/mymath/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -Math helper functions -""" - -def mean(x_array): - """ - Calculate the mean. - Input: x: array of x-values - Return: mean - """ - if len(x_array) > 0: - return sum(x_array) / len(x_array) - return None diff --git a/src/mymath/unittest/__init__.py b/src/mymath/unittest/__init__.py deleted file mode 100644 index 02c2f28..0000000 --- a/src/mymath/unittest/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -""" Unit tests for mymath module """ -import unittest -import mymath - -class Test(unittest.TestCase): - """ Test class for untit tests """ - def test_mean_even(self): - """ Unit test for mean function """ - test_arr = [1,2,3,4,5] - result = mymath.mean(test_arr) - self.assertEqual(result, 3, "test_mean_even - exp: 3, res: " + str(result)) - - def test_mean_odd(self): - """ Unit test for mean function """ - test_arr = [-1,2,-3,4,-5] - result = mymath.mean(test_arr) - self.assertEqual(result, -1, "test_mean_odd - exp: -1, res: " + str(result)) - - def test_mean_float(self): - """ Unit test for mean function """ - test_arr = [1.9,2.007,3.4,4,50.678] - result = mymath.mean(test_arr) - self.assertEqual(result, 12.397, "test_mean_float - exp: 12.397, res: " + str(result)) - - def test_mean_empty(self): - """ Unit test for mean function """ - test_arr = [] - result = mymath.mean(test_arr) - self.assertIsNone(result) diff --git a/src/wifi_fieldstrength/wifi_fieldstrength.py b/src/wifi_fieldstrength/wifi_fieldstrength.py deleted file mode 100644 index 0810d04..0000000 --- a/src/wifi_fieldstrength/wifi_fieldstrength.py +++ /dev/null @@ -1,23 +0,0 @@ - -from re import match, sub - -class wifi_fieldstrength: - def __init__(self, name = "wlan0"): - self._name = name - self.wf_name = "/proc/net/wireless" - - def name(self): - return self._name - - def read(self): - ret = False - f = open(self.wf_name, "r") - for line in f: - line = line.strip() - mstr = "^" + self._name - if match(mstr, line): - line = sub("\s+", " ", line) - tmp = line.split(" ") - tmp[3] = sub("\.", "", tmp[3]) - ret = int(tmp[3]) - return ret diff --git a/src/wifi_fieldstrength/__init__.py b/tests/__init__.py similarity index 100% rename from src/wifi_fieldstrength/__init__.py rename to tests/__init__.py diff --git a/tests/unittests/__init__.py b/tests/unittests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/data_buffer/test/test_data_buffer.py b/tests/unittests/test_data_buffer.py similarity index 100% rename from src/data_buffer/test/test_data_buffer.py rename to tests/unittests/test_data_buffer.py