source: tol/tol/stdlib/TolPackage/def_catalog.tol @ 4f283c7

1941-docker-image-to-testdevel
Last change on this file since 4f283c7 was 4f283c7, checked in by Pedro Gea <pgea@…>, 12 years ago

Refs #1779
Se recupera el mensaje de advertencia existente que no se mostraba por error.
Se incorpora un mensaje también para los paquetes con una url desconocida.

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@6264 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 18.7 KB
Line 
1
2//////////////////////////////////////////////////////////////////////////////
3// Record: [[
4//   Text identifier
5//   Text name
6//   Real high
7//   Real low
8//   Text repository
9//   Date release
10//   Text minTolVersion
11//   Text maxTolVersion
12//   Set freeDependencies
13//   Real ozaSize (extra)
14//   Real ozaTime (extra)
15// ]]
16
17//////////////////////////////////////////////////////////////////////////////
18Class @Catalog
19//////////////////////////////////////////////////////////////////////////////
20{
21  Text _.type;
22  Set _.records;
23  Set _rebuild;
24
25  ////////////////////////////////////////////////////////////////////////////
26  Real FindRecord(Text identifier)
27  ////////////////////////////////////////////////////////////////////////////
28  {
29    Set parts = Tokenizer(identifier, ".");
30    Real index = FindIndexByName(_.records, parts[1]);
31    If(index, FindIndexByName(_.records[index], identifier), 0)
32  };
33
34  ////////////////////////////////////////////////////////////////////////////
35  Set GetRecord(Text identifier)
36  ////////////////////////////////////////////////////////////////////////////
37  {
38    Set parts = Tokenizer(identifier, ".");
39    Real index = FindIndexByName(_.records, parts[1]);
40    If(index, {
41      Real index2 = FindIndexByName(_.records[index], identifier);
42      If(index2, _.records[index][index2], Copy(Empty))
43    }, Copy(Empty))
44  };
45
46  ////////////////////////////////////////////////////////////////////////////
47  Text ObtainIdentifier(Text pattern, Text tolVersion, Date date)
48  ////////////////////////////////////////////////////////////////////////////
49  {
50    ObtainIdentifier_Show(pattern, tolVersion, date, 1)
51  };
52
53  ////////////////////////////////////////////////////////////////////////////
54  Text ObtainLastCompatible(Text pattern)
55  ////////////////////////////////////////////////////////////////////////////
56  {
57    ObtainIdentifier(pattern, Version, UnknownDate)
58  };
59
60  ////////////////////////////////////////////////////////////////////////////
61  Text ObtainIdentifier_Show(Text pattern, Text tolVersion, Date date,
62    Real show)
63  ////////////////////////////////////////////////////////////////////////////
64  {
65    // show: 0 none, 1 info, 2 warning, 3 error
66    Text identifier = _ObtainIdentifier(pattern, tolVersion, date);
67    If(Text Sub(identifier, 1, 1)=="#", {
68      Text wm = Case(show==0, "", show==1, "U", show==2, "W", show==3, "E");
69      Case(show==0, {
70        ""
71      }, identifier=="#NotCompatible", {
72        Text msg = If(tolVersion=="any", "", " para la versión de TOL '"
73          <<tolVersion<<"'");
74        Text msg2 = If(""<<date=="Unknown", If(TextLength(msg), msg, ""), {
75          If(TextLength(msg), msg<<" y", " para")<<" la fecha "<<date
76        });
77        WriteLn("[TolPackage::@Catalog "<<_.type
78          <<"] No se encuentra el paquete '"<<pattern<<"'"<<msg2<<".", wm);
79          ""
80      }, identifier=="#NotFound", {
81        WriteLn("[TolPackage::@Catalog "<<_.type
82          <<"] No se encuentra el paquete '"<<Tokenizer(pattern, ".")[1]
83          <<"'.", wm);
84        ""
85      })
86    }, identifier)
87  };
88
89  ////////////////////////////////////////////////////////////////////////////
90  Text _ObtainIdentifier(Text pattern, Text tolVersion, Date date)
91  ////////////////////////////////////////////////////////////////////////////
92  {
93    Set parts = Tokenizer(pattern, ".");
94    Real index = FindIndexByName(_.records, parts[1]);
95    If(index, {
96      Set records = _.records[index];
97      Set parts := parts << For(1, 3-Card(parts), Text (Real i) { "*" });
98      Text txtPattern = parts[1]<<"."<<parts[2]<<"."<<parts[3];
99      Set options = Select(records, Real (Set record) {
100        Real ok = TextMatch(record::identifier, txtPattern);
101        // Se comprueba la versión de TOL
102        Real If(ok, {
103          Real ok := Case(
104            tolVersion=="any",
105              True,
106            TolVersion::CompareVersions(record::minTolVersion, tolVersion)>0,
107              False,
108            TolVersion::CompareVersions(tolVersion, record::maxTolVersion)>0,
109              False
110          )
111        });
112        // Se comprueba que la fecha no es posterior a la indicada
113        Real If(ok, {
114          Real ok := Case(""<<date=="Unknown", True,
115            record::release>date, False)
116        });
117        ok
118      });
119      If(Card(options), options[1]["identifier"], "#NotCompatible")
120    }, "#NotFound")
121  };
122
123  ////////////////////////////////////////////////////////////////////////////
124  Set GetFreeDependencies(Text pattern, Text tolVersion, Date date)
125  ////////////////////////////////////////////////////////////////////////////
126  {
127    Text identifier = ObtainIdentifier(pattern, tolVersion, date);
128    If(TextLength(identifier), {
129      GetRecord(identifier)["freeDependencies"]
130    }, {
131      WriteLn("No se encuentra el paquete "<<pattern, "W");
132      Copy(Empty)
133    })
134  };
135
136  ////////////////////////////////////////////////////////////////////////////
137  Set GetMainDependencies(Text pattern, Text tolVersion, Date date)
138  ////////////////////////////////////////////////////////////////////////////
139  {
140    Set freeD = GetFreeDependencies(pattern, tolVersion, date);
141    Set extraD = Unique(SetConcat(EvalSet(freeD, Set (Text p) {
142      GetFreeDependencies(p, tolVersion, date)
143    })));
144    freeD-extraD
145  };
146
147  ////////////////////////////////////////////////////////////////////////////
148  Anything IdentifyPackages(Anything patterns, Text tolVersion, Date date)
149  ////////////////////////////////////////////////////////////////////////////
150  {
151    Case(Grammar(patterns)=="Set", {
152      EvalSet(patterns, Anything (Anything a) {
153        IdentifyPackages(a, tolVersion, date)
154      })
155    }, Grammar(patterns)=="Text", {
156      If(TextLength(patterns),
157        ObtainIdentifier(patterns, tolVersion, date), "")
158    })
159  };
160
161  ////////////////////////////////////////////////////////////////////////////
162  Set GetDeepDependencies(Text pattern, Text tolVersion, Date date)
163  ////////////////////////////////////////////////////////////////////////////
164  {
165    Set mainDs = GetMainDependencies(pattern, tolVersion, date);
166    If(Card(mainDs), {
167      Unique(SetConcat(EvalSet(mainDs, Set (Text mainD) {
168        Set deepDs = GetDeepDependencies(mainD, tolVersion, date);
169        If(Card(deepDs), {
170          Set [[ mainD ]] << deepDs
171        }, [[ mainD ]])
172      })))
173    }, Copy(Empty))
174  };
175
176  ////////////////////////////////////////////////////////////////////////////
177  Real Update(Real void)
178  ////////////////////////////////////////////////////////////////////////////
179  {
180    Case(_.type=="Local", {
181      Set _.records := @Catalog::BuildRecords_Local(_rebuild[1]);
182    1}, _.type=="Remote", {
183      Set _.records := @Catalog::BuildRecords_Remote(_rebuild[1]);
184    1}, _.type=="Stored", {
185      Set _.records := Include(_rebuild[1]);
186    1})
187  };
188
189  ////////////////////////////////////////////////////////////////////////////
190  Set GetRecords(Real void)
191  ////////////////////////////////////////////////////////////////////////////
192  {
193    Copy(_.records)
194  };
195
196  ////////////////////////////////////////////////////////////////////////////
197  Set GetCompatibleRecords(Text tolVersion, Date date)
198  ////////////////////////////////////////////////////////////////////////////
199  {
200    If(tolVersion=="any" & ""<<date=="Unknown", Copy(_.records), {
201      Set records = Select(EvalSet(_.records, Set (Set subrecords) {
202        Set selection = Select(subrecords, Real (Set record) {
203          Real okTolVersion = If(tolVersion=="any", True, {
204            TolVersion::CompareVersions(record::minTolVersion, tolVersion)<=0
205            & TolVersion::CompareVersions(tolVersion, record::maxTolVersion)<=0
206          });
207          If(okTolVersion, {
208            If(""<<date=="Unknown", True, Not(record::release>date))
209          }, False)
210        });
211        Real If(Card(selection), SetIndexByName(selection));
212        PutName(Name(subrecords), selection)
213      }), Card);
214      Real SetIndexByName(records);
215      records
216    })
217  };
218
219  ////////////////////////////////////////////////////////////////////////////
220  Set GetCompatibleList(Text tolVersion, Date date)
221  ////////////////////////////////////////////////////////////////////////////
222  {
223    Set compRecords = GetCompatibleRecords(tolVersion, date);
224    EvalSet(compRecords, Set (Set subrecords) { subrecords[1] })
225  };
226
227  ////////////////////////////////////////////////////////////////////////////
228  Set ObtainUpdateList(@Catalog reference, Text tolVersion, Date date)
229  ////////////////////////////////////////////////////////////////////////////
230  {
231    Set records = GetCompatibleRecords(tolVersion, date);
232    Set refRecords = reference::GetCompatibleRecords(tolVersion, date);
233    SetConcat(EvalSet(refRecords, Set (Set refSubrecords) {
234      Real index = FindIndexByName(records, Name(refSubrecords));
235      If(index, {
236        Set subrecords = records[index];
237        // Se buscan todos los paquetes que necesiten ser actualizados
238        // (manteniendo los números de versión) por otra versión más reciente.
239        Set Select(EvalSet(refSubrecords, Set (Set refRecord) {
240          Real index2 = FindIndexByName(subrecords, Name(refRecord));
241          If(index2, {
242            Set record = subrecords[index2];
243            If(DateToIndex(refRecord::release)<DateToIndex(record::release), {
244              subrecords[index2] // se mantiene así el nombre del set
245            }, Empty)
246          }, Empty)
247        }), Card)
248      }, Empty)
249    }))
250  };
251
252  ////////////////////////////////////////////////////////////////////////////
253  Set ObtainUpgradeList(@Catalog reference, Text tolVersion, Date date)
254  ////////////////////////////////////////////////////////////////////////////
255  {
256    Set records = GetCompatibleRecords(tolVersion, date);
257    Set refRecords = reference::GetCompatibleRecords(tolVersion, date);
258    SetConcat(EvalSet(refRecords, Set (Set refSubrecords) {
259      Real index = FindIndexByName(records, Name(refSubrecords));
260      If(index, {
261        Set subrecords = records[index];
262        // Se buscan todos los archivos que necesiten ser actualizados
263        // (o mejorados) por otra versión (con distintos números de versión).
264        Set lastRecord = subrecords[1];
265        Set refLastRecord = refSubrecords[1];
266        If(lastRecord::high > refLastRecord::high, {
267          [[ PutName(lastRecord::identifier, lastRecord) ]]
268        }, {
269          If(lastRecord::low > refLastRecord::low, {
270            [[ PutName(lastRecord::identifier, lastRecord) ]]
271          }, Empty)
272        })
273      }, Empty)
274    }))
275  };
276
277  ////////////////////////////////////////////////////////////////////////////
278  Set ObtainLowUpgradeList(@Catalog reference, Text tolVersion, Date date)
279  ////////////////////////////////////////////////////////////////////////////
280  {
281    Set records = RegroupRecords(GetCompatibleRecords(tolVersion, date));
282    Set refRecords =
283      RegroupRecords(reference::GetCompatibleRecords(tolVersion, date));
284    SetConcat(EvalSet(refRecords, Set (Set refSubrecords) {
285      Real index = FindIndexByName(records, Name(refSubrecords));
286      If(index, {
287        Set subrecords = records[index];
288        // Se buscan todos los archivos que necesiten ser actualizados
289        // (o mejorados) por otra versión (con distintos números de versión).
290        Set lastRecord = subrecords[1];
291        Set refLastRecord = refSubrecords[1];
292        If(lastRecord::high > refLastRecord::high, {
293          [[ PutName(lastRecord::identifier, lastRecord) ]]
294        }, {
295          If(lastRecord::low > refLastRecord::low, {
296            [[ PutName(lastRecord::identifier, lastRecord) ]]
297          }, Empty)
298        })
299      }, Empty)
300    }))
301  };
302
303  ////////////////////////////////////////////////////////////////////////////
304  Set ObtainFullUpgradeList(@Catalog reference, Text tolVersion, Date date)
305  ////////////////////////////////////////////////////////////////////////////
306  {
307    Set fullList = ObtainUpgradeList(reference, tolVersion, date)
308      << ObtainLowUpgradeList(reference, tolVersion, date);
309    EvalSet(Classify(fullList, Real (Set r1, Set r2) {
310      Compare(Name(r1), Name(r2))
311    }), Set (Set group) { group[1] })
312  };
313
314  ////////////////////////////////////////////////////////////////////////////
315  Static @Catalog Local(Text path)
316  ////////////////////////////////////////////////////////////////////////////
317  {
318    @Catalog catalog = [[
319      Text _.type = "Local";
320      Set _rebuild = [[ path ]];
321      Set _.records = @Catalog::BuildRecords_Local(path)
322    ]]
323  };
324
325  ////////////////////////////////////////////////////////////////////////////
326  Static @Catalog Remote(Set repositories)
327  ////////////////////////////////////////////////////////////////////////////
328  {
329    @Catalog catalog = [[
330      Text _.type = "Remote";
331      Set _rebuild = [[ repositories ]];
332      Set _.records = @Catalog::BuildRecords_Remote(repositories)
333    ]]
334  };
335
336  ////////////////////////////////////////////////////////////////////////////
337  Static @Catalog Stored(Text file)
338  ////////////////////////////////////////////////////////////////////////////
339  {
340    @Catalog catalog = [[
341      Text _.type = "Stored";
342      Set _rebuild = [[ file ]];
343      Set _.records = Include(file)
344    ]]
345  };
346
347  ////////////////////////////////////////////////////////////////////////////
348  Static Set BuildRecords_Local(Text path)
349  ////////////////////////////////////////////////////////////////////////////
350  {
351    Set autodocs = Select(EvalSet(GetDir(path)[2], Set (Text package) {
352      Text autodocFile = path<<package<<"/info.oza";
353      If(FileExist(autodocFile), Ois.Load(autodocFile)[1], {
354        // Si no tiene info se obtiene de la lista A
355        Text ozaFile = path<<package<<"/"<<package<<".oza";
356        If(FileExist(ozaFile), {
357          Real ozaSize = FileBytes(ozaFile);
358          Text ozaId = package<<"."<<ozaSize;
359          TolPackage::AtListA(ozaId)
360        }, {
361          WriteLn("La carpeta local '"<<package
362            <<"' no contiene el archivo OZA de un paquete.", "W");
363          Copy(Empty)
364        })
365      })
366    }), Card);
367    Set records = EvalSet(autodocs, Set (Set autodoc) {
368      @Catalog::ObtainRecord_Autodoc(autodoc)
369    });
370    @Catalog::ClassifyRecords(records)
371  };
372
373  ////////////////////////////////////////////////////////////////////////////
374  Static Set BuildRecords_Remote(Set repositories)
375  ////////////////////////////////////////////////////////////////////////////
376  {
377    If(TolConfigManager::Config::Upgrading::TolPackage::LocalOnly, {
378      // Si la configuración está a local-only, no se accede al catálogo
379      // Podría plantearse una caché para trabajar sin conexión
380      Copy(Empty)
381    }, {
382      Set repositories_ = If(Card(repositories), repositories,
383        TolConfigManager::Config::Upgrading::TolPackage::Repositories);
384      Set rows = SetConcat(EvalSet(repositories_, Set (Text repository) {
385        Text urlAnswer = GetUrlContents(repository
386          <<"?tol_package_version=1.1&action=versions");
387        If(Text Sub(urlAnswer, 1, 3)=="Set", IncludeText(urlAnswer), {
388          Error("El repositorio '"<<repository
389            <<"' no está respondiendo adecuadamente.");
390          Empty
391        })
392      }));
393      Set records = EvalSet(rows, Set (Set row) {
394        Set pkgInfo = Tokenizer(row[3], ".");
395        Set freeDeps = Unique(Select(EvalSet(Tokenizer(row[7], ","),
396          Text (Text t) { Tokenizer(t, ".")[1] }), TextLength));
397        Set pkgRecord =  [[
398          Text identifier = row[3];
399          Text name = pkgInfo[1];
400          Real high = Eval(pkgInfo[2]);
401          Real low = Eval(pkgInfo[3]);
402          Text repository = row[1];
403          Date release = row[8];
404          Text minTolVersion = row[5];
405          Text maxTolVersion = row[6];
406          Set freeDependencies = freeDeps
407        ]];
408        PutName(row[3], pkgRecord)
409      });
410      @Catalog::ClassifyRecords(records)
411    })
412  };
413
414  ////////////////////////////////////////////////////////////////////////////
415  Static Set ObtainRecord_Autodoc(Set autodoc)
416  ////////////////////////////////////////////////////////////////////////////
417  {
418    Set pkgRecord = [[
419      Text identifier = autodoc::_.autodoc.name
420        <<"."<<autodoc::_.autodoc.version.high
421        <<"."<<autodoc::_.autodoc.version.low;
422      Text name = autodoc::_.autodoc.name;
423      Real high = autodoc::_.autodoc.version.high;
424      Real low = autodoc::_.autodoc.version.low;
425      Text repository = autodoc::_.autodoc.url;
426      Date release = autodoc::_.autodoc.releaseDate;
427      Text minTolVersion = autodoc::_.autodoc.minTolVersion;
428      Text maxTolVersion = If(
429        ObjectExist("Text", "autodoc::_.autodoc.maxTolVersion"),
430        autodoc::_.autodoc.maxTolVersion, "v999");
431      Set freeDependencies = Unique(EvalSet(autodoc::_.autodoc.dependencies,
432        Text (Text dep) { Tokenizer(dep, ".")[1] }))
433    ]];
434    PutName(pkgRecord::identifier, pkgRecord)
435  };
436
437  ////////////////////////////////////////////////////////////////////////////
438  Static Set FormatRecord_Autodoc(Set record)
439  ////////////////////////////////////////////////////////////////////////////
440  {
441    Set autodoc = [[
442      Text _.autodoc.name = record::name;
443      Real _.autodoc.version.high = record::high;
444      Real _.autodoc.version.low = record::low;
445      Text _.autodoc.url = record::repository;
446      Date _.autodoc.releaseDate = record::release;
447      Text _.autodoc.minTolVersion = record::minTolVersion;
448      Text _.autodoc.maxTolVersion = record::maxTolVersion;
449      Set _.autodoc.dependencies = record::freeDependencies
450    ]];
451    PutName(record::identifier, autodoc)
452  };
453
454  ////////////////////////////////////////////////////////////////////////////
455  Static Set ClassifyRecords(Set flatCatalog)
456  ////////////////////////////////////////////////////////////////////////////
457  {
458    Set catalog0 = Classify(flatCatalog,
459      Real (Set rec1, Set rec2) { Compare(rec1::name, rec2::name) });
460    Set hierCatalog = EvalSet(catalog0, Set (Set class) {
461      Set sortedClass = Sort(class, Real (Set rec1, Set rec2) {
462        Real compareHigh = Compare(rec2::high, rec1::high);
463        If(compareHigh, compareHigh, Compare(rec2::low, rec1::low))
464      });
465      Real SetIndexByName(sortedClass);
466      PutName(class[1]["name"], sortedClass)
467    });
468    Real SetIndexByName(hierCatalog);
469    hierCatalog
470  };
471
472  ////////////////////////////////////////////////////////////////////////////
473  Static Set RegroupRecords(Set records)
474  ////////////////////////////////////////////////////////////////////////////
475  {
476    SetConcat(EvalSet(records, Set (Set subrecords) { 
477      EvalSet(Classify(subrecords, Real (Set rec1, Set rec2) {
478        Compare(rec1::high, rec2::high)
479      }), Set (Set class) {
480        Set class2 = Sort(class, Real (Set rec1, Set rec2) {
481          Compare(rec2::low, rec1::low)
482        });
483        PutName(class[1]["name"]<<"."<<class[1]["high"], class2)
484      })
485    }))
486  }
487};
488//////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.