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