Skip to content

Commit

Permalink
Use buildops for file_exists and file_matches
Browse files Browse the repository at this point in the history
This is part of a larger refactor, to reduce the size and complexity of the Buildozer class.

* Remove `file_matches()` method from the Buildozer class.
* Change the singular reference to it in `android.py` to use the buildops function.
   * This raises the question: Will this be useful to other targets in the future, or should this be moved into the android target? Left for future refactors.

* Remove the `file_exists()` method from the Buildozer class.
* Change all the references to use the buildops function.
   * Note for consistency with the other buildop functions, buildops.file_exists() takes a single parameter rather than `*args` and doesn't join the path together. One client call needed to be changed to accommodate that.
   * Note that in several places, the code checks for the existence of *directories* with os.path.exists(). I did **not** change these over because:
     * Historically, the name as been **file**_exists().
     * With the join statement removed, this function doesn't add anything to calling `os.path.exists()` or `pathlib.Path.exists()`, so enforcing it on the code seemed counter-productive. I went with for consistency with existing code.
* Update mocks
  • Loading branch information
Julian-O committed Aug 26, 2023
1 parent 722f440 commit b72d309
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 42 deletions.
21 changes: 5 additions & 16 deletions buildozer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, filename='buildozer.spec', target=None):

self.logger = Logger()

if exists(filename):
if buildops.file_exists(filename):
self.config.read(filename, "utf-8")
self.check_configuration_tokens()

Expand Down Expand Up @@ -356,7 +356,7 @@ def check_build_layout(self):
'''
self.logger.info('Ensure build layout')

if not exists(self.specfilename):
if not buildops.file_exists(self.specfilename):
print('No {0} found in the current directory. Abandon.'.format(
self.specfilename))
exit(1)
Expand Down Expand Up @@ -440,7 +440,7 @@ def _ensure_virtualenv(self):
if hasattr(self, 'venv'):
return
self.venv = join(self.buildozer_dir, 'venv')
if not self.file_exists(self.venv):
if not buildops.file_exists(self.venv):
self.cmd(["python3", "-m", "venv", "./venv"],
cwd=self.buildozer_dir)

Expand All @@ -465,17 +465,6 @@ def _ensure_virtualenv(self):
self.env_venv['CC'] = '/bin/false'
self.env_venv['CXX'] = '/bin/false'

def file_matches(self, patterns):
from glob import glob
result = []
for pattern in patterns:
matches = glob(expanduser(pattern.strip()))
result.extend(matches)
return result

def file_exists(self, *args):
return exists(join(*args))

def file_extract(self, archive, cwd=None):
if archive.endswith('.tgz') or archive.endswith('.tar.gz'):
self.cmd(["tar", "xzf", archive], cwd=cwd)
Expand Down Expand Up @@ -518,7 +507,7 @@ def report_hook(index, blksize, size):
url = url + filename
if cwd:
filename = join(cwd, filename)
if self.file_exists(filename):
if buildops.file_exists(filename):
unlink(filename)

self.logger.debug('Downloading {0}'.format(url))
Expand Down Expand Up @@ -674,7 +663,7 @@ def _add_sitecustomize(self):
join(self.app_dir, 'sitecustomize.py'))

main_py = join(self.app_dir, 'service', 'main.py')
if not self.file_exists(main_py):
if not buildops.file_exists(main_py):
return

header = (b'import sys, os; '
Expand Down
2 changes: 1 addition & 1 deletion buildozer/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def install_or_update_repo(self, repo, **kwargs):
cmd = self.buildozer.cmd
install_dir = join(self.buildozer.platform_dir, repo)
custom_dir, clone_url, clone_branch = self.path_or_git_url(repo, **kwargs)
if not self.buildozer.file_exists(install_dir):
if not buildops.file_exists(install_dir):
if custom_dir:
buildops.mkdir(install_dir)
buildops.file_copytree(custom_dir, install_dir)
Expand Down
24 changes: 12 additions & 12 deletions buildozer/targets/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def check_requirements(self):
# Check for C header <zlib.h>.
is_debian_like = which("dpkg") is not None
if is_debian_like and \
not self.buildozer.file_exists('/usr/include/zlib.h'):
not buildops.file_exists('/usr/include/zlib.h'):
raise BuildozerException(
'zlib headers must be installed, '
'run: sudo apt-get install zlib1g-dev')
Expand Down Expand Up @@ -344,7 +344,7 @@ def _locate_java(self, s):

def _install_apache_ant(self):
ant_dir = self.apache_ant_dir
if self.buildozer.file_exists(ant_dir):
if buildops.file_exists(ant_dir):
self.logger.info('Apache ANT found at {0}'.format(ant_dir))
return ant_dir

Expand All @@ -364,7 +364,7 @@ def _install_apache_ant(self):

def _install_android_sdk(self):
sdk_dir = self.android_sdk_dir
if self.buildozer.file_exists(sdk_dir):
if buildops.file_exists(sdk_dir):
self.logger.info('Android SDK found at {0}'.format(sdk_dir))
return sdk_dir

Expand Down Expand Up @@ -396,7 +396,7 @@ def _install_android_sdk(self):

def _install_android_ndk(self):
ndk_dir = self.android_ndk_dir
if self.buildozer.file_exists(ndk_dir):
if buildops.file_exists(ndk_dir):
self.logger.info('Android NDK found at {0}'.format(ndk_dir))
return ndk_dir

Expand Down Expand Up @@ -579,7 +579,7 @@ def _install_android_packages(self):
# 3. finally, install the android for the current api
self.logger.info('Downloading platform api target if necessary')
android_platform = join(self.android_sdk_dir, 'platforms', 'android-{}'.format(self.android_api))
if not self.buildozer.file_exists(android_platform):
if not buildops.file_exists(android_platform):
if not skip_upd:
self._sdkmanager(f"platforms;android-{self.android_api}")
else:
Expand Down Expand Up @@ -663,14 +663,14 @@ def _install_p4a(self):
'p4a.source_dir')
if system_p4a_dir:
# Don't install anything, just check that the dir does exist
if not self.buildozer.file_exists(p4a_dir):
if not buildops.file_exists(p4a_dir):
self.logger.error(
'Path for p4a.source_dir does not exist')
self.logger.error('')
raise BuildozerException()
else:
# check that url/branch has not been changed
if self.buildozer.file_exists(p4a_dir):
if buildops.file_exists(p4a_dir):
cur_url = cmd(
["git", "config", "--get", "remote.origin.url"],
get_stdout=True,
Expand All @@ -685,7 +685,7 @@ def _install_p4a(self):
)
buildops.rmdir(p4a_dir)

if not self.buildozer.file_exists(p4a_dir):
if not buildops.file_exists(p4a_dir):
cmd(
[
"git",
Expand Down Expand Up @@ -1053,7 +1053,7 @@ def build_package(self):
continue

self.logger.debug('Search and copy libs for {}'.format(lib_dir))
for fn in self.buildozer.file_matches(patterns):
for fn in buildops.file_matches(patterns):
buildops.file_copy(
join(self.buildozer.root_dir, fn),
join(dist_dir, 'libs', lib_dir, basename(fn)))
Expand Down Expand Up @@ -1305,7 +1305,7 @@ def _update_libraries_references(self, dist_dir):
# ensure the project.properties exist
project_fn = join(dist_dir, 'project.properties')

if not self.buildozer.file_exists(project_fn):
if not buildops.file_exists(project_fn):
content = [
'target=android-{}\n'.format(self.android_api),
'APP_PLATFORM={}\n'.format(self.android_minapi)]
Expand All @@ -1328,7 +1328,7 @@ def _update_libraries_references(self, dist_dir):
for cref in app_references:
# get the full path of the current reference
ref = realpath(join(source_dir, cref))
if not self.buildozer.file_exists(ref):
if not buildops.file_exists(ref):
self.logger.error(
'Invalid library reference (path not found): {}'.format(
cref))
Expand Down Expand Up @@ -1402,7 +1402,7 @@ def cmd_deploy(self, *args):
# search the APK in the bin dir
apk = state['android:latestapk']
full_apk = join(self.buildozer.bin_dir, apk)
if not self.buildozer.file_exists(full_apk):
if not buildops.file_exists(full_apk):
self.logger.error(
'Unable to found the latest APK. Please run "debug" again.')

Expand Down
9 changes: 5 additions & 4 deletions buildozer/targets/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def compile_platform(self):

self.toolchain(["build", *ios_requirements])

if not self.buildozer.file_exists(self.ios_deploy_dir, 'ios-deploy'):
if not buildops.file_exists(
join(self.ios_deploy_dir, 'ios-deploy')):
self.xcodebuild(cwd=self.ios_deploy_dir)

self.buildozer.state['ios.requirements'] = ios_requirements
Expand All @@ -207,7 +208,7 @@ def build_package(self):
frameworks_cmd.append(f"--add-framework={framework}")

self.app_project_dir = join(self.ios_dir, '{0}-ios'.format(app_name.lower()))
if not self.buildozer.file_exists(self.app_project_dir):
if not buildops.file_exists(self.app_project_dir):
cmd = ["create", *frameworks_cmd, app_name, self.buildozer.app_dir]
else:
cmd = ["update", *frameworks_cmd, f"{app_name}-ios"]
Expand Down Expand Up @@ -364,7 +365,7 @@ def _create_icons(self):
if not icon:
return
icon_fn = join(self.buildozer.app_dir, icon)
if not self.buildozer.file_exists(icon_fn):
if not buildops.file_exists(icon_fn):
self.logger.error('Icon {} does not exists'.format(icon_fn))
return

Expand Down Expand Up @@ -416,7 +417,7 @@ def _get_available_identities(self):
def _unlock_keychain(self):
password_file = join(self.buildozer.buildozer_dir, '.ioscodesign')
password = None
if self.buildozer.file_exists(password_file):
if buildops.file_exists(password_file):
with open(password_file) as fd:
password = fd.read()

Expand Down
6 changes: 3 additions & 3 deletions tests/targets/test_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
patch_buildozer,
patch_buildozer_checkbin,
patch_buildozer_cmd,
patch_buildozer_file_exists,
patch_buildops_file_exists,
)


Expand Down Expand Up @@ -182,15 +182,15 @@ def test_check_configuration_tokens(self):
def test_install_android_sdk(self, platform):
"""Basic tests for the _install_android_sdk() method."""
target_android = init_target(self.temp_dir)
with patch_buildozer_file_exists() as m_file_exists, patch_buildozer_download() as m_download:
with patch_buildops_file_exists() as m_file_exists, patch_buildozer_download() as m_download:
m_file_exists.return_value = True
sdk_dir = target_android._install_android_sdk()
assert m_file_exists.call_args_list == [
mock.call(target_android.android_sdk_dir)
]
assert m_download.call_args_list == []
assert sdk_dir.endswith(".buildozer/android/platform/android-sdk")
with patch_buildozer_file_exists() as m_file_exists, \
with patch_buildops_file_exists() as m_file_exists, \
patch_buildozer_download() as m_download, \
patch_buildozer_file_extract() as m_file_extract, \
patch_platform(platform):
Expand Down
7 changes: 4 additions & 3 deletions tests/targets/test_ios.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
import sys
import tempfile
from unittest import mock
Expand All @@ -10,7 +11,7 @@
init_buildozer,
patch_buildozer_checkbin,
patch_buildozer_cmd,
patch_buildozer_file_exists,
patch_buildops_file_exists,
patch_logger_error,
)

Expand Down Expand Up @@ -145,15 +146,15 @@ def test_compile_platform(self):
# fmt: off
with patch_target_ios("get_available_packages") as m_get_available_packages, \
patch_target_ios("toolchain") as m_toolchain, \
patch_buildozer_file_exists() as m_file_exists:
patch_buildops_file_exists() as m_file_exists:
m_get_available_packages.return_value = ["hostpython3", "python3"]
m_file_exists.return_value = True
target.compile_platform()
# fmt: on
assert m_get_available_packages.call_args_list == [mock.call()]
assert m_toolchain.call_args_list == [mock.call(["build", "python3"])]
assert m_file_exists.call_args_list == [
mock.call(target.ios_deploy_dir, "ios-deploy")
mock.call(os.path.join(target.ios_deploy_dir, "ios-deploy"))
]

def test_get_package(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/targets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def patch_buildozer_checkbin():
return patch_buildozer("checkbin")


def patch_buildozer_file_exists():
return patch_buildozer("file_exists")
def patch_buildops_file_exists():
return mock.patch("buildozer.buildops.file_exists")


def patch_logger_error():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_buildozer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_android_ant_path(self):
assert download.call_args_list == [
mock.call("https://archive.apache.org/dist/ant/binaries/", mock.ANY, cwd=my_ant_path)]
# Mock ant already installed
with mock.patch.object(Buildozer, 'file_exists', return_value=True):
with mock.patch('buildozer.buildops.file_exists', return_value=True):
ant_path = target._install_apache_ant()
assert ant_path == my_ant_path

Expand Down

0 comments on commit b72d309

Please sign in to comment.