Rev 647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
647 | daniel-mar | 1 | #!/usr/bin/env php |
2 | <?php |
||
3 | |||
4 | /* |
||
5 | * OIDplus 2.0 |
||
6 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
||
7 | * |
||
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | // This script will be called at the ViaThinkSoft server side |
||
22 | |||
23 | $argc = $_SERVER['argc']; // to please Eclipse for PHP |
||
24 | $argv = $_SERVER['argv']; // to please Eclipse for PHP |
||
25 | |||
26 | if (PHP_SAPI != 'cli') { |
||
27 | die("This file can only be invoked in CLI mode.\n"); |
||
28 | } |
||
29 | |||
30 | if ($argc != 2) { |
||
31 | echo "Usage: ".$argv[0]." <targetpath>\n"; |
||
32 | exit(2); |
||
33 | } |
||
34 | |||
35 | $output_dir = $argv[1]; |
||
36 | |||
37 | if (!is_dir($output_dir)) { |
||
38 | echo "Path $output_dir does not exist!\n"; |
||
39 | exit(1); |
||
40 | } |
||
41 | |||
42 | $outscript = ''; |
||
43 | |||
44 | function getDirContents_del($dir_old, $dir_new, $basepath_old=null, $basepath_new=null) { |
||
45 | global $outscript; |
||
46 | |||
47 | if (is_null($basepath_old)) $basepath_old = $dir_old; |
||
48 | $basepath_old = realpath($basepath_old) . DIRECTORY_SEPARATOR; |
||
49 | if ($basepath_old == '/') die('ARG'); |
||
50 | |||
51 | $dir_old = realpath($dir_old) . DIRECTORY_SEPARATOR; |
||
52 | $dir_new = realpath($dir_new) . DIRECTORY_SEPARATOR; |
||
53 | $files_old = scandir($dir_old); |
||
54 | $files_new = scandir($dir_new); |
||
55 | |||
56 | foreach ($files_old as $file_old) { |
||
57 | if ($file_old === '.') continue; |
||
58 | if ($file_old === '..') continue; |
||
59 | if ($file_old === '.svn') continue; |
||
60 | if ($file_old === '.git') continue; |
||
61 | |||
62 | $path_old = realpath($dir_old . DIRECTORY_SEPARATOR . $file_old); |
||
63 | $path_new = realpath($dir_new . DIRECTORY_SEPARATOR . $file_old); |
||
64 | |||
65 | $xpath_old = substr($path_old, strlen($basepath_old)); |
||
66 | |||
67 | if (is_dir($path_old)) { |
||
68 | getDirContents_del($path_old, $path_new, $basepath_old, $basepath_new); |
||
69 | } |
||
70 | |||
71 | if (is_dir($path_old) && !is_dir($path_new)) { |
||
72 | $outscript .= "// Dir deleted: $xpath_old\n"; |
||
73 | $outscript .= "@rmdir('$xpath_old');\n"; |
||
74 | $outscript .= "if (is_dir('$xpath_old'))\n\twarn('Directory could not be deleted (was not empty?): $xpath_old');\n\n"; |
||
75 | |||
76 | } else if (file_exists($path_old) && !file_exists($path_new)) { |
||
77 | $outscript .= "// File deleted: $xpath_old\n"; |
||
78 | $outscript .= "@unlink('$xpath_old');\n"; |
||
79 | $outscript .= "if (file_exists('$xpath_old'))\n\twarn('File could not be deleted: $xpath_old');\n\n"; |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | function getDirContents_diff($dir_old, $dir_new, $basepath_old=null, $basepath_new=null) { |
||
85 | global $outscript; |
||
86 | |||
87 | if (is_null($basepath_old)) $basepath_old = $dir_old; |
||
88 | $basepath_old = realpath($basepath_old) . DIRECTORY_SEPARATOR; |
||
89 | if ($basepath_old == '/') die('ARG'); |
||
90 | |||
91 | $dir_old = realpath($dir_old) . DIRECTORY_SEPARATOR; |
||
92 | $dir_new = realpath($dir_new) . DIRECTORY_SEPARATOR; |
||
93 | $files_old = scandir($dir_old); |
||
94 | $files_new = scandir($dir_new); |
||
95 | |||
96 | foreach ($files_old as $file_old) { |
||
97 | if ($file_old === '.') continue; |
||
98 | if ($file_old === '..') continue; |
||
99 | if ($file_old === '.svn') continue; |
||
100 | if ($file_old === '.git') continue; |
||
101 | |||
102 | $path_old = realpath($dir_old . DIRECTORY_SEPARATOR . $file_old); |
||
103 | $path_new = realpath($dir_new . DIRECTORY_SEPARATOR . $file_old); |
||
104 | |||
105 | $xpath_old = substr($path_old, strlen($basepath_old)); |
||
106 | |||
107 | if (file_exists($path_old) && file_exists($path_new)) { |
||
108 | if (file_get_contents($path_old) != file_get_contents($path_new)) { |
||
109 | $outscript .= "// Files different: $xpath_old\n"; |
||
110 | $outscript .= "if (@sha1_file('$xpath_old') !== '".sha1_file($path_old)."')\n\twarn('File was probably modified. Will overwrite the changes now: $xpath_old');\n"; |
||
111 | $outscript .= "@file_put_contents('$xpath_old', base64_decode('".base64_encode(file_get_contents($path_new))."'));\n"; |
||
112 | $outscript .= "if (@sha1_file('$xpath_old') !== '".sha1_file($path_new)."')\n\twarn('File cannot be written (checksum mismatch): $xpath_old');\n\n"; |
||
113 | } |
||
114 | if ((fileperms($path_old) & 0777) != (fileperms($path_new) & 0777)) { |
||
115 | $outscript .= "// Different file chmod: $xpath_old\n"; |
||
116 | $outscript .= "if ((DIRECTORY_SEPARATOR === '/') && !@chmod('$xpath_old', 0".sprintf('%o', fileperms($path_new) & 0777)."))\n\twarn('Could not change file permissions of ".$xpath_old."');\n\n"; |
||
117 | } |
||
118 | } else if (is_dir($path_old) && is_dir($path_new)) { |
||
119 | if ((fileperms($path_old) & 0777) != (fileperms($path_new) & 0777)) { |
||
120 | $outscript .= "// Different dir chmod: $xpath_old\n"; |
||
121 | $outscript .= "if ((DIRECTORY_SEPARATOR === '/') && !@chmod('$xpath_old', 0".sprintf('%o', fileperms($path_new) & 0777)."))\n\twarn('Could not change dir permissions of ".$xpath_old."');\n\n"; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | if (is_dir($path_old)) { |
||
126 | getDirContents_diff($path_old, $path_new, $basepath_old, $basepath_new); |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | function getDirContents_add($dir_old, $dir_new, $basepath_old=null, $basepath_new=null) { |
||
132 | global $outscript; |
||
133 | |||
134 | if (is_null($basepath_new)) $basepath_new = $dir_new; |
||
135 | $basepath_new = realpath($basepath_new) . DIRECTORY_SEPARATOR; |
||
136 | if ($basepath_new == '/') die('ARG'); |
||
137 | |||
138 | $dir_old = realpath($dir_old) . DIRECTORY_SEPARATOR; |
||
139 | $dir_new = realpath($dir_new) . DIRECTORY_SEPARATOR; |
||
140 | $files_old = scandir($dir_old); |
||
141 | $files_new = scandir($dir_new); |
||
142 | |||
143 | foreach ($files_new as $file_new) { |
||
144 | if ($file_new === '.') continue; |
||
145 | if ($file_new === '..') continue; |
||
146 | if ($file_new === '.svn') continue; |
||
147 | if ($file_new === '.git') continue; |
||
148 | |||
149 | $path_old = realpath($dir_old . DIRECTORY_SEPARATOR . $file_new); |
||
150 | $path_new = realpath($dir_new . DIRECTORY_SEPARATOR . $file_new); |
||
151 | |||
152 | $xpath_new = substr($path_new, strlen($basepath_new)); |
||
153 | |||
154 | if (is_dir($path_new) && !is_dir($path_old)) { |
||
155 | $outscript .= "// Dir added: $xpath_new\n"; |
||
156 | $outscript .= "@mkdir('$xpath_new');\n"; |
||
157 | $outscript .= "if (!is_dir('$xpath_new'))\n\twarn('Directory could not be created: $xpath_new');\n"; |
||
158 | $outscript .= "else if ((DIRECTORY_SEPARATOR === '/') && !@chmod('$xpath_new', 0".sprintf('%o', fileperms($path_new) & 0777)."))\n\twarn('Could not change directory permissions of ".$xpath_new."');\n\n"; |
||
159 | } else if (file_exists($path_new) && !file_exists($path_old)) { |
||
160 | $outscript .= "// File added: $xpath_new\n"; |
||
161 | $outscript .= "@file_put_contents('$xpath_new', base64_decode('".base64_encode(file_get_contents($path_new))."'));\n"; |
||
162 | $outscript .= "if (!file_exists('$xpath_new'))\n\twarn('File cannot be created (not existing): $xpath_new');\n"; |
||
163 | $outscript .= "else if (sha1_file('$xpath_new') != '".sha1_file($path_new)."')\n\twarn('File cannot be created (checksum mismatch): $xpath_new');\n"; |
||
164 | $outscript .= "else if ((DIRECTORY_SEPARATOR === '/') && !@chmod('$xpath_new', 0".sprintf('%o', fileperms($path_new) & 0777)."))\n\twarn('Could not change file permissions of ".$xpath_new."');\n\n"; |
||
165 | } |
||
166 | |||
167 | if (is_dir($path_new)) { |
||
168 | getDirContents_add($path_old, $path_new, $basepath_old, $basepath_new); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | function getDirContents($dir_old, $dir_new) { |
||
174 | getDirContents_del($dir_old, $dir_new); |
||
175 | getDirContents_add($dir_old, $dir_new); |
||
176 | getDirContents_diff($dir_old, $dir_new); |
||
177 | } |
||
178 | |||
179 | |||
180 | $out = array(); |
||
181 | $ec = -1; |
||
182 | exec('svn info https://svn.viathinksoft.com/svn/oidplus/trunk/ | grep "Revision:" | cut -d " " -f 2', $out, $ec); |
||
183 | $max_svn = implode("", $out); |
||
184 | |||
185 | |||
186 | for ($i=2; $i<=$max_svn; $i++) { |
||
187 | echo "SVN revision $i / $max_svn\r"; |
||
188 | |||
189 | $outfile = $output_dir."/update_".($i-1)."_to_$i.txt"; |
||
190 | if (file_exists($outfile)) continue; |
||
191 | |||
192 | $outdir_old = "/tmp/oidplus_svntmp2_".($i-1)."/"; |
||
193 | if (is_dir($outdir_old)) exec("rm -rf $outdir_old", $out, $ec); |
||
194 | exec("svn co https://svn.viathinksoft.com/svn/oidplus/trunk/@".($i-1)." $outdir_old", $out, $ec); |
||
195 | |||
196 | $outdir_new = "/tmp/oidplus_svntmp2_$i/"; |
||
197 | if (is_dir($outdir_new)) exec("rm -rf $outdir_new", $out, $ec); |
||
198 | exec("svn co https://svn.viathinksoft.com/svn/oidplus/trunk/@$i $outdir_new", $out, $ec); |
||
199 | |||
200 | // TODO: check for PHP version etc. |
||
201 | $outscript = "<?php\n"; |
||
202 | $outscript .= "\n"; |
||
203 | $outscript .= "/*\n"; |
||
204 | $outscript .= " * OIDplus 2.0\n"; |
||
205 | $outscript .= " * Copyright 2019 - ".date('Y')." Daniel Marschall, ViaThinkSoft\n"; |
||
206 | $outscript .= " *\n"; |
||
207 | $outscript .= " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"; |
||
208 | $outscript .= " * you may not use this file except in compliance with the License.\n"; |
||
209 | $outscript .= " * You may obtain a copy of the License at\n"; |
||
210 | $outscript .= " *\n"; |
||
211 | $outscript .= " * http://www.apache.org/licenses/LICENSE-2.0\n"; |
||
212 | $outscript .= " *\n"; |
||
213 | $outscript .= " * Unless required by applicable law or agreed to in writing, software\n"; |
||
214 | $outscript .= " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"; |
||
215 | $outscript .= " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"; |
||
216 | $outscript .= " * See the License for the specific language governing permissions and\n"; |
||
217 | $outscript .= " * limitations under the License.\n"; |
||
218 | $outscript .= " */\n"; |
||
219 | $outscript .= "\n"; |
||
220 | $outscript .= "function info(\$str) { echo \"INFO: \$str\\n\"; }\n"; |
||
221 | $outscript .= "function warn(\$str) { echo \"WARNING: \$str\\n\"; }\n"; |
||
222 | $outscript .= "function err(\$str) { die(\"FATAL ERROR: \$str\\n\"); }\n"; |
||
223 | $outscript .= "\n"; |
||
224 | $outscript .= "chdir(__DIR__);\n"; |
||
225 | $outscript .= "if (trim(@file_get_contents('oidplus_version.txt')) !== 'Revision ".($i-1)."') err('This update can only be applied to OIDplus SVN Rev ".($i-1)."!');\n"; |
||
226 | $outscript .= "\n"; |
||
227 | $outscript .= "info('Update to SVN Rev $i...');\n"; |
||
228 | $outscript .= "\n"; |
||
229 | getDirContents($outdir_old, $outdir_new); |
||
230 | $outscript .= "\n"; |
||
231 | $outscript .= "file_put_contents('oidplus_version.txt', \"Revision $i\\n\");\n"; |
||
232 | $outscript .= "if (trim(@file_get_contents('oidplus_version.txt')) !== 'Revision $i') err('Could not write to oidplus_version.txt!');\n"; |
||
233 | $outscript .= "\n"; |
||
234 | $outscript .= "\n"; |
||
235 | $outscript .= "info('Update to SVN Rev $i done!');\n"; |
||
236 | $outscript .= "\n"; |
||
237 | $outscript .= "unlink(__FILE__);\n"; |
||
238 | $outscript .= "\n"; |
||
239 | |||
240 | exec("rm -rf $outdir_old", $out, $ec); |
||
241 | exec("rm -rf $outdir_new", $out, $ec); |
||
242 | |||
243 | file_put_contents($outfile, $outscript); |
||
244 | } |
||
245 | echo "\n"; |
||
648 | daniel-mar | 246 | |
247 | |||
248 | // Now write the release messages |
||
249 | |||
250 | |||
251 | $out = array(); |
||
252 | $ec = 0; |
||
253 | exec('svn log https://svn.viathinksoft.com/svn/oidplus/trunk --xml', $out, $ec); |
||
254 | if ($ec != 0) die(); |
||
255 | |||
256 | $str = implode("\n",$out); |
||
257 | |||
258 | $xml = simplexml_load_string($str); |
||
259 | |||
260 | $out = array(); |
||
261 | |||
262 | foreach ($xml as $a) { |
||
263 | |||
264 | $out[(int)$a->attributes()->revision] = array( |
||
265 | |||
266 | 'date' => date('Y-m-d H:i:s',strtotime((string)$a->date)), |
||
267 | 'author' => (string)$a->author, |
||
268 | 'msg' => trim((string)$a->msg), |
||
269 | |||
270 | ); |
||
271 | |||
272 | } |
||
273 | |||
274 | ksort($out); |
||
275 | |||
276 | file_put_contents($output_dir.'/releases.ser', serialize($out)); |
||
277 |