Skip to content

Commit

Permalink
fix: Remove PY2 compatibility code
Browse files Browse the repository at this point in the history
* Set Python requires Python 3.6+ but < Py4
* Remove six from dependencies
* Use click.confirm instead of self implemented confirm code
* Fix imports for 3.6+ compatibility

References for updated imports:
* https://docs.python.org/3/library/configparser.html
* https://docs.python.org/3/library/urllib.request.html#legacy-interface
* https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse
* https://docs.python.org/3/library/importlib.html#importlib.reload
  • Loading branch information
gavindsouza committed May 11, 2021
1 parent 5d563f2 commit 54d48f6
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 32 deletions.
8 changes: 3 additions & 5 deletions bench/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# imports - compatibility imports
from __future__ import print_function

# imports - standard imports
import json
import logging
Expand Down Expand Up @@ -390,8 +387,9 @@ def get_repo_dir(app, bench_path='.'):

def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrade=True):
import git
from six.moves import reload_module
import importlib
from bench.utils import update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, post_upgrade

apps_dir = os.path.join(bench_path, 'apps')
version_upgrade = (False,)
switched_apps = []
Expand Down Expand Up @@ -437,7 +435,7 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad
if version_upgrade[0] and upgrade:
update_requirements()
update_node_packages()
reload_module(bench.utils)
importlib.reload(bench.utils)
backup_all_sites()
patch_sites()
build_assets()
Expand Down
4 changes: 2 additions & 2 deletions bench/commands/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def init(path, apps_path, frappe_path, frappe_branch, no_procfile, no_backups, c
except SystemExit:
pass
except Exception as e:
import os, shutil, time, six
import os, shutil, time
# add a sleep here so that the traceback of other processes doesnt overlap with the prompts
time.sleep(1)
print(e)
log(f"There was a problem while creating {path}", level=2)
if six.moves.input("Do you want to rollback these changes? [Y/n]: ").lower() == "y":
if click.confirm("Do you want to rollback these changes?"):
print(f'Rolling back Bench "{path}"')
if os.path.exists(path):
shutil.rmtree(path)
Expand Down
7 changes: 1 addition & 6 deletions bench/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,14 @@ def setup_requirements(node=False, python=False):
@click.option("--port", help="Port on which you want to run bench manager", default=23624)
@click.option("--domain", help="Domain on which you want to run bench manager")
def setup_manager(yes=False, port=23624, domain=None):
from six.moves import input
from bench.utils import get_sites
from bench.config.common_site_config import get_config
from bench.config.nginx import make_bench_manager_nginx_conf

create_new_site = True

if "bench-manager.local" in os.listdir("sites"):
ans = input("Site already exists. Overwrite existing site? [Y/n]: ").lower()
while ans not in ("y", "n", ""):
ans = input("Please enter 'y' or 'n'. Site already exists. Overwrite existing site? [Y/n]: ").lower()
if ans == "n":
create_new_site = False
create_new_site = click.confirm("Site already exists. Overwrite existing site?")

if create_new_site:
exec_cmd("bench new-site --force bench-manager.local")
Expand Down
2 changes: 1 addition & 1 deletion bench/config/common_site_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def update_config_for_frappe(config, bench_path):
# TODO Optionally we need to add the host or domain name in case dns_multitenant is false

def make_ports(bench_path):
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

benches_path = os.path.dirname(os.path.abspath(bench_path))

Expand Down
4 changes: 2 additions & 2 deletions bench/config/lets_encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ def create_dir_if_missing(path):


def get_certbot():
from six.moves.urllib.request import urlretrieve
from urllib.request import urlretrieve

certbot_path = get_certbot_path()
create_dir_if_missing(certbot_path)

if not os.path.isfile(certbot_path):
urlretrieve ("https://dl.eff.org/certbot-auto", certbot_path)
urlretrieve("https://dl.eff.org/certbot-auto", certbot_path)
os.chmod(certbot_path, 0o744)


Expand Down
3 changes: 1 addition & 2 deletions bench/config/nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# imports - third party imports
import click
from six import string_types

# imports - module imports
import bench
Expand Down Expand Up @@ -218,7 +217,7 @@ def get_sites_with_config(bench_path):
if dns_multitenant and site_config.get('domains'):
for domain in site_config.get('domains'):
# domain can be a string or a dict with 'domain', 'ssl_certificate', 'ssl_certificate_key'
if isinstance(domain, string_types):
if isinstance(domain, str):
domain = { 'domain': domain }

domain['name'] = site
Expand Down
2 changes: 1 addition & 1 deletion bench/config/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def generate_config(bench_path):
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

config = get_config(bench_path)

Expand Down
3 changes: 1 addition & 2 deletions bench/config/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def get_supervisord_conf():

def update_supervisord_config(user=None, yes=False):
"""From bench v5.x, we're moving to supervisor running as user"""
from six.moves import configparser

import configparser
from bench.config.production_setup import service

if not user:
Expand Down
5 changes: 1 addition & 4 deletions bench/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import traceback
import unittest

# imports - third party imports
from six import PY2

# imports - module imports
import bench
import bench.utils

if PY2:
if sys.version_info.major == 2:
FRAPPE_BRANCH = "version-12"
else:
FRAPPE_BRANCH = "develop"
Expand Down
6 changes: 2 additions & 4 deletions bench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,9 @@ def update_npm_packages(bench_path='.'):

if os.path.exists(package_json_path):
with open(package_json_path, "r") as f:
from six import iteritems

app_package_json = json.loads(f.read())
# package.json is usually a dict in a dict
for key, value in iteritems(app_package_json):
for key, value in app_package_json.items():
if not key in package_json:
package_json[key] = value
else:
Expand Down Expand Up @@ -978,7 +976,7 @@ def find_benches(directory=None):

def migrate_env(python, backup=False):
import shutil
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse
from bench.config.common_site_config import get_config
from bench.app import get_apps

Expand Down
3 changes: 1 addition & 2 deletions install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
from __future__ import print_function
import os
import sys
import subprocess
Expand Down Expand Up @@ -234,7 +233,7 @@ def install_bench(args):
# create user if not exists
extra_vars = vars(args)
extra_vars.update(frappe_user=args.user)

extra_vars.update(user_directory=get_user_home_directory(args.user))

if os.path.exists(tmp_bench_repo):
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ python-crontab==2.4.0
requests==2.22.0
semantic-version==2.8.2
setuptools
six
virtualenv
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
author_email='info@frappe.io',
version=VERSION,
packages=find_packages(),
python_requires='~=3.6',
zip_safe=False,
include_package_data=True,
install_requires=install_requires,
Expand Down

0 comments on commit 54d48f6

Please sign in to comment.