source: TI04-geosplat/trunk/setup.py @ 798

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/setup.py@798
Revision 798, 13.8 KB checked in by astephen, 14 years ago (diff)

Latest working version with install method.
Can accept more than one file but doesn't combine variables yet.

Line 
1#!/usr/bin/env python
2
3"""
4setup.py
5========
6
7Set up script for the GeoSPlAT package.
8
9The GeoSPlAT is a server package (gss) that is exposed
10as a Web Service using python's ZSI SOAP Library.
11
12There is also a client package to primarily support the CGI client
13package (gsc).
14
15This installation includes both server and client and assumes you want
16to install both on your local machine. If you only wish to install either
17server or client use one of the following options:
18
19--gsc-only # only install the client package
20--gss-only # only install the server package
21
22
23Usage:
24======
25
26    [<your_python>/bin/python] setup.py [options] 
27
28[options] can include the above as well as the usual
29
30build | install
31
32Note that using "--overwrite" will automatically overwrite
33existing directories regardless of whether
34
35"""
36
37from distutils.core import setup
38import sys, os, shutil, time, re
39from ConfigParser import ConfigParser
40version="0.2.1"
41overwrite="OFF"
42
43
44def createSourceBundle(packages=["gss", "gsc"]):
45    """
46    Creates the packages required and includes the directories needed.
47    """
48    basePackage=packages[0][:2]
49    packMap={"s":"Server", "c":"Client"}
50       
51    for package in packages: 
52        if os.path.isfile("MANIFEST"): os.remove("MANIFEST")
53        pypackage="py"+package
54        packageName="Python GeoSPlAT %s" % packMap[pypackage[-1]]
55        print "\nBundling up source of python package: %s" % packageName
56   
57        setup(name=pypackage,
58            version=version,
59            description=packageName,
60            author="Ag Stephens",           
61            author_email="a.stephens@rl.ac.uk",
62            url="http://ndg.nerc.ac.uk/GeoSPlAT",
63            license="""Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
64This software may be distributed under the terms of the
65Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QP""",
66            packages=[pypackage])   
67           
68        print "Bundling up the source of the non-python stuff and python scripts for: %s" % packageName
69        tarfile="%s-%s.tar" % (package, version)
70        gzipfile=tarfile+".gz"
71        os.system("tar -cvf %s %s" % (tarfile, package))
72        os.system("gzip %s" % tarfile)
73       
74        if not os.path.isfile(gzipfile):
75            raise "Did not successfully package up non-python source for '%s'." % package
76           
77        print "Moving gzip file into distribution..."
78        os.rename(gzipfile, os.path.join("dist", gzipfile))
79
80        if package=="gsc":
81            print "Copying cgi directory..."
82            if os.path.isdir("dist/cgi"):
83                if os.path.isfile("dist/cgi/splatui"):
84                    os.remove("dist/cgi/splatui")
85                os.rmdir("dist/cgi")
86            shutil.copytree("cgi", "dist/cgi")
87           
88            print "Copying web_extras directory..."
89            if os.path.isdir("dist/web_extras"):
90                os.system("rm -fR dist/web_extras")
91            shutil.copytree("web_extras", "dist/web_extras")       
92           
93    print "Copying README.txt and install.conf files..."
94    shutil.copy("README.txt", "dist")
95    shutil.copy("install.sh", "dist")   
96    shutil.copy("install.conf", "dist") 
97    shutil.copy("install.conf", "dist/install.conf.orig")           
98       
99    print "Tarring up everything in dist directory..."
100    tarAllFile="%s-%s-all.tar" % (basePackage, version)
101    os.system("cd dist ; tar -cvf %s *" % tarAllFile)
102       
103    if not os.path.isfile(os.path.join("dist", tarAllFile)):
104        raise "Did not successfully package up tar-all file '%s'." % tarAllFile
105       
106
107
108def setupPackages(packages=["gss", "gsc"]):
109    """
110    Controls the entire setup and creates directories and
111    permissions where necessary to get the ancillary stuff working.
112    """   
113    testImportExternalPackages()
114   
115    packMap={"s":"Server", "c":"Client"}
116       
117    for package in packages: 
118        pypackage="py"+package
119        packageName="Python GeoSPlAT %s" % packMap[package[-1]]
120           
121        print "Installing the non-python stuff and python scripts for: %s" % packageName
122        unpackageDir="%s-%s" % (package, version)
123        tarfile="%s.tar" % unpackageDir
124        gzipfile=tarfile+".gz"
125        os.system("cd .. ; gunzip %s" % tarfile)       
126        os.system("cd .. ; tar -xvf %s" % tarfile)
127
128        packageDir=os.path.join("..", package)
129        if not os.path.isdir(packageDir):
130            raise "Did not successfully unpack the source files for %s." % package
131           
132        print "Reading configuration file to get %s local details." % package
133        config=ConfigParser()
134        config.read("../install.conf")
135 
136        print "Set the configuration variables that are used to generate others..."
137        for item in ["BASEDIR", "STATIC_WEBSERVER_LOCAL_PATH", "STATIC_WEBSERVER_URL_PATH",
138                     "WEB_EXTRAS_URL_PATH"]:
139            if item.lower() in config.options(package):
140                exec("%s=%s" % (item.upper(), config.get(package, item.lower())))
141                print "SET:", item
142 
143        print "\n\n"
144        configVarDict={}
145        for configVar in config.options(package):
146                value=config.get(package, configVar)
147                print "Setting: %s=%s" % (configVar.upper(), value)
148                exec("%s=%s" % (configVar.upper(), value))
149                configVarDict[configVar.upper()]=value
150
151        print "Now we have parsed the config file we can start copying and making the source directories."
152        print "Installing base package '%s' in: %s" % (package, BASEDIR)
153
154        if overwrite!="ON":
155            destroy=raw_input("Type 'yes' to remove existing directory before re-installing: '%s' :" % packageDir)
156            if destroy!="yes":
157                print "Exiting setup without completing."
158                sys.exit()
159           
160        if not os.path.isdir(BASEDIR):
161            try:
162                os.makedirs(BASEDIR)
163            except:
164                pass
165       
166        for item in os.listdir(packageDir):
167            sourceCopy=os.path.join(packageDir, item)
168            target=os.path.join(BASEDIR, item)
169            if os.path.exists(target): 
170                os.system("rm -fr %s" % target)
171           
172            if os.path.isfile(sourceCopy):
173                shutil.copy(sourceCopy, target)
174            elif os.path.isdir(sourceCopy):
175                shutil.copytree(sourceCopy, target)
176       
177        print "Installed non-python package and scripts under:", BASEDIR
178
179        if "OUTPUT_DIR" in configVarDict.keys():
180            if overwrite!="ON":
181                destroy=raw_input("Type 'yes' to remove existing directory before re-installing: '%s' :" % OUTPUT_DIR)
182                if destroy!="yes":
183                    print "Exiting setup without completing."
184                    sys.exit()   
185            try:
186                os.system("rm -fR %s" % OUTPUT_DIR)
187                os.makedirs(OUTPUT_DIR, OUTPUT_DIR_PERMISSION)
188                #os.chmod(OUTPUT_DIR, OUTPUT_DIR_PERMISSION)
189                os.system("chown -Rf %s.%s %s" % (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP, OUTPUT_DIR))
190            except:
191                raise "Cannot create directory: %s" % OUTPUT_DIR
192
193        for dirToMake in ["OUTPUT_DIR_LOCAL_PATH"]:
194         
195          if dirToMake in configVarDict.keys():
196            dirToMake=eval(configVarDict[dirToMake])
197            if overwrite!="ON":
198                destroy=raw_input("Type 'yes' to remove existing directory before re-installing: '%s' :" % dirToMake)
199                if destroy!="yes":
200                    print "Exiting setup without completing."
201                    sys.exit()   
202            try:
203                os.system("rm -fR %s" % dirToMake)
204                os.makedirs(dirToMake, 0755)
205                #os.chmod(dirToMake, 0755)
206                #os.system("chown -Rf %s.%s %s" % (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP, OUTPUT_DIR))
207            except:
208                raise "Cannot create directory: %s" % dirToMake
209               
210        if "CGI_SCRIPT_LOCAL_PATH" in configVarDict.keys():
211            if overwrite!="ON":
212                destroy=raw_input("Type 'yes' to remove existing script before re-installing: '%s' :" % CGI_SCRIPT_URL_PATH)
213                if destroy!="yes":
214                    print "Exiting setup without completing."
215                    sys.exit()   
216            try:
217                shutil.copy("../cgi/splatui", CGI_SCRIPT_LOCAL_PATH)
218                os.chmod(CGI_SCRIPT_LOCAL_PATH, 0755)
219                thisPython=sys.modules["os"].__file__.split("lib")[0]+"bin/python"
220                print thisPython
221                os.system("perl -p -i -w -e 's:<YOUR_PYTHON_HERE>:%s:g;' %s*" % (thisPython, CGI_SCRIPT_LOCAL_PATH))
222                #os.system("perl -p -i -w -e 's!BASEDIR=\".*?\"!BASEDIR=\"%s\"!g;' %s*" % ("/home/as56/abstractWebService/test_installs/20060328/pygsc-0.1/pygsc", CGI_SCRIPT_LOCAL_PATH))
223                print "\nCopied CGI script to: %s" % CGI_SCRIPT_LOCAL_PATH
224            except:
225                raise "Cannot move CGI script to: %s" % CGI_SCRIPT_LOCAL_PATH
226
227        if "WEB_EXTRAS_LOCAL_PATH" in configVarDict.keys():
228            if overwrite!="ON":
229                destroy=raw_input("Type 'yes' to remove existing directory before re-installing: '%s' :" % WEB_EXTRAS_LOCAL_PATH)
230                if destroy!="yes":
231                    print "Exiting setup without completing."
232                    sys.exit()   
233            try:
234                os.system("rm -fR %s" % WEB_EXTRAS_LOCAL_PATH)
235                os.makedirs(WEB_EXTRAS_LOCAL_PATH, 0755)
236                #os.chmod(OUTPUT_DIR, OUTPUT_DIR_PERMISSION)
237                #os.system("chown -Rf %s.%s %s" % (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP, OUTPUT_DIR))
238            except:
239                raise "Cannot create directory: %s" % WEB_EXTRAS_LOCAL_PATH                     
240
241            webExtrasDir="../web_extras"
242            for item in os.listdir(webExtrasDir):
243                sourceCopy=os.path.join(webExtrasDir, item)
244                target=os.path.join(WEB_EXTRAS_LOCAL_PATH, item)
245                if os.path.exists(target): 
246                    os.system("rm -fR %s" % target)
247           
248                if os.path.isfile(sourceCopy):
249                    shutil.copy(sourceCopy, target)
250                elif os.path.isdir(sourceCopy):
251                    shutil.copytree(sourceCopy, target)
252       
253            print "Installed web extras components under:", WEB_EXTRAS_LOCAL_PATH
254
255        if "REQUEST_XML_DIR_LOCAL_PATH" in configVarDict.keys():
256            if overwrite!="ON":
257                destroy=raw_input("Type 'yes' to remove existing directory before re-installing: '%s' :" % REQUEST_XML_DIR_LOCAL_PATH)
258                if destroy!="yes":
259                    print "Exiting setup without completing."
260                    sys.exit()   
261            try:
262                os.system("rm -fR %s" % REQUEST_XML_DIR_LOCAL_PATH)
263                os.makedirs(REQUEST_XML_DIR_LOCAL_PATH)
264                print "Made Request XML directory: %s" % REQUEST_XML_DIR_LOCAL_PATH
265                os.chmod(REQUEST_XML_DIR_LOCAL_PATH, 0777)
266                #os.system("chown -Rf %s.%s %s" % (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP, OUTPUT_DIR))
267            except:
268                raise "Cannot create directory: %s" % REQUEST_XML_DIR_LOCAL_PATH
269       
270        configMap={"s":os.path.join("..", "%s-%s" % (pypackage, version), pypackage, "serverConfig.py"), 
271                   "c":os.path.join("..", "%s-%s" % (pypackage, version), pypackage, "clientConfig.py")}
272        print "Renaming configuration variables in config file:"
273       
274        configFileName=configMap[package[-1]]
275        configFile=open(configFileName)
276        configLines=configFile.readlines()
277        configFile.close()
278       
279        shutil.copy(configFileName, configFileName+".bak")
280       
281        newLines=[]
282        for line in configLines:
283            matched=None
284            for confVar, value in configVarDict.items():
285               
286                if re.match(r"\s*%s\s*=" % confVar, line):
287                    newLine="%s=%s\n" % (confVar, value)
288                    print "CHANGING:   %sTO:         %s" % (line, newLine)
289                    newLines.append(newLine)
290                    matched=1
291                    break
292                   
293            if matched==None:
294                newLines.append(line)     
295
296        print len(configLines), len(newLines)
297        configFile=open(configFileName, "w")
298        configFile.write("".join(newLines))
299        configFile.close()
300        print "Re-written config file: %s\n" % (configFileName)
301
302        if package=="gss":
303            print "Updating VCS templates in your '~/PCMDI_GRAPHICS/initial.attributes' file is needed."
304            print "Note that a backup version will be written.\n"
305            os.system("cd %s/scripts ; addGeosplatTemplates.py ~/PCMDI_GRAPHICS/initial.attributes" % BASEDIR)
306            print "\nDone.\n"       
307
308        print "Installing python package: %s" % packageName
309
310        os.chdir(os.path.join("..", "%s-%s" % (pypackage, version)))
311        setup(name=pypackage,
312            version=version,
313            description=packageName,
314            author="Ag Stephens",           
315            author_email="a.stephens@rl.ac.uk",
316            url="http://ndg.nerc.ac.uk/DataExtractor",
317            license="""Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
318This software may be distributed under the terms of the
319Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QP""",
320            packages=[pypackage])       
321
322    print "\nIt looks like setup has completed successfully!\n"
323    print """You can start the server Web Service by typing:
324    python %s/bin/GSWSInterface.py
325
326You should then be able to view the client package via a browser at:
327    %s
328   
329Have fun!....and of course send any bugs to <a.stephens@rl.ac.uk>
330
331""" % (BASEDIR, CGI_SCRIPT_URL_PATH)
332       
333   
334def testImportExternalPackages():
335    """
336    Tests that external packages can be imported.
337    """
338    print "Testing import of required cdms (CDAT) package...",
339    try:
340        import cdms         
341        print "OK!"
342    except:
343        print """\n\nCannot import cdms package. Please try the one or more of the following to fix this:
344
3451. Set your PYTHONPATH environment variable to pick up the correct directory containing the cdms package.
3462. Retry using the correct python installation such as "<my_python>/bin/python setup.py"
3473. Install CDAT (cdms)."""
348        sys.exit()
349
350    print "Testing import required ZSI package...",
351    try:
352        import ZSI
353        print "OK!"
354    except:
355        print """\n\nCannot import ZSI package. Please try one or more of the following to fix this:
356
3571. Set your PYTHONPATH environment variable to pick up the correct directory containing the ZSI package.
3582. Install ZSI."""
359        sys.exit()
360 
361
362if __name__=="__main__":
363
364    packages=["gss", "gsc"]       
365
366    if "--overwrite" in sys.argv[1:]:
367        print "Setting overwrite to ON"
368        overwrite="ON"
369        sys.argv.remove("--overwrite")
370       
371    if "--gss-only" in sys.argv[1:]:
372        sys.argv.remove("--gss-only")
373        packages=["gss"]
374    elif "--gsc-only" in sys.argv[1:]:
375        sys.argv.remove("--gsc-only")
376        packages=["gsc"]       
377
378    if "sdist" in sys.argv[1:]:
379        createSourceBundle(packages)
380    else:
381        if "install" not in sys.argv[1:]:
382            print "\nWARNING: Building without installing may mean your version won't pick up the location of the python packages."
383            time.sleep(0.3)   
384        setupPackages(packages)
385       
Note: See TracBrowser for help on using the repository browser.