Skip to content

Commit

Permalink
fix(api): Make the Makefile have more real prerequisites/targets (#2510)
Browse files Browse the repository at this point in the history
* fix(api): Make the Makefile have more real prerequisites/targets

Instead of using all phony targets, the makefile now pulls the version out of
package.json so it can find the name of the wheel that will be built and can use
it as a normal Makefile target. Then, we can use that as the prereq for wheel
instead of having wheel contain the commands directly, which means that all the
stuff that now depends on wheel won’t rerun it every single time it’s called.
  • Loading branch information
sfoster1 authored Oct 18, 2018
1 parent 36c9f73 commit 1eb207a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
59 changes: 40 additions & 19 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
SHELL := /bin/bash

# add yarn CLI dev deps to PATH (for cross platform POSIX commands via shx)
# and also make an explicit version for shx for use in the shell function,
# where PATH won’t be propagated
PATH := $(shell cd .. && yarn bin):$(PATH)
SHX := npx shx

# make push wheel file (= rather than := to expand at every use)
wheel = $(wildcard dist/*.whl)
firmware = $(wildcard smoothie/*.hex)

# python and pipenv config
Expand All @@ -16,9 +18,32 @@ pytest := pipenv run py.test
pipenv_opts := --dev
pipenv_opts += $(and $(CI),--keep-outdated)

# Find the version of the wheel from package.json using a helper script. We
# use python here so we can use the same version normalization that will be
# used to create the wheel.
wheel_file = dist/opentrons-$(shell $(python) normalized_version.py)-py2.py3-none-any.whl
wheel_pattern := dist/opentrons-%-py2.py3-none-any.whl

# These variables can be overriden when make is invoked to customize the
# behavior of pytest. For instance,
# make test tests=tests/opentrons/tools/test_qc_scripts.py would run only the
# specified test
tests ?= tests
test_opts ?=

# Source discovery
# For the python sources
ot_py_sources := $(filter %.py,$(shell $(SHX) find src/opentrons/))
# And the out of tree shared data
ot_shared_data_sources := $(filter %.json,$(shell $(SHX) find ../shared-data/))
# And the arbitrary stuff in resources
ot_resources := $(filter %,$(shell $(SHX) find src/opentrons/resources))
ot_sources := $(ot_py_sources) $(ot_shared_data_sources) $(ot_resources)

# Defined separately than the clean target so the wheel file doesn’t have to
# depend on a PHONY target
clean_cmd = shx rm -rf build dist .coverage coverage.xml '*.egg-info' '**/__pycache__' '**/*.pyc'

.PHONY: install
install:
pipenv sync $(pipenv_opts)
Expand All @@ -28,21 +53,23 @@ all: lint test

.PHONY: clean
clean:
shx rm -rf \
build \
dist \
.coverage \
coverage.xml \
'*.egg-info' \
'**/__pycache__' \
'**/*.pyc'
$(clean_cmd)

$(wheel_pattern): setup.py $(ot_sources)
$(clean_cmd)
$(python) setup.py bdist_wheel
shx rm -rf build
shx ls dist

.PHONY: wheel
wheel: $(wheel_file)

.PHONY: test
test: local-install
${pytest} ${tests} ${test_opts}
$(pytest) $(tests) $(test_opts)

.PHONY: lint
lint:
lint: $(ot_py_sources)
$(python) -m mypy src/opentrons
$(python) -m pylama src/opentrons tests

Expand All @@ -62,15 +89,9 @@ dev: export ENABLE_VIRTUAL_SMOOTHIE := true
dev: local-install
$(python) -m opentrons.main -P 31950

.PHONY: wheel
wheel: clean
$(python) setup.py bdist_wheel
shx rm -rf build
shx ls dist

.PHONY: local-install
local-install: wheel
$(pip) install --ignore-installed --no-deps dist/opentrons-*.whl
$(pip) install --ignore-installed --no-deps $(wildcard dist/opentrons-*.whl)

.PHONY: local-shell
local-shell: local-install
Expand All @@ -80,7 +101,7 @@ local-shell: local-install
push: wheel
curl -X POST \
-H "Content-Type: multipart/form-data" \
-F "whl=@$(wheel)" \
-F "whl=@$(wildcard dist/opentrons-*.whl)" \
http://$(host):31950/server/update

.PHONY: flash
Expand Down
18 changes: 18 additions & 0 deletions api/normalized_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
import os
# Pipenv requires setuptools >= 36.2.1. Since 36.2.1, setuptools changed
# the way they vendor dependencies, like the packaging module that
# provides the way to normalize version numbers for wheel file names. So
# we try all the possible ways to find it.
try:
# new way
from setuptools.extern import packaging
except ImportError:
# old way
from pkg_resources.extern import packaging

pkg_json_path = os.path.join('src', 'opentrons', 'package.json')
old_ver = json.load(open(pkg_json_path))['version']
vers_obj = packaging.version.Version(old_ver)

print(str(vers_obj))

0 comments on commit 1eb207a

Please sign in to comment.