#!/usr/bin/env python # # Copyright (C) 2001-2003 Leonardo J. Milano (lmilano@udel.edu) # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # You can also get a copy of the license at: # http://www.gnu.org/copyleft/gpl.html ######################################################################## # # The idea of the installer is to be as simple and safe as possible. # Even at the price of efficiency. It runs in a snap anyway ;-) # ######################################################################## #### imports import getopt import os import pickle import shutil import sys import string import glob #### Environment variables try: HOME = os.path.abspath(os.environ['HOME'])+'/' except: print 'Enviroment variable HOME is not set !, using current directory' HOME = os.path.abspath(os.curdir)+'/' #### Constants def get_version(): str = os.popen('./latex2slides --version').readline() return string.split(str)[1] __version__ = get_version() USER_DIR = HOME+'.latex2slides/' # Please edit the following 3 paths for a Custom install BIN_DIR = '/usr/local/bin/' INSTALL_DIR = '/usr/local/share/latex2slides/latex2slides-'+__version__+'/' MAN_DIR = '/usr/local/man/man1/' #### Classes and Functions class Parameters: 'parameters for the program' def __init__(self): self.version = __version__ self.bin_dir = BIN_DIR self.install_dir = INSTALL_DIR self.man_dir = MAN_DIR self.install_type = '(you may need to become root)' self.install_info = '.install_info' self.force = 'no' self.source_dirs = [] self.target_dirs = [] self.files = [] self.prefix = '' def set_params_for_user(self): self.bin_dir = HOME+'bin/' self.install_dir = USER_DIR+'latex2slides-'+__version__+'/' self.man_dir = self.install_dir+'doc/' #better than nothing:-) self.install_type = 'locally (in your home directory)' def save(self): 'write install info to file' f=open(self.install_info, 'w') pickle.dump(self,f) f.close() def load(self): 'read install info from file' try: f=open(self.install_info, 'r') q=pickle.load(f) f.close() except: return self else: return q def make_dir(dir_name): if not os.access(dir_name, os.F_OK): try: os.makedirs(os.path.abspath(dir_name)) # (os.path.abspath is required if dir_name finishes in a slash ) except: abort("Couldn't create "+dir_name) return def remove_dir(dir): try: os.rmdir(dir) except: pass print "Warning: couldn't remove directory "+dir else: print "Removed directory "+dir def dirpath(dir_name): "check that paths are absolute and end in a '/' " dir_name = os.path.abspath(dir_name)+'/' return dir_name def append_to_list(filename, source_dir, target_dir): " 'filename' may contain wildcards, such as '*.gif' " dirpath(source_dir) dirpath(target_dir) for path_plus_file in glob.glob(source_dir+filename): file = os.path.basename(path_plus_file) p.files.append(file) p.source_dirs.append(source_dir) p.target_dirs.append(target_dir) def copy(file, source_dir, target_dir): try: shutil.copy(source_dir+file,target_dir) except: abort("Couldn't copy "+source_dir+file+" to "+target_dir) else: print target_dir+file def install(): # check if already installed if os.access(p.install_info, os.F_OK): if p.force == "yes": print "I will try to force installation" else: abort("This version of latex2slides seems to be installed. \n" +\ "To re-install it, please run ./uninstall first " +\ "(you may need to do it as root).") print "Installing "+p.install_type+" ..." # make target dirs if necessary print "Making target directories ..." make_dir(p.bin_dir) make_dir(p.man_dir) make_dir(p.install_dir+'doc/') make_dir(p.install_dir+'icons/') make_dir(p.install_dir+'samples/') # make the list of files to install append_to_list('latex2slides', './', p.bin_dir) append_to_list('latex2slides.1.gz','./man/', p.man_dir) append_to_list('*','./doc/',p.install_dir+'doc/') append_to_list('*','./icons/',p.install_dir+'icons/') append_to_list('*','./samples/',p.install_dir+'samples/') # copy the files print "Copying files ..." for i in range(len(p.files)): copy(p.files[i],p.source_dirs[i],p.target_dirs[i]) # that's it ! install_success() def install_success(): p.save() print "latex2slides version "+p.version+" succesfully installed" def uninstall(): 'The uninstaller tries to uninstall as much as possible, even if it fails' 'to complete the uninstallation' # initialize flag = 0 # to keep track of exceptions r=Parameters() p=r.load() if not os.access(p.install_info, os.F_OK): flag = 1 print "latex2slides doesn't appear to be installed. I will go on anyway." print "Uninstalling "+p.install_type+" ..." # remove the files for i in range(len(p.files)): try: os.remove(p.target_dirs[i]+p.files[i]) except: flag=1 print "Couldn't remove "+p.target_dirs[i]+p.files[i] else: print "Removed "+p.target_dirs[i]+p.files[i] # remove the directories remove_dir(p.bin_dir) remove_dir(p.man_dir) remove_dir(p.install_dir+'doc/') remove_dir(p.install_dir+'icons/') remove_dir(p.install_dir+'samples/') remove_dir(p.install_dir) remove_dir(os.path.abspath(p.install_dir+'/../')) # that's it ! if flag == 0: uninstall_success() else: print 'Failed to uninstall latex2slides' def uninstall_success(): os.remove(p.install_info) print "latex2slides version "+p.version+" succesfully uninstalled." def abort(message): print "ERROR ocurred, aborting installation ..." print message print "Type ./install -h for help" sys.exit() def query(): r=Parameters() p=r.load() if not os.access(p.install_info, os.F_OK): print "latex2slides doesn't appear to have been installed from here." else: for i in range(len(p.files)): print p.target_dirs[i]+p.files[i] def query_no_prefix(): r=Parameters() p=r.load() if not os.access(p.install_info, os.F_OK): print "latex2slides doesn't appear to have been installed from here." else: for i in range(len(p.files)): # subtract prefix if p.prefix != '': p.target_dirs[i]=string.replace(p.target_dirs[i],p.prefix,'',1) print p.target_dirs[i]+p.files[i] def usage(): print 'Usage: ./install [OPTION]' print 'latex2slides installation/uninstallation script' print '' print 'OPTION is one or many of the following:' print '' print ' -f, --force force installation if possible (use with care)' print ' -h, --help display this help and exit' print ' -l, --locally install locally (in your home directory)' print ' -p, --prefix set a temporary directory for a test install' print ' -q, --query display a list of installed files and exit' print ' --query-no-prefix same as query, but subtract prefix from names' print ' -u, --uninstall same as ./uninstall' print '' print 'Report bugs to ' def parse_command_line(): long_opts = ["force","help","locally","prefix=",\ "query","query-no-prefix","uninstall"] short_opts = "fhlp:qu" try: opts, args = getopt.getopt(sys.argv[1:],short_opts,long_opts) except getopt.error: usage() sys.exit(2) for o, a in opts: # options that sys.exit() if o in ("-h", "--help"): usage() sys.exit() if o in ("-q", "--query"): query() sys.exit() elif o == "--query-no-prefix": query_no_prefix() sys.exit() if o in ("-u", "--uninstall"): uninstall() sys.exit() # other options if o in ("-f", "--force"): p.force="yes" if o in ("-l", "--locally"): p.set_params_for_user() if o in ("-p", "--prefix"): p.bin_dir = a+p.bin_dir p.install_dir = a+p.install_dir p.man_dir = a+p.man_dir p.prefix = a # execution starts here if not os.access(USER_DIR, os.F_OK): os.mkdir(USER_DIR) # make sure USER_DIR exists p=Parameters() parse_command_line() install()