Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check python environment #49

Merged
merged 19 commits into from
Feb 10, 2020
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
9 changes: 4 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,8 @@ cmake .. -G "%CMAKE_GENERATOR%"
cmake --build . --config Release

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

POPD
python main.py
python -m venv _exproto
_exproto\bin\activate.bat
pip install -U protobuf
python ..\main.py
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not able to make it work with a virtualenv 😢 , so I use the main python environment and uninstall the new package afterward

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protobuf is evil 😈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you remove all python package installed by protobuf as last step? So I think is possible to use with virtualenv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with the virtualenv is that I'm not able to install protobuf inside the virtualenv in Linux (like I do in Windows or Mac), and to pass this new check (--- tests do not modify the python environment ---) I need to remove it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows has failed too 😢 ...so it is something related to the levels of bash calling bash calling python calling bash... so I've moved to the uninstall strategy

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}")