Dotclear

source: debian/mkdcl.php @ 0:54703be25dd6

Revision 0:54703be25dd6, 3.7 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

Line 
1#!/usr/bin/env php
2<?php
3
4class debianChangelog
5{
6     public $f = 'debian/changelog';
7     
8     public function __construct()
9     {
10          if (!is_file($this->f)) {
11               throw new Exception('No changelog file found');
12          }
13     }
14     
15     private function readLastRevision()
16     {
17          $f = file($this->f);
18          $res = array();
19          $done = false;
20         
21          foreach ($f as $v)
22          {
23               $v = rtrim($v,"\n");
24               
25               # First line of a change
26               if (strpos($v,' ') !== 0 && trim($v) != '')
27               {
28                    if ($done) {
29                         break;
30                    }
31                   
32                    $done = true;
33                    $res = $this->getPackageInfo($v,$res[$i]);
34               }
35               # Maintainer information
36               elseif (strpos($v,' --') === 0)
37               {
38                    $res['maintainer'] = $this->getMaintainerInfo($v);
39               }
40               # Changelog
41               elseif (strpos($v,'  ') === 0)
42               {
43                    $res['changelog'] .= $v."\n";
44               }
45          }
46         
47          return $res;
48     }
49     
50     public function writeChangelog()
51     {
52          $ch = $this->readLastRevision();
53         
54          # Get debian revision
55          $rev = 1;
56          if (preg_match('/^(.*)-(\d+)$/',$ch['version'],$m)) {
57               $ch['version'] = $m[1];
58               $rev = $m[2];
59          }
60          $rev++;
61         
62          # Get SVN revision
63          $svnrev = isset($ch['keywords']['svnrev']) ? (integer) $ch['keywords']['svnrev'] : 1;
64         
65          # Get current SVN revision
66          $currev = svnInfo::getCurrentRevision();
67          if ($currev <= $svnrev) {
68               return;
69          }
70         
71          $changelog = '';
72          $changes = svnInfo::getChangeLog($svnrev+1,$currev);
73          foreach ($changes as $k => $v)
74          {
75               $changelog .=
76               '  * SVN Revision '.$k.' - '.$v['author'].
77               ', on '.date('r',strtotime($v['date']))."\n".
78               '    '.trim(preg_replace('/\n/ms',"\n    ",$v['msg']))."\n\n";
79               
80          } 
81         
82          $res =
83          $ch['package'].' ('.$ch['version'].'-'.$rev.') '.$ch['dist'].'; urgency='.$ch['keywords']['urgency'].
84          ' ; svnrev='.$currev.
85          "\n\n".
86          rtrim($changelog)."\n\n".
87          ' -- '.$ch['maintainer']['name'].' <'.$ch['maintainer']['email'].'>  '.date('r')."\n".
88          "\n";
89         
90          $old_changelog = file_get_contents($this->f);
91          $fp = fopen($this->f,'wb');
92          fwrite($fp,$res.$old_changelog);
93          fclose($fp);
94     }
95     
96     private function getPackageInfo($l)
97     {
98          $res = array(
99               'package' => '',
100               'version' => '',
101               'dist' => '',
102               'keywords' => '',
103               'changelog' => '',
104               'maintainer' => array()
105          );
106         
107          $l = explode(';',$l);
108         
109          # Info
110          $info = array_shift($l);
111          $res['package'] = strtok($info,' ');
112          $res['version'] = strtok('()');
113          $res['dist'] = trim(strtok(';'));
114         
115          # Keywords
116          foreach ($l as $v) {
117               $v = explode('=',$v);
118               if (count($v) == 2) {
119                    $res['keywords'][trim($v[0])] = trim($v[1]);
120               }
121          }
122         
123          return $res;
124     }
125     
126     private function getMaintainerInfo($l)
127     {
128          $res = array(
129               'name' => '',
130               'email' => '',
131               'date' => ''
132          );
133         
134          if (preg_match('/^ -- (.+?) <(.+?)>  (.+?)$/',$l,$m)) {
135               $res['name'] = $m[1];
136               $res['email'] = $m[2];
137               $res['date'] = $m[3];
138          }
139         
140          return $res;
141     }
142}
143
144class svnInfo
145{
146     public static function getCurrentRevision()
147     {
148          $info = `LANG=C svn info --xml`;
149         
150          $x = @simplexml_load_string($info);
151          if (!$x) {
152               throw new Exception('Unable to get current SVN revision');
153          }
154         
155          $rev = $x->entry->commit['revision'];
156         
157          if (!$rev) {
158               throw new Exception('Last revision number is invalid');
159          }
160         
161          return (integer) $rev;
162     }
163     
164     public static function getChangeLog($fromrev,$torev)
165     {
166          $log = `LANG=C svn log --xml -r $fromrev:$torev`;
167         
168          $x = @simplexml_load_string($log);
169          if (!$x) {
170               throw new Exception('Unable to open SVN log');
171          }
172         
173          $res = array();
174          foreach ($x->logentry as $change)
175          {
176               $res[(integer) $change['revision']] = array(
177                    'author' => (string) $change->author,
178                    'date' => (string) $change->date,
179                    'msg' => trim((string) $change->msg)
180               );
181          }
182         
183          return $res;
184     }
185}
186
187# Main
188try
189{
190     $ch = new debianChangelog();
191     $ch->writeChangelog();
192}
193catch (Exception $e)
194{
195     fwrite(STDERR,$e->getMessage()."\n");
196     exit(1);
197}
198?>
Note: See TracBrowser for help on using the repository browser.

Sites map