From ccb2a5bf8de204ee94e27a07dac406e2229e62f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Severin=20Gl=C3=B6ckner?= Date: Fri, 21 Apr 2017 14:35:56 +0200 Subject: [PATCH] ported wescamp and libgithub to python3 --- data/tools/wesnoth/campaignserver_client.py | 2 +- data/tools/wesnoth/libgithub.py | 22 ++++---- data/tools/wesnoth/wescamp.py | 61 +++++++++------------ 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/data/tools/wesnoth/campaignserver_client.py b/data/tools/wesnoth/campaignserver_client.py index d07775d7190..89750723e60 100755 --- a/data/tools/wesnoth/campaignserver_client.py +++ b/data/tools/wesnoth/campaignserver_client.py @@ -7,7 +7,7 @@ import wesnoth.wmlparser3 as wmlparser # src/network.cpp def append_attributes(tag, **attributes): - for k, v in attributes.items(): + for k, v in list(attributes.items()): if isinstance(k, str): k = k.encode("utf8") if isinstance(v, str): v = v.encode("utf8") kn = wmlparser.AttributeNode(k) diff --git a/data/tools/wesnoth/libgithub.py b/data/tools/wesnoth/libgithub.py index 07ae3306d2b..8018985b4b8 100644 --- a/data/tools/wesnoth/libgithub.py +++ b/data/tools/wesnoth/libgithub.py @@ -16,12 +16,12 @@ except ImportError: import shutil import subprocess import tempfile -import urllib2 +import urllib.request #TODO: document and log where missing -class Error(StandardError): +class Error(Exception): """Base class for exceptions in this module.""" pass @@ -91,7 +91,7 @@ class Addon(object): def remove_untracked(): - untracked = [line.replace("?? ","",1) for line in self._status() if line.startswith("??")] + untracked = [line.replace("?? ", "", 1) for line in self._status() if line.startswith("??")] for item in untracked: try: path = os.path.join(self.get_dir(), item) @@ -161,7 +161,7 @@ class Addon(object): self._rmtree(".", exclude) #actual copying - self._copytree(src, self.get_dir(), ignore=lambda src,names: [n for n in names if n in exclude]) + self._copytree(src, self.get_dir(), ignore=lambda src, names: [n for n in names if n in exclude]) self._execute(["git", "add", "."], check_error=True) status = self._status() @@ -468,7 +468,7 @@ class GitHub(object): def _github_api_request(self, url, data=None, method=None, authenticate=False): logging.debug("Making github API request {0}".format(url)) - request = urllib2.Request(url) + request = urllib.request.Request(url) if method: request.get_method = lambda: method @@ -486,14 +486,14 @@ class GitHub(object): auth = self._github_authorization() if ":" in auth: # username:password - base64string = encodestring(auth).replace('\n','') + base64string = encodestring(auth).replace('\n', '') request.add_header("Authorization", "Basic {0}".format(base64string)) else: # token request.add_header("Authorization", "Bearer {0}".format(auth)) try: - response = urllib2.urlopen(request) + response = urllib.request.urlopen(request) except IOError as e: raise Error("GitHub API failure: " + str(e)) if response.code == 204: @@ -509,7 +509,7 @@ class GitHub(object): links_raw = link_header.split(",") links_split_raw = [link.split(";") for link in links_raw] links_split_proc = [(l[1].strip().lstrip('rel="').rstrip('"'), l[0].strip().lstrip("<").rstrip(">")) for l in links_split_raw] - links_dict = dict((k,v) for (k,v) in links_split_proc) + links_dict = dict((k, v) for (k, v) in links_split_proc) if "next" in links_dict: logging.debug("Link with rel=\"next\" found, recursing to deal with pagination") rest = self._github_api_request(links_dict["next"], data, method, authenticate) @@ -582,7 +582,7 @@ def _gen(possible_dirs): logging.debug("No candidates left, creating new checkout") - realish_github = GitHub(tempfile.mkdtemp(),"system") + realish_github = GitHub(tempfile.mkdtemp(), "system") build_system = realish_github.addon("build", readonly=True) return build_system, True try: @@ -592,7 +592,7 @@ def _gen(possible_dirs): # Exception to make sure nobody catches it # Use raise ... from syntax in python3 import sys - raise Exception(str(e)), None, sys.exc_info()[2] + raise Exception(str(e)).with_traceback(sys.exc_info()[2]) # Add references to shutil and os to ensure we're destructed before they are stored_shutil = shutil stored_os = os @@ -616,4 +616,4 @@ def get_build_system(possible_dirs=[]): global _g if _g == None: _g = _gen(possible_dirs) - return _g.next() + return next(_g) diff --git a/data/tools/wesnoth/wescamp.py b/data/tools/wesnoth/wescamp.py index ed2afadf13e..0ea56d8816e 100755 --- a/data/tools/wesnoth/wescamp.py +++ b/data/tools/wesnoth/wescamp.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # vim: tabstop=4: shiftwidth=4: expandtab: softtabstop=4: autoindent: # """ @@ -23,14 +23,6 @@ import sys, os.path, argparse, tempfile, shutil, logging, socket # in case the wesnoth python package has not been installed sys.path.append("data/tools") -print(""" -Note: campaignserver_client has since been moved to Python 3 - the -easiest way to run this script is to use campaginserver_client from -an earlier Wesnoth version. And then in the long run convert this -script to Python 3 as well. -""") -sys.exit(1) - #import CampaignClient as libwml import wesnoth.campaignserver_client as libwml @@ -48,13 +40,14 @@ class tempdir: # We need to add a reference to shutil, otherwise __del__() will fail. # This is because when the object is destructed other globals may - #have already been torn down. + # have already been torn down. # In C++ this is known as the static deinitialization fiasco. - self.dummy = shutil + self.shutil = shutil + self.logging = logging def __del__(self): - self.dummy.rmtree(self.path) - logging.debug("removed tempdir '%s'", self.path) + self.shutil.rmtree(self.path) + self.logging.debug("removed tempdir '%s'", self.path) if __name__ == "__main__": git_version = None @@ -508,23 +501,23 @@ if __name__ == "__main__": try: addons = list_addons(server, args.list_translatable) except libgithub.AddonError as e: - print "[ERROR github in {0}] {1}".format(e.addon, str(e.message)) + print("[ERROR github in {0}] {1}".format(e.addon, str(e.message))) sys.exit(1) except libgithub.Error as e: - print "[ERROR github] " + str(e) + print("[ERROR github] " + str(e)) sys.exit(1) except socket.error as e: - print "Socket error: " + str(e) + print("Socket error: " + str(e)) sys.exit(e[0]) except IOError as e: - print "Unexpected error occured: " + str(e) + print("Unexpected error occured: " + str(e)) sys.exit(e[0]) - for k, v in addons.iteritems(): + for k, v in list(addons.items()): if(v): - print k + " translatable" + print(k + " translatable") else: - print k + print(k) # Upload an addon to wescamp. elif(args.upload != None): @@ -537,16 +530,16 @@ if __name__ == "__main__": try: upload(server, args.upload, target, wescamp, build_sys_dir) except libgithub.AddonError as e: - print "[ERROR github in {0}] {1}".format(e.addon, str(e.message)) + print("[ERROR github in {0}] {1}".format(e.addon, str(e.message))) sys.exit(1) except libgithub.Error as e: - print "[ERROR github] " + str(e) + print("[ERROR github] " + str(e)) sys.exit(1) except socket.error as e: - print "Socket error: " + str(e) + print("Socket error: " + str(e)) sys.exit(e[0]) except IOError as e: - print "Unexpected error occured: " + str(e) + print("Unexpected error occured: " + str(e)) sys.exit(e[0]) # Upload all addons from wescamp. @@ -561,25 +554,25 @@ if __name__ == "__main__": try: addons = list_addons(server, True) except socket.error as e: - print "Socket error: " + str(e) + print("Socket error: " + str(e)) sys.exit(e[0]) - for k, v in addons.iteritems(): + for k, v in list(addons.items()): try: logging.info("Processing addon '%s'", k) # Create a new temp dir for every upload. tmp = tempdir() upload(server, k, tmp.path, wescamp, build_sys_dir) except libgithub.AddonError as e: - print "[ERROR github in {0}] {1}".format(e.addon, str(e.message)) + print("[ERROR github in {0}] {1}".format(e.addon, str(e.message))) error = True except libgithub.Error as e: - print "[ERROR github] in addon '{0}' {1}".format(k, str(e)) + print("[ERROR github] in addon '{0}' {1}".format(k, str(e))) error = True except socket.error as e: - print "Socket error: " + str(e) + print("Socket error: " + str(e)) error = True except IOError as e: - print "Unexpected error occured: " + str(e) + print("Unexpected error occured: " + str(e)) error = True if(error): @@ -595,16 +588,16 @@ if __name__ == "__main__": try: checkout(wescamp, auth=git_auth, readonly=(args.checkout_readonly)) except libgithub.AddonError as e: - print "[ERROR github in {0}] {1}".format(e.addon, str(e.message)) + print("[ERROR github in {0}] {1}".format(e.addon, str(e.message))) sys.exit(1) except libgithub.Error as e: - print "[ERROR github] " + str(e) + print("[ERROR github] " + str(e)) sys.exit(1) except socket.error as e: - print "Socket error: " + str(e) + print("Socket error: " + str(e)) sys.exit(e[0]) except IOError as e: - print "Unexpected error occured: " + str(e) + print("Unexpected error occured: " + str(e)) sys.exit(e[0]) else: