from functools import partial

from pythonforandroid.toolchain import Recipe, shprint, current_directory
import sh


class OpenSSLRecipe(Recipe):
    version = '1.0.2e'
    url = 'https://www.openssl.org/source/openssl-{version}.tar.gz'

    def should_build(self, arch):
        return not self.has_libs(arch, 'libssl.so', 'libcrypto.so')

    def check_symbol(self, env, sofile, symbol):
        nm = env.get('NM', 'nm')
        syms = sh.sh('-c', "{} -gp {} | cut -d' ' -f3".format(
                nm, sofile), _env=env).splitlines()
        if symbol in syms:
            return True
        print('{} missing symbol {}; rebuilding'.format(sofile, symbol))
        return False

    def get_recipe_env(self, arch=None):
        env = super(OpenSSLRecipe, self).get_recipe_env(arch)
        env['CFLAGS'] += ' ' + env['LDFLAGS']
        env['CC'] += ' ' + env['LDFLAGS']
        return env

    def select_build_arch(self, arch):
        aname = arch.arch
        if 'arm64' in aname:
            return 'linux-aarch64'
        if 'v7a' in aname:
            return 'android-armv7'
        if 'arm' in aname:
            return 'android'
        return 'linux-armv4'

    def build_arch(self, arch):
        env = self.get_recipe_env(arch)
        with current_directory(self.get_build_dir(arch.arch)):
            # sh fails with code 255 trying to execute ./Configure
            # so instead we manually run perl passing in Configure
            perl = sh.Command('perl')
            buildarch = self.select_build_arch(arch)
            shprint(perl, 'Configure', 'shared', 'no-dso', 'no-krb5', buildarch, _env=env)
            self.apply_patch('disable-sover.patch', arch.arch)

            check_crypto = partial(self.check_symbol, env, 'libcrypto.so')
            # check_ssl = partial(self.check_symbol, env, 'libssl.so')
            while True:
                shprint(sh.make, 'build_libs', _env=env)
                if all(map(check_crypto, ('SSLeay', 'MD5_Transform', 'MD4_Init'))):
                    break
                shprint(sh.make, 'clean', _env=env)

            self.install_libs(arch, 'libssl.so', 'libcrypto.so')

recipe = OpenSSLRecipe()

pythonforandroid/recipes/openssl/__init__.py
from functools import partial

from pythonforandroid.toolchain import Recipe, shprint, current_directory
import sh


class OpenSSLRecipe(Recipe):
    version = '1.0.2f'
    url = 'https://www.openssl.org/source/openssl-{version}.tar.gz'

    def should_build(self, arch):
        return not self.has_libs(arch, 'libssl.so', 'libcrypto.so')

    def check_symbol(self, env, sofile, symbol):
        nm = env.get('NM', 'nm')
        syms = sh.sh('-c', "{} -gp {} | cut -d' ' -f3".format(
                nm, sofile), _env=env).splitlines()
        if symbol in syms:
            return True
        print('{} missing symbol {}; rebuilding'.format(sofile, symbol))
        return False

    def get_recipe_env(self, arch=None):
        env = super(OpenSSLRecipe, self).get_recipe_env(arch)
        env['CFLAGS'] += ' ' + env['LDFLAGS']
        env['CC'] += ' ' + env['LDFLAGS']
        return env

    def select_build_arch(self, arch):
        aname = arch.arch
        if 'arm64' in aname:
            return 'linux-aarch64'
        if 'v7a' in aname:
            return 'android-armv7'
        if 'arm' in aname:
            return 'android'
        return 'linux-armv4'

    def build_arch(self, arch):
        env = self.get_recipe_env(arch)
        with current_directory(self.get_build_dir(arch.arch)):
            # sh fails with code 255 trying to execute ./Configure
            # so instead we manually run perl passing in Configure
            perl = sh.Command('perl')
            buildarch = self.select_build_arch(arch)
            shprint(perl, 'Configure', 'shared', 'no-dso', 'no-krb5', buildarch, _env=env)
            self.apply_patch('disable-sover.patch', arch.arch)

            check_crypto = partial(self.check_symbol, env, 'libcrypto.so')
            # check_ssl = partial(self.check_symbol, env, 'libssl.so')
            while True:
                shprint(sh.make, 'build_libs', _env=env)
                if all(map(check_crypto, ('SSLeay', 'MD5_Transform', 'MD4_Init'))):
                    break
                shprint(sh.make, 'clean', _env=env)

            self.install_libs(arch, 'libssl.so', 'libcrypto.so')

recipe = OpenSSLRecipe()

pythonforandroid/recipes/openssl/__init__.py
diff --git a/pythonforandroid/recipes/openssl/__init__.py b/pythonforandroid/recipes/openssl/__init__.py
index f3df432a2a..8348f8d552 100644
--- a/pythonforandroid/recipes/openssl/__init__.py
+++ b/pythonforandroid/recipes/openssl/__init__.py
@@ -5,7 +5,7 @@
 
 
 class OpenSSLRecipe(Recipe):
-    version = '1.0.2e'
+    version = '1.0.2f'
     url = 'https://www.openssl.org/source/openssl-{version}.tar.gz'
 
     def should_build(self, arch):
diff --git a/pythonforandroid/recipes/openssl/disable-sover.patch b/pythonforandroid/recipes/openssl/disable-sover.patch
index 12020a494b..6099fadcef 100644
--- a/pythonforandroid/recipes/openssl/disable-sover.patch
+++ b/pythonforandroid/recipes/openssl/disable-sover.patch
@@ -1,6 +1,6 @@
---- openssl/Makefile	2015-12-28 16:48:49.159522273 -0600
-+++ b/Makefile	2015-12-29 10:31:54.358438402 -0600
-@@ -343,7 +343,7 @@
+--- openssl/Makefile	2016-01-28 17:26:49.159522273 +0100
++++ b/Makefile	2016-01-28 17:26:54.358438402 +0100
+@@ -342,7 +342,7 @@
  link-shared:
  	@ set -e; for i in $(SHLIBDIRS); do \
  		$(MAKE) -f $(HERE)/Makefile.shared -e $(BUILDENV) \
@@ -9,7 +9,7 @@
  			LIBCOMPATVERSIONS=";$(SHLIB_VERSION_HISTORY)" \
  			symlink.$(SHLIB_TARGET); \
  		libs="$$libs -l$$i"; \
-@@ -357,7 +357,7 @@
+@@ -356,7 +356,7 @@
  			libs="$(LIBKRB5) $$libs"; \
  		fi; \
  		$(CLEARENV) && $(MAKE) -f Makefile.shared -e $(BUILDENV) \

OpenSSL 1.0.2e outdated (replaced by 1.0.2f)
A new version has been published today. The recipe no longer can download version 1.0.2e.

Does `disable-solver.patch` still apply?

