Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from misaelnieto/master
Browse files Browse the repository at this point in the history
Python 3 Compatibility
  • Loading branch information
prsephton committed Sep 6, 2017
2 parents f0d0d3d + 1f71d49 commit d341731
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
13 changes: 7 additions & 6 deletions grokproject/main.py
@@ -1,3 +1,4 @@
from __future__ import print_function
import sys
import pkg_resources
from paste.script import command
Expand Down Expand Up @@ -38,7 +39,7 @@ def main(vars=GrokProject.vars, template_name='grok'):
options, args = parser.parse_args()

if options.version:
print get_version()
print(get_version())
return 0

if len(args) != 1:
Expand Down Expand Up @@ -70,9 +71,9 @@ def main(vars=GrokProject.vars, template_name='grok'):
# Assert that the project name is a valid Python identifier.
if not project_name_re.match(project):
print
print "Error: The chosen project name is not a valid " \
"package name: %s." % project
print "Please choose a different project name."
print("Error: The chosen project name is not a valid " \
"package name: %s." % project)
print("Please choose a different project name.")
sys.exit(1)

existing = False
Expand All @@ -83,8 +84,8 @@ def main(vars=GrokProject.vars, template_name='grok'):
pass
if existing:
print
print "Error: The package '%s' is already on sys.path." % project
print "Please choose a different project name."
print("Error: The package '%s' is already on sys.path." % project)
print("Please choose a different project name.")
sys.exit(1)

# Create the project.
Expand Down
47 changes: 31 additions & 16 deletions grokproject/templates.py
@@ -1,7 +1,16 @@
from __future__ import print_function
import sys
import os
import urllib2
import urlparse
try:
import urllib2
import urlparse
from urlparse import urljoin
from urllib2 import HTTPError, urlopen
except ImportError:
import urllib as urllib2
from urllib.parse import urlparse, urljoin
from urllib.error import HTTPError
from urllib.request import urlopen
import xml.sax.saxutils
from paste.script import templates
from paste.script.templates import NoDefault
Expand Down Expand Up @@ -57,10 +66,9 @@ class GrokProject(templates.Template):

def check_vars(self, vars, cmd):
if vars['package'] in ('grok', 'zope'):
print
print "Error: The chosen project name results in an invalid " \
"package name: %s." % vars['package']
print "Please choose a different project name."
print("Error: The chosen project name results in an invalid " \
"package name: %s." % vars['package'])
print("Please choose a different project name.")
sys.exit(1)

explicit_eggs_dir = vars.get('eggs_dir')
Expand All @@ -78,18 +86,21 @@ def check_vars(self, vars, cmd):
vars['passwd'] = get_ssha_encoded_string(vars['passwd'])
for var_name in ['user', 'passwd']:
# Escape values that go in site.zcml.
vars[var_name] = xml.sax.saxutils.quoteattr(vars[var_name])
if isinstance(vars[var_name], str):
vars[var_name] = xml.sax.saxutils.quoteattr(vars[var_name])
else:
vars[var_name] = xml.sax.saxutils.quoteattr(vars[var_name].decode())
vars['app_class_name'] = vars['project'].capitalize()
vars['project_lowercase'] = vars['project'].lower()

# Handling the version.cfg file.
version_url = vars.get('version_url')
find_links_url = ''
if version_url is None:
print "Determining current grok version..."
print("Determining current grok version...")
# If no version URL was specified, we look up the current
# version first and construct a URL.
current_info_url = urlparse.urljoin(GROK_RELEASE_URL, 'current')
current_info_url = urljoin(GROK_RELEASE_URL, 'current')
version = self.download(current_info_url).strip().replace(
'grok-', '').replace('.cfg', '')
version_url = GROK_RELEASE_URL + version + '/versions.cfg'
Expand Down Expand Up @@ -128,17 +139,21 @@ def download(self, url):
"""
contents = None
try:
contents = urllib2.urlopen(url).read()
except urllib2.HTTPError:
contents = urlopen(url).read()
except HTTPError:
# Some 404 or similar happened...
print "Error: cannot download required %s" % url
print "Maybe you specified a non-existing version?"
print("Error: cannot download required %s" % url)
print("Maybe you specified a non-existing version?")
sys.exit(1)
except IOError, e:
except IOError as e:
# Some serious problem: no connect to server...
print "Error: cannot download required %s" % url
print "Server may be down. Please try again later."
print("Error: cannot download required %s" % url)
print("Server may be down. Please try again later.")
sys.exit(1)
if isinstance(contents, str):
contents = xml.sax.saxutils.quoteattr(contents)
else:
contents = xml.sax.saxutils.quoteattr(contents.decode())
return contents

def post(self, command, output_dir, vars):
Expand Down
14 changes: 7 additions & 7 deletions grokproject/utils.py
Expand Up @@ -55,10 +55,10 @@ def get_ssha_encoded_string(password):
cannot depend on that package.
"""
encoder = codecs.getencoder('utf-8')
hash = sha1(encoder(password)[0])
_hash = sha1(encoder(password)[0])
salt = os.urandom(4)
hash.update(salt)
return '{SSHA}' + urlsafe_b64encode(hash.digest() + salt)
_hash.update(salt)
return b'{SSHA}' + urlsafe_b64encode(_hash.digest() + salt)

def get_boolean_value_for_option(vars, option):
value = vars.get(option.name)
Expand All @@ -79,8 +79,8 @@ def get_boolean_value_for_option(vars, option):
else:
value = 'false'
else:
print ""
print "Error: %s should be true or false." % option.name
print("")
print("Error: %s should be true or false." % option.name)
sys.exit(1)
else:
value = option.default
Expand All @@ -95,11 +95,11 @@ def run_buildout(verbose=False, use_distribute=False):
cmd = sys.executable + ' ' + os.path.join(os.getcwd(), 'bootstrap.py')
if use_distribute:
cmd += ' --distribute'
print 'Running %s...' % cmd
print('Running %s...' % cmd)
subprocess.call(cmd, shell=True)
# Then, run the project's buildout.
cmd = os.path.join(os.getcwd(), 'bin', 'buildout')
if verbose:
cmd += ' -v'
print 'Running %s...' % cmd
print('Running %s...' % cmd)
subprocess.call(cmd, shell=True)

0 comments on commit d341731

Please sign in to comment.