Advertisement
mixster

mixster

Mar 26th, 2010
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.60 KB | None | 0 0
  1. program SRLUpdater;
  2.  
  3. function GetRemoteSRLVersion(tryagain: Boolean): string;
  4. var
  5.   pageSauce: string;
  6. begin
  7.   pageSauce := ps_GetPage('http://vila.villavu.com/cgi-bin/gitweb/gitweb.cgi?p=srl-opendev.git;a=commit');
  8.   Result := Between('<tr><td>commit</td><td class="sha1">', '</td></tr>', pageSauce);
  9.   if (Result = '') or (pageSauce = '') then
  10.   begin
  11.     if (tryagain) then
  12.     begin
  13.       Writeln('Error retrieving remote SRL version. Trying again');
  14.       Result := GetRemoteSRLVersion(False);
  15.     end
  16.     else
  17.     begin
  18.       Writeln('Error retrieving the remote SRL version. Not trying again');
  19.     end;
  20.   end;
  21. end;
  22.  
  23. function IsSRLDownloaded(out uptodate: Boolean): Boolean;
  24. var
  25.   remoteVersion, localVersion: string;
  26. begin
  27.   Result := SettingsIsKey('SRLVersion');
  28.  
  29.   if (Result) then
  30.   begin
  31.     remoteVersion := GetRemoteSRLVersion(True);
  32.     if (remoteVersion = '') then
  33.     begin
  34.       Writeln('Unable to get remote version - assuming SRL include is up to date');
  35.       uptodate := True;
  36.     end
  37.     else
  38.     begin
  39.       localVersion := SettingsGetKeyValue('SRLVersion');
  40.       uptodate := localVersion = remoteVersion;
  41.  
  42.       if (uptodate) then
  43.         Writeln('The local SRL include is up-to-date!')
  44.       else
  45.         Writeln('The local SRL include is not up-to-date');
  46.     end;
  47.   end
  48.   else
  49.   begin
  50.     uptodate := False;
  51.     Writeln('There currently is no local version of the SRL includes!');
  52.   end;
  53. end;
  54.  
  55. type
  56.   TFile = record
  57.     path, filename: string;
  58.   end;
  59.  
  60.   TSVNFile = record
  61.     local, remote: TFile;
  62.   end;
  63.  
  64.   TMatch = record
  65.     start, finish: string;
  66.   end;
  67.  
  68.   TFilter = record
  69.     exp, dir, fil: string;
  70.     local, remote: TMatch;
  71.   end;
  72.  
  73.   TSVN = record
  74.     name: string;
  75.     skip: TStringArray;
  76.     baseloc: TSVNFile;
  77.     files: array of TSVNFile;
  78.     filter: TFilter;
  79.     rev: TMatch;
  80.     col: Boolean;
  81.   end;
  82.  
  83. function Explode(str, del: string): TStringArray;
  84. var
  85.   i, l, dL: Integer;
  86. begin
  87.   i := 0;
  88.   l := -1;
  89.   SetLength(Result, 0);
  90.   if (str = '') then
  91.     Exit;
  92.   dL := Length(del) - 1;
  93.   repeat
  94.     Inc(l);
  95.     SetLength(Result, l + 1);
  96.     i := Pos(del, str);
  97.     if i <= 0 then
  98.       Break;
  99.     Result[l] := Copy(str, 1, i - 1);
  100.     Delete(str, 1, i + dL);
  101.   until false
  102.   Result[l] := Copy(str, 1, Length(str));
  103. end;
  104.  
  105. function ReturnPage(svn: TSVN; dir: TSVNFile; tryagain: Boolean): string;
  106. var
  107.   url: string;
  108. begin
  109.   url := svn.baseloc.remote.path + dir.remote.path;
  110.  
  111.   Result := GetPage(url);
  112.  
  113.   if (Result = '') then
  114.   begin
  115.     if (tryagain) then
  116.     begin
  117.       Writeln('Downloading url "' + url + '" failed - trying again');
  118.       Result := ReturnPage(svn, dir, False);
  119.     end
  120.     else
  121.     begin
  122.       Writeln('Downloading url "' + url + '" failed - not trying again');
  123.     end;
  124.   end;
  125. end;
  126.  
  127. procedure RawCollectSVN(var svn: TSVN; cur_dir: TSVNFile);
  128. var
  129.   input, tmpLoc, tmpRem: string;
  130.   split: TStringArray;
  131.   h, i: Integer;
  132.   new_dir: TSVNFile;
  133.   sk: Boolean;
  134. begin
  135.   input := ReturnPage(svn, cur_dir, True);
  136.   split := Explode(input, svn.filter.exp);
  137.  
  138.   for i := 0 to High(split) do
  139.   begin
  140.     for h := 0 to High(svn.skip) do
  141.     begin
  142.       sk := Pos(svn.skip[h], split[i]) > 0;
  143.       if sk then
  144.         Break;
  145.     end;
  146.  
  147.     if sk then
  148.       Continue;
  149.  
  150.     tmpLoc := Between(svn.filter.local.start, svn.filter.local.finish, split[i]);
  151.     tmpRem := Between(svn.filter.remote.start, svn.filter.remote.finish, split[i]);
  152.  
  153.     if (tmpLoc = '') or (tmpRem = '') then
  154.       Continue;
  155.  
  156.     if Pos(svn.filter.dir, split[i]) > 0 then // Handle directory
  157.     begin
  158.       new_dir.local.path := cur_dir.local.path + tmpLoc;
  159.       new_dir.local.path := new_dir.local.path + '/'
  160.  
  161.       new_dir.remote.path := cur_dir.remote.path + tmpRem;
  162.       Writeln('Discovered directory "' + cur_dir.local.path + tmpLoc + '"');
  163.  
  164.       if not DirectoryExists(svn.baseloc.local.path + new_dir.local.path) then
  165.         if not CreateDirectory(svn.baseloc.local.path + new_dir.local.path) then
  166.           Writeln(format('Could not create the folder: %s',[svn.baseloc.local.path + new_dir.local.path]));
  167.  
  168.       RawCollectSVN(svn, new_dir);
  169.     end
  170.     else if Pos(svn.filter.fil, split[i]) > 0 then // Handle file
  171.     begin;
  172.       h := Length(svn.files);
  173.       SetLength(svn.files, h + 1);
  174.  
  175.       svn.files[h].local.path := cur_dir.local.path;
  176.       svn.files[h].local.filename := tmpLoc;
  177.       svn.files[h].remote.path := cur_dir.remote.path;
  178.       svn.files[h].remote.filename := tmpRem;
  179.     end;
  180.   end;
  181. end;
  182.  
  183. procedure CollectSVN(var svn: TSVN);
  184. var
  185.   start: TSVNFile;
  186.   t: Integer;
  187. begin
  188.   Writeln('Now starting to collect "' + svn.name + '"');
  189.   t := GetSystemTime;
  190.   RawCollectSVN(svn, start);
  191.   svn.col := True;
  192.   t := GetSystemTime - t;
  193.   Writeln('Finished collecting "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  194.   Writeln('A total of ' + IntToStr(Length(svn.files)) + ' files were collected');
  195. end;
  196.  
  197. procedure DownloadSVN(svn: TSVN);
  198. var
  199.   i, f, l, t: Integer;
  200.   s, remoteVersion: string;
  201.   opt: TReplaceFlags;
  202. begin
  203.   Writeln('Now starting to download "' + svn.name + '"');
  204.   Writeln('');
  205.  
  206.   opt := [rfReplaceAll];
  207.  
  208.   t := GetSystemTime;
  209.   l := Length(svn.files);
  210.   for i := 0 to l - 1 do
  211.     try
  212.       s := svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename;
  213.  
  214.       f := RewriteFile(s, False);
  215.       if (f = -1) then
  216.       begin
  217.         Writeln('Error writing ''' + s + '''' + ' (' + IntToStr(i) + ' of ' + IntToStr(l - 1) + ')');
  218.         Continue;
  219.       end
  220.       else
  221.         Writeln(s + ' (' + IntToStr(i) + ' of ' + IntToStr(l - 1) + ')');
  222.  
  223.       WriteFileString(f, GetPage(svn.baseloc.remote.path + svn.files[i].remote.path + svn.files[i].remote.filename));
  224.       CloseFile(f);
  225.     except
  226.       Writeln('Failed to download file.');
  227.     end;
  228.   t := GetSystemTime - t;
  229.   Writeln('Finished downloading "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  230.  
  231.   remoteVersion := GetRemoteSRLVersion(True);
  232.   if (remoteVersion = '') then
  233.   begin
  234.     Writeln('Appears to have successfully updated SRL, but unable to update the settings with the new version');
  235.     Writeln('This will most likely mean you will be told your SRL include is outdated, even if it is not');
  236.     Writeln('Please bear with us and either ignore it until you know SRL has been updated or update again :)');
  237.   end
  238.   else
  239.   begin
  240.     SettingsGetSetDefaultKeyValue('SRLVersion', remoteVersion);
  241.   end;
  242. end;
  243.  
  244. function ReturnSRLSVN: TSVN;
  245. var
  246.   opt: TReplaceFlags;
  247. begin
  248.   Result.name := 'SRL';
  249.   Result.baseloc.remote.path := 'http://www.villavu.com/repositories/srl-opendev/';
  250.   opt := [rfReplaceAll];
  251.   Result.baseloc.local.path := Replace(IncludePath + 'SRL/', '\', '/', opt);
  252.  
  253.   if not DirectoryExists(Result.baseloc.local.path) then
  254.     CreateDirectory(Result.baseloc.local.path);
  255.  
  256.   Result.filter.exp := '<';
  257.   Result.filter.dir := 'dir';
  258.   Result.filter.fil := 'file';
  259.  
  260.   Result.filter.local.start := 'name="';
  261.   Result.filter.local.finish := '"';
  262.  
  263.   Result.filter.remote.start := 'href="';
  264.   Result.filter.remote.finish := '"';
  265.  
  266.   Result.rev.start := 'rev="';
  267.   Result.rev.finish := '"';
  268.   Result.skip := [];
  269. end;
  270.  
  271. procedure MovePlugins(svn: TSVN; dest: string);
  272. var
  273.   i, f: Integer;
  274.   s: string;
  275.   o: Boolean;
  276. begin
  277.   for i := 0 to High(svn.files) do
  278.     if {$IFDEF LINUX} (Pos('.so', svn.files[i].local.filename) > 0)
  279.        {$ELSE} (Pos('.dll', svn.files[i].local.filename) > 0) {$ENDIF} then
  280.       try
  281.         Writeln('Found plugin "' + svn.files[i].local.filename);
  282.         if (Pos('Simba', svn.files[i].local.path) <= 0) then
  283.         begin
  284.           Writeln('Not a Simba plugin - ignoring');
  285.           Continue;
  286.         end;
  287.         o := False;
  288.         f := OpenFile(svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename, False);
  289.         if (f = -1) then
  290.         begin
  291.           Writeln('Error opening file ''' + svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename + '''');
  292.           Continue;
  293.         end;
  294.         ReadFileString(f, s, FileSize(f));
  295.         CloseFile(f);
  296.         o := True;
  297.         f := RewriteFile(dest + svn.files[i].local.filename, False);
  298.         if (f = -1) then
  299.         begin
  300.           Writeln('Error opening file ''' + dest + svn.files[i].local.filename + '''');
  301.           Continue;
  302.         end;
  303.         WriteFileString(f, s);
  304.         CloseFile(f);
  305.       except
  306.         Writeln('Failed to save plugin.');
  307.         if o then
  308.           Writeln('Problem involved rewriting the file at target folder')
  309.         else
  310.           Writeln('Problem involved opening the sauce file to read');
  311.       end;
  312. end;
  313.  
  314. var
  315.   MainMenuItem, MenuCheck, MenuUpdate, MenuMove : TMenuItem;
  316.   started: Boolean;
  317.  
  318. procedure CheckSRLVersions;
  319. var
  320.   tmp: Boolean;
  321. begin
  322.   IsSRLDownloaded(tmp);
  323. end;
  324.  
  325. procedure UpdateSRL;
  326. var
  327.   svn: TSVN;
  328. begin
  329.   svn := ReturnSRLSVN;
  330.   CollectSVN(svn);
  331.   DownloadSVN(svn);
  332. end;
  333.  
  334. procedure OnSRLUpdaterClick(Sender: TObject);
  335. var
  336.   svn: TSVN;
  337. begin
  338.   Writeln('Click!');
  339.   if (Sender = MenuCheck) then
  340.   begin
  341.     CheckSRLVersions;
  342.     Writeln('We checked and you''re fine, or maybe not!');
  343.   end
  344.   else if (Sender = MenuUpdate) then
  345.   begin
  346.     UpdateSRL;
  347.     Writeln('Updated you - looks good, or does it?');
  348.   end
  349.   else if (Sender = MenuMove) then
  350.   begin
  351.     svn := ReturnSRLSVN;
  352.     CollectSVN(svn);
  353.     MovePlugins(svn, PluginPath);
  354.   end;
  355. end;
  356.  
  357. procedure Init;
  358. begin;
  359.   MainMenuItem := TMenuItem.Create(Simba_MainMenu);
  360.   MainMenuItem.Caption := 'SRL';
  361.   Simba_MainMenu.Items.Add(MainMenuItem);
  362.  
  363.   MenuCheck := TMenuItem.Create(MainMenuItem);
  364.   MenuCheck.Caption := 'Check for new SRL';
  365.   MenuCheck.OnClick := @OnSRLUpdaterClick;
  366.   MainMenuItem.Add(MenuCheck);
  367.  
  368.   MenuUpdate := TMenuItem.Create(MainMenuItem);
  369.   MenuUpdate.Caption := 'Update SRL';
  370.   MenuUpdate.OnClick := @OnSRLUpdaterClick;
  371.   MainMenuItem.Add(MenuUpdate);
  372.  
  373.   MenuMove := TMenuItem.Create(MainMenuItem);
  374.   MenuMove.Caption := 'Move SRL plugins';
  375.   MenuMove.OnClick := @OnSRLUpdaterClick;
  376.   MainMenuItem.Add(MenuMove);
  377.  
  378.   started := True;
  379. end;
  380.  
  381. procedure Free;
  382. begin
  383.   if (started) then
  384.   begin
  385.     MenuCheck.Free;
  386.     MenuUpdate.Free;
  387.     MainMenuItem.Free;
  388.   end;
  389. end;
  390.  
  391. procedure Attach;
  392. var
  393.   tmp: Boolean;
  394. begin
  395.   MainMenuItem.Visible := True;
  396.   IsSRLDownloaded(tmp);
  397. end;
  398.  
  399. Procedure Detach;
  400. begin
  401.   Writeln('Fine, look after your own includes. See if we care!');
  402.   MainMenuItem.Visible := False;
  403. end;
  404.  
  405. function GetName : string;
  406. begin;
  407.   result := 'SRL Updater';
  408. end;
  409.  
  410. function GetVersion : string;
  411. begin;
  412.   result := '1.0';
  413. end;
  414.  
  415.  
  416. begin
  417. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement