...
[iramuteq] / checkinstall.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 import os
8 import sys
9 import shutil
10 from chemins import ConstructConfigPath
11 from functions import exec_rcode
12 import wx
13 import tempfile
14 from ConfigParser import *
15 from time import sleep
16
17 def IsNew(self):
18     version_glob = self.ConfigGlob.get('DEFAULT', 'version_nb').split('.')
19     try :
20         version_user = self.pref.get('iramuteq','version_nb').split('.')
21     except NoOptionError :
22         return True
23     if version_user :
24         version_user[0] = int(version_user[0])
25         version_user[1] = int(version_user[1])
26         version_glob[0] = int(version_glob[0])
27         version_glob[1] = int(version_glob[1])
28         if len(version_user) == len(version_glob) :
29             if version_glob > version_user :
30                 return True
31             else :
32                 return False
33         if len(version_glob) == 2 :
34             if version_glob[:2] >= version_user[:2] :
35                 return True
36             else :
37                 return False
38         elif len(version_glob) == 3 :
39             if version_glob[:2] <= version_user[:2] :
40                 return False
41             else :
42                 return True
43
44 def UpgradeConf(self) :
45     dictuser = self.ConfigPath
46     dictappli = ConstructConfigPath(self.AppliPath, user = False)
47     for item,filein in dictuser.iteritems():
48         if not item == u'global' and not item == u'history':
49             shutil.copyfile(dictappli[item], filein)
50
51 def CreateIraDirectory(UserConfigPath,AppliPath):
52     if not os.path.exists(UserConfigPath):
53         os.mkdir(UserConfigPath)
54
55 def CopyConf(self) :
56     DictUser = self.ConfigPath
57     DictAppli = ConstructConfigPath(self.AppliPath,user=False)
58     for item, filein in DictUser.iteritems():
59         if not item == u'global' and not item == u'path' and not item == u'preferences' and not item == u'history' :
60             shutil.copyfile(DictAppli[item],filein)
61         if item == u'path':
62             if not os.path.exists(filein):
63                 shutil.copyfile(DictAppli[item],filein)
64         if item == u'preferences' :
65             if not os.path.exists(filein) :
66                 shutil.copyfile(DictAppli[item],filein)
67
68 def CheckRPath(PathPath):
69     if not os.path.exists(PathPath.get('PATHS','rpath')):
70         return False
71     else :
72         return True
73         
74 def FindRPAthWin32():
75     BestPath=False
76     progpaths=[]
77     if 'ProgramFiles' in os.environ :
78         progpaths.append(os.environ['ProgramFiles'])
79     if 'ProgramFiles(x86)' in os.environ :
80         progpaths.append(os.environ['ProgramFiles(x86)'])
81     if 'ProgramW6432' in os.environ :
82         progpaths.append(os.environ['ProgramW6432'])
83     progpaths = list(set(progpaths))
84     if progpaths != [] :
85         for progpath in progpaths :
86             rpath = os.path.join(progpath, "R")
87         if os.path.exists(rpath) :
88             for i in range(7,20):
89                 for j in range(0,15):
90                     path=os.path.join(rpath,"R-2."+str(i)+"."+str(j),'bin','R.exe')
91                     if os.path.exists(path):
92                         BestPath=path
93     return BestPath
94
95 def FindRPathNix():
96     BestPath=False
97     if os.path.exists('/usr/bin/R'):
98         BestPath='/usr/bin/R'
99     elif os.path.exists('/usr/local/bin/R'):
100         BestPath='/usr/local/bin/R'
101     return BestPath
102
103 def CheckRapp(Path):
104     return os.path.exists(Path)
105
106 def RLibsAreInstalled(self) :
107     rlibs = self.pref.get('iramuteq', 'rlibs')
108     if rlibs == 'false' :
109         return False
110     else :
111         return True
112
113 def CheckRPackages(self):
114     listdep = ['ca', 'gee', 'ape', 'igraph','proxy', 'Matrix','wordcloud']
115     nolib = []
116     i=0
117     dlg = wx.ProgressDialog("Test des librairies de R", "test en cours...", maximum = len(listdep), parent=self, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME | wx.PD_CAN_ABORT)
118     for bib in listdep :
119         dlg.Center()
120         i+=1
121         dlg.Update(i, "test de %s" % bib)
122         txt = """library("%s")""" % bib
123         tmpscript = tempfile.mktemp(dir=self.TEMPDIR)
124         file = open(tmpscript, 'w')
125         file.write(txt)
126         file.close()
127         test = exec_rcode(self.RPath, tmpscript, wait = True)
128         if test :
129             nolib.append(bib)
130     dlg.Update(len(listdep),'fini')
131     dlg.Destroy()
132     if nolib != [] :
133         txt = '\n'.join(nolib)
134         msg = u"""Les bibliothèques de R suivantes sont manquantes :
135 %s
136
137 Sans ces bibliothèques, IRamuteq ne fonctionnera pas.
138     
139 - Vous pouvez installer ces bibliothèques manuellement :
140         Cliquez sur Annuler
141         Lancez R
142         Tapez install.packages('nom de la bibiothèque')
143         
144 - ou laisser IRamuteq les installer automatiquement en cliquant sur VALIDER .
145         Les bibliothèques seront téléchargées depuis le site miroir de R du CICT de Toulouse.
146         """ % txt
147         dial = wx.MessageDialog(self, msg, u"Installation incomplète", wx.OK | wx.CANCEL | wx.NO_DEFAULT | wx.ICON_WARNING)
148         dial.CenterOnParent()
149         val = dial.ShowModal()
150         if val == wx.ID_OK :
151             dlg = wx.ProgressDialog("Installation",
152                                        "Veuillez patientez...",
153                                        maximum=len(nolib) + 1,
154                                        parent=self,
155                                        style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME | wx.PD_CAN_ABORT
156                                        )
157             dlg.Center()
158             dlg.Update(1, u"installation...") 
159             compt = 0
160             for bib in nolib :
161                 compt += 1
162                 dlg.Update(compt, u"installation librairie %s" % bib)
163                 txt = """
164                 userdir <- unlist(strsplit(Sys.getenv("R_LIBS_USER"), .Platform$path.sep))[1]
165                 if (!file.exists(userdir)) {
166                     if (!dir.create(userdir, recursive = TRUE)) 
167                         print('pas possible')
168                     lib <- userdir
169                     .libPaths(c(userdir, .libPaths()))
170                 }
171                 print(userdir)
172                 .libPaths
173                 """
174                 txt += """
175                 install.packages("%s", repos = 'http://cran.cict.fr')
176                 """ % bib
177                 tmpscript = tempfile.mktemp(dir=self.TEMPDIR)
178                 file = open(tmpscript, 'w')
179                 file.write(txt)
180                 file.close()
181                 test = exec_rcode(self.RPath, tmpscript, wait = False)
182                 while test.poll() == None :
183                     dlg.Pulse(u"installation librairie %s" % bib)
184                     sleep(0.2)
185             dlg.Update(len(nolib) + 1, 'fin')
186             dlg.Destroy()
187         else :
188             pass
189         dial.Destroy()
190     else :
191         self.pref.set('iramuteq', 'rlibs', True)
192         with open(self.ConfigPath['preferences'], 'w') as f :
193             self.pref.write(f)
194         return True