Skip to content

Commit d786718

Browse files
committed
Refactored out function for getting valid filens from a folder
1 parent 1ebdcee commit d786718

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

pythonforandroid/recipes/python3/__init__.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
from pythonforandroid.toolchain import shprint, current_directory
33
from pythonforandroid.patching import (is_darwin, is_api_gt,
44
check_all, is_api_lt, is_ndk)
5-
from pythonforandroid.logger import logger, info, debug
6-
from pythonforandroid.util import ensure_dir
5+
from pythonforandroid.logger import logger, info
6+
from pythonforandroid.util import ensure_dir, walk_valid_filens
77
from os.path import exists, join, realpath, basename
88
from os import environ, listdir, walk
99
import glob
10-
from fnmatch import fnmatch
1110
import sh
1211

1312

@@ -152,7 +151,7 @@ def create_python_bundle(self, dirn, arch):
152151
# zip up the standard library
153152
stdlib_zip = join(dirn, 'stdlib.zip')
154153
with current_directory(join(self.get_build_dir(arch.arch), 'Lib')):
155-
stdlib_filens = self.get_stdlib_filens('.')
154+
stdlib_filens = walk_valid_filens('.', STDLIB_DIR_BLACKLIST, STDLIB_FILEN_BLACKLIST)
156155
shprint(sh.zip, stdlib_zip, *stdlib_filens)
157156

158157
# copy the site-packages into place
@@ -174,24 +173,5 @@ def create_python_bundle(self, dirn, arch):
174173
info('Renaming .so files to reflect cross-compile')
175174
self.reduce_object_file_names(join(dirn, 'site-packages'))
176175

177-
def get_stdlib_filens(self, basedir):
178-
return_filens = []
179-
for dirn, subdirs, filens in walk(basedir):
180-
if basename(dirn) in STDLIB_DIR_BLACKLIST:
181-
debug('stdlib.zip ignoring directory {}'.format(dirn))
182-
while subdirs:
183-
subdirs.pop()
184-
continue
185-
for filen in filens:
186-
for pattern in STDLIB_FILEN_BLACKLIST:
187-
if fnmatch(filen, pattern):
188-
debug('stdlib.zip ignoring file {}'.format(join(dirn, filen)))
189-
break
190-
else:
191-
return_filens.append(join(dirn, filen))
192-
return return_filens
193-
194-
195-
196176

197177
recipe = Python3Recipe()

pythonforandroid/util.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import contextlib
2-
from os.path import exists
3-
from os import getcwd, chdir, makedirs
2+
from os.path import exists, join
3+
from os import getcwd, chdir, makedirs, walk
44
import io
55
import json
66
import shutil
77
import sys
8+
from fnmatch import fnmatch
89
from tempfile import mkdtemp
910
try:
1011
from urllib.request import FancyURLopener
@@ -124,3 +125,33 @@ def is_exe(fpath):
124125
return exe_file
125126

126127
return None
128+
129+
def walk_valid_filens(base_dir, invalid_dir_names, invalid_file_patterns):
130+
"""Recursively walks all the files and directories in ``dirn``,
131+
ignoring directories that match any pattern in ``invalid_dirns``
132+
and files that patch any pattern in ``invalid_filens``.
133+
134+
``invalid_dirns`` and ``invalid_filens`` should both be lists of
135+
strings to match. ``invalid_dir_patterns`` expects a list of
136+
invalid directory names, while ``invalid_file_patterns`` expects a
137+
list of glob patterns compared against the full filepath.
138+
139+
File and directory paths are evaluated as full paths relative to ``dirn``.
140+
141+
"""
142+
143+
return_filens = []
144+
for dirn, subdirs, filens in walk(base_dir):
145+
146+
# Remove invalid subdirs so that they will not be walked
147+
for i in reversed(range(len(subdirs))):
148+
subdir = subdirs[i]
149+
if subdir in invalid_dir_names:
150+
subdirs.pop(i)
151+
152+
for filen in filens:
153+
for pattern in invalid_file_patterns:
154+
if fnmatch(filen, pattern):
155+
break
156+
else:
157+
yield join(dirn, filen)

0 commit comments

Comments
 (0)