# HG changeset patch
# User Leif Leonhardy <not.really@online.de>
# Date 1288190015 -7200
# Node ID 3eafed436bc6b892a0f03b69455fb52e37c6cf1d
# Parent  b68fd5956e6d9e741cfa4d0a602e80e44e2ecabb
#10176 Improve generation of spkg-install (in spkg-dist)

diff -r b68fd5956e6d -r 3eafed436bc6 setup.py
--- a/setup.py	Fri Oct 08 00:02:44 2010 -0700
+++ b/setup.py	Wed Oct 27 16:33:35 2010 +0200
@@ -21,7 +21,7 @@
     
 
 code = setup(name = 'sagenb',
-      version     = '0.8.7',  # the spkg-dist script assumes single quotes here
+      version     = '0.8.7-patched',  # the spkg-dist script assumes single quotes here
       description = 'The Sage Notebook',
       license     = 'GNU Public License (GPL) v2+',
       author      = 'William Stein et al.',
diff -r b68fd5956e6d -r 3eafed436bc6 spkg-dist
--- a/spkg-dist	Fri Oct 08 00:02:44 2010 -0700
+++ b/spkg-dist	Wed Oct 27 16:33:35 2010 +0200
@@ -42,7 +42,6 @@
 
 spkg_install = os.path.abspath(os.path.join(os.path.curdir, 'spkg-install'))
 spkg_install_fd = open(spkg_install, 'w')
-spkg_install_fd.write("cd src\n")
 
 shutil.copy(os.path.join(os.path.pardir, os.path.pardir, 'SPKG.txt'),
             os.path.curdir)
@@ -75,20 +74,36 @@
 
 os.chdir(os.path.pardir)
 
-spkg_install_fd.write("""
+spkg_install_fd.write(r"""
+#!/usr/bin/env bash
+
+# spkg-install for SageNB, generated by SageNB's spkg-dist
+
+if [ -z "$SAGE_LOCAL" ]; then
+    echo "SAGE_LOCAL undefined - exiting..."
+    exit 1
+fi
+
+cd src
+
 cd sagenb
 python setup.py install
+if [ $? -ne 0 ]; then
+    echo "Error running setup.py install"
+    exit 1
+fi
 
 mkdir -p "$SAGE_ROOT/devel"
 
 echo "Copying SageNB package to '$SAGE_ROOT/devel/sagenb-main'."
 if [ -d "$SAGE_ROOT/devel/sagenb-main" ]; then
-  echo "Copying old SageNB package to '$SAGE_ROOT/devel/sagenb-main-old'."
+  echo "Moving old SageNB package to '$SAGE_ROOT/devel/sagenb-main-old'."
   rm -rf "$SAGE_ROOT/devel/sagenb-main-old"
-  cp -pr "$SAGE_ROOT/devel/sagenb-main" "$SAGE_ROOT/devel/sagenb-main-old"
+  mv "$SAGE_ROOT/devel/sagenb-main" "$SAGE_ROOT/devel/sagenb-main-old"
 fi
 
-rm -f "$SAGE_ROOT/devel/sagenb"
+# Rather than copying, we now use 'mv' above, so nothing to delete here:
+# rm -rf "$SAGE_ROOT/devel/sagenb" # Should point to sagenb-main if it exists
 
 cd ..
 cp -pr sagenb "$SAGE_ROOT/devel/sagenb-main"
@@ -99,10 +114,27 @@
 # We use relative paths for relocatability.
 cd "$SAGE_ROOT/devel/sagenb"
 python setup.py develop --egg-path ../../../../devel/sagenb
+if [ $? -ne 0 ]; then
+    echo "Error running setup.py develop"
+    exit 1
+fi
 
 cd "$SAGE_ROOT/local/lib/python/site-packages"
-sed 's/^.*sagenb.*$/..\/..\/..\/..\/devel\/sagenb/' easy-install.pth > easy-install.pth.new
-mv -f easy-install.pth.new easy-install.pth
+sed 's/^.*sagenb.*$/..\/..\/..\/..\/devel\/sagenb/' easy-install.pth > easy-install.pth.$$
+if [ $? -ne 0 ]; then
+    echo "Error patching easy-install.pth to have relative path to SageNB"
+    exit 1
+fi
+if true; then # DEBUG
+    echo "Old path: \"`grep '^.*sagenb' easy-install.pth`\""
+    echo "New path: \"`grep '^.*sagenb' easy-install.pth.$$`\""
+fi
+# The following fails only on wrong file permissions etc.:
+mv -f easy-install.pth.$$ easy-install.pth
+if [ $? -ne 0 ]; then
+    echo "Error overwriting original easy-install.pth"
+    exit 1
+fi
 """)
 spkg_install_fd.close()
 os.chmod(spkg_install, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
# HG changeset patch
# User Leif Leonhardy <not.really@online.de>
# Date 1288219007 -7200
# Node ID cfcdef9e7a8220f09f3c5268b7f1b27e60f88261
# Parent  3eafed436bc6b892a0f03b69455fb52e37c6cf1d
#10176 Version 0.8.7.p1 adds temporary work-around - not fully tracked down yet!

On *some* OpenSuSE (11.1 and 11.3) installations, the sagenb path doesn't
get added to $SAGE_ROOT/local/lib/python/site-packages/easy-install.pth,
so we add (rather than change) it (also a relative one) in that case.

Note that Sage 4.6.rc0 (with SageNB 0.8.7) apparently built ok and passed
all tests on another OpenSuSE 11.1 system (Skynet's "menas").

(This patch also fixes the previous one not putting #! on the first line.)

The previous patch makes the spkg-install created by spkg-dist more robust:
 * Adds "#!/usr/bin/env bash" which was missing.
 * Adds the usual sanity check (making sure SAGE_LOCAL is defined).
 * Adds a lot more error checking.
 * Uses the shell's PID in the temporary file name.
 * Prints some more messages, including the original (if present) and
   the new path of sagenb recorded in easy-install.pth.

Some clean-up:
 * Move a previous SageNB installation rather than copy and delete it.
 * Use a single, *raw* Python string for the shell script code.
 * Add a warning comment to the currently unused fetch_packages()
   function, which also writes commands to spkg-install. In case
   somebody uses this function again, the current single string
   written to spkg-install has to get split into parts to put these
   commands in the right place (neither the beginning nor the end
   of the script!).

diff -r 3eafed436bc6 -r cfcdef9e7a82 setup.py
--- a/setup.py	Wed Oct 27 16:33:35 2010 +0200
+++ b/setup.py	Thu Oct 28 00:36:47 2010 +0200
@@ -21,7 +21,7 @@
     
 
 code = setup(name = 'sagenb',
-      version     = '0.8.7-patched',  # the spkg-dist script assumes single quotes here
+      version     = '0.8.7.p1',  # the spkg-dist script assumes single quotes here
       description = 'The Sage Notebook',
       license     = 'GNU Public License (GPL) v2+',
       author      = 'William Stein et al.',
diff -r 3eafed436bc6 -r cfcdef9e7a82 spkg-dist
--- a/spkg-dist	Wed Oct 27 16:33:35 2010 +0200
+++ b/spkg-dist	Thu Oct 28 00:36:47 2010 +0200
@@ -50,6 +50,9 @@
 
 def fetch_packages():
     # This block is here in case we ever need it again.
+    # XXX Then also make sure the easy_install commands
+    # XXX get written to the correct part of spkg-install!
+    # XXX (We currently use a single string for the whole file.)
     print "Fetching the required packages"
     pkg_index = PackageIndex()
 
@@ -74,8 +77,8 @@
 
 os.chdir(os.path.pardir)
 
-spkg_install_fd.write(r"""
-#!/usr/bin/env bash
+spkg_install_fd.write(
+r"""#!/usr/bin/env bash
 
 # spkg-install for SageNB, generated by SageNB's spkg-dist
 
@@ -97,9 +100,9 @@
 
 echo "Copying SageNB package to '$SAGE_ROOT/devel/sagenb-main'."
 if [ -d "$SAGE_ROOT/devel/sagenb-main" ]; then
-  echo "Moving old SageNB package to '$SAGE_ROOT/devel/sagenb-main-old'."
-  rm -rf "$SAGE_ROOT/devel/sagenb-main-old"
-  mv "$SAGE_ROOT/devel/sagenb-main" "$SAGE_ROOT/devel/sagenb-main-old"
+    echo "Moving old SageNB package to '$SAGE_ROOT/devel/sagenb-main-old'."
+    rm -rf "$SAGE_ROOT/devel/sagenb-main-old"
+    mv "$SAGE_ROOT/devel/sagenb-main" "$SAGE_ROOT/devel/sagenb-main-old"
 fi
 
 # Rather than copying, we now use 'mv' above, so nothing to delete here:
@@ -120,14 +123,28 @@
 fi
 
 cd "$SAGE_ROOT/local/lib/python/site-packages"
-sed 's/^.*sagenb.*$/..\/..\/..\/..\/devel\/sagenb/' easy-install.pth > easy-install.pth.$$
-if [ $? -ne 0 ]; then
-    echo "Error patching easy-install.pth to have relative path to SageNB"
-    exit 1
+# Dave says Solaris' non-POSIX grep in the default path
+# doesn't understand "-q" (which *is* POSIX):
+if ! grep sagenb easy-install.pth >/dev/null; then
+    # Ugly work-around, we haven't found the real cause yet (see #10176):
+    echo 'No sagenb path found in easy-install.pth!'
+    echo "Adding relative sagenb path to easy-install.pth"
+    sed -e '$ i \../../../../devel/sagenb' easy-install.pth > easy-install.pth.$$
+    if [ $? -ne 0 ]; then
+        echo "Error adding relative sagenb path to easy-install.pth"
+        exit 1
+    fi
+else
+    echo "Making sagenb path in easy-install.pth relative"
+    sed 's/^.*sagenb.*$/..\/..\/..\/..\/devel\/sagenb/' easy-install.pth > easy-install.pth.$$
+    if [ $? -ne 0 ]; then
+        echo "Error patching easy-install.pth to have relative path to SageNB"
+        exit 1
+    fi
 fi
-if true; then # DEBUG
-    echo "Old path: \"`grep '^.*sagenb' easy-install.pth`\""
-    echo "New path: \"`grep '^.*sagenb' easy-install.pth.$$`\""
+if true; then # DEBUG (cf. #10176)
+    echo "Old path: \"`grep sagenb easy-install.pth`\""
+    echo "New path: \"`grep sagenb easy-install.pth.$$`\""
 fi
 # The following fails only on wrong file permissions etc.:
 mv -f easy-install.pth.$$ easy-install.pth
