summaryrefslogtreecommitdiff
path: root/pear/PEAR/Packager.php
blob: b790ac956f00c1c6c06b38a8912b248ad12bd4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@fast.no>                                   |
// |          Tomas V.V.Cox <cox@idecnet.com>                             |
// +----------------------------------------------------------------------+
//
// $Id$

require_once 'PEAR/Common.php';

/**
 * Administration class used to make a PEAR release tarball.
 *
 * TODO:
 *  - add an extra param the dir where to place the created package
 *  - preserve file permissions (solve umask in copy problem)
 *
 * @since PHP 4.0.2
 * @author Stig Bakken <ssb@fast.no>
 */
class PEAR_Packager extends PEAR_Common
{
    // {{{ properties

    /** XML_Parser object */
    var $parser;

    /** stack of elements, gives some sort of XML context */
    var $element_stack;

    /** name of currently parsed XML element */
    var $current_element;

    /** array of attributes of the currently parsed XML element */
    var $current_attributes = array();

    /** assoc with information about the package */
    var $pkginfo = array();

    /** name of the package directory, for example Foo-1.0 */
    var $pkgdir;

    /** directory where PHP code files go */
    var $phpdir;

    /** directory where PHP extension files go */
    var $extdir;

    /** directory where documentation goes */
    var $docdir;

    /** directory where system state information goes */
    var $statedir;

    /** debug mode (integer) */
    var $debug = 0;

    /** temporary directory */
    var $tmpdir;

    /** whether file list is currently being copied */
    var $recordfilelist;

    /** temporary space for copying file list */
    var $filelist;

    /** package name and version, for example "HTTP-1.0" */
    var $pkgver;

    // }}}

    // {{{ constructor

    function PEAR_Packager($phpdir = PEAR_INSTALL_DIR,
                           $extdir = PEAR_EXTENSION_DIR,
                           $docdir = '')
    {
        $this->PEAR();
        $this->phpdir = $phpdir;
        $this->extdir = $extdir;
        $this->docdir = $docdir;
    }

    // }}}
    // {{{ destructor

    function _PEAR_Packager() {
        $this->_PEAR();
        while (is_array($this->_tempfiles) &&
               $file = array_shift($this->_tempfiles))
        {
            if (is_dir($file)) {
                system("rm -rf $file"); // XXX FIXME Windows
            } else {
                unlink($file);
            }
        }
    }

    // }}}

    // {{{ package()

    function package($pkgfile = 'package.xml')
    {
        $pkginfo = $this->infoFromDescriptionFile($pkgfile);
        if (PEAR::isError($pkginfo)) {
            return $pkginfo;
        }
        // TMP DIR -------------------------------------------------
        $orig_pwd = getcwd();
        // We allow calls like "pear package /home/user/mypack/package.xml"
        if (!@chdir(dirname($pkgfile))) {
            return $this->raiseError('Couldn\'t chdir to package.xml dir',
                              null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
        }
        $pwd = getcwd();
        $pkgfile = basename($pkgfile);
        $pkgver = $pkginfo['package'] . '-' . $pkginfo['version'];
        // don't want strange characters
        $pkgver = ereg_replace ('[^a-zA-Z0-9._-]', '_', $pkgver);
        $this->tmpdir = $pwd . DIRECTORY_SEPARATOR . $pkgver;
        if (file_exists($this->tmpdir)) {
            return $this->raiseError('Tmpdir: ' . $this->tmpdir .' already exists',
                              null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
        }
        if (!mkdir($this->tmpdir, 0755)) {
            return $this->raiseError("Unable to create temporary directory $this->tmpdir.",
                              null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
        } else {
            $this->log(2, "+ tmp dir created at: " . $this->tmpdir);
        }
        $this->_tempfiles[] = $this->tmpdir;

        // Copy files -----------------------------------------------
        foreach ($pkginfo['filelist'] as $fname => $atts) {
            $file = $this->tmpdir . DIRECTORY_SEPARATOR . $fname;
            $dir = dirname($file);
            if (!@is_dir($dir)) {
                if (!$this->mkDirHier($dir)) {
                    return $this->raiseError("could not mkdir $dir");
                }
            }
            if (!@copy($fname, $file)) {
                $this->log(0, "could not copy $fname to $file");
            } else {
                $this->log(2, "+ copying from $fname to $file");
            }
        }
        // XXX TODO: Rebuild the package file as the old method did?

        // This allows build packages from different pear pack def files
        if (!@copy($pkgfile, $this->tmpdir . DIRECTORY_SEPARATOR . 'package.xml')) {
            return $this->raiseError("could not copy $pkgfile to " . $this->tmpdir);
        }
        $this->log(2, "+ copying package $pkgfile to " . $this->tmpdir);

        // TAR the Package -------------------------------------------
        chdir(dirname($this->tmpdir));
        $dest_package = $orig_pwd . DIRECTORY_SEPARATOR . "${pkgver}.tgz";
        // XXX FIXME Windows and non-GNU tar
        $cmd = "tar -cvzf $dest_package $pkgver";
        $this->log(2, "+ launched cmd: $cmd");
        // XXX TODO: add an extra param where to leave the package?
        $this->log(1, `$cmd`);
        $this->log(1, "Package $dest_package done");
        chdir($orig_pwd);
        return $dest_package;
    }

    /* XXXX REMOVEME: this is the old code
    function _package($pkgfile = "package.xml")
    {
        $pwd = getcwd();
        $fp = @fopen($pkgfile, "r");
        if (!is_resource($fp)) {
            return $this->raiseError($php_errormsg);
        }

        $xp = xml_parser_create();
        if (!$xp) {
            return $this->raiseError("Unable to create XML parser.");
        }
        xml_set_object($xp, $this);
        xml_set_element_handler($xp, "startHandler", "endHandler");
        xml_set_character_data_handler($xp, "charHandler");
        xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false);
        xml_parser_set_option($xp, XML_OPTION_TARGET_ENCODING, "UTF-8");

        $this->element_stack = array();
        $this->pkginfo = array();
        $this->current_element = false;

        $data = fread($fp, filesize($pkgfile));
        fclose($fp);
        if (!xml_parse($xp, $data, true)) {
            $msg = sprintf("XML error: %s at line %d",
                           xml_error_string(xml_get_error_code($xp)),
                           xml_get_current_line_number($xp));
            xml_parser_free($xp);
            return $this->raiseError($msg);
        }
        xml_parser_free($xp);

        $pkginfofile = $this->tmpdir . DIRECTORY_SEPARATOR . "package.xml";
        $fp = fopen($pkginfofile, "w");
        if (!is_resource($fp)) {
            return $this->raiseError("Could not create $pkginfofile: $php_errormsg");
        }

        $this->filelist = preg_replace('/^[\r\n]+\s+/', '    ', $this->filelist);
        $this->filelist = preg_replace('/\n\s+/', "\n    ", $this->filelist);
        $this->filelist = preg_replace('/\n\s+$/', "", $this->filelist);

        fputs($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n".
              "<!DOCTYPE Package PUBLIC \"-//PHP Group//DTD PEAR Package 1.0//EN//XML\" \"http://php.net/pear/package.dtd\">\n".
              "<Package Type=\"Source\">\n".
              "  <Name>".$this->pkginfo["Package,Name"]."</Name>\n".
              "  <Summary>".$this->pkginfo["Package,Summary"]."</Summary>\n".
              "  <Maintainer>\n".
              "    <Initials>".$this->pkginfo["Maintainer,Initials"]."</Initials>\n".
              "    <Name>".$this->pkginfo["Maintainer,Name"]."</Name>\n".
              "    <Email>".$this->pkginfo["Maintainer,Email"]."</Email>\n".
              "  </Maintainer>\n".
              "  <Release>\n".
              "    <Version>".$this->pkginfo["Release,Version"]."</Version>\n".
              "    <Date>".$this->pkginfo["Release,Date"]."</Date>\n".
              "    <Notes>".$this->pkginfo["Release,Notes"]."</Notes>\n".
              "  </Release>\n".
              "  <FileList>\n".
              "$this->filelist\n".
              "  </FileList>\n".
              "</Package>\n");
        fclose($fp);
        chdir(dirname($this->tmpdir));
        // XXX FIXME Windows and non-GNU tar
        $pkgver = $this->pkgver;
        $cmd = "tar -cvzf $pwd/${pkgver}.tgz $pkgver";
        $this->log(1, `$cmd`);
        $this->log(1, "Package $pwd/${pkgver}.tgz done");
    }

    // }}}

    // {{{ startHandler()

    function startHandler($xp, $name, $attribs)
    {
        array_push($this->element_stack, $name);
        $this->current_element = $name;
        $this->current_attributes = $attribs;
        $this->tmpdata = '';
        if ($this->recordfilelist) {
            $this->filelist .= "<$name";
            foreach ($attribs as $k => $v) {
                $this->filelist .= " $k=\"$v\"";
            }
            $this->filelist .= ">";
        }
        switch ($name) {
            case "Package":
                if ($attribs["Type"]) {
                    // warning
                }
                break;
            case "FileList":
                // XXX FIXME Windows
                $this->recordfilelist = true;
                $pwd = getcwd();
                $this->pkgver = $this->pkginfo["Package,Name"] . "-" .
                    $this->pkginfo["Release,Version"];
                // don't want extrange characters
                $this->pkgver = ereg_replace ("[^a-zA-Z0-9._-]", '_', $this->pkgver);
                $this->tmpdir = $pwd . DIRECTORY_SEPARATOR . $this->pkgver;
                if (file_exists($this->tmpdir)) {
                    xml_parser_free($xp);
                    $this->raiseError("$this->tmpdir already exists",
                                      null, PEAR_ERROR_TRIGGER,
                                      E_USER_ERROR);
                }
                if (!mkdir($this->tmpdir, 0755)) {
                    xml_parser_free($xp);
                    $this->raiseError("Unable to create temporary directory $this->tmpdir.",
                                      null, PEAR_ERROR_TRIGGER,
                                      E_USER_ERROR);
                }
                $this->_tempfiles[] = $this->tmpdir;
                break;
        }
    }

    // }}}
    // {{{ endHandler()

    function endHandler($xp, $name)
    {
        array_pop($this->element_stack);
        $this->current_element = $this->element_stack[sizeof($this->element_stack)-1];
        switch ($name) {
            case "FileList":
                $this->recordfilelist = false;
                break;
        }
        if ($this->recordfilelist) {
            $this->filelist .= "</$name>";
        }
    }

    // }}}
    // {{{ charHandler()

    function charHandler($xp, $data)
    {
        if ($this->recordfilelist) {
            $this->filelist .= $data;
        }
        switch ($this->current_element) {
            case "Dir":
                break;
            case "File":
                $file = "$this->tmpdir/$data";
                $dir = dirname($file);
                if (!is_dir($dir)) {
                    if (!$this->mkDirHier($dir)) {
                        $this->log(0, "could not mkdir $dir");
                        break;
                    }
                }
                if (!@copy($data, $file)) {
                    $this->log(0, "could not copy $data to $file");
                }
                // fall through
            default:
                $data = trim($data);
                if ($data) {
                    $id = implode(",", array_slice($this->element_stack, -2));
                    $this->pkginfo[$id] = $data;
                }
                break;
        }
    }
    */
    // }}}
}

?>