Skip to content

Commit 6246b48

Browse files
committed
Refactor hostpython3 recipe into TargetHostPythonRecipe
1 parent 5fc5241 commit 6246b48

File tree

2 files changed

+82
-44
lines changed

2 files changed

+82
-44
lines changed

pythonforandroid/recipe.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,80 @@ def reduce_object_file_names(self, dirn):
10911091
shprint(sh.mv, filen, join(file_dirname, parts[0] + '.so'))
10921092

10931093

1094+
class TargetHostPythonRecipe(Recipe):
1095+
'''
1096+
This is the base class for the hostpython recipes. This class will take
1097+
care to do all the work to build a hostpython recipe but, be careful, it
1098+
is intended to be subclassed because some of the vars needs to be set:
1099+
1100+
- :attr:`name`
1101+
- :attr:`version`
1102+
1103+
.. versionadded:: 0.6.0
1104+
'''
1105+
1106+
name = ''
1107+
'''The hostpython's recipe name. This should be ``hostpython2`` or
1108+
``hostpython3``
1109+
1110+
.. warning:: This must be set in inherited class.'''
1111+
1112+
version = ''
1113+
'''The hostpython's recipe version.
1114+
1115+
.. warning:: This must be set in inherited class.'''
1116+
1117+
build_subdir = 'native-build'
1118+
'''Specify the sub build directory for the hostpython recipe. Defaults
1119+
to ``native-build``.'''
1120+
1121+
url = 'https://www.python.org/ftp/python/{version}/Python-{version}.tgz'
1122+
'''The default url to download our host python recipe. This url will
1123+
change depending on the python version set in attribute :attr:`version`.'''
1124+
1125+
def get_build_container_dir(self, arch=None):
1126+
choices = self.check_recipe_choices()
1127+
dir_name = '-'.join([self.name] + choices)
1128+
return join(self.ctx.build_dir, 'other_builds', dir_name, 'desktop')
1129+
1130+
def get_build_dir(self, arch=None):
1131+
'''
1132+
.. note:: Unlike other recipes, the hostpython build dir doesn't
1133+
depend on the target arch
1134+
'''
1135+
return join(self.get_build_container_dir(), self.name)
1136+
1137+
def get_path_to_python(self):
1138+
return join(self.get_build_dir(), self.build_subdir)
1139+
1140+
def build_arch(self, arch):
1141+
recipe_build_dir = self.get_build_dir(arch.arch)
1142+
1143+
# Create a subdirectory to actually perform the build
1144+
build_dir = join(recipe_build_dir, self.build_subdir)
1145+
ensure_dir(build_dir)
1146+
1147+
if not exists(join(build_dir, 'python')):
1148+
with current_directory(recipe_build_dir):
1149+
# Configure the build
1150+
with current_directory(build_dir):
1151+
if not exists('config.status'):
1152+
shprint(
1153+
sh.Command(join(recipe_build_dir, 'configure')))
1154+
1155+
# Create the Setup file. This copying from Setup.dist
1156+
# seems to be the normal and expected procedure.
1157+
shprint(sh.cp, join('Modules', 'Setup.dist'),
1158+
join(build_dir, 'Modules', 'Setup'))
1159+
1160+
result = shprint(sh.make, '-C', build_dir)
1161+
else:
1162+
info('Skipping {name} ({version}) build, as it has already '
1163+
'been completed'.format(name=self.name, version=self.version))
1164+
1165+
self.ctx.hostpython = join(build_dir, 'python')
1166+
1167+
10941168
def md5sum(filen):
10951169
'''Calculate the md5sum of a file.
10961170
'''
Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
1-
from pythonforandroid.toolchain import Recipe, shprint, info
2-
from pythonforandroid.util import ensure_dir, current_directory
3-
from os.path import join, exists
4-
import sh
1+
from pythonforandroid.recipe import TargetHostPythonRecipe
52

6-
BUILD_SUBDIR = 'native-build'
73

4+
class Hostpython3Recipe(TargetHostPythonRecipe):
5+
'''
6+
The hostpython3's recipe.
87
9-
class Hostpython3Recipe(Recipe):
8+
.. versionchanged:: 0.6.0
9+
Refactored into the new class
10+
:class:`~pythonforandroid.recipe.TargetHostPythonRecipe`
11+
'''
1012
version = '3.7.1'
11-
url = 'https://www.python.org/ftp/python/{version}/Python-{version}.tgz'
1213
name = 'hostpython3'
13-
1414
conflicts = ['hostpython2', 'hostpython3crystax']
1515

16-
def get_build_container_dir(self, arch=None):
17-
choices = self.check_recipe_choices()
18-
dir_name = '-'.join([self.name] + choices)
19-
return join(self.ctx.build_dir, 'other_builds', dir_name, 'desktop')
20-
21-
def get_build_dir(self, arch=None):
22-
# Unlike other recipes, the hostpython build dir doesn't depend on the target arch
23-
return join(self.get_build_container_dir(), self.name)
24-
25-
def get_path_to_python(self):
26-
return join(self.get_build_dir(), BUILD_SUBDIR)
27-
28-
def build_arch(self, arch):
29-
recipe_build_dir = self.get_build_dir(arch.arch)
30-
31-
# Create a subdirectory to actually perform the build
32-
build_dir = join(recipe_build_dir, BUILD_SUBDIR)
33-
ensure_dir(build_dir)
34-
35-
if not exists(join(build_dir, 'python')):
36-
with current_directory(recipe_build_dir):
37-
# Configure the build
38-
with current_directory(build_dir):
39-
if not exists('config.status'):
40-
shprint(sh.Command(join(recipe_build_dir, 'configure')))
41-
42-
# Create the Setup file. This copying from Setup.dist
43-
# seems to be the normal and expected procedure.
44-
shprint(sh.cp, join('Modules', 'Setup.dist'), join(build_dir, 'Modules', 'Setup'))
45-
46-
result = shprint(sh.make, '-C', build_dir)
47-
else:
48-
info('Skipping hostpython3 build as it has already been completed')
49-
50-
self.ctx.hostpython = join(build_dir, 'python')
51-
5216

5317
recipe = Hostpython3Recipe()

0 commit comments

Comments
 (0)