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
2422def pythonic_name (name ):
2523 s1 = re .sub ('(.)([A-Z][a-z]+)' , r'\1_\2' , name )
2624 name = re .sub ('([a-z0-9])([A-Z])' , r'\1_\2' , s1 ).lower ()
27- if hasattr (builtin , name ) is True or name in ['global' ]:
25+ if hasattr (builtin , name ) is True or name in ['global' , 'file' , 'apply' ]:
2826 name += "_p"
2927 return name
3028
3129
30+ def to_string (value ):
31+ if isinstance (value , str ):
32+ return value
33+ return value .decode ('utf-8' )
34+
35+
3236LIB_IMPORTS = """\
3337 # A Pythonic VirtalBox Main API
3438#
@@ -621,18 +625,18 @@ def preprocess(xidl, target):
621625 emit = True
622626 for line in xidl .splitlines ():
623627 line = line .strip ()
624- if line .startswith ('<if target=' ):
628+ if line .startswith (b '<if target=' ):
625629 if target in line :
626630 emit = True
627631 else :
628632 emit = False
629633 continue
630- elif line == '</if>' :
634+ elif line == b '</if>' :
631635 emit = True
632636 continue
633637 if emit :
634638 lines .append (line )
635- return "\n " .join (lines )
639+ return b "\n " .join (lines )
636640
637641
638642###########################################################
@@ -645,13 +649,13 @@ def get_vbox_version(config_kmk):
645649 "Return the vbox config major, minor, build"
646650 with open (config_kmk , 'rb' ) as f :
647651 config = f .read ()
648- major = re .search ("VBOX_VERSION_MAJOR = (?P<major>[\d])" ,
652+ major = re .search (b "VBOX_VERSION_MAJOR = (?P<major>[\d])" ,
649653 config ).groupdict ()['major' ]
650- minor = re .search ("VBOX_VERSION_MINOR = (?P<minor>[\d])" ,
654+ minor = re .search (b "VBOX_VERSION_MINOR = (?P<minor>[\d])" ,
651655 config ).groupdict ()['minor' ]
652- build = re .search ("VBOX_VERSION_BUILD = (?P<build>[\d])" ,
656+ build = re .search (b "VBOX_VERSION_BUILD = (?P<build>[\d])" ,
653657 config ).groupdict ()['build' ]
654- return "." .join ([major , minor , build ])
658+ return b "." .join ([major , minor , build ])
655659
656660def download_master (downloads ):
657661 print ("Download the master xidl" )
@@ -663,11 +667,11 @@ def download_master(downloads):
663667
664668def download_stable (downloads ):
665669 print ("Download latest tarball for stable release then unpack xidl" )
666- url = urllib2 .urlopen ('https://www.virtualbox.org/wiki/Downloads' )
667- page = url .read ()
670+ with requests .get ('https://www.virtualbox.org/wiki/Downloads' ) as r :
671+ page = r .content
672+
668673 match = re .search ("http://download.virtualbox.org/virtualbox/"
669- "([0-9\.]+)/VirtualBox-([0-9\.]+).tar.bz2"
670- , page )
674+ "([0-9\.]+)/VirtualBox-([0-9\.]+).tar.bz2" , page )
671675 if not match :
672676 raise Exception ("Failed to find source tarball url" )
673677 sourceurl = page [match .start ():match .end ()]
@@ -710,7 +714,7 @@ def main(virtualbox_xidl='VirtualBox.xidl',
710714
711715 print ("Create new virtualbox/library.py" )
712716 xidl = open (virtualbox_xidl , 'rb' ).read ()
713- xidl = preprocess (xidl , target = 'xpidl' )
717+ xidl = preprocess (xidl , target = b 'xpidl' )
714718
715719 xml = minidom .parseString (xidl )
716720
@@ -764,11 +768,11 @@ def main(virtualbox_xidl='VirtualBox.xidl',
764768 uuid = library .getAttribute ('uuid' )
765769 version = library .getAttribute ('version' )
766770 xidl_hash = hashlib .md5 (xidl ).hexdigest ()
767- lib_meta = LIB_META % dict (vbox_version = vbox_version ,
768- uuid = uuid ,
769- version = version ,
770- app_uuid = app_uuid ,
771- xidl_hash = xidl_hash )
771+ lib_meta = LIB_META % dict (vbox_version = to_string ( vbox_version ) ,
772+ uuid = to_string ( uuid ) ,
773+ version = to_string ( version ),
774+ app_uuid = to_string ( app_uuid ) ,
775+ xidl_hash = to_string ( xidl_hash ) )
772776
773777 code = []
774778 code .append (LIB_IMPORTS )
@@ -778,14 +782,14 @@ def main(virtualbox_xidl='VirtualBox.xidl',
778782 code .extend (source ['result' ])
779783 code .extend (source ['enum' ])
780784 code .extend (source ['interface' ])
781- code = "\n " .join (code )
782- print (" vbox version : %s" % vbox_version )
785+ code = b "\n " .join ([ c . encode ( 'utf-8' ) if not isinstance ( c , bytes ) else c for c in code ] )
786+ print (" vbox version : %s" % to_string ( vbox_version ) )
783787 print (" xidl hash : %s" % xidl_hash )
784788 print (" version : %s" % version )
785- print (" line count : %s" % code .count ("\n " ))
789+ print (" line count : %s" % code .count (b "\n " ))
786790 library_path = os .path .join ('.' , 'virtualbox' , 'library.py' )
787791 if os .path .exists (library_path ):
788792 os .unlink (library_path )
789- with open (library_path , 'w ' ) as f :
793+ with open (library_path , 'wb ' ) as f :
790794 f .write (code )
791795
0 commit comments