Commit:    87c84793a9ed07795905f1e2b973476889653515
Author:    Anatol Belski <a...@php.net>         Fri, 27 Sep 2013 21:28:26 +0200
Parents:   e063661267263a9a0f8a4da3f2e605145889cff3
Branches:  master

Link:       
http://git.php.net/?p=web/rmtools.git;a=commitdiff;h=87c84793a9ed07795905f1e2b973476889653515

Log:
basic yet functionality for aggregated mails

Changed paths:
  M  client/bin/pecl_build_all.bat
  M  client/bin/pecl_build_next.bat
  M  client/include/PeclExt.php
  A  client/include/PeclMail.php
  M  client/script/pecl.php

diff --git a/client/bin/pecl_build_all.bat b/client/bin/pecl_build_all.bat
index 074658b..29abe16 100644
--- a/client/bin/pecl_build_all.bat
+++ b/client/bin/pecl_build_all.bat
@@ -39,10 +39,13 @@ GOTO EXIT_LOCKED
 
 ECHO running > c:\php-sdk\locks\pecl.lock 
 
-call %BAT_DIR%pecl.bat --config=pecl55_x64 %* >> %LOG_FILE% 2<&1
+rem Notice the --first and the --last calls marked, that's important
+rem to maintain the state between call for the same package. For instance
+rem if --aggregate-mail is used.
+call %BAT_DIR%pecl.bat --config=pecl55_x64 --first %* >> %LOG_FILE% 2<&1
 call %BAT_DIR%pecl.bat --config=pecl55_x86 %* >> %LOG_FILE% 2<&1
 call %BAT_DIR%pecl.bat --config=pecl54 %* >> %LOG_FILE% 2<&1
-call %BAT_DIR%pecl.bat --config=pecl53 %* >> %LOG_FILE% 2<&1
+call %BAT_DIR%pecl.bat --config=pecl53 --last %* >> %LOG_FILE% 2<&1
 
 echo Done.>> %LOG_FILE%
 
diff --git a/client/bin/pecl_build_next.bat b/client/bin/pecl_build_next.bat
index 96e8e3b..7499b99 100644
--- a/client/bin/pecl_build_next.bat
+++ b/client/bin/pecl_build_next.bat
@@ -4,10 +4,12 @@ echo pick the next PECL pkg and pass to pecl_build_all.bat
 
 SET BAT_DIR=%~dp0
 
+%PECL_PHP_CMD% %BAT_DIR%\..\script\pecl_mail.php
+
 cd c:\pecl-in-pkg
 
 for /r %%i in (*) do (
-       call %BAT_DIR%pecl_build_all.bat --upload --mail --package=%%i
+       call %BAT_DIR%pecl_build_all.bat --upload --aggregate-mail --package=%%i
        del %%i
        goto ONLY_ONE   
 )
diff --git a/client/include/PeclExt.php b/client/include/PeclExt.php
index 5dc34fe..2ac5155 100644
--- a/client/include/PeclExt.php
+++ b/client/include/PeclExt.php
@@ -696,29 +696,39 @@ if (!function_exists('rmtools\combinations')) {
                return $to;
        }
 
-       public function mailMaintainers($success, $is_snap, array $logs, 
$force_email = NULL)
+       public function mailMaintainers($success, $is_snap, array $logs, 
PeclMail $mailer, $force_email = NULL)
        {
                $seg = $is_snap ? 'snaps' : 'releases';
                $url = 'http://windows.php.net/downloads/pecl/' . $seg . '/' . 
$this->name . '/' . $this->version;
 
-               if ($success) {
-                       $msg = "PECL Windows build for " . 
$this->getPackageName() . " succeeded\n\n";
-                       /* $msg .= "The package was uploaded to $url/" . 
$this->getPackageName() . ".zip\n\n";*/
-                       $msg .= "The package was uploaded to $url/\n\n";
+               if ($mailer->isAggregated()) {
+                       /* NOTE we're not able to send all the build logs in an 
aggregated mail right now.
+                          But that's fine, they are uploaded anyway. */
+                       if ($success) {
+                               $msg = $this->getPackageName() . " 
succeeded\n\n";
+                       } else {
+                               $msg = $this->getPackageName() . " failed\n\n";
+                       }
                } else {
-                       $msg = "PECL Windows build for " . 
$this->getPackageName() . " failed\n\n";
-               }
-               /* No need to link the logs as we attach them. */
-               /* $msg .= "The logs was uploaded to $url/logs/" . 
$this->getPackageName() . "-logs.zip\n\n"; */
-               if (!$success) {
-                       $msg .= "Please look into the logs for what's to be 
fixed. ";
-                       $msg .= "You can ask for help on pecl-...@lists.php.net 
or internals-...@lists.php.net. \n\n";
+                       if ($success) {
+                               $msg = "PECL Windows build for " . 
$this->getPackageName() . " succeeded\n\n";
+                               /* $msg .= "The package was uploaded to $url/" 
. $this->getPackageName() . ".zip\n\n";*/
+                               $msg .= "The package was uploaded to $url/\n\n";
+                       } else {
+                               $msg = "PECL Windows build for " . 
$this->getPackageName() . " failed\n\n";
+                       }
+                       /* No need to link the logs as we attach them. */
+                       /* $msg .= "The logs was uploaded to $url/logs/" . 
$this->getPackageName() . "-logs.zip\n\n"; */
+                       if (!$success) {
+                               $msg .= "Please look into the logs for what's 
to be fixed. ";
+                               $msg .= "You can ask for help on 
pecl-...@lists.php.net or internals-...@lists.php.net. \n\n";
+                       }
+                       $msg .= "Have a nice day :)\n";
                }
-               $msg .= "Have a nice day :)\n";
 
                $to = $force_email ? $force_email : $this->getToEmail();
 
-               return xmail(
+               return $mailer->xmail(
                        MAIL_FROM,
                        $to,
                        '[PECL-DEV] Windows build: ' . $this->getPackageName(),
diff --git a/client/include/PeclMail.php b/client/include/PeclMail.php
new file mode 100644
index 0000000..a5491e0
--- /dev/null
+++ b/client/include/PeclMail.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace rmtools;
+
+class PeclMail {
+       protected $buffer_file;
+       protected $aggregate;
+       protected $aggregated_sent = false;
+       protected $buffer = array (
+               'from' => array(),
+               'to' => array(),
+               'subject' => '',
+               'text' => '',
+               'attachment' => array()
+       );
+
+       public function __construct($pkg_path, $aggregate = false)
+       {
+               if (!file_exists($pkg_path)) {
+                       throw new \Exception("'$pkg_path' does not exist");
+               } 
+
+               $this->buffer_file = TMP_DIR . DIRECTORY_SEPARATOR . 
md5($pkg_path);
+               $this->aggregate = (boolean)$aggregate;
+
+               if ($this->aggregate && file_exists($this->buffer_file)) {
+                       $tmp = file_get_contents($this->buffer_file);
+                       $this->buffer = unserialize($tmp);
+               }
+       }
+
+       public function __destruct()
+       {
+               if (!$this->aggregated_sent) {
+                       $this->saveState();
+               }
+       }
+
+       public function saveState()
+       {
+               $tmp = serialize($this->buffer);
+               $ret = file_put_contents($this->buffer_file, $tmp);
+       }
+
+       public function cleanup()
+       {
+               unlink($this->buffer_file);
+       }
+
+       public function isAggregated()
+       {
+               return $this->aggregate;
+       }
+
+       /* XXX the state with from, to and subject is somehow unclean */
+       public function xmail($from, $to, $subject, $text, array $attachment = 
array())
+       {
+               if (!$this->aggregate) {
+                       return xmail($from, $to, $subject, $text, $attachment);
+               }
+
+               $this->buffer['to'][] = $to;
+               $this->buffer['from'][] = $from;
+               /* subject can't be aggregated anyway */
+               $this->buffer['subject'] = $subject;
+               $this->buffer['text'] = $this->buffer['text'] ."\n\n" . $text;
+               $this->buffer['attachment'] = array_merge($attachment, 
$this->buffer['attachment']);
+       }
+
+       public function mailAggregated($from, $to, $subject, $open, $close)
+       {
+               if (!$to) {
+                       $to = implode(',', array_unique($this->buffer['to']));
+               }
+               if (!$from) {
+                       $from = implode(',', 
array_unique($this->buffer['from']));
+               }
+               $text = "$open\n\n" . $this->buffer['text'] . "\n\n$close";
+
+               $this->aggregated_sent = true;
+               
+               return xmail($from, $to, $subject, $text, 
$this->buffer['attachment']);
+       }
+
+       public function setFrom($from)
+       {
+               $this->buffer['from'] = $from;
+       }
+
+       public function setTo($to)
+       {
+               $this->buffer['to'] = $to;
+       }
+
+       public function setSubject($subject)
+       {
+               $this->buffer['subject'] = $subject;
+       }
+
+}
diff --git a/client/script/pecl.php b/client/script/pecl.php
index 4a68c21..3b98a97 100644
--- a/client/script/pecl.php
+++ b/client/script/pecl.php
@@ -4,12 +4,13 @@ include __DIR__ . '/../include/PeclBranch.php';
 include __DIR__ . '/../include/Tools.php';
 include __DIR__ . '/../include/PeclExt.php';
 include __DIR__ . '/../include/PeclDb.php';
+include __DIR__ . '/../include/PeclMail.php';
 
 use rmtools as rm;
 
 
 $shortopts = NULL; //"c:p:mu";
-$longopts = array("config:", "package:", "mail", "upload", "is-snap", 
"force-name:", "force-version:", "force-email:");
+$longopts = array("config:", "package:", "mail", "aggregate-mail", "upload", 
"is-snap", "first", "last", "force-name:", "force-version:", "force-email:",);
 
 $options = getopt($shortopts, $longopts);
 
@@ -21,19 +22,41 @@ $is_snap = isset($options['is-snap']);
 $force_name = isset($options['force-name']) ? $options['force-name'] : NULL;
 $force_version = isset($options['force-version']) ? $options['force-version'] 
: NULL;
 $force_email = isset($options['force-email']) ? $options['force-email'] : NULL;
+$aggregate_mail = isset($options['aggregate-mail']);
+$is_last_run = isset($options['last']);
+$is_first_run = isset($options['first']);
+
+$mail_maintainers = $mail_maintainers || $aggregate_mail;
 
 if (NULL == $branch_name || NULL == $pkg_path) {
        echo "Usage: pecl.php [OPTION] ..." . PHP_EOL;
        echo "  --config         Configuration file name without suffix, 
required." . PHP_EOL;
        echo "  --package        Path to the PECL package, required." . PHP_EOL;
        echo "  --mail           Send build logs to the extension maintainers, 
optional." . PHP_EOL;
+       echo "  --aggregate-mail Save data so it can be sent to extension 
maintainers aggregated, optional." . PHP_EOL;
        echo "  --upload         Upload the builds to the windows.php.net, 
optional." . PHP_EOL;
        echo "  --is-snap        We upload to releases by default, but this one 
goes to snaps, optional." . PHP_EOL;
        echo "  --force-name     Force this name instead of reading the package 
data, optional." . PHP_EOL;
        echo "  --force-version  Force this version instead of reading the 
package data, optional." . PHP_EOL;
        echo "  --force-email    Send the results to this email instead of any 
from package.xml, optional." . PHP_EOL;
+       echo "  --first          This call is the first in the series for the 
same package file, optional." . PHP_EOL;
+       echo "  --last           This call is the last in the series for the 
same package file, optional." . PHP_EOL;
+       echo PHP_EOL;
+       echo "Examples: " . PHP_EOL;
+       echo PHP_EOL;
+       echo "Just build, binaries and logs will stay in TMP_DIR" . PHP_EOL;
+       echo "pecl --config=php55_x64 --package=c:\pecl_in_pkg\some-1.0.0.tgz" 
. PHP_EOL;
+       echo PHP_EOL;
+       echo "Build and upload to windows.php.net/pecl/releases/some/1.0.0/" . 
PHP_EOL;
+       echo "pecl --config=php55_x64 --upload 
--package=c:\pecl_in_pkg\some-1.0.0.tgz" . PHP_EOL;
+       echo PHP_EOL;
+       echo "Build, upload and mail results after each build" . PHP_EOL;
+       echo "pecl --config=php55_x64 --upload 
--package=c:\pecl_in_pkg\some-1.0.0.tgz" . PHP_EOL;
        echo PHP_EOL;
-       echo "Example: pecl --config=php55_x64 
--package=c:\pecl_in_pkg\some-1.0.0.tgz" . PHP_EOL;
+       echo "Build, upload and send an aggregated mail over both build runs" . 
PHP_EOL;
+       echo "pecl --config=php54 --upload --aggregate-mail 
--package=c:\pecl_in_pkg\some-1.0.0.tgz --first" . PHP_EOL;
+       echo "pecl --config=php55_x64 --upload --aggregate-mail 
--package=c:\pecl_in_pkg\some-1.0.0.tgz" . PHP_EOL;
+       echo "pecl --config=php55_x86 --upload --aggregate-mail 
--package=c:\pecl_in_pkg\some-1.0.0.tgz --last" . PHP_EOL;
        echo PHP_EOL;
        exit(0);
 }
@@ -47,6 +70,20 @@ $branch = new rm\PeclBranch($config_path);
 
 $branch_name = $branch->config->getName();
 
+/* Init things if --first was given */
+if ($is_first_run) {
+       echo "First invocation for <$pkg_path> started." . PHP_EOL . PHP_EOL;
+
+       try {
+               /* Not sure it's needed anymore, but let it persist */
+               $mailer = new rm\PeclMail($pkg_path, $aggregate_mail);
+       } catch (Exception $e) {
+               echo 'Error: ' . $e->getMessage() . PHP_EOL;
+               $was_errors = true;
+       }
+}
+
+
 echo PHP_EOL;
 echo "Run started for <" . realpath($config_path) . ">" . PHP_EOL;
 echo "Branch <$branch_name>" . PHP_EOL;
@@ -82,8 +119,22 @@ foreach ($builds as $build_name) {
                }
                
                $build->setSourceDir($build_src_path);
+       } catch (Exception $e) {
+               echo 'Error: ' . $e->getMessage() . PHP_EOL;
+               $build->clean();
+               $was_errors = true;
 
+               unset($build);
+
+               /* no sense to continue as something in ext setup went wrong */
+               /* XXX maibe a mail should be sent to ostc or alike */
+               continue;
+       }
+
+
+       try {
                $ext = new rm\PeclExt($pkg_path, $build);
+               $mailer = new rm\PeclMail($pkg_path, $aggregate_mail);
        } catch (Exception $e) {
                echo 'Error: ' . $e->getMessage() . PHP_EOL;
 
@@ -92,7 +143,7 @@ foreach ($builds as $build_name) {
 
                        echo "Mailing info to <$maintainer_mailto>" . PHP_EOL;
 
-                       rm\xmail(
+                       xmail(
                                MAIL_FROM,
                                /* no chance to have the maintainers mailto at 
this stage */
                                $maintainer_mailto,
@@ -277,7 +328,7 @@ foreach ($builds as $build_name) {
 
                        echo "Mailing logs to <$maintainer_mailto>" . PHP_EOL;
 
-                       $res = $ext->mailMaintainers(0 == $build_error, 
$is_snap, array($logs_zip), $maintainer_mailto);
+                       $res = $ext->mailMaintainers(0 == $build_error, 
$is_snap, array($logs_zip), $mailer, $maintainer_mailto);
                        if (!$res) {
                                throw new \Exception("Mail operation failed");
                        }
@@ -315,5 +366,25 @@ Coventry:
 
 echo "Run finished." . PHP_EOL . PHP_EOL;
 
+/* Cleanup things if --last was given */
+if ($is_last_run) {
+       echo "Last invocation for <$pkg_path> finished." . PHP_EOL . PHP_EOL;
+       try {
+               $mailer = new rm\PeclMail($pkg_path, $aggregate_mail);
+
+               $mailer->mailAggredated(
+                       NULL,
+                       NULL,
+                       '[PECL-DEV] Windows build: ' . basename($pkg_path),
+                       'Hi,',
+                       'Have a nice day'
+               );
+               $mailer->cleanup();
+       } catch (Exception $e) {
+               echo 'Error: ' . $e->getMessage() . PHP_EOL;
+               $was_errors = true;
+       }
+}
+
 exit((int)$was_errors);
-- 
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to