Skip to content
This repository has been archived by the owner on Mar 18, 2019. It is now read-only.

Perform sanity checks with Python3 #25

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include mk/python.mk
DCURL_DIR := deps/dcurl
DCURL_LIB := $(DCURL_DIR)/build/libdcurl.so
DEPS += $(DCURL_LIB)
Expand All @@ -19,7 +20,7 @@ TESTS += $(wildcard tests/tangleid/*.sh)
check: server.py $(DCURL_LIB)
@ TMP_PID=`mktemp /tmp/server_pid.XXXXXX`; \
echo "Running test suite..." ; \
( python $^ & echo $$! > $${TMP_PID} ); \
( $(PYTHON) $^ & echo $$! > $${TMP_PID} ); \
sleep 3 ; \
for i in $(TESTS); do \
( echo "\n\n==[ $$i ]==\n"; $$i || kill -9 `cat $${TMP_PID}` ) \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Most use cases for micropayments involve a single user or device interacting
repeatedly with a few vendors.

## Prerequisites
This package depends on PyOTA, which is known to work with Python 3.5. However, before PyOTA issue [#203](https://github.com/iotaledger/iota.lib.py/issues/203) is resolved, Python 3.6/3.7 will be excluded for running this package. The build system would detect and validate the environment in advance.
Copy link
Contributor

Choose a reason for hiding this comment

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

PyOTA doesn't support Python 3.7 because it has package Filters which doesn't either.


Install dependent packages:
```shell
Expand Down
47 changes: 47 additions & 0 deletions mk/python.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# check Python version
PY_FAVORED_VER = 3.5
PYTHON := $(shell which "python")
ifndef PYTHON
Copy link
Collaborator

@jserv jserv Aug 18, 2018

Choose a reason for hiding this comment

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

Add extra condition if python indicated by $PATH is exactly of version 3. Think of the case that somebody built Python3 from source and the generated executable is python.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If python is not present, try python3. Then, if version is mismatching, try python3.5.

PYTHON := $(shell which "python3")
endif

Copy link
Collaborator

Choose a reason for hiding this comment

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

No blank line here.

ifndef PYTHON
PYTHON := $(shell which "python$(PY_FAVORED_VER)")
endif

ifndef PYTHON
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto. Focus on detailed items.

$(error "Python is not executable, try to check the PATH environment variable or \
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is ambiguous to say "Python is not executable". Use precise words.

install python$(PY_FAVORED_VER) package.")
endif # $(shell which "python")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove non-effective "$(shell which "python")".

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's say, "no valid Python interpreter is found".


Copy link
Collaborator

Choose a reason for hiding this comment

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

Avoid this blank line.

PY_CHECK_VERSION := $(shell $(PYTHON) -V)

ifeq "$(filter $(PY_FAVORED_VER).%, $(PY_CHECK_VERSION))" ""
PYTHON := $(shell which "python"$(PY_FAVORED_VER))
ifdef PYTHON
$(error "Found python$(PY_FAVORED_VER), but it should be set as \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it is valid according to the error message?

default interpreter, try to set $(PYTHON) in PATH environment variable.")
else
$(error "$(shell python -V):Unsupported python version, only available for Python$(PY_FAVORED_VER).")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Explain the exact reason and point unresolved issue(s) here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Validate ifdef-else statement. Logic error.

endif # PYTHON
endif # "$(filter ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto. You don't have to mention "$(filter ..." once you remove nested conditions.


# check "iota" module in Python installation
PY_CHECK_MOD_IOTA := $(shell python -c "import iota" 2>/dev/null && \
echo 1 || echo 0)
ifeq ("$(PY_CHECK_MOD_IOTA)","0")
PIP := $(shell pip -V && echo 1 || echo 0))
PIP_CHECK_VERSION := $(findstring python 3, $(PIP) && echo $@ || echo 0)
endif

ifneq ("$(PIP)", "1")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use ifdef rather than ifneq or ifeq for consistency.

$(error "PIP package doesn't pre-configured, try to install it.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fix grammar error.

endif

ifeq ("$(PIP_CHECK_VERSION)", "0")
PIP_FAVORED_VER = 3
endif

ifeq ("$(PY_CHECK_MOD_IOTA)", "0")
$(error "PyOTA package doesn't pre-configured, install the latest version: pip$(PIP_FAVORED_VER) install pyota")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto. Fix grammar.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Using pip is not the only way to install Python packages. Sometimes, package maintainer might prepare prebuilt one or recipes that describe how to install Python packages. You can leave the message: "please ask your administrator to install the package 'pyota' or build from source: https://github.com/iotaledger/iota.lib.py"

endif
3 changes: 2 additions & 1 deletion swarm_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ def send_transfer(tag, messages, address, values, dict_tips, debug=0):
time_end_pow = time.time()
elapsed_pow = elapsed_pow + (time_end_pow - time_start_pow)

print("Prepare to broadcast ...")
print("Prepare to store and broadcast ...")
try:
api.store_transactions([tx_tryte[0:2673]])
api.broadcast_transactions([tx_tryte[0:2673]])
except Exception as e:
print("Error: %s" % (str(e.context)))
Expand Down