+
+########## for uploading onto pypi
+# this assumes you have an entry 'pypi' in your .pypirc
+# see pypi documentation on how to create .pypirc
+LOCAL_ID=$(shell id)
+ifneq "$(LOCAL_ID)" "$(filter $(LOCAL_ID),parmen)"
+BUILD_ID=thierry
+USE_TWINE=
+else
+BUILD_ID=mario
+USE_TWINE=true
+endif
+
+PYPI_TARGET=pypi
+PYPI_TARBALL_HOST=$(BUILD_ID)@build.onelab.eu
+PYPI_TARBALL_TOPDIR=/build/nepi
+
+VERSION = $(shell cat VERSION)
+VERSIONTAG = nepi-$(VERSION)-pypi
+VERSIONTAR = nepi-$(VERSION).tar.gz
+GIT-TAG-ALREADY-SET = $(shell git tag | grep '^$(VERSIONTAG)$$')
+# to check for uncommitted changes
+GIT-CHANGES = $(shell echo $$(git diff HEAD | wc -l))
+GIT-URL = git://git.onelab.eu/nepi.git
+
+version: nepi/util/version.py
+nepi/util/version.py: VERSION nepi/util/version.py.in
+ sed -e "s,@VERSIONTAG@,$(VERSION),g" -e "s,@SCMURL@,$(GIT-URL),g" nepi/util/version.py.in > $@
+
+clean: version-clean
+version-clean:
+ rm -f nepi/util/version.py
+
+##########
+# depending on the value of USE_TWINE, we either do the upload with setup.py itself,
+# or with the help of twine which apparently is the only way that mario could find out on his setup
+define upload_pypi
+$(if $(USE_TWINE),./setup.py sdist && twine upload dist/* -r $(1),./setup.py sdist upload -r $(1))
+endef
+
+# run this only once the sources are in on the right tag
+pypi: version
+ @if [ $(GIT-CHANGES) != 0 ]; then echo "You have uncommitted changes - cannot publish"; false; fi
+ @if [ -n "$(GIT-TAG-ALREADY-SET)" ] ; then echo "tag $(VERSIONTAG) already set"; false; fi
+ @if ! grep -q ' $(VERSION)' CHANGELOG.md ; then echo no mention of $(VERSION) in CHANGELOG.md; false; fi
+ @echo "You are about to release $(VERSION) - OK (Ctrl-c if not) ? " ; read _
+ git tag $(VERSIONTAG)
+ $(call upload_pypi,pypi)
+ @if [ ssh $(PYPI_TARBALL_HOST) ls $(PYPI_TARBALL_TOPDIR)/$(VERSIONTAR) ] ;\
+ then echo "$(VERSIONTAR) already present on $(PYPI_TARBALL_HOST) - ignored" ;\
+ else rsync -av dist/$(VERSIONTAR) $(PYPI_TARBALL_HOST):$(PYPI_TARBALL_TOPDIR)/ ;\
+ fi
+
+
+# it can be convenient to define a test entry, say testpypi, in your .pypirc
+# that points at the testpypi public site
+# no upload to build.onelab.eu is done in this case
+# try it out with
+# pip install -i https://testpypi.python.org/pypi nepi
+# dependencies need to be managed manually though
+testpypi: version
+ $(call upload_pypi,testpypi)
+
+#################### convenience, for debugging only
+# make +foo : prints the value of $(foo)
+# make ++foo : idem but verbose, i.e. foo=$(foo)
+++%: varname=$(subst +,,$@)
+++%:
+ @echo "$(varname)=$($(varname))"
++%: varname=$(subst +,,$@)
++%:
+ @echo "$($(varname))"