[svnbook] r6039 committed - www/bin/build-and-deploy.py

cmpilato at users.sourceforge.net cmpilato at users.sourceforge.net
Mon Aug 16 20:57:39 UTC 2021


Revision: 6039
          http://sourceforge.net/p/svnbook/source/6039
Author:   cmpilato
Date:     2021-08-16 20:57:39 +0000 (Mon, 16 Aug 2021)
Log Message:
-----------
Convert the nightly build script to Python 3.

Modified Paths:
--------------
    www/bin/build-and-deploy.py

Modified: www/bin/build-and-deploy.py
===================================================================
--- www/bin/build-and-deploy.py	2021-02-16 06:37:36 UTC (rev 6038)
+++ www/bin/build-and-deploy.py	2021-08-16 20:57:39 UTC (rev 6039)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # vim:sw=4
 
 # ----------------------------------------------------------------------
@@ -15,9 +15,10 @@
 import getopt
 import shutil
 import time
-import subprocess
 import traceback
-import ConfigParser
+import configparser
+from email.mime.text import MIMEText
+from subprocess import Popen, PIPE, STDOUT
 
 # ----------------------------------------------------------------------
 # The configuration file has a [general] section, then a set of build
@@ -121,24 +122,14 @@
     SUBJECT and BODY."""
 
     assert mail_from and mail_to and mail_from_name
+    msg = MIMEText(body)
+    msg["From"] = f"{mail_from_name} <{mail_from}>"
+    msg["To"] = mail_to
+    msg["Subject"] = subject
     try:
-        p = subprocess.Popen(['/usr/sbin/sendmail', '-f', mail_from, mail_to],
-                             stdin=subprocess.PIPE,
-                             stdout=subprocess.PIPE,
-                             stderr=subprocess.STDOUT)
-        p.stdin.write("Subject: %s\n" % subject)
-        p.stdin.write("To: %s\n" % mail_to)
-        p.stdin.write("From: %s <%s>\n" % (mail_from_name, mail_from))
-        p.stdin.write("\n")
-        p.stdin.write(body)
-        p.stdin.close()
-        output = p.stdout.read()
-        status = p.wait()
-        if len(output) or status:
-            sys.stderr.write("MTA output when sending email (%s):\n" % subject)
-            sys.stderr.write(output)
-            sys.stderr.write("Exit status %d\n" % status)
-    except IOError:
+        p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
+        p.communicate(msg.as_bytes())
+    except Exception:
         etype, value, tb = sys.exc_info()
         sys.stderr.write("Failed sending email (%s):\n" % subject)
         traceback.print_exception(etype, value, tb)
@@ -245,7 +236,7 @@
                               lines[j][end_offset + 1:])
                 open(file, 'w').writelines(lines)
                 return
-    raise Exception, "Never found <body> tag in file '%s'" % (file)
+    raise Exception("Never found <body> tag in file '%s'" % (file))
 
 
 def add_adsense_bottom_html(file):
@@ -260,7 +251,7 @@
                       lines[i][start_offset:])
         open(file, 'w').writelines(lines)
         return
-    raise Exception, "Never found <div class=\"nav_footer\"> tag in file '%s'" % (file)
+    raise Exception("Never found <div class=\"nav_footer\"> tag in file '%s'" % (file))
 
 
 def add_analytics_bug(file):
@@ -275,7 +266,7 @@
                       lines[i][start_offset:])
         open(file, 'w').writelines(lines)
         return
-    raise Exception, "Never found </body> tag in file '%s'" % (file)
+    raise Exception("Never found </body> tag in file '%s'" % (file))
 
 
 def add_adsense_css(file):
@@ -284,7 +275,7 @@
 
 def adsensify(dst_path, dry_run, verbose):
     if verbose:
-        print "Adding AdSense bits in '%s'" % (dst_path)
+        print("Adding AdSense bits in '%s'" % (dst_path))
     if dry_run:
         return
     stylesheet = os.path.join(dst_path, 'styles.css')
@@ -315,13 +306,13 @@
     temp_dir = os.path.join(src_path, '__TEMPINSTALL__')
     
     if verbose:
-        print "Build requested:"
-        print "   Locale = %s" % (locale)
-        print "   Version = %s" % (version)
-        print "   Source Path = %s" % (src_path)
-        print "   Destination Path = %s" % (dst_path)
-        print "   Formats = %s" % (str(formats))
-        print "   Temporary Directory = %s" % (temp_dir)
+        print("Build requested:")
+        print("   Locale = %s" % (locale))
+        print("   Version = %s" % (version))
+        print("   Source Path = %s" % (src_path))
+        print("   Destination Path = %s" % (dst_path))
+        print("   Formats = %s" % (str(formats)))
+        print("   Temporary Directory = %s" % (temp_dir))
         
     # Extend FORMATS to include relevant archived formats, too.
     if 'html' in formats:
@@ -332,7 +323,7 @@
     # Remove the temporary directory if it already exists.
     if os.path.isdir(temp_dir):
         if verbose:
-            print "Erase: %s" % (temp_dir)
+            print("Erase: %s" % (temp_dir))
         if not dry_run:
             shutil.rmtree(temp_dir)
 
@@ -339,12 +330,12 @@
     # Create the destination directory (and parents) as necessary.
     if not os.path.isdir(dst_path):
         if verbose:
-            print "Create (with parents): %s" % (dst_path)
+            print("Create (with parents): %s" % (dst_path))
         if not dry_run:
             os.makedirs(dst_path)
 
     make_cmd = (['make', 'INSTALL_SUBDIR=__TEMPINSTALL__', 'clean', 'valid']
-                + map(lambda x: 'install-%s' % x, formats))
+                + ['install-%s' % x for x in formats])
     build_log = os.path.join(dst_path, BUILD_LOG_FILENAME)
     log_fp = open(build_log, 'w', 1)
     log_fp.write("::: %s :::\n" % time.asctime())
@@ -354,19 +345,18 @@
     os.chdir(src_path)
     try:
         if verbose:
-            print "Change directory: %s" % (src_path)
+            print("Change directory: %s" % (src_path))
         if verbose:
-            print "Building: %s" % (' '.join(make_cmd))
+            print("Building: %s" % (' '.join(make_cmd)))
         if not dry_run:
-            p = subprocess.Popen(make_cmd,
-                                 stdout=subprocess.PIPE,
-                                 stderr=subprocess.STDOUT)
+            p = Popen(make_cmd, stdout=PIPE, stderr=STDOUT,
+                      universal_newlines=True)
             while 1:
                 data = p.stdout.readline()
                 if not data:
                     break
                 if verbose:
-                    print data.rstrip("\n")
+                    print(data.rstrip("\n"))
                 log_fp.write(data)
             exitcode = p.wait()
             if exitcode:
@@ -376,7 +366,7 @@
     finally:
         log_fp.close()
         if verbose:
-            print "Change directory: %s" % (cwd)
+            print("Change directory: %s" % (cwd))
         os.chdir(cwd)
 
     # Move stuff into place, deleting old stuff first.
@@ -383,16 +373,16 @@
     build_log = os.path.join(dst_path, BUILD_LOG_FILENAME)
     temp_build_log = os.path.join(temp_dir, BUILD_LOG_FILENAME)
     if verbose:
-        print "Rename: %s -> %s" % (build_log, temp_build_log)
+        print("Rename: %s -> %s" % (build_log, temp_build_log))
     if not dry_run:
         os.rename(build_log, temp_build_log)
     if os.path.isdir(dst_path):
         if verbose:
-            print "Erase: %s" % (dst_path)
+            print("Erase: %s" % (dst_path))
         if not dry_run:
             shutil.rmtree(dst_path)
     if verbose:
-        print "Move into place: %s -> %s" % (temp_dir, dst_path)
+        print("Move into place: %s -> %s" % (temp_dir, dst_path))
     if not dry_run:
         os.rename(temp_dir, dst_path)
 
@@ -414,7 +404,7 @@
     
     if not value:
         return []
-    return map(lambda x: x.strip(), value.split(','))
+    return [x.strip() for x in value.split(',')]
 
 
 def main(conf_file, dry_run, verbose):
@@ -422,7 +412,7 @@
     DRY_RUN is set, don't really do anything.  If VERBOSE is set, be
     verbose."""
     
-    cfg = ConfigParser.RawConfigParser()
+    cfg = configparser.RawConfigParser()
     cfg.read(conf_file)
     build_sections = []
     mail_to = get_option(cfg, 'general', 'report_target')
@@ -439,7 +429,7 @@
 
     # Update the working copy
     if verbose:
-        print "SVN Update: ."
+        print("SVN Update: .")
     if not dry_run:
         if verbose:
             os.system('svn up')
@@ -475,11 +465,11 @@
                 raise Exception("AdSense integration has been discontinued.")
                 adsensify(dst_path, dry_run, verbose)
             build_end_time = time.time()
-        except Exception, e:
+        except Exception as e:
             if verbose:
-                print "Build error occurred: %s" % (str(e))
+                print("Build error occurred: %s" % (str(e)))
             if dry_run:
-                print "Send failure email: %s" % (locale)
+                print("Send failure email: %s" % (locale))
             else:
                 body = "The svnbook build for the '%s' locale has failed.\n" \
                        "Please investigate.\n\n%s\n" \
@@ -518,7 +508,7 @@
     try:
         opts, args = getopt.getopt(sys.argv[1:], "hnv",
                                    ["help", "dry-run", "verbose"])
-    except getopt.GetoptError, e:
+    except getopt.GetoptError as e:
         usage_and_exit(str(e))
     for option, value in opts:
         if option in ["-h", "--help"]:




More information about the svnbook-dev mailing list