Ticket #7909: sage-spkg

File sage-spkg, 12.1 KB (added by drkirkby, 11 years ago)

A complete copy of the revised sage-env

Line 
1#!/usr/bin/env bash
2
3# William Stein, 2005-12-20 -- removed "m" option from tar,
4# which was seriously confusing the build process on some
5# (too fast?) machines, especially for mpfr.
6
7#######################################################
8#  Install a Sage package.  This script is
9#  typically invoked by giving the command
10#      sage -i <package name>
11#
12#  A package may assume that the following environment
13#  variables are defined:
14#
15#      SAGE_ROOT   -- root directory of sage install
16#      SAGE_LOCAL  -- $SAGE_ROOT/local
17#      SAGE_DATA   -- $SAGE_ROOT/data
18#      LIBRARY_PATH, PYTHONPATH, LD_LIBRARY_PATH, DYLD_LIBRARY_PATH
19#      CC, CXX, CFLAGS, CXXFLAGS, LDFLAGS, MAKE
20#
21#  Your package script should try to build using the giving CC, CXX,
22#  CFLAGS, MAKE, etc, via a file spkg-install in your script.
23#
24#  This script does the following:
25#
26#      1. Set environment variables (by calling sage-env)
27#      2. Decompress package into a build directory
28#      3. Run the script in the package called spkg-install
29#      4. Return error 1 if anything goes wrong.
30#
31#######################################################
32
33mymkdir()
34{
35    if [ ! -d $1 ]; then
36            mkdir $1
37    fi
38}
39
40no_version()
41{
42    if [ -z "`echo "$1" | grep -o -`" ]; then
43        echo "no version"
44    fi
45}
46# The following sets environment variables for building
47# packages.  (Using dot suggested by W. Cheung.)
48
49. "$SAGE_ROOT/local/bin/sage-env"
50
51cd "$SAGE_PACKAGES"
52
53if [ $# -eq 0 ]; then
54    echo "Currently installed packages:"
55    /bin/ls -1 "$SAGE_PACKAGES/installed/"
56    exit 0
57fi
58
59
60cd "$SAGE_PACKAGES"
61mymkdir "$BUILD"
62mymkdir installed
63cd "$BUILD"
64mymkdir old
65
66# the following two options are mutually exclusive -- i.e., you
67# can give only one.
68
69INFO=0
70if [ $1 = '-info' ]; then
71    INFO=1
72    shift
73fi
74
75FORCE=0
76if [ $1 = '-f' ]; then
77    FORCE=1
78    shift
79fi
80export FORCE
81
82DELETE_TMP=1
83if [ $1 = '-s' -o $1 = '-m' ]; then
84    DELETE_TMP=0
85    shift
86fi
87
88INSTALLED="$SAGE_PACKAGES/installed/"
89PKG_NAME=`echo "$1" | sed -e "s/\.spkg$//"`
90PKG_NAME=`basename "$PKG_NAME"`
91PKG_SRC="$1"
92PKG_BASE=`echo "$PKG_NAME" | sed -e "s/-.*//"`
93
94# check if noclobber is set and warn about it
95if [ $PKG_SRC == "noclobber" ]; then
96    echo "***********************************************************"
97    echo "* WARNING WARNING WARNING WARNING WARNING WARNING WARNING *"
98    echo "*                                                         *"
99    echo "* noclobber is set in .bashrc and/or .bash_profile - you  *"
100    echo "* should consider disabling it. The Sage install should   *"
101    echo "* continue to work, so don't worry about it too much.     *"
102    echo "*                                                         *"
103    echo "* WARNING WARNING WARNING WARNING WARNING WARNING WARNING *"
104    echo "***********************************************************"
105    exit 0
106fi
107
108if [ ! -f "$PKG_SRC" ]; then
109    if [ -f "$SAGE_PACKAGES/standard/$PKG_NAME.spkg" ]; then
110        PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NAME.spkg"
111    else
112        if [ -f "$SAGE_PACKAGES/optional/$PKG_NAME.spkg" ]; then
113            PKG_SRC="$SAGE_PACKAGES/optional/$PKG_NAME.spkg"
114        else
115            CUR=`pwd`
116            cd "$SAGE_PACKAGES"
117            PKG_NEWEST_VER=`./standard/newest_version "$PKG_NAME" 2> /dev/null`
118            if [ -n "$PKG_NEWEST_VER" ]; then
119                PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NEWEST_VER.spkg"
120            else
121                PKG_NEWEST_VER=`./standard/newest_version -base "$PKG_NAME" 2> /dev/null`
122                if [ -n "$PKG_NEWEST_VER" ]; then
123                    PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NEWEST_VER.spkg"
124                fi
125            fi
126        fi
127    fi
128fi
129
130if [ $INFO -ne 0 ]; then
131    if [ ! -f "$PKG_SRC" ]; then
132        echo "Package $PKG_NAME not found"
133    fi
134    bunzip2 -c "$PKG_SRC" 2>/dev/null | tar Ofx - $PKG_NAME/SAGE.txt 2>/dev/null
135    if [ $? -ne 0 ]; then
136        tar Ofx  "$PKG_SRC" "$PKG_NAME/SAGE.txt" 2>/dev/null
137    fi
138    echo ""
139    if [ $? -ne 0 ]; then
140        echo "No file SAGE.txt in $PKG_NAME"
141        exit 1
142    fi
143    exit 0
144fi
145
146echo "$PKG_NAME"
147
148echo "Machine:"
149uname -a
150
151if [ -f "$INSTALLED/$PKG_NAME" -a $FORCE -eq 0 ]; then
152    echo "sage: $1 is already installed"
153    touch "$INSTALLED/$PKG_NAME"
154    exit 0
155fi
156
157cd "$SAGE_PACKAGES/build"
158
159if [ $DELETE_TMP -eq 1 ]; then
160    echo "Deleting directories from past builds of previous/current versions of $PKG_NAME"
161    # Make triply sure that we are in the build directory before doing
162    # a scary "rm -rf".
163    cd "$SAGE_PACKAGES/build"
164    if [ $? -ne 0 ]; then
165        echo "Unable to find build directory."
166    else
167        rm -rf "$PKG_BASE-"*
168    fi
169else
170    echo "Moving directories from past builds of previous/current versions of $PKG_NAME to build/old"
171    mv -f "$PKG_BASE-"* old/  2>/dev/null
172fi
173
174if [ ! -f "$PKG_SRC" ]; then
175
176    echo "$0: file $PKG_NAME does not exist" 
177    echo "Attempting to download it."
178    CUR=`pwd`
179    cd "$SAGE_PACKAGES"
180    if [ ! -d optional ]; then
181        mkdir optional
182    fi
183    cd optional
184   
185    if [ -n "`no_version "$PKG_NAME"`" ]; then
186        echo "Searching for latest version of $PKG_NAME"
187        PKG_NAME=`sage-latest-online-package "$PKG_NAME"`
188        if [ $? -eq 0 ]; then
189            echo "Found package $PKG_NAME"
190        else
191            echo "$PKG_NAME"
192            exit 1
193        fi
194    fi
195
196    # sage-download_package "$PKG_NAME"
197    # See trac ticket #7544. One should be able to install a package using
198    # sage -i <package-name> where <package-name> can be any of the
199    # following values:
200    #
201    # 1. <package>-x.y.z, i.e. the name of the package plus the package's
202    #    version numbers.
203    # 2. the full name <package>-x.y.z.spkg, i.e. the name of the package in
204    #    addition to the version numbers and the ".spkg" extension.
205    # 3. <URL>/<package>-x.y.z.spkg, i.e. the full URL where the package is
206    #    hosted. This can be a URL on the Sage website or somewhere else.
207    # 4. /path/to/<package>-x.y.z.spkg, i.e. the package is found somewhere
208    #    in your file system and you're giving an absolute or relative path
209    #    to the package.
210    sage-download_package "$1"
211    if [ ! -f "$PKG_NAME.spkg" ]; then
212            echo "sage: Failed to download package $PKG_NAME from $SAGE_SERVER"
213            exit 1
214    fi
215    PKG_SRC="`pwd`/$PKG_NAME.spkg"
216    cd "$CUR"
217fi
218
219# * The -i option below to ignore checksum errors, since
220#   I've had problems with this on Solaris.
221# * The m option avoids clock skew problems.   
222
223echo "Extracting package $PKG_SRC ..."
224ls -l "$PKG_SRC"
225
226if [ -d "$PKG_NAME" ]; then
227    echo "Removing previous version."
228    rm -rf "$PKG_NAME"
229fi
230
231bunzip2 -c "$PKG_SRC" 2>/dev/null | tar fxv -  2>/dev/null
232if [ ! -d "$PKG_NAME" ]; then
233    tar fxv "$PKG_SRC"
234fi
235echo "Finished extraction"
236
237if [ ! -d "$PKG_NAME" ]; then
238    echo "sage: After decompressing the directory $PKG_NAME does not exist"
239    echo "This means that the corresponding .spkg needs to be downloaded"
240    echo "again."
241    if [ -n "`no_version "$PKG_NAME"`" ]; then
242        echo "Searching for latest version of $PKG_NAME"
243        PKG_NAME=`sage-latest-online-package "$PKG_NAME"`
244        if [ $? -eq 0 ]; then
245            echo "Found package $PKG_NAME"
246        else
247            echo "$PKG_NAME"
248            exit 1
249        fi
250    fi
251    sage-download_package "$PKG_NAME"
252    echo `pwd`
253    bunzip2 -c "$PKG_NAME.spkg"  | tar fxv -
254    if [ ! -d "$PKG_NAME.spkg" ]; then
255        tar fxv "$PKG_NAME.spkg"
256    fi
257    if [ ! -d "$PKG_NAME" ]; then
258        echo "Second download resulted in a corrupted package."
259        exit 1
260    fi
261fi
262
263cd "$PKG_NAME"
264if [ ! -f spkg-install ]; then
265    echo "#!/usr/bin/env bash" > spkg-install
266    echo "" >> spkg-install
267    if [ -f setup.py ]; then
268        echo "python setup.py install" >> spkg-install
269    else
270        if [ -f configure ]; then
271            echo "./configure --prefix=\$SAGE_ROOT/local/" >> spkg-install
272            echo "make" >> spkg-install
273            echo "make install" >> spkg-install
274        else
275            echo "There is no spkg-install script, no setup.py, and no configure script,"
276            echo "so I do not know how to install $PKG_SRC."
277            exit 1
278        fi
279    fi
280fi
281
282chmod +x spkg-install
283
284# this is just wrong... (so don't do it)
285#echo "TOUCHING"
286#touch * */* */*/* */*/*/* 1>/dev/null 2>/dev/null
287
288echo "****************************************************"
289echo "Host system"
290echo "uname -a:"
291uname -a
292if [ $? -ne 0 ]; then
293    echo "Unable to determine host system information."
294fi
295echo "****************************************************"
296
297echo "****************************************************"
298echo "CC Version"
299echo "$CC -v"
300$CC -v
301if [ $? -ne 0 ]; then
302    echo "Unable to determine C compiler version."
303fi
304echo "****************************************************"
305
306BASEDIR=`pwd`
307if [ -n "$DEBIAN_RELEASE" ]; then
308    SAGE_CHECK=''
309    if [ -e ./spkg-debian ]; then
310            time ./spkg-debian
311    else
312            time sage-build-debian $BASEDIR
313    fi
314else
315    time ./spkg-install
316fi
317
318if [ $? -eq 0 ]; then
319    cd $INSTALLED
320    # TURNED OFF: Remove all old packages with the same name up to the first "-":
321    # rm -f $PKG_BASE-*
322
323    # Mark that the new package has been installed.
324    # This file will eventually be a certificate like in OS X.
325    echo "PACKAGE NAME: $PKG_NAME" > "$PKG_NAME"
326    echo "INSTALL DATE: `date`" >> "$PKG_NAME"
327    echo "UNAME: `uname -a`" >> "$PKG_NAME"
328    echo "Sage VERSION: `grep Sage $SAGE_LOCAL/bin/sage-banner`" >> "$PKG_NAME"
329    echo "Successfully installed $PKG_NAME"
330
331    cd $BASEDIR
332    if [ "$SAGE_CHECK" != "" -a -f spkg-check ]; then
333        echo "Running the test suite."
334        chmod +x spkg-check
335        ./spkg-check
336        if [ $? -ne 0 ]; then
337            echo "*************************************"
338            echo "Error testing package ** $PKG_NAME **"
339            echo "*************************************"
340            rm -f $SAGE_ROOT/spkg/installed/$PKG_NAME
341            echo "sage: An error occurred while testing $PKG_NAME"
342            echo "Please email sage-devel http://groups.google.com/group/sage-devel"
343            echo "explaining the problem and send the relevant part of"
344            echo "of $SAGE_ROOT/install.log.  Describe your computer, operating system, etc."
345            echo "If you want to try to fix the problem yourself, *don't* just cd to"
346            echo "`pwd` and type 'make check' or whatever is appropriate."
347            echo "Instead, the following commands setup all environment variables"
348            echo "correctly and load a subshell for you to debug the error:"
349            echo "(cd '`pwd`' && '$SAGE_ROOT/sage' -sh)"
350            echo "When you are done debugging, you can type \"exit\" to leave the"
351            echo "subshell."
352            exit 1
353        else
354            echo "TEST SUITE: passed" >> "$PKG_NAME"
355        fi
356    fi
357
358
359    # Delete the temporary build directory if required.
360    if [ $DELETE_TMP -eq 1 ]; then
361        echo "Now cleaning up tmp files."
362        if [ -d "$SAGE_PACKAGES/build/$PKG_NAME" ]; then
363           # the if is there only to avoid the possibility of a weird bug.
364            rm -rf "$SAGE_PACKAGES/build/$PKG_NAME"
365        fi
366    else
367        echo "You can safely delete the temporary build directory"
368        echo "$SAGE_PACKAGES/build/$PKG_NAME"
369    fi
370
371else
372    echo "sage: An error occurred while installing $PKG_NAME"
373    echo "Please email sage-devel http://groups.google.com/group/sage-devel"
374    echo "explaining the problem and send the relevant part of"
375    echo "of $SAGE_ROOT/install.log.  Describe your computer, operating system, etc."
376    echo "If you want to try to fix the problem yourself, *don't* just cd to"
377    echo "`pwd` and type 'make check' or whatever is appropriate."
378    echo "Instead, the following commands setup all environment variables"
379    echo "correctly and load a subshell for you to debug the error:"
380    echo "(cd '`pwd`' && '$SAGE_ROOT/sage' -sh)"
381    echo "When you are done debugging, you can type \"exit\" to leave the"
382    echo "subshell."
383    exit 1
384fi
385
386
387echo "Making Sage/Python scripts relocatable..."
388
389cd "$SAGE_LOCAL"/bin
390./sage-make_relative
391
392echo "Finished installing $PKG_NAME.spkg" 
393
394# It's OK if the above fails -- in fact it will until Python
395# itself gets installed. That's fine.
396exit 0