From 78427e8d3250ee865a53c5d67148809fadf00554 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Feb 07 2020 12:29:28 +0000
Subject: Support werkzeug 1.0 which changed some of its top-level imports


What used to be accessible as ``werkzeug.secure_filename`` now is
accessible only as ``werkzeug.utils.secure_filename``.

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py
index db25549..9a31c0d 100644
--- a/pagure/internal/__init__.py
+++ b/pagure/internal/__init__.py
@@ -18,7 +18,7 @@ import os
 
 import flask
 import pygit2
-import werkzeug
+import werkzeug.utils
 
 from functools import wraps
 from sqlalchemy.exc import SQLAlchemyError
@@ -108,7 +108,7 @@ def lookup_ssh_key():
         result["username"] = key.user.username
     elif key.project:
         result["username"] = "deploykey_%s_%s" % (
-            werkzeug.secure_filename(key.project.fullname),
+            werkzeug.utils.secure_filename(key.project.fullname),
             key.id,
         )
     else:
diff --git a/pagure/lib/git_auth.py b/pagure/lib/git_auth.py
index 3210e54..59e2501 100644
--- a/pagure/lib/git_auth.py
+++ b/pagure/lib/git_auth.py
@@ -18,7 +18,7 @@ import subprocess
 import tempfile
 from io import open
 
-import werkzeug
+import werkzeug.utils
 from six import with_metaclass
 from six.moves import dbm_gnu
 
@@ -270,7 +270,7 @@ class Gitolite2Auth(GitAuthHelper):
                     "  %s = deploykey_%s_%s"
                     % (
                         access,
-                        werkzeug.secure_filename(project.fullname),
+                        werkzeug.utils.secure_filename(project.fullname),
                         deploykey.id,
                     )
                 )
diff --git a/pagure/lib/query.py b/pagure/lib/query.py
index 4cba70e..a9246db 100644
--- a/pagure/lib/query.py
+++ b/pagure/lib/query.py
@@ -44,7 +44,7 @@ import redis
 import six
 import sqlalchemy
 import sqlalchemy.schema
-import werkzeug
+import werkzeug.utils
 
 from six.moves.urllib_parse import urlparse, urlencode, parse_qsl
 from sqlalchemy import asc, desc, func, cast, Text
@@ -292,7 +292,7 @@ def create_deploykeys_ssh_keys_on_disk(project, gitolite_keydir):
     # First remove deploykeys that no longer exist
     keyfiles = [
         "deploykey_%s_%s.pub"
-        % (werkzeug.secure_filename(project.fullname), key.id)
+        % (werkzeug.utils.secure_filename(project.fullname), key.id)
         for key in project.deploykeys
     ]
 
@@ -311,7 +311,7 @@ def create_deploykeys_ssh_keys_on_disk(project, gitolite_keydir):
         # See the comment in lib/git.py:write_gitolite_acls about why this
         # name for a file is sane and does not inject a new security risk.
         keyfile = "deploykey_%s_%s.pub" % (
-            werkzeug.secure_filename(project.fullname),
+            werkzeug.utils.secure_filename(project.fullname),
             deploykey.id,
         )
         if not os.path.exists(os.path.join(project_key_dir, keyfile)):
@@ -3472,13 +3472,13 @@ def add_attachment(repo, issue, attachmentfolder, user, filename, filestream):
     _log.info(
         "Adding file: %s to the git repo: %s",
         repo.path,
-        werkzeug.secure_filename(filename),
+        werkzeug.utils.secure_filename(filename),
     )
 
     # Prefix the filename with a timestamp:
     filename = "%s-%s" % (
         hashlib.sha256(filestream.read()).hexdigest(),
-        werkzeug.secure_filename(filename),
+        werkzeug.utils.secure_filename(filename),
     )
     filedir = os.path.join(attachmentfolder, repo.fullname, "files")
     filepath = os.path.join(filedir, filename)
diff --git a/pagure/lib/tasks_mirror.py b/pagure/lib/tasks_mirror.py
index e5302e1..24154ba 100644
--- a/pagure/lib/tasks_mirror.py
+++ b/pagure/lib/tasks_mirror.py
@@ -17,7 +17,7 @@ import stat
 import struct
 
 import six
-import werkzeug
+import werkzeug.utils
 
 from celery import Celery
 from cryptography import utils
@@ -117,7 +117,7 @@ def setup_mirroring(self, session, username, namespace, name):
         session, namespace=namespace, name=name, user=username
     )
 
-    public_key_name = werkzeug.secure_filename(project.fullname)
+    public_key_name = werkzeug.utils.secure_filename(project.fullname)
     ssh_folder = pagure_config["MIRROR_SSHKEYS_FOLDER"]
 
     if not os.path.exists(ssh_folder):
@@ -173,7 +173,7 @@ def teardown_mirroring(self, session, username, namespace, name):
 
     ssh_folder = pagure_config["MIRROR_SSHKEYS_FOLDER"]
 
-    public_key_name = werkzeug.secure_filename(project.fullname)
+    public_key_name = werkzeug.utils.secure_filename(project.fullname)
     private_key_file = os.path.join(ssh_folder, public_key_name)
     public_key_file = os.path.join(ssh_folder, "%s.pub" % public_key_name)
 
@@ -207,7 +207,7 @@ def mirror_project(self, session, username, namespace, name):
         return
 
     ssh_folder = pagure_config["MIRROR_SSHKEYS_FOLDER"]
-    public_key_name = werkzeug.secure_filename(project.fullname)
+    public_key_name = werkzeug.utils.secure_filename(project.fullname)
     private_key_file = os.path.join(ssh_folder, public_key_name)
 
     if not os.path.exists(private_key_file):
diff --git a/pagure/ui/clone.py b/pagure/ui/clone.py
index 93d8196..0caffbc 100644
--- a/pagure/ui/clone.py
+++ b/pagure/ui/clone.py
@@ -17,7 +17,7 @@ import os
 
 import flask
 import requests
-import werkzeug
+import werkzeug.wsgi
 
 import pagure.exceptions
 import pagure.lib.git
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index 05f199c..0e57369 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -30,7 +30,7 @@ import flask
 import pygit2
 import kitchen.text.converters as ktc
 import six
-import werkzeug
+import werkzeug.utils
 
 from six import BytesIO
 from PIL import Image
@@ -1191,7 +1191,7 @@ def new_release(repo, username=None, namespace=None):
         filenames = []
         error = False
         for filestream in flask.request.files.getlist("filestream"):
-            filename = werkzeug.secure_filename(filestream.filename)
+            filename = werkzeug.utils.secure_filename(filestream.filename)
             filenames.append(filename)
             try:
                 folder = os.path.join(
@@ -3559,7 +3559,7 @@ def generate_project_archive(
         _log.debug("%s no in %s", extension, extensions)
         flask.abort(400, description="Invalid archive format specified")
 
-    name = werkzeug.secure_filename(name)
+    name = werkzeug.utils.secure_filename(name)
 
     repo_obj = flask.g.repo_obj
 
@@ -3589,7 +3589,7 @@ def generate_project_archive(
     tag_path = ""
     tag_filename = None
     if tag:
-        tag_filename = werkzeug.secure_filename(ref)
+        tag_filename = werkzeug.utils.secure_filename(ref)
         tag_path = os.path.join("tags", tag_filename)
 
     path = os.path.join(
diff --git a/pagure/utils.py b/pagure/utils.py
index 00e02d9..3225d3b 100644
--- a/pagure/utils.py
+++ b/pagure/utils.py
@@ -21,7 +21,7 @@ from functools import wraps
 import flask
 import pygit2
 import six
-import werkzeug
+import werkzeug.utils
 
 from pagure.exceptions import (
     PagureException,
@@ -595,7 +595,7 @@ def get_remote_repo_path(remote_git, branch_from, ignore_non_exist=False):
     """
     repopath = os.path.join(
         pagure_config["REMOTE_GIT_FOLDER"],
-        werkzeug.secure_filename("%s_%s" % (remote_git, branch_from)),
+        werkzeug.utils.secure_filename("%s_%s" % (remote_git, branch_from)),
     )
 
     if not os.path.exists(repopath) and not ignore_non_exist:
@@ -787,7 +787,7 @@ def lookup_deploykey(project, username):
         return None
     username = username[len("deploykey_") :]
     rest, keyid = username.rsplit("_", 1)
-    if rest != werkzeug.secure_filename(project.fullname):
+    if rest != werkzeug.utils.secure_filename(project.fullname):
         # This is not a deploykey for the specified project
         return None
     keyid = int(keyid)
diff --git a/tests/test_pagure_flask.py b/tests/test_pagure_flask.py
index 0a1b8d1..0ad4fd7 100644
--- a/tests/test_pagure_flask.py
+++ b/tests/test_pagure_flask.py
@@ -18,7 +18,6 @@ import os
 import mock
 import munch
 import pygit2
-import werkzeug
 
 sys.path.insert(
     0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")

