Skip to content

Commit

Permalink
Standardise ensure_dir and rmdir
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-O committed Jul 31, 2023
1 parent 7a0c935 commit 7a1d7b8
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 174 deletions.
18 changes: 8 additions & 10 deletions pythonforandroid/bdistapk.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from glob import glob
from os.path import realpath, join, dirname, curdir, basename, split
from setuptools import Command

from shutil import copyfile
import sys
from os.path import realpath, join, exists, dirname, curdir, basename, split
from os import makedirs
from glob import glob
from shutil import rmtree, copyfile

from pythonforandroid.util import rmdir, ensure_dir


def argv_contains(t):
Expand Down Expand Up @@ -90,9 +90,8 @@ def prepare_build_dir(self):
'that.')

bdist_dir = 'build/bdist.android-{}'.format(self.arch)
if exists(bdist_dir):
rmtree(bdist_dir)
makedirs(bdist_dir)
rmdir(bdist_dir)
ensure_dir(bdist_dir)

globs = []
for directory, patterns in self.distribution.package_data.items():
Expand All @@ -107,8 +106,7 @@ def prepare_build_dir(self):
if not argv_contains('--launcher'):
for filen in filens:
new_dir = join(bdist_dir, dirname(filen))
if not exists(new_dir):
makedirs(new_dir)
ensure_dir(new_dir)
print('Including {}'.format(filen))
copyfile(filen, join(bdist_dir, filen))
if basename(filen) in ('main.py', 'main.pyc'):
Expand Down
6 changes: 3 additions & 3 deletions pythonforandroid/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from pythonforandroid.logger import (shprint, info, logger, debug)
from pythonforandroid.util import (
current_directory, ensure_dir, temp_directory, BuildInterruptingException)
current_directory, ensure_dir, temp_directory, BuildInterruptingException,
rmdir)
from pythonforandroid.recipe import Recipe


Expand Down Expand Up @@ -70,7 +71,6 @@ class Bootstrap:
'''An Android project template, containing recipe stuff for
compilation and templated fields for APK info.
'''
name = ''
jni_subdir = '/jni'
ctx = None

Expand Down Expand Up @@ -397,7 +397,7 @@ def fry_eggs(self, sitepackages):
files = [join(rd, f) for f in listdir(rd) if f != 'EGG-INFO']
if files:
shprint(sh.mv, '-t', sitepackages, *files)
shprint(sh.rm, '-rf', d)
rmdir(d)


def expand_dependencies(recipes, ctx):
Expand Down
17 changes: 7 additions & 10 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from fnmatch import fnmatch
import jinja2

from pythonforandroid.util import rmdir, ensure_dir


def get_dist_info_for(key, error_if_missing=True):
try:
Expand Down Expand Up @@ -93,11 +95,6 @@ def get_bootstrap_name():
DEFAULT_PYTHON_SERVICE_JAVA_CLASS = 'org.kivy.android.PythonService'


def ensure_dir(path):
if not exists(path):
makedirs(path)


def render(template, dest, **kwargs):
'''Using jinja2, render `template` to the filename `dest`, supplying the
Expand Down Expand Up @@ -241,7 +238,7 @@ def make_package(args):
assets_dir = "src/main/assets"

# Delete the old assets.
shutil.rmtree(assets_dir, ignore_errors=True)
rmdir(assets_dir, ignore_errors=True)
ensure_dir(assets_dir)

# Add extra environment variable file into tar-able directory:
Expand Down Expand Up @@ -290,7 +287,7 @@ def make_package(args):
not exists(
join(main_py_only_dir, dir_path)
)):
os.mkdir(join(main_py_only_dir, dir_path))
ensure_dir(join(main_py_only_dir, dir_path))
# Copy actual file:
shutil.copyfile(
join(args.private, variant),
Expand Down Expand Up @@ -328,17 +325,17 @@ def make_package(args):
)
finally:
for directory in _temp_dirs_to_clean:
shutil.rmtree(directory)
rmdir(directory)

# Remove extra env vars tar-able directory:
shutil.rmtree(env_vars_tarpath)
rmdir(env_vars_tarpath)

# Prepare some variables for templating process
res_dir = "src/main/res"
res_dir_initial = "src/res_initial"
# make res_dir stateless
if exists(res_dir_initial):
shutil.rmtree(res_dir, ignore_errors=True)
rmdir(res_dir, ignore_errors=True)
shutil.copytree(res_dir_initial, res_dir)
else:
shutil.copytree(res_dir, res_dir_initial)
Expand Down
10 changes: 6 additions & 4 deletions pythonforandroid/bootstraps/sdl2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pythonforandroid.toolchain import (
Bootstrap, shprint, current_directory, info, info_main)
from pythonforandroid.util import ensure_dir
from os.path import join

import sh

from pythonforandroid.toolchain import (
Bootstrap, shprint, current_directory, info, info_main)
from pythonforandroid.util import ensure_dir, rmdir


class SDL2GradleBootstrap(Bootstrap):
name = 'sdl2'
Expand All @@ -15,8 +17,8 @@ class SDL2GradleBootstrap(Bootstrap):
def assemble_distribution(self):
info_main("# Creating Android project ({})".format(self.name))

rmdir(self.dist_dir)
info("Copying SDL2/gradle build")
shprint(sh.rm, "-rf", self.dist_dir)
shprint(sh.cp, "-r", self.build_dir, self.dist_dir)

# either the build use environment variable (ANDROID_HOME)
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/bootstraps/service_only/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from os.path import join
from pythonforandroid.toolchain import (
Bootstrap, current_directory, info, info_main, shprint)
from pythonforandroid.util import ensure_dir
from pythonforandroid.util import ensure_dir, rmdir


class ServiceOnlyBootstrap(Bootstrap):
Expand All @@ -18,7 +18,7 @@ def assemble_distribution(self):
self.name))

info('This currently just copies the build stuff straight from the build dir.')
shprint(sh.rm, '-rf', self.dist_dir)
rmdir(self.dist_dir)
shprint(sh.cp, '-r', self.build_dir, self.dist_dir)
with current_directory(self.dist_dir):
with open('local.properties', 'w') as fileh:
Expand Down
8 changes: 5 additions & 3 deletions pythonforandroid/bootstraps/webview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from pythonforandroid.toolchain import Bootstrap, current_directory, info, info_main, shprint
from pythonforandroid.util import ensure_dir
from os.path import join

import sh

from pythonforandroid.toolchain import Bootstrap, current_directory, info, info_main, shprint
from pythonforandroid.util import ensure_dir, rmdir


class WebViewBootstrap(Bootstrap):
name = 'webview'
Expand All @@ -15,7 +17,7 @@ def assemble_distribution(self):
info_main('# Creating Android project from build and {} bootstrap'.format(
self.name))

shprint(sh.rm, '-rf', self.dist_dir)
rmdir(self.dist_dir)
shprint(sh.cp, '-r', self.build_dir, self.dist_dir)
with current_directory(self.dist_dir):
with open('local.properties', 'w') as fileh:
Expand Down
32 changes: 14 additions & 18 deletions pythonforandroid/build.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
from contextlib import suppress
import copy
import glob
import os
from os import environ
from os.path import (
abspath, join, realpath, dirname, expanduser, exists
)
from os import environ
import copy
import os
import glob
import re
import sh
import shutil
import subprocess
from contextlib import suppress

from pythonforandroid.util import (
current_directory, ensure_dir,
BuildInterruptingException,
)
from pythonforandroid.logger import (info, warning, info_notify, info_main, shprint)
import sh

from pythonforandroid.androidndk import AndroidNDK
from pythonforandroid.archs import ArchARM, ArchARMv7_a, ArchAarch_64, Archx86, Archx86_64
from pythonforandroid.logger import (info, warning, info_notify, info_main, shprint)
from pythonforandroid.pythonpackage import get_package_name
from pythonforandroid.recipe import CythonRecipe, Recipe
from pythonforandroid.recommendations import (
check_ndk_version, check_target_api, check_ndk_api,
RECOMMENDED_NDK_API, RECOMMENDED_TARGET_API)
from pythonforandroid.androidndk import AndroidNDK
from pythonforandroid.util import (
current_directory, ensure_dir,
BuildInterruptingException, rmdir
)


def get_targets(sdk_dir):
Expand Down Expand Up @@ -77,11 +78,6 @@ class Context:
# the Android project folder where everything ends up
dist_dir = None

# where Android libs are cached after build
# but before being placed in dists
libs_dir = None
aars_dir = None

# Whether setup.py or similar should be used if present:
use_setup_py = False

Expand Down Expand Up @@ -642,7 +638,7 @@ def run_setuppy_install(ctx, project_dir, env=None, arch=None):
for f in set(copied_over_contents + new_venv_additions):
full_path = os.path.join(venv_site_packages_dir, f)
if os.path.isdir(full_path):
shutil.rmtree(full_path)
rmdir(full_path)
else:
os.remove(full_path)
finally:
Expand Down
13 changes: 7 additions & 6 deletions pythonforandroid/distribution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from os.path import exists, join
import glob
import json
import glob
from os.path import exists, join

from pythonforandroid.logger import (debug, info, info_notify, warning, Err_Style, Err_Fore)
from pythonforandroid.util import current_directory, BuildInterruptingException
from shutil import rmtree
from pythonforandroid.logger import (
debug, info, info_notify, warning, Err_Style, Err_Fore)
from pythonforandroid.util import (
current_directory, BuildInterruptingException, rmdir)


class Distribution:
Expand Down Expand Up @@ -201,7 +202,7 @@ def folder_exists(self):
return exists(self.dist_dir)

def delete(self):
rmtree(self.dist_dir)
rmdir(self.dist_dir)

@classmethod
def get_distributions(cls, ctx, extra_dist_dirs=[]):
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_dependency_tuple_list_for_recipe(recipe, blacklist=None):
"""
if blacklist is None:
blacklist = set()
assert type(blacklist) == set
assert type(blacklist) is set
if recipe.depends is None:
dependencies = []
else:
Expand Down Expand Up @@ -160,7 +160,7 @@ def obvious_conflict_checker(ctx, name_tuples, blacklist=None):
current_to_be_added = list(to_be_added)
to_be_added = []
for (added_tuple, adding_recipe) in current_to_be_added:
assert type(added_tuple) == tuple
assert type(added_tuple) is tuple
if len(added_tuple) > 1:
# No obvious commitment in what to add, don't check it itself
# but throw it into deps for later comparing against
Expand Down
16 changes: 6 additions & 10 deletions pythonforandroid/prerequisites.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python3

import sys
import platform
import os
import subprocess
import platform
import shutil
import subprocess
import sys

from pythonforandroid.logger import info, warning, error
from pythonforandroid.util import ensure_dir


class Prerequisite(object):
Expand Down Expand Up @@ -247,13 +249,7 @@ def darwin_installer(self):
"~/Library/Java/JavaVirtualMachines"
)
info(f"Extracting {filename} to {user_library_java_path}")
subprocess.check_output(
[
"mkdir",
"-p",
user_library_java_path,
],
)
ensure_dir(user_library_java_path)
subprocess.check_output(
["tar", "xzf", f"/tmp/{filename}", "-C", user_library_java_path],
)
Expand Down
Loading

0 comments on commit 7a1d7b8

Please sign in to comment.