From d4ed926e5549b4bf383dc2c381c7c3561f4d96cb Mon Sep 17 00:00:00 2001 From: Sylvain Tricot Date: Wed, 2 Dec 2020 18:18:47 +0100 Subject: [PATCH] Work on Makefiles to make install easier... --- Makefile | 23 +++++++--- doc/Makefile | 5 ++- src/Makefile | 22 ++++++--- src/msspec.sh.template | 100 +++++++++++++++++++++++++++++++++++++++++ src/msspec/version.py | 5 ++- src/options.mk | 16 +++---- src/setup.py | 4 +- 7 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 src/msspec.sh.template diff --git a/Makefile b/Makefile index 298f5fb..b725978 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include src/options.mk -PYTHON = python3 +PYTHON = python # Checking Python version PYTHON_EXE := $(shell command -v $(PYTHON) 2> /dev/null) @@ -17,21 +17,30 @@ ifneq ($(shell test $(PYTHON_VERSION_MAJOR) -ge 3 -a $(PYTHON_VERSION_MINOR) -ge endif -.PHONY: pybinding venv doc +.PHONY: pybinding venv doc clean pybinding: @+$(MAKE) -C src pybinding +install: venv + @+$(MAKE) -C src sdist + @+$(MAKE) -C src frontend + @. $(VENV_PATH)/bin/activate && pip install src/dist/msspec-$(VERSION).tar.gz + venv: - virtualenv --python=$(PYTHON_EXE) --prompt="(msspec-$(VERSION)) " venv - . ./venv/bin/activate && pip install --upgrade pip && pip install -r src/pip.freeze + virtualenv --python=$(PYTHON_EXE) --prompt="(msspec-$(VERSION)) " $(VENV_PATH) + . $(VENV_PATH)/bin/activate && pip install --upgrade pip && pip install -r src/pip.freeze doc: @echo "Building pdf and html documentation..." @mkdir -p package/ - @+$(MAKE) -C doc/ latexpdf $(SUPPRESS_OUPUT) + @+$(MAKE) -C doc/ latexpdf @rm -rf package/*.pdf - @cp "doc/build/latex/MsSpec-python.pdf" "./package/MsSpec-${VERSION}.pdf" - @+$(MAKE) -C doc/ html $(SUPPRESS_OUPUT) + @cp "doc/build/latex/MsSpec-python.pdf" "./package/MsSpec-$(VERSION).pdf" + @+$(MAKE) -C doc/ html + +clean:: + @+$(MAKE) -C src/ clean + diff --git a/doc/Makefile b/doc/Makefile index 7fec930..9a2b5fc 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -83,8 +83,9 @@ html0: banner images @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." -html: html0 ownership $(BUILDDIR)/html/sitemap.xml coverage_report - @echo +#html: html0 ownership $(BUILDDIR)/html/sitemap.xml coverage_report + +html: html0 ownership $(BUILDDIR)/html/sitemap.xml coverage_report: @echo "Generating coverage report..." diff --git a/src/Makefile b/src/Makefile index a6ac732..7091a4a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,20 +1,31 @@ include options.mk -.PHONY: sdist pybinding results clean +.PHONY: sdist frontend pybinding results clean -install: sdist - @echo "Installing msspec python package" - @pip install dist/msspec-*.tar.gz -sdist: pybinding +foo: + echo $(VERSION) + +sdist: dist/msspec-$(VERSION).tar.gz + + +frontend: $(INSTALL_PREFIX)/bin/msspec + + +dist/msspec-$(VERSION).tar.gz: pybinding @echo "Creating Python source distribution..." @python setup.py sdist +$(INSTALL_PREFIX)/bin/msspec: + @cat msspec.sh.template | sed 's/__VERSION__/$(VERSION)/' > $@ + @chmod u+x $@ + pybinding: @echo "Building Python binding for phagen and spec..." @+$(MAKE) -C msspec/phagen/fortran all @+$(MAKE) -C msspec/spec/fortran all + @echo "$(VERSION)" > VERSION results: msspec/results.txt @@ -38,6 +49,7 @@ clean:: # remove previous sdist @rm -rf dist @rm -rf *.egg* + @rm VERSION help: diff --git a/src/msspec.sh.template b/src/msspec.sh.template new file mode 100644 index 0000000..153d924 --- /dev/null +++ b/src/msspec.sh.template @@ -0,0 +1,100 @@ +#!/bin/bash + +SCRIPT_NAME="$0" +VERSION="__VERSION__" +VENV_PATH="$HOME/.local/src/msspec_venv_$VERSION" + +# Check venv path +if ! [ -d "$VENV_PATH" ]; then + echo "ERROR: Unable to find version $VERSION of msspec!!" + exit 1 +fi + +launch_script() { + . "$VENV_PATH/bin/activate" && python $@ +} + +show_help () { + echo "Usage: 1) $SCRIPT_NAME -p [PYTHON OPTIONS] SCRIPT [ARGUMENTS...]" + echo " 2) $SCRIPT_NAME [-l FILE | -i | -h]" + echo " 3) eval \$($SCRIPT_NAME -e)" + echo "" + echo "Form (1) is used to launch a script" + echo "Form (2) is used to load a hdf5 data file" + echo "Form (3) is used to activate the msspec virtual environment" + echo "" + echo "List of possible options:" + echo " -p Pass every arguments after this option to the msspec" + echo " virtual environment Python interpreter." + echo " -i Run the interactive Python interpreter within msspec" + echo " virtual environment." + echo " -l Load and display a *.hdf5 data file in a graphical" + echo " window." + echo " -h Show this help message." +} + +launch_interpreter() { + source $VENV_PATH/bin/activate + echo "Python interpreter of the MsSpec virtual environment." + echo "" + echo "This environment includes the following packages:" + pip list + echo "" + echo "Type in +D to exit." + echo "" + ipython --profile=msspec +} + +load_data() { + . "$VENV_PATH/bin/activate" && python -m msspec.iodata $1 +} + +read_yes_no () { + PROMPT="$1" + DEFAULT="$2" + BYPASS="$3" + + ANSWER="" + RESPONSE="" + + VALID=1 + while test $VALID -ne 0; do + test "$BYPASS" = "y" || read -p "${PROMPT} (y/n) [${DEFAULT}]? " "RESPONSE" + ANSWER="${RESPONSE:-${DEFAULT}}" + case "${ANSWER}" in + y|n) VALID=0 ;; + *) echo "Invalid choice, please answer \"y\" or \"n\"."; VALID=1 ;; + esac + done +} + +uninstall() { + read_yes_no "This will completely remove msspec from your computer. Are you sure" "n" "$BYPASS" + case "${ANSWER}" in + y) rm -rv "$VENV_PATH" && rm "$SCRIPT_NAME" && echo "MsSpec successfully uninstalled." ;; + n) echo "Uninstallation aborted." ;; + esac +} + + + +while getopts "hil:p:eu" option; do + case $option in + p) shift; launch_script "$@" + ;; + i) launch_interpreter + ;; + l) load_data "$OPTARG"; shift + ;; + e) echo ". $VENV_PATH/bin/activate" + ;; + u) uninstall + ;; + *|h) show_help + ;; + esac +done + +if [ $OPTIND -eq 1 ]; then + show_help +fi diff --git a/src/msspec/version.py b/src/msspec/version.py index 53caa06..0dd57e8 100644 --- a/src/msspec/version.py +++ b/src/msspec/version.py @@ -44,7 +44,10 @@ except Exception as err: __version__ = get_distribution(__name__.strip('.version')).version except Exception as err: try: - with open("../VERSION", "r") as fd: + thisfile_path = os.path.abspath(__file__) + thisfile_dir = os.path.dirname(thisfile_path) + versionfile = os.path.join(thisfile_dir, "../VERSION") + with open(versionfile, "r") as fd: __version__ = fd.readline() except Exception as err: print("Unable to get the version number!") diff --git a/src/options.mk b/src/options.mk index cf38c15..6e432dd 100644 --- a/src/options.mk +++ b/src/options.mk @@ -4,6 +4,7 @@ F2PY = f2py3 DEBUG = 0 VERBOSE = 0 BUILDDIR = ./build +INSTALL_PREFIX = $(HOME)/.local ################################################################################ @@ -37,6 +38,7 @@ F2PYFLAGS_DBG = --debug-capi --debug ################################################################################ #VERSION:=$(shell python -c "import msspec; print(msspec.__version__)") VERSION:=$(shell git describe|sed 's/-\([[:digit:]]\+\)-.*/\.post\1/') +VENV_PATH := $(INSTALL_PREFIX)/src/msspec_venv_$(VERSION) ifeq ($(VERBOSE),0) OUPUT_REDIRECTION := 1>/dev/null 2>/dev/null @@ -81,18 +83,10 @@ obj: $(OBJS) clean:: - @for objfile in $(OBJS); do \ - if test -f $$objfile; then \ - echo "Removing $$objfile..."; \ - rm $$objfile; \ - fi;\ - done @if test -d $(abspath $(BUILDDIR)); then \ - if test `find $(abspath $(BUILDDIR)) -type f|wc -l` -eq 0; then \ - echo "Removing empty folder $(abspath $(BUILDDIR))..."; \ - rm -r $(abspath $(BUILDDIR)); \ - fi; \ - fi + echo "Removing $(abspath $(BUILDDIR))..."; \ + rm -r $(abspath $(BUILDDIR)); \ + fi @if test x$(SO) != x; then \ if test -f $(SO); then \ echo "Removing $(SO)..."; rm $(SO); \ diff --git a/src/setup.py b/src/setup.py index 21b552e..8dd9c43 100644 --- a/src/setup.py +++ b/src/setup.py @@ -73,6 +73,6 @@ if __name__ == "__main__": ], keywords='spectroscopy atom electron photon multiple scattering', license='GPL', - entry_points={ - 'console_scripts': ['msspec=msspec.cli:main']} + #entry_points={ + # 'console_scripts': ['msspec=msspec.cli:main']} )