66
77"""
88from xml .dom import minidom
9- import pprint
10- import sys
119import os
1210import re
1311import hashlib
14- import urllib2
12+ import requests
1513import shutil
1614import begin
1715
@@ -621,18 +619,18 @@ def preprocess(xidl, target):
621619 emit = True
622620 for line in xidl .splitlines ():
623621 line = line .strip ()
624- if line .startswith ('<if target=' ):
622+ if line .startswith (b '<if target=' ):
625623 if target in line :
626624 emit = True
627625 else :
628626 emit = False
629627 continue
630- elif line == '</if>' :
628+ elif line == b '</if>' :
631629 emit = True
632630 continue
633631 if emit :
634632 lines .append (line )
635- return "\n " .join (lines )
633+ return b "\n " .join (lines )
636634
637635
638636###########################################################
@@ -645,13 +643,13 @@ def get_vbox_version(config_kmk):
645643 "Return the vbox config major, minor, build"
646644 with open (config_kmk , 'rb' ) as f :
647645 config = f .read ()
648- major = re .search ("VBOX_VERSION_MAJOR = (?P<major>[\d])" ,
646+ major = re .search (b "VBOX_VERSION_MAJOR = (?P<major>[\d])" ,
649647 config ).groupdict ()['major' ]
650- minor = re .search ("VBOX_VERSION_MINOR = (?P<minor>[\d])" ,
648+ minor = re .search (b "VBOX_VERSION_MINOR = (?P<minor>[\d])" ,
651649 config ).groupdict ()['minor' ]
652- build = re .search ("VBOX_VERSION_BUILD = (?P<build>[\d])" ,
650+ build = re .search (b "VBOX_VERSION_BUILD = (?P<build>[\d])" ,
653651 config ).groupdict ()['build' ]
654- return "." .join ([major , minor , build ])
652+ return b "." .join ([major , minor , build ])
655653
656654def download_master (downloads ):
657655 print ("Download the master xidl" )
@@ -663,11 +661,11 @@ def download_master(downloads):
663661
664662def download_stable (downloads ):
665663 print ("Download latest tarball for stable release then unpack xidl" )
666- url = urllib2 .urlopen ('https://www.virtualbox.org/wiki/Downloads' )
667- page = url .read ()
664+ with requests .get ('https://www.virtualbox.org/wiki/Downloads' ) as r :
665+ page = r .content
666+
668667 match = re .search ("http://download.virtualbox.org/virtualbox/"
669- "([0-9\.]+)/VirtualBox-([0-9\.]+).tar.bz2"
670- , page )
668+ "([0-9\.]+)/VirtualBox-([0-9\.]+).tar.bz2" , page )
671669 if not match :
672670 raise Exception ("Failed to find source tarball url" )
673671 sourceurl = page [match .start ():match .end ()]
@@ -710,7 +708,7 @@ def main(virtualbox_xidl='VirtualBox.xidl',
710708
711709 print ("Create new virtualbox/library.py" )
712710 xidl = open (virtualbox_xidl , 'rb' ).read ()
713- xidl = preprocess (xidl , target = 'xpidl' )
711+ xidl = preprocess (xidl , target = b 'xpidl' )
714712
715713 xml = minidom .parseString (xidl )
716714
@@ -778,14 +776,14 @@ def main(virtualbox_xidl='VirtualBox.xidl',
778776 code .extend (source ['result' ])
779777 code .extend (source ['enum' ])
780778 code .extend (source ['interface' ])
781- code = "\n " .join (code )
779+ code = b "\n " .join ([ c . encode ( 'utf-8' ) if not isinstance ( c , bytes ) else c for c in code ] )
782780 print (" vbox version : %s" % vbox_version )
783781 print (" xidl hash : %s" % xidl_hash )
784782 print (" version : %s" % version )
785- print (" line count : %s" % code .count ("\n " ))
783+ print (" line count : %s" % code .count (b "\n " ))
786784 library_path = os .path .join ('.' , 'virtualbox' , 'library.py' )
787785 if os .path .exists (library_path ):
788786 os .unlink (library_path )
789- with open (library_path , 'w ' ) as f :
787+ with open (library_path , 'wb ' ) as f :
790788 f .write (code )
791789
0 commit comments