From 99d6868f2574bb6d6b0f91f11f44cfb9f98f4f80 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 19 May 2020 13:55:17 +0100 Subject: [PATCH] Switch tile server CGI scripts to use python3 --- cookbooks/tile/recipes/default.rb | 10 ++- cookbooks/tile/templates/default/debug.erb | 82 ++++++++++----------- cookbooks/tile/templates/default/export.erb | 52 ++++++------- 3 files changed, 73 insertions(+), 71 deletions(-) diff --git a/cookbooks/tile/recipes/default.rb b/cookbooks/tile/recipes/default.rb index a5f135a64..c5a6b6177 100644 --- a/cookbooks/tile/recipes/default.rb +++ b/cookbooks/tile/recipes/default.rb @@ -126,12 +126,14 @@ template "/srv/tile.openstreetmap.org/html/index.html" do end package %w[ - python-cairo - python-mapnik - python-setuptools + python3-cairo + python3-mapnik + python3-setuptools ] -python_package "pyotp" +python_package "pyotp" do + python_version "3" +end package %w[ fonts-noto-cjk diff --git a/cookbooks/tile/templates/default/debug.erb b/cookbooks/tile/templates/default/debug.erb index 03af6549f..e3e3852a0 100755 --- a/cookbooks/tile/templates/default/debug.erb +++ b/cookbooks/tile/templates/default/debug.erb @@ -1,4 +1,4 @@ -#!/usr/bin/python -u +#!/usr/bin/python3 -u # -*- coding: utf-8 -*- import cgi @@ -22,35 +22,35 @@ resource.setrlimit(resource.RLIMIT_AS,(4000000000, 4000000000)) # Routine to output HTTP headers def output_headers(content_type, filename = "", length = 0): - print "Cache-Control: no-cache, no-store, must-revalidate')" - print "Pragma: no-cache" - print "Expires: 0" - print "Content-Type: %s" % content_type + print("Cache-Control: no-cache, no-store, must-revalidate')") + print("Pragma: no-cache") + print("Expires: 0") + print("Content-Type: %s" % content_type) if filename: - print "Content-Disposition: attachment; filename=\"%s\"" % filename + print("Content-Disposition: attachment; filename=\"%s\"" % filename) if length: - print "Content-Length: %d" % length - print "" + print("Content-Length: %d" % length) + print("") # Routine to report an error def output_error(message): output_headers("text/html") - print "" - print "" - print "Error" - print "" - print "" - print "

Error

" - print "

%s

" % message - print "" - print "" + print("") + print("") + print("Error") + print("") + print("") + print("

Error

") + print("

%s

" % message) + print("") + print("") # Make sure we have a user agent -if not os.environ.has_key('HTTP_USER_AGENT'): +if 'HTTP_USER_AGENT' not in os.environ: os.environ['HTTP_USER_AGENT'] = 'NONE' # Get the cache server name -if os.environ.has_key('HTTP_VIA'): +if 'HTTP_VIA' in os.environ: cache_server = re.search('[a-z0-9-]+\.openstreetmap\.org', os.environ['HTTP_VIA']).group(0) else: cache_server = None @@ -59,35 +59,35 @@ else: loadavg = float(open("/proc/loadavg").readline().split(" ")[0]) output_headers("text/html") -print "" -print "" -print "tile.openstreetmap.org debug" -print "" -print "" -print "

tile.openstreetmap.org debug

" -print "

Server Stats

" +print("") +print("") +print("tile.openstreetmap.org debug") +print("") +print("") +print("

tile.openstreetmap.org debug

") +print("

Server Stats

") if cache_server: - print "

Cache Server: %s

" % cache_server -print "

" -print "Render Server: <%= node['fqdn'] %>
" -print "Load Average: %s

" % loadavg -print "

File Status

" -print "

" + print("

Cache Server: %s

" % cache_server) +print("

") +print("Render Server: <%= node['fqdn'] %>
") +print("Load Average: %s

" % loadavg) +print("

File Status

") +print("

") <% node[:tile][:data].each do |name,data| url = data[:url] file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}" file_basename = File.basename(url) -%> -print "%s last modified: %s
" % ("<%= file_basename %>", time.ctime(os.path.getmtime("<%= file %>"))) +print("%s last modified: %s
" % ("<%= file_basename %>", time.ctime(os.path.getmtime("<%= file %>")))) <% end -%> -print "

" -print "

Browser Request Headers

" -print "

" -for param in os.environ.keys(): - print "%20s: %s
" % (param, os.environ[param]) -print "

" -print "" -print "" +print("

") +print("

Browser Request Headers

") +print("

") +for param in sorted(os.environ): + print("%20s: %s
" % (param, os.environ[param])) +print("

") +print("") +print("") diff --git a/cookbooks/tile/templates/default/export.erb b/cookbooks/tile/templates/default/export.erb index 0cd115271..b8075a17a 100644 --- a/cookbooks/tile/templates/default/export.erb +++ b/cookbooks/tile/templates/default/export.erb @@ -1,9 +1,9 @@ -#!/usr/bin/python -u +#!/usr/bin/python3 -u # -*- coding: utf-8 -*- import cairo import cgi -import Cookie +import http.cookies import mapnik import os import pyotp @@ -23,17 +23,17 @@ resource.setrlimit(resource.RLIMIT_AS,(4000000000, 4000000000)) # Routine to output HTTP headers def output_headers(content_type, filename = "", length = 0): - print "Content-Type: %s" % content_type + print("Content-Type: %s" % content_type) if filename: - print "Content-Disposition: attachment; filename=\"%s\"" % filename + print("Content-Disposition: attachment; filename=\"%s\"" % filename) if length: - print "Content-Length: %d" % length - print "" + print("Content-Length: %d" % length) + print("") # Routine to output the contents of a file def output_file(file): file.seek(0) - shutil.copyfileobj(file, sys.stdout) + shutil.copyfileobj(file, sys.stdout.buffer) # Routine to get the size of a file def file_size(file): @@ -41,17 +41,17 @@ def file_size(file): # Routine to report an error def output_error(message, status = "400 Bad Request"): - print "Status: %s" % status + print("Status: %s" % status) output_headers("text/html") - print "" - print "" - print "Error" - print "" - print "" - print "

Error

" - print "

%s

" % message - print "" - print "" + print("") + print("") + print("Error") + print("") + print("") + print("

Error

") + print("

%s

" % message) + print("") + print("") # Create TOTP token validator totp = pyotp.TOTP('<%= @totp_key %>', interval = 3600) @@ -60,18 +60,18 @@ totp = pyotp.TOTP('<%= @totp_key %>', interval = 3600) form = cgi.FieldStorage() # Import cookies -cookies = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE')) +cookies = http.cookies.SimpleCookie(os.environ.get('HTTP_COOKIE')) # Make sure we have a user agent -if not os.environ.has_key('HTTP_USER_AGENT'): +if 'HTTP_USER_AGENT' not in os.environ: os.environ['HTTP_USER_AGENT'] = 'NONE' # Make sure we have a referer -if not os.environ.has_key('HTTP_REFERER'): +if 'HTTP_REFERER' not in os.environ: os.environ['HTTP_REFERER'] = 'NONE' # Look for TOTP token -if cookies.has_key('_osm_totp_token'): +if '_osm_totp_token' in cookies: token = cookies['_osm_totp_token'].value else: token = None @@ -97,13 +97,13 @@ elif os.environ['HTTP_REFERER'] == '<%= referer %>': # Block scraper output_error("The server is too busy at the moment. Please wait a few minutes before trying again.", "503 Service Unavailable") <% end -%> -elif not form.has_key("bbox"): +elif "bbox" not in form: # No bounding box specified output_error("No bounding box specified") -elif not form.has_key("scale"): +elif "scale" not in form: # No scale specified output_error("No scale specified") -elif not form.has_key("format"): +elif "format" not in form: # No format specified output_error("No format specified") else: @@ -152,13 +152,13 @@ else: mapnik.render(map, image) png = image.tostring("png") output_headers("image/png", "map.png", len(png)) - sys.stdout.write(png) + sys.stdout.buffer.write(png) elif form.getvalue("format") == "jpeg": image = mapnik.Image(map.width, map.height) mapnik.render(map, image) jpeg = image.tostring("jpeg") output_headers("image/jpeg", "map.jpg", len(jpeg)) - sys.stdout.write(jpeg) + sys.stdout.buffer.write(jpeg) elif form.getvalue("format") == "svg": file = tempfile.NamedTemporaryFile(prefix = "export") surface = cairo.SVGSurface(file.name, map.width, map.height) -- 2.43.2