Skip to content

Commit 4d50d7d

Browse files
authored
Standardise on touch files (#2886)
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.
1 parent 3107e4e commit 4d50d7d

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

pythonforandroid/recipe.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from urlparse import urlparse
1717
except ImportError:
1818
from urllib.parse import urlparse
19-
from pythonforandroid.logger import (logger, info, warning, debug, shprint, info_main)
20-
from pythonforandroid.util import (current_directory, ensure_dir,
21-
BuildInterruptingException, rmdir, move)
19+
from pythonforandroid.logger import (
20+
logger, info, warning, debug, shprint, info_main)
21+
from pythonforandroid.util import (
22+
current_directory, ensure_dir, BuildInterruptingException, rmdir, move,
23+
touch)
2224
from pythonforandroid.util import load_source as import_recipe
2325

2426

@@ -395,7 +397,7 @@ def download(self):
395397

396398
shprint(sh.rm, '-f', marker_filename)
397399
self.download_file(self.versioned_url, filename)
398-
shprint(sh.touch, marker_filename)
400+
touch(marker_filename)
399401

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

533-
shprint(sh.touch, join(build_dir, '.patched'))
535+
touch(join(build_dir, '.patched'))
534536

535537
def should_build(self, arch):
536538
'''Should perform any necessary test and return True only if it needs

pythonforandroid/recipes/protobuf_cpp/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from pythonforandroid.recipe import CppCompiledComponentsPythonRecipe
2-
from pythonforandroid.logger import shprint, info_notify
3-
from pythonforandroid.util import current_directory
4-
from os.path import exists, join
5-
import sh
61
from multiprocessing import cpu_count
2+
import os
3+
from os.path import exists, join
74
from pythonforandroid.toolchain import info
5+
import sh
86
import sys
9-
import os
7+
8+
from pythonforandroid.recipe import CppCompiledComponentsPythonRecipe
9+
from pythonforandroid.logger import shprint, info_notify
10+
from pythonforandroid.util import current_directory, touch
1011

1112

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

3536
# During building, host needs to transpile .proto files to .py
3637
# ideally with the same version as protobuf runtime, or with an older one.

pythonforandroid/recipes/reportlab/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22
import sh
3+
4+
from pythonforandroid.logger import info
35
from pythonforandroid.recipe import CompiledComponentsPythonRecipe
4-
from pythonforandroid.util import (current_directory, ensure_dir)
5-
from pythonforandroid.logger import (info, shprint)
6+
from pythonforandroid.util import current_directory, ensure_dir, touch
67

78

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

2930
# Apply patches:
3031
self.apply_patch('patches/fix-setup.patch', arch.arch)
31-
shprint(sh.touch, os.path.join(recipe_dir, '.patched'))
32+
touch(os.path.join(recipe_dir, '.patched'))
3233
ft = self.get_recipe('freetype', self.ctx)
3334
ft_dir = ft.get_build_dir(arch.arch)
3435
ft_lib_dir = os.environ.get('_FT_LIB_', os.path.join(ft_dir, 'objs', '.libs'))

pythonforandroid/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from os.path import exists, join
55
from os import getcwd, chdir, makedirs, walk
6+
from pathlib import Path
67
from platform import uname
78
import shutil
89
from tempfile import mkdtemp
@@ -123,3 +124,7 @@ def ensure_dir(dn):
123124
def move(source, destination):
124125
LOGGER.debug("Moving {} to {}".format(source, destination))
125126
shutil.move(source, destination)
127+
128+
129+
def touch(filename):
130+
Path(filename).touch()

tests/recipes/test_reportlab.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_prebuild_arch(self):
3030
# these sh commands are not relevant for the test and need to be mocked
3131
with \
3232
patch('sh.patch'), \
33-
patch('sh.touch'), \
33+
patch('pythonforandroid.recipe.touch'), \
3434
patch('sh.unzip'), \
3535
patch('os.path.isfile'):
3636
self.recipe.prebuild_arch(self.arch)

tests/test_recipe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_download_url_is_set(self):
134134
with (
135135
patch_logger_debug()) as m_debug, (
136136
mock.patch.object(Recipe, 'download_file')) as m_download_file, (
137-
mock.patch('pythonforandroid.recipe.sh.touch')) as m_touch, (
137+
mock.patch('pythonforandroid.recipe.touch')) as m_touch, (
138138
tempfile.TemporaryDirectory()) as temp_dir:
139139
recipe.ctx.setup_dirs(temp_dir)
140140
recipe.download()

tests/test_util.py

+9
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,12 @@ def test_move(self):
178178
m_logger.debug.assert_called()
179179
m_logger.error.assert_not_called()
180180
m_logger.reset_mock()
181+
182+
def test_touch(self):
183+
# Just checking the new file case.
184+
# Assume the existing file timestamp case will work if this does.
185+
with TemporaryDirectory() as base_dir:
186+
new_file_path = Path(base_dir) / "new_file"
187+
assert not new_file_path.exists()
188+
util.touch(new_file_path)
189+
assert new_file_path.exists()

0 commit comments

Comments
 (0)