From 3fa656946d28bb8cac061b745e2edc26bc2d56ae Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Sat, 12 Oct 2019 16:47:16 -0500 Subject: [PATCH] Move bup to cmd/ and symlink ./bup to cmd/bup Move main.py to cmd/bup and make ./bup a permanent symlink to it. Make the installed bup executable a relative symlink to the LIBDIR/cmd/bup file. Always use the location of the bup executable as a way to locate the other subcommands and the lib and resource directories, either when running from the source tree or from an install tree. This work finishes the switch to have all the commands to rely on the bup-python wrapper so that we can establish some norms we'll need to support Python 3. It should also allow us to simplify the main.py startup process. Signed-off-by: Rob Browning --- .gitignore | 1 - Makefile | 17 +++++------------ bup | 1 + main.py => cmd/bup | 28 ++++++++++++++++++---------- lib/bup/t/tclient.py | 2 +- lib/bup/t/tgit.py | 2 +- 6 files changed, 26 insertions(+), 25 deletions(-) create mode 120000 bup rename main.py => cmd/bup (93%) diff --git a/.gitignore b/.gitignore index d82e27a..8ea45fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/bup /cmd/bup-* randomgen memtest diff --git a/Makefile b/Makefile index 338d595..d285f3a 100644 --- a/Makefile +++ b/Makefile @@ -50,22 +50,13 @@ bup_cmds := cmd/bup-python \ $(patsubst cmd/%-cmd.py,cmd/bup-%,$(wildcard cmd/*-cmd.py)) \ $(patsubst cmd/%-cmd.sh,cmd/bup-%,$(wildcard cmd/*-cmd.sh)) -bup_deps := bup lib/bup/_checkout.py lib/bup/_helpers$(SOEXT) $(bup_cmds) +bup_deps := lib/bup/_checkout.py lib/bup/_helpers$(SOEXT) $(bup_cmds) all: $(bup_deps) Documentation/all $(current_sampledata) -bup: - ln -s main.py bup - $(current_sampledata): t/configure-sampledata --setup -define install-python-bin - set -e; \ - sed -e '1 s|.*|#!$(bup_python)|; 2,/^# end of bup preamble$$/d' $1 > $2; \ - chmod 0755 $2; -endef - PANDOC ?= $(shell type -p pandoc) ifeq (,$(PANDOC)) @@ -98,8 +89,11 @@ install: all test -z "$(man_roff)" || $(INSTALL) -m 0644 $(man_roff) $(dest_mandir)/man1 test -z "$(man_html)" || install -d $(dest_docdir) test -z "$(man_html)" || $(INSTALL) -m 0644 $(man_html) $(dest_docdir) - $(call install-python-bin,bup,"$(dest_bindir)/bup") + $(INSTALL) -pm 0755 cmd/bup $(dest_libdir)/cmd/ $(INSTALL) -pm 0755 cmd/bup-* $(dest_libdir)/cmd/ + cd "$(dest_bindir)" && \ + ln -sf "$$($(bup_python) -c 'import os; print(os.path.relpath("$(abspath $(dest_libdir))/cmd/bup"))')" + set -e; \ $(INSTALL) -pm 0644 \ lib/bup/*.py \ $(dest_libdir)/bup @@ -321,7 +315,6 @@ clean: Documentation/clean cmd/bup-python rm -f *.o lib/*/*.o *.so lib/*/*.so *.dll lib/*/*.dll *.exe \ .*~ *~ */*~ lib/*/*~ lib/*/*/*~ \ *.pyc */*.pyc lib/*/*.pyc lib/*/*/*.pyc \ - bup \ randomgen memtest \ testfs.img lib/bup/t/testfs.img for x in $$(ls cmd/*-cmd.py cmd/*-cmd.sh | grep -vF python-cmd.sh | cut -b 5-); do \ diff --git a/bup b/bup new file mode 120000 index 0000000..f4083c1 --- /dev/null +++ b/bup @@ -0,0 +1 @@ +cmd/bup \ No newline at end of file diff --git a/main.py b/cmd/bup similarity index 93% rename from main.py rename to cmd/bup index 05e62c4..e442541 100755 --- a/main.py +++ b/cmd/bup @@ -1,7 +1,17 @@ #!/bin/sh """": # -*-python-*- # -*-python-*- -bup_python="$(dirname "$0")/cmd/bup-python" || exit $? -exec "$bup_python" "$0" ${1+"$@"} +set -e +top="$(pwd)" +cmdpath="$0" +# loop because macos doesn't have recursive readlink/realpath utils +while test -L "$cmdpath"; do + link="$(readlink "$cmdpath")" + cd "$(dirname "$cmdpath")" + cmdpath="$link" +done +script_home="$(cd "$(dirname "$cmdpath")" && pwd -P)" +cd "$top" +exec "$script_home/bup-python" "$0" ${1+"$@"} """ # end of bup preamble @@ -21,19 +31,17 @@ import select argv = sys.argv exe = os.path.realpath(argv[0]) exepath = os.path.split(exe)[0] or '.' -exeprefix = os.path.split(os.path.abspath(exepath))[0] # fix the PYTHONPATH to include our lib dir -if os.path.exists("%s/lib/bup/cmd/." % exeprefix): - # installed binary in /.../bin. - # eg. /usr/bin/bup means /usr/lib/bup/... is where our libraries are. - cmdpath = "%s/lib/bup/cmd" % exeprefix - libpath = "%s/lib/bup" % exeprefix +if os.path.exists("%s/../bup/." % exepath): + # Everything is relative to exepath (i.e. LIBDIR/cmd/) + cmdpath = exepath + libpath = os.path.join(exepath, '..') resourcepath = libpath else: # running from the src directory without being installed first - cmdpath = os.path.join(exepath, 'cmd') - libpath = os.path.join(exepath, 'lib') + cmdpath = exepath + libpath = os.path.join(exepath, '../lib') resourcepath = libpath sys.path[:0] = [libpath] os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '') diff --git a/lib/bup/t/tclient.py b/lib/bup/t/tclient.py index f529c22..2ea4dca 100644 --- a/lib/bup/t/tclient.py +++ b/lib/bup/t/tclient.py @@ -17,7 +17,7 @@ def randbytes(sz): top_dir = os.path.realpath('../../..') -bup_exe = top_dir + '/bup' +bup_exe = top_dir + '/cmd/bup' s1 = randbytes(10000) s2 = randbytes(10000) diff --git a/lib/bup/t/tgit.py b/lib/bup/t/tgit.py index bcc58bd..2cf8230 100644 --- a/lib/bup/t/tgit.py +++ b/lib/bup/t/tgit.py @@ -12,7 +12,7 @@ from buptest import no_lingering_errors, test_tempdir top_dir = os.path.realpath('../../..') -bup_exe = top_dir + '/bup' +bup_exe = top_dir + '/cmd/bup' def exc(*cmd): -- 2.39.2