Improve m2sci tool
[scilab.git] / scilab / modules / m2sci / macros / translatepaths.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
3 // Copyright (C) ???? - INRIA - Serge STEER
4 //
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution.  The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10
11 function   transorder=translatepaths(Paths,res_path)
12     // Perform translation of Matlab M-files to Scilab for all M-files found in Paths
13
14     // Output :
15     //  -transorder : a vector which contains the files names with a determinated order
16     // Input :
17     //  -Paths : a vector of strings (paths of Matlab M-files to translate)
18     //  -res_paths : a string (resolution path of translated files)
19
20     // M2SCI kernel functions called:
21     //  -lst_funcall
22     //  -translateorder
23     //  - mfile2sci
24
25     // Get default arguments
26
27     [lhs,rhs]=argn(0)
28     if rhs<2 then res_path="./",end
29     if rhs<1 then m2sci_gui();transorder=[];return;end
30
31     // Loads libraries related to m2sci
32     if exists("m2skernellib")==0 then load("SCI/modules/m2sci/macros/kernel/lib"),end
33     if exists("m2spercentlib")==0 then load("SCI/modules/m2sci/macros/percent/lib"),end
34     if exists("m2ssci_fileslib")==0 then load("SCI/modules/m2sci/macros/sci_files/lib"),end
35
36     // Convert paths so that they can be used according to the platform
37
38     sep = filesep();
39     if getos() == "Windows" then
40         Paths=strsubst(Paths,"/",sep)
41         res_path=strsubst(res_path,"/",sep)
42     else
43         Paths=strsubst(Paths,"\",sep)
44         res_path=strsubst(res_path,"\",sep)
45     end
46
47     // Close resolution path with a / or a \
48     res_path=stripblanks(res_path)
49     if part(res_path,length(res_path))<>sep then
50         res_path=res_path+sep
51     end
52
53     // Create a logfile and a whatis file
54     Paths=stripblanks(Paths)
55     [tempfd,ierr]=file("open",pathconvert(TMPDIR)+gettext("unitfile.dat"),"old");
56
57     if ierr==0 then
58         load(pathconvert(TMPDIR)+gettext("unitfile.dat"))
59         file("close",whsfil_unit);
60         file("close",tempfd);
61         mdelete(pathconvert(TMPDIR)+gettext("unitfile.dat"))
62     end
63
64     whsfil_unit=file("open",res_path+"whatis","unknown")
65     save(pathconvert(TMPDIR)+gettext("unitfile.dat"),whsfil_unit)
66     // Close paths with a / or a \
67     for k=1:size(Paths,"*")
68         if part(Paths(k),length(Paths(k)))<>sep then
69             Paths(k)=Paths(k)+sep,
70         end
71     end
72
73     // Find names of files to translate
74     // mfiles is a vector which contains the names (and the paths) of files to translate
75     mfiles=[]
76     for k=1:size(Paths,"*")
77         path = Paths(k);
78         mfiles = [mfiles; ls(path+"*.m")];
79     end
80
81     // fnamvect is a vector which contains all M-files names (just the names) found in Paths
82     // filefuncallname is a list of vectors. The first component of each vector is the name of a M-file (found in the Path(s)) to translate, followed by the called functions by this M-file)
83     fnamvect=[]
84     filefuncallname=list()
85     for k1=1:size(mfiles,1)
86         kk=strindex(mfiles(k1),sep)
87         fnamvect=[fnamvect;part(mfiles(k1),kk($)+1:length(mfiles(k1))-2)]
88     end
89
90     for k1=1:size(mfiles,1)
91         mpath=mfiles(k1)
92         //    disp(gettext("********************lst_funcall**********************"))
93         //   disp(mpath)
94         filefuncallname($+1)=lst_funcall(mpath,fnamvect)
95     end
96
97     // Determination of a translation order for the M-files found in the Paths
98     // transorder is a vector containing the M-files names which are arranged to order to respect an priority order of translation
99     transorder=[]
100     for i=1:size(filefuncallname)
101         execstr(filefuncallname(i)(1)+"vect=filefuncallname(i)")
102     end
103
104     overfunname=[]
105     for i=1:size(filefuncallname)
106         execstr("transorder=translateorder(transorder,"+filefuncallname(i)(1)+"vect,overfunname)")
107     end
108
109     // funpath is a vector which contains the M-files names (ands their paths) to translate
110     funpath=[]
111     for i=1:size(transorder,1)
112         ind=find(transorder(i)==fnamvect)
113         if ind<>[] then
114             funpath=[funpath;mfiles(ind)]
115         end
116     end
117
118     // Translation is done only if M-file has changed
119     logtxt=[]
120     resumelogtxt="";
121
122     for i=1:size(funpath,1)
123         kk=strindex(funpath(i),sep)
124         mpath=funpath(i)
125         fnam=part(funpath(i),kk($)+1:length(funpath(i))-2)
126         scipath=res_path+fnam+".sci"
127         scepath=res_path+fnam+".sce"
128
129         if newest(mpath,scipath,scepath)==1 then
130             [fd,ierr]=file("open",pathconvert(TMPDIR)+fnam+".m","old");
131             if ierr==0 & strindex(mpath,TMPDIR)==[] then
132                 mfile2sci(pathconvert(TMPDIR)+fnam+".m",res_path, %t, %t)
133                 file("close",fd)
134                 mdelete(pathconvert(TMPDIR)+fnam+".m")
135                 rmdir(pathconvert(TMPDIR)+pathconvert(fnam),"s")
136             else
137                 mfile2sci(funpath(i),res_path, %t, %t)
138             end
139
140             tmp_sci_file=pathconvert(TMPDIR)+"tmp_"+fnam+".sci"
141             ierr=execstr("exec(tmp_sci_file)","errcatch");errclear();
142             if ierr==0 & strindex(mpath,TMPDIR)==[] then
143                 txt=[]
144                 txt=mgetl(scipath)
145                 txt=[txt;" ";mgetl(tmp_sci_file)]
146                 mputl(txt,scipath);
147                 mdelete(tmp_sci_file)
148             end
149
150             // LOG
151             tmp_m2sci_file=pathconvert(TMPDIR)+"tmp_m2sci_"+fnam+".log"
152             m2scipath=res_path+"m2sci_"+fnam+".log"
153             logtxt=[logtxt;" ";" ";mgetl(m2scipath)]
154             mdelete(m2scipath)
155
156             [fd,ierr]=file("open",tmp_m2sci_file,"old");
157             if ierr==0 & strindex(mpath,TMPDIR)==[] then
158                 logtxt=[logtxt;" ";mgetl(tmp_m2sci_file)]
159                 file("close",fd)
160                 mdelete(tmp_m2sci_file)
161             end
162
163             // RESUMELOG
164             tmp_resume_m2sci_file=pathconvert(TMPDIR)+"tmp_resume_m2sci_"+fnam+".log"
165             resumem2scipath=res_path+"resume_m2sci_"+fnam+".log"
166             if fileinfo(resumem2scipath)<>[] then
167                 resumelogtxt=[resumelogtxt;" ";" ";mgetl(resumem2scipath)]
168                 mdelete(resumem2scipath)
169             end
170
171             [fd,ierr]=file("open",tmp_resume_m2sci_file,"old");
172             if ierr==0 & strindex(mpath,TMPDIR)==[] then
173                 resumelogtxt=[resumelogtxt;" ";mgetl(tmp_resume_m2sci_file)]
174                 file("close",fd)
175                 mdelete(tmp_resume_m2sci_file)
176             end
177         end
178         if isempty(logtxt) then
179             logtxt = ""
180         end
181         mputl(logtxt,res_path+"log");
182         if isempty(resumelogtxt) then
183             resumelogtxt = ""
184         end
185         mputl(resumelogtxt,res_path+"resumelog");
186     end
187
188     // File closing
189     file("close",whsfil_unit);
190     mdelete(pathconvert(TMPDIR)+gettext("unitfile.dat"))
191
192     // create builder.sce and loader.sce files
193     // get the directory name where the Scilab functions are written
194     if res_path=="./" then
195         current_path=pathconvert(unix_g("pwd"))
196         index_slash=strindex(current_path,"/")
197         if size(index_slash,"*")==1 then index_slash=[0 index_slash],end
198         namelib=part(current_path,index_slash($-1)+1:index_slash($)-1)
199     else
200         index_slash=strindex(res_path,"/")
201         if size(index_slash,"*")==1 then index_slash=[0 index_slash],end
202         namelib=part(res_path,index_slash($-1)+1:index_slash($)-1)
203     end
204
205     //builder.sce
206     buildertxt=[]
207     buildertxt($+1)="path=get_absolute_file_path(""builder.sce"")"
208     buildertxt($+1)="genlib("""+namelib+"lib"",path)"
209     builderfile=res_path+"builder.sce"
210     mputl(buildertxt,builderfile);
211     //loader.sce
212     loadertxt=[]
213     loadertxt($+1)="path=get_absolute_file_path(""loader.sce"")"
214     loadertxt($+1)="load(path+"+"""lib"")"
215     loaderfile=res_path+"loader.sce"
216     mputl(loadertxt,loaderfile);
217
218 endfunction