Build script fixes to work on both python 2 and 3

This commit is contained in:
Kevin 2013-04-22 02:08:51 -07:00
parent 60a65d227a
commit 31d6855535

View File

@ -9,10 +9,13 @@ import sys
import os import os
import shutil import shutil
import json import json
import urllib2
import shelve import shelve
import hashlib import hashlib
try:
import urllib2
except ImportError:
import urllib.request as urllib2
# load settings file # load settings file
from buildsettings import buildSettings from buildsettings import buildSettings
@ -72,23 +75,25 @@ def loaderRaw(var):
def loaderMD(var): def loaderMD(var):
fn = var.group(1) fn = var.group(1)
db = shelve.open('build/MD.dat') # use different MD.dat's for python 2 vs 3 incase user switches versions, as they are not compatible
if db.has_key('files'): db = shelve.open('build/MDv' + str(sys.version_info.major) + '.dat')
if 'files' in db:
files = db['files'] files = db['files']
else: else:
files = {} files = {}
file = readfile(fn) file = readfile(fn)
filemd5 = hashlib.md5(file).hexdigest() filemd5 = hashlib.md5(file.encode('utf8')).hexdigest()
# check if file has already been parsed by the github api # check if file has already been parsed by the github api
if fn in files and filemd5 in files[fn]: if fn in files and filemd5 in files[fn]:
# use the stored copy if nothing has changed to avoid hiting the api more then the 60/hour when not signed in # use the stored copy if nothing has changed to avoid hiting the api more then the 60/hour when not signed in
db.close()
return files[fn][filemd5] return files[fn][filemd5]
else: else:
url = 'https://api.github.com/markdown' url = 'https://api.github.com/markdown'
payload = {'text': file, 'mode': 'markdown'} payload = {'text': file, 'mode': 'markdown'}
req = urllib2.Request(url) headers = {'Content-Type': 'application/json'}
req.add_header('Content-Type', 'application/json') req = urllib2.Request(url, json.dumps(payload).encode('utf8'), headers)
md = urllib2.urlopen(req, json.dumps(payload)).read().replace('\n', '').replace('\'', '\\\'') md = urllib2.urlopen(req).read().decode('utf8').replace('\n', '').replace('\'', '\\\'')
files[fn] = {} files[fn] = {}
files[fn][filemd5] = md files[fn][filemd5] = md
db['files'] = files db['files'] = files