Skip to content

Commit

Permalink
Standardise on touch files (#2886)
Browse files Browse the repository at this point in the history
Existing code shells out to run the `touch` Unix command.
This can be done faster (and cross-platform) with `pathlib`'s touch method.

* Created a utility to accept string paths or pathlib paths and applies touch.
* Created test for it.
* Update all instances to use it, and updated their tests.
  • Loading branch information
Julian-O committed Sep 3, 2023
1 parent 3107e4e commit 4d50d7d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
12 changes: 7 additions & 5 deletions pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from pythonforandroid.logger import (logger, info, warning, debug, shprint, info_main)
from pythonforandroid.util import (current_directory, ensure_dir,
BuildInterruptingException, rmdir, move)
from pythonforandroid.logger import (
logger, info, warning, debug, shprint, info_main)
from pythonforandroid.util import (
current_directory, ensure_dir, BuildInterruptingException, rmdir, move,
touch)
from pythonforandroid.util import load_source as import_recipe


Expand Down Expand Up @@ -395,7 +397,7 @@ def download(self):

shprint(sh.rm, '-f', marker_filename)
self.download_file(self.versioned_url, filename)
shprint(sh.touch, marker_filename)
touch(marker_filename)

if exists(filename) and isfile(filename):
for alg, expected_digest in expected_digests.items():
Expand Down Expand Up @@ -530,7 +532,7 @@ def apply_patches(self, arch, build_dir=None):
patch.format(version=self.version, arch=arch.arch),
arch.arch, build_dir=build_dir)

shprint(sh.touch, join(build_dir, '.patched'))
touch(join(build_dir, '.patched'))

def should_build(self, arch):
'''Should perform any necessary test and return True only if it needs
Expand Down
15 changes: 8 additions & 7 deletions pythonforandroid/recipes/protobuf_cpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from pythonforandroid.recipe import CppCompiledComponentsPythonRecipe
from pythonforandroid.logger import shprint, info_notify
from pythonforandroid.util import current_directory
from os.path import exists, join
import sh
from multiprocessing import cpu_count
import os
from os.path import exists, join
from pythonforandroid.toolchain import info
import sh
import sys
import os

from pythonforandroid.recipe import CppCompiledComponentsPythonRecipe
from pythonforandroid.logger import shprint, info_notify
from pythonforandroid.util import current_directory, touch


class ProtobufCppRecipe(CppCompiledComponentsPythonRecipe):
Expand All @@ -30,7 +31,7 @@ def prebuild_arch(self, arch):
patch_mark = join(self.get_build_dir(arch.arch), '.protobuf-patched')
if self.ctx.python_recipe.name == 'python3' and not exists(patch_mark):
self.apply_patch('fix-python3-compatibility.patch', arch.arch)
shprint(sh.touch, patch_mark)
touch(patch_mark)

# During building, host needs to transpile .proto files to .py
# ideally with the same version as protobuf runtime, or with an older one.
Expand Down
7 changes: 4 additions & 3 deletions pythonforandroid/recipes/reportlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import sh

from pythonforandroid.logger import info
from pythonforandroid.recipe import CompiledComponentsPythonRecipe
from pythonforandroid.util import (current_directory, ensure_dir)
from pythonforandroid.logger import (info, shprint)
from pythonforandroid.util import current_directory, ensure_dir, touch


class ReportLabRecipe(CompiledComponentsPythonRecipe):
Expand All @@ -28,7 +29,7 @@ def prebuild_arch(self, arch):

# Apply patches:
self.apply_patch('patches/fix-setup.patch', arch.arch)
shprint(sh.touch, os.path.join(recipe_dir, '.patched'))
touch(os.path.join(recipe_dir, '.patched'))
ft = self.get_recipe('freetype', self.ctx)
ft_dir = ft.get_build_dir(arch.arch)
ft_lib_dir = os.environ.get('_FT_LIB_', os.path.join(ft_dir, 'objs', '.libs'))
Expand Down
5 changes: 5 additions & 0 deletions pythonforandroid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from os.path import exists, join
from os import getcwd, chdir, makedirs, walk
from pathlib import Path
from platform import uname
import shutil
from tempfile import mkdtemp
Expand Down Expand Up @@ -123,3 +124,7 @@ def ensure_dir(dn):
def move(source, destination):
LOGGER.debug("Moving {} to {}".format(source, destination))
shutil.move(source, destination)


def touch(filename):
Path(filename).touch()
2 changes: 1 addition & 1 deletion tests/recipes/test_reportlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_prebuild_arch(self):
# these sh commands are not relevant for the test and need to be mocked
with \
patch('sh.patch'), \
patch('sh.touch'), \
patch('pythonforandroid.recipe.touch'), \
patch('sh.unzip'), \
patch('os.path.isfile'):
self.recipe.prebuild_arch(self.arch)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_download_url_is_set(self):
with (
patch_logger_debug()) as m_debug, (
mock.patch.object(Recipe, 'download_file')) as m_download_file, (
mock.patch('pythonforandroid.recipe.sh.touch')) as m_touch, (
mock.patch('pythonforandroid.recipe.touch')) as m_touch, (
tempfile.TemporaryDirectory()) as temp_dir:
recipe.ctx.setup_dirs(temp_dir)
recipe.download()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,12 @@ def test_move(self):
m_logger.debug.assert_called()
m_logger.error.assert_not_called()
m_logger.reset_mock()

def test_touch(self):
# Just checking the new file case.
# Assume the existing file timestamp case will work if this does.
with TemporaryDirectory() as base_dir:
new_file_path = Path(base_dir) / "new_file"
assert not new_file_path.exists()
util.touch(new_file_path)
assert new_file_path.exists()

0 comments on commit 4d50d7d

Please sign in to comment.