Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78677 - in website/public_html/beta/site-tools: . boost_site
From: dnljms_at_[hidden]
Date: 2012-05-27 08:48:45


Author: danieljames
Date: 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
New Revision: 78677
URL: http://svn.boost.org/trac/boost/changeset/78677

Log:
Website: Try to support python 3.

This is pretty messy, seems to work okay.
Text files modified:
   website/public_html/beta/site-tools/boost_site/boostbook_parser.py | 4 +-
   website/public_html/beta/site-tools/boost_site/pages.py | 25 ++++++++++--------
   website/public_html/beta/site-tools/boost_site/site_tools.py | 14 +++++++--
   website/public_html/beta/site-tools/boost_site/state.py | 55 ++++++++++++++++++++++++----------------
   website/public_html/beta/site-tools/boost_site/upgrade.py | 4 +-
   website/public_html/beta/site-tools/boost_site/util.py | 31 ++++++++++++++++------
   website/public_html/beta/site-tools/release.py | 10 +++---
   7 files changed, 88 insertions(+), 55 deletions(-)

Modified: website/public_html/beta/site-tools/boost_site/boostbook_parser.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/boostbook_parser.py (original)
+++ website/public_html/beta/site-tools/boost_site/boostbook_parser.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -19,7 +19,7 @@
 
         article_node = article.documentElement
         if article_node.nodeName != 'article':
- print "Boostbook file not article:", filename
+ print("Boostbook file not article:", filename)
             return
 
         id = ''
@@ -207,7 +207,7 @@
     
     def new_node( self, tag, *child, **kwargs ):
         result = self.document.createElement(tag)
- for k in kwargs.keys():
+ for k in list(kwargs.keys()):
             if kwargs[k] != '':
                 if k == 'id':
                     result.setAttribute('id',kwargs[k])

Modified: website/public_html/beta/site-tools/boost_site/pages.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/pages.py (original)
+++ website/public_html/beta/site-tools/boost_site/pages.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -4,7 +4,7 @@
 # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 
 import boost_site.state, boost_site.util
-import os, hashlib, xml.dom.minidom, re, fnmatch, subprocess, tempfile, time
+import os, sys, hashlib, xml.dom.minidom, re, fnmatch, subprocess, tempfile, time
 
 class Pages:
     """ Tracks which items in an rss feed have been updated.
@@ -36,10 +36,13 @@
         boost_site.state.save(save_hashes, self.hash_file)
 
     def add_qbk_file(self, qbk_file, location, page_data):
- file = open(qbk_file)
- try:
+ if sys.version_info < (3,0):
+ file = open(qbk_file, 'r')
             qbk_hash = hashlib.sha256(file.read()).hexdigest()
- finally:
+ file.close()
+ else:
+ file = open(qbk_file, 'rb')
+ qbk_hash = hashlib.sha256(file.read().replace(bytes([13,10]), bytes([10]))).hexdigest()
             file.close()
 
         record = None
@@ -83,8 +86,8 @@
                         'last_modified': self.pages[self.rss_hashes[hashed]].last_modified
                     }
                 else:
- print "Unable to find quickbook file for rss item:"
- print hashed
+ print("Unable to find quickbook file for rss item:")
+ print(hashed)
 
         return rss_items
 
@@ -95,7 +98,7 @@
         try:
             subprocess.check_call(['quickbook', '--version'])
         except:
- print "Problem running quickbook, will not convert quickbook articles."
+ print("Problem running quickbook, will not convert quickbook articles.")
             return
         
         bb_parser = boost_site.boostbook_parser.BoostBookParser()
@@ -107,7 +110,7 @@
                 os.close(xml_file[0])
                 xml_filename = xml_file[1]
                 try:
- print "Converting " + page + ":"
+ print("Converting " + page + ":")
                     subprocess.check_call(['quickbook', '--output-file', xml_filename, '-I', 'feed', page])
                     page_data.load(bb_parser.parse(xml_filename), refresh)
                 finally:
@@ -158,7 +161,7 @@
         for pattern in patterns:
             pattern_parts = pattern.split('|')
             matches = [x for x in
- fnmatch.filter(self.pages.keys(), pattern_parts[0])
+ fnmatch.filter(list(self.pages.keys()), pattern_parts[0])
                 if self.pages[x].is_published(pattern_parts[1:])]
             filtered = filtered | set(matches)
 
@@ -209,7 +212,7 @@
                 self.release_status = 'dev'
             status_parts = self.release_status.split(' ', 2)
             if status_parts[0] not in ['released', 'beta', 'dev']:
- print "Error: Unknown release status: " + self.release_status
+ print("Error: Unknown release status: " + self.release_status)
                 self.release_status = None
             if self.release_status:
                 self.flags.add(status_parts[0])
@@ -361,7 +364,7 @@
 
 def number_suffix(x):
     x = x % 100
- if x / 10 == 1:
+ if x // 10 == 1:
         return "th"
     else:
         return ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"][x % 10]

Modified: website/public_html/beta/site-tools/boost_site/site_tools.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/site_tools.py (original)
+++ website/public_html/beta/site-tools/boost_site/site_tools.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -86,9 +86,9 @@
                         rss_feed.importNode(
                             old_rss_items[qbk_page.qbk_file]['item'], True))
                 else:
- print "Missing entry for %s" % qbk_page.qbk_file
+ print("Missing entry for %s" % qbk_page.qbk_file)
                     
- output_file = open(feed_file, 'w')
+ output_file = open(feed_file, 'wb')
             try:
                 output_file.write(rss_feed.toxml('utf-8'))
             finally:
@@ -109,6 +109,12 @@
 ################################################################################
 
 def generate_rss_feed(feed_file, details):
+ title = details['title']
+ link = "http://www.boost.org/" + details['link']
+ if sys.version_info < (3, 0):
+ title = title.encode('utf-8')
+ link = link.encode('utf-8')
+
     rss = xml.dom.minidom.parseString('''<?xml version="1.0" encoding="UTF-8"?>
 <rss version="2.0" xmlns:boostbook="urn:boost.org:boostbook">
   <channel>
@@ -121,8 +127,8 @@
   </channel>
 </rss>
 ''' % {
- 'title' : details['title'].encode('utf-8'),
- 'link' : "http://www.boost.org/" + details['link'],
+ 'title' : title,
+ 'link' : link,
     'description' : '',
     'language' : 'en-us',
     'copyright' : 'Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)'

Modified: website/public_html/beta/site-tools/boost_site/state.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/state.py (original)
+++ website/public_html/beta/site-tools/boost_site/state.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -3,9 +3,9 @@
 # Distributed under the Boost Software License, Version 1.0.
 # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 
-import os
+import os, sys
 
-class StateParseError:
+class StateParseError(BaseException):
     None
 
 def load(file_path):
@@ -64,9 +64,15 @@
                         if(not key): raise StateParseError()
                         if(type == 'None'):
                             type = 'String'
- value = file.readline().decode('utf-8')
+ if sys.version_info < (3, 0):
+ value = file.readline().decode('utf-8')
+ else:
+ value = file.readline()
                         elif(type == 'String'):
- value = value + file.readline().decode('utf-8')
+ if sys.version_info < (3, 0):
+ value = value + file.readline().decode('utf-8')
+ else:
+ value = value + file.readline()
                         else:
                             raise StateParseError()
                     else:
@@ -78,36 +84,41 @@
     return state
 
 def save(state, file_path):
- file = open(file_path, "w")
+ file = open(file_path, "wb")
     try:
         for record_key in sorted(state.keys()):
             record = state[record_key]
 
- file.write("(")
- file.write(record_key)
- file.write("\n")
+ write(file, "(")
+ write(file, record_key)
+ write(file, "\n")
 
             for key in sorted(record.keys()):
- file.write("-")
- file.write(key)
- file.write("\n")
+ write(file, "-")
+ write(file, key)
+ write(file, "\n")
 
                 if record[key] is not None:
- if isinstance(record[key], basestring):
- file.write('"')
- file.write(record[key].replace("\n", "\n\"").encode('utf-8'))
- file.write("\n")
+ if isinstance(record[key], str) or \
+ (sys.version_info < (3,0) and isinstance(record[key], unicode)):
+ write(file, '"')
+ write(file, record[key].replace("\n", "\n\""))
+ write(file, "\n")
                     elif isinstance(record[key], bool):
- file.write('!')
- file.write(str(record[key]))
- file.write("\n")
+ write(file, '!')
+ write(file, str(record[key]))
+ write(file, "\n")
                     elif isinstance(record[key], (int, float)):
- file.write('.')
- file.write(str(record[key]))
- file.write("\n")
+ write(file, '.')
+ write(file, str(record[key]))
+ write(file, "\n")
                     else:
+ print(type(record[key]))
                         assert False
 
- file.write(")\n")
+ write(file, ")\n")
     finally:
         file.close()
+
+def write(file, str):
+ file.write(str.encode('utf-8'))
\ No newline at end of file

Modified: website/public_html/beta/site-tools/boost_site/upgrade.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/upgrade.py (original)
+++ website/public_html/beta/site-tools/boost_site/upgrade.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -66,10 +66,10 @@
     version = Version()
 
     if(version.version < len(versions)):
- print "Upgrading to new version."
+ print("Upgrading to new version.")
 
         for v in range(version.version, len(versions)):
- print "Upgrade", v + 1
+ print("Upgrade " + (v + 1))
             versions[v]()
             version.version = v + 1
             version.save()

Modified: website/public_html/beta/site-tools/boost_site/util.py
==============================================================================
--- website/public_html/beta/site-tools/boost_site/util.py (original)
+++ website/public_html/beta/site-tools/boost_site/util.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -3,7 +3,12 @@
 # Distributed under the Boost Software License, Version 1.0.
 # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 
-import urlparse, re, string
+import sys, re, string
+
+try:
+ from urllib.parse import urljoin
+except ImportError:
+ from urlparse import urljoin
 
 def htmlencode(text):
     return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&rt;')
@@ -16,11 +21,10 @@
     http://bugs.python.org/issue9883
     """
     x = ''.join(x.toxml('utf-8').decode('utf-8') for x in fragment.childNodes)
- x = re.sub(r' +$', '', x, flags = re.M)
- return x
+ return re.compile(r' +$', flags = re.M).sub('', x)
 
 def base_links(node, base_link):
- transform_links(node, lambda x: urlparse.urljoin(base_link,x))
+ transform_links(node, lambda x: urljoin(base_link, x))
 
 def transform_links(node, func):
     transform_links_impl(node, 'a', 'href', func)
@@ -37,18 +41,27 @@
 
 def write_template(dst_path, template_path, data):
     file = open(template_path)
- s = string.Template(file.read().decode('utf-8'))
+ if sys.version_info < (3, 0):
+ s = string.Template(file.read().decode('utf-8'))
+ else:
+ s = string.Template(file.read())
     output = s.substitute(data)
- output = re.sub(r' +$', '', output, flags = re.M)
- out = open(dst_path, "w")
- out.write(output.encode('utf-8'))
+ output = re.compile(r' +$', flags = re.M).sub('', output)
+ out = open(dst_path, 'w')
+ if sys.version_info < (3, 0):
+ out.write(output.encode('utf-8'))
+ else:
+ out.write(output)
 
 def write_py_template(dst_path, template_path, data):
     data['emit'] = Emitter()
     exec(open(template_path).read(), {}, data)
 
     out = open(dst_path, 'w')
- out.write(data['emit'].output.encode('utf-8'))
+ if sys.version_info < (3, 0):
+ out.write(data['emit'].output.encode('utf-8'))
+ else:
+ out.write(data['emit'].output)
 
 class Emitter:
     def __init__(self):

Modified: website/public_html/beta/site-tools/release.py
==============================================================================
--- website/public_html/beta/site-tools/release.py (original)
+++ website/public_html/beta/site-tools/release.py 2012-05-27 08:48:44 EDT (Sun, 27 May 2012)
@@ -12,24 +12,24 @@
 import boost_site.site_tools
 
 if len(sys.argv) != 2:
- print __doc__
+ print(__doc__)
     exit(1)
 
 # Because I'm an idiot, we need to get the real path to the quickbook file
 # before calling init.
 
 if not os.path.isfile(sys.argv[1]):
- print "Unable to find release notes at:", sys.argv[1]
+ print("Unable to find release notes at: " + sys.argv[1])
     exit(1)
 
 release_notes = os.path.realpath(sys.argv[1])
 
 boost_site.site_tools.init()
 
-# Note: Not using os.path.relpath because I want to support Python 2.5
+# Note: Not using os.path.realpath because I want to support Python 2.5
 cwd = os.getcwd()
 if not release_notes.startswith(cwd):
- print "Release notes aren't in current site:", sys.argv[1]
+ print("Release notes aren't in current site: " + sys.argv[1])
     exit(1)
 
 release_notes = release_notes[len(cwd):].lstrip('/')
@@ -39,7 +39,7 @@
 
 # Flag the released page
 if release_notes not in pages.pages:
- print "Unable to find page:", release_notes
+ print("Unable to find page: " + release_notes)
     exit(1)
 pages.pages[release_notes].flags.add('released')
 pages.save()


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk