1#!/usr/bin/env python
2import sys
3import errno
4import os
5import platform
6
7if sys.version_info < (2, 4):
8    oldpython=True
9else:
10    import subprocess
11    oldpython=False
12
13__author__ = "beerli"
14__date__ = "$Nov 26, 2010 3:10:04 PM$"
15
16def find_os():
17    return platform.system()
18
19def mkdir_p(path):
20    try:
21        os.makedirs(path)
22    except OSError,e:
23        if errno.errocode[e.errno] == errno.EEXIST:
24            pass
25        else: raise
26#    except OSError as exc: # Python >2.5
27#        if exc.errno == errno.EEXIST:
28#            pass
29#        else: raise
30
31
32def main():
33    myos = find_os()
34    homedir = os.path.expanduser('~')
35    mypath = os.path.dirname(os.path.realpath(__file__))
36    arg = ['./findshell']
37    if oldpython==True:
38        (child_stdin, child_stdout) = os.popen2(arg, 'r', 1024)
39        shell = child_stdout.read()
40    else:
41        myprocess = subprocess.Popen(arg,
42                  	  	             shell=True,
43                        	         stdout=subprocess.PIPE)
44        shelllist = myprocess.communicate()
45        shell = shelllist[0].rstrip()
46
47    if myos == 'Darwin':
48        (migshell,migkit) = os.path.split(mypath)
49        print migshell
50        print migkit
51        if migkit == "migrate_kit":
52            (ownpath,dummy) = os.path.split(migshell)
53            app = True
54        else:
55            ownpath = mypath
56            migshell = mypath+"/migrateshell.app"
57            app = False
58    else:
59        # linux
60        (migshell,migkit) = os.path.split(mypath)
61        migkit = "migrate_kit"
62        ownpath = mypath
63        app = False
64
65    print
66    print "Current directory:       "+ownpath
67    print "Migrateshell application:"+migshell
68    print "Home directory           "+homedir
69    print "To install MIGRATE for all users you need the administrator password for this machine."
70    print "Administrator install so that all users on the machen can use MIGRATE: /usr/local/migrate"
71    print "User install put the MIGRATE executable in the User's home directory: home_directory/opt/migrate"
72    answer =""
73    answer = raw_input("Are you an administrator? If YES press return,  otherwise press NO: ")
74    if answer == '':
75        if myos == 'Darwin':
76            if os.path.exists(migshell):
77                os.system("sudo rsync -a "+migshell+"/* /Applications/migrateshell.app")
78        else:
79            if os.path.exists(migshell):
80                linux=True
81        if not os.path.exists("/usr/local/migrate/bin"):
82            os.system("sudo mkdir -p /usr/local/migrate/bin")
83            os.system("sudo mkdir -p /usr/local/migrate/share")
84        if os.path.exists(mypath+"/migrate-n"):
85            os.system( "sudo rsync -a "+mypath+"/migrate-n /usr/local/migrate/bin/")
86        if os.path.exists(mypath+"/migrate-n-mpi"):
87            os.system("sudo rsync -a "+mypath+"/migrate-n-mpi /usr/local/migrate/bin/")
88        if os.path.exists(mypath+"/findcores"):
89            os.system("sudo rsync -a "+mypath+"/findcores /usr/local/migrate/bin/")
90            os.system("sudo chmod +x /usr/local/migrate/bin/findcores")
91        if os.path.exists(mypath+"/fastmigrate-n"):
92            os.system("sudo rsync -a "+mypath+"/fastmigrate-n /usr/local/migrate/bin/")
93            os.system("sudo chmod +x /usr/local/migrate/bin/fastmigrate-n")
94        found = False
95        for path in os.environ.get('PATH', '').split(':'):
96            if path == "/usr/local/migrate/bin":
97                found=True
98                break
99        if found == True:
100            print "No insertion of execution path needed, because it is already present"
101        else:
102            if "bash" in shell:
103                f = open(homedir+'/.bash_profile', 'r')
104		for line in f:
105		    if "###migrate" in line:
106			sys.exit()
107		f.close()
108                f = open(homedir+'/.bash_profile', 'a')
109                f.write("###migrate (popgen.sc.fsu.edu) start Path addition\n")
110                f.write("PATH=$PATH:/usr/local/migrate/bin\n")
111                f.write("export PATH\n")
112                f.write("###migrate (popgen.sc.fsu.edu) end Path addition\n\n")
113                f.close()
114            else:
115                print "Not implemented yet"
116    else:
117        #installation in user's home directory
118        #
119        answer = raw_input("Confirm that you want to install MIGRATE into your home directory [YES, NO]\n")
120        if answer.upper() != "YES":
121            print "Goodbye"
122            sys.exit()
123
124        if myos == 'Darwin':
125            if os.path.exists(migshell):
126                if not os.path.exists(homedir+"/Applications"):
127                    os.system("mkdir -p "+homedir+"/Applications")
128                os.system("rsync -a "+migshell+"/* "+homedir+"/Applications/migrateshell.app")
129        else:
130            if os.path.exists(migshell):
131                linux=True
132        if not os.path.exists(homedir+"/opt/migrate"):
133            os.system("mkdir -p "+homedir+"/opt/migrate/bin")
134            os.system("mkdir -p "+homedir+"/opt/migrate/share")
135        if os.path.exists(mypath+"/migrate-n"):
136            os.system( "rsync -a "+mypath+"/migrate-n "+homedir+"/opt/migrate/bin/")
137        if os.path.exists(mypath+"/migrate-n-mpi"):
138            os.system("rsync -a "+mypath+"/migrate-n-mpi "+homedir+"/opt/migrate/bin/")
139        if os.path.exists(mypath+"/findcores"):
140            os.system("sudo rsync -a "+mypath+"/findcores /opt/migrate/bin/")
141            os.system("sudo chmod +x /opt/migrate/bin/findcores")
142        if os.path.exists(mypath+"/fastmigrate-n"):
143            os.system("sudo rsync -a "+mypath+"/fastmigrate-n /opt/migrate/bin/")
144            os.system("sudo chmod +x /opt/migrate/bin/fastmigrate-n")
145        found = False
146        for path in os.environ.get('PATH', '').split(':'):
147            if path == homedir+"/opt/migrate/bin":
148                found=True
149                break
150        if found == True:
151            print "No insertion of execution path needed, because it is already present"
152        else:
153            if "bash" in shell:
154                f = open(homedir+'/.bash_profile', 'r')
155		for line in f:
156		    if "###migrate" in line:
157			sys.exit()
158		f.close()
159                f = open(homedir+'/.bash_profile', 'a')
160                f.write("###migrate (popgen.sc.fsu.edu) start Path addition\n")
161                f.write("PATH=$PATH:"+homedir+"/opt/migrate/bin\n")
162                f.write("export PATH\n")
163                f.write("###migrate (popgen.sc.fsu.edu) end Path addition\n\n")
164                f.close()
165            else:
166                print "Not implemented yet"
167
168        print "MIGRATE was installed in your home directory under ~/opt/migrate"
169        print " to remove simply delete the migrate folder in ~/opt"
170
171
172if __name__ == "__main__":
173    main()
174
175