Skip to content

Commit

Permalink
Check python environment (#49)
Browse files Browse the repository at this point in the history
* check python environment

* fix protobuf test

* list files

* try if pip

* ensure pip

* activate env

* exec activate

* same in win

* source it

* print diff

* decode output

* install in venv

* typo

* copy things to env

* copy the env

* use sys.executable

* uninstall protobuf after the example

* uninstall non interactive

* same strategy for Windows
  • Loading branch information
jgsogo authored Feb 10, 2020
1 parent 2eae973 commit 29cdcc8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
23 changes: 21 additions & 2 deletions .ci/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ def ensure_cache_preserved():
raise Exception("Example modifies cache!")


@contextmanager
def ensure_python_environment_preserved():
freeze = subprocess.check_output("{} -m pip freeze".format(sys.executable), stderr=subprocess.STDOUT, shell=True).decode()
try:
yield
finally:
freeze_after = subprocess.check_output("{} -m pip freeze".format(sys.executable), stderr=subprocess.STDOUT, shell=True).decode()
if freeze != freeze_after:
writeln_console(">>> " + colorama.Fore.RED + "This example modifies the Python dependencies!")
removed = set(freeze.splitlines()) - set(freeze_after.splitlines())
added = set(freeze_after.splitlines()) - set(freeze.splitlines())
for it in removed:
writeln_console("- " + it)
for it in added:
writeln_console("+ " + it)
raise Exception("Example modifies Python environment!")


def run_scripts(scripts):
results = OrderedDict.fromkeys(scripts, '')
for script in scripts:
Expand All @@ -140,8 +158,9 @@ def run_scripts(scripts):
except:
pass

with ensure_cache_preserved():
result = subprocess.call(build_script, env=env)
with ensure_python_environment_preserved():
with ensure_cache_preserved():
result = subprocess.call(build_script, env=env)

results[script] = result
if result != 0 and FAIL_FAST:
Expand Down
8 changes: 3 additions & 5 deletions libraries/protobuf/serialization/build.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
@ECHO ON

pip install -U protobuf

RMDIR /Q /S build
MKDIR build
PUSHD build
Expand All @@ -11,7 +9,7 @@ cmake .. -G "%CMAKE_GENERATOR%"
cmake --build . --config Release

bin\sensor.exe
ECHO. 2>__init__.py

POPD
python main.py
pip install protobuf
python ../main.py
pip uninstall -y protobuf
8 changes: 3 additions & 5 deletions libraries/protobuf/serialization/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
set -e
set -x

pip install -U protobuf

rm -rf build
mkdir build
pushd build
Expand All @@ -14,7 +12,7 @@ cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .

bin/sensor
touch __init__.py

popd
python main.py
pip install protobuf
python ../main.py
pip uninstall -y protobuf
19 changes: 10 additions & 9 deletions libraries/protobuf/serialization/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from build.sensor_pb2 import Sensor

if __name__ == "__main__":
with open(os.path.join("build", "sensor.data"), 'rb') as file:
with open("sensor.data", 'rb') as file:
content = file.read()
print("Retrieve Sensor object from sensor.data")
sensor = Sensor()
sensor.ParseFromString(content)
door = "Open" if sensor.door else "Closed"
print(f"Sensor name: {sensor.name}")
print(f"Sensor temperature: {sensor.temperature}")
print(f"Sensor humidity: {sensor.humidity}")
print(f"Sensor door: {door}")

print("Retrieve Sensor object from sensor.data")
sensor = Sensor()
sensor.ParseFromString(content)
door = "Open" if sensor.door else "Closed"
print(f"Sensor name: {sensor.name}")
print(f"Sensor temperature: {sensor.temperature}")
print(f"Sensor humidity: {sensor.humidity}")
print(f"Sensor door: {door}")

0 comments on commit 29cdcc8

Please sign in to comment.