From 04b82d8c851f34e8ad27fbe29a7183bd1560e091 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Wed, 6 May 2020 20:06:51 +0200 Subject: [PATCH] DRY via the find_xcodeproj() helper method Also removes try/except on `pbxproj` imports. This is now part of the `setup.py` dependencies and installed automatically. Users bypassing the `setup.py` should know what they're doing, hence they should be able to read an `ImportError` exception. --- kivy_ios/toolchain.py | 53 ++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/kivy_ios/toolchain.py b/kivy_ios/toolchain.py index 5e2887788..b4807b70a 100755 --- a/kivy_ios/toolchain.py +++ b/kivy_ios/toolchain.py @@ -26,14 +26,9 @@ from pprint import pformat import logging from urllib.request import FancyURLopener, urlcleanup +from pbxproj import XcodeProject +from pbxproj.pbxextensions.ProjectFiles import FileOptions -try: - from pbxproj import XcodeProject - from pbxproj.pbxextensions.ProjectFiles import FileOptions -except ImportError: - print("ERROR: Python requirements are missing") - print("To install: pip install -r requirements.txt") - sys.exit(1) curdir = dirname(__file__) @@ -1241,6 +1236,18 @@ def __init__(self): exit(1) getattr(self, args.command)() + @staticmethod + def find_xcodeproj(filename): + if not filename.endswith(".xcodeproj"): + # try to find the xcodeproj + from glob import glob + xcodeproj = glob(join(filename, "*.xcodeproj")) + if not xcodeproj: + logger.error("Unable to find a xcodeproj in {}".format(filename)) + sys.exit(1) + filename = xcodeproj[0] + return filename + def build(self): ctx = Context() parser = argparse.ArgumentParser( @@ -1385,16 +1392,7 @@ def update(self): parser.add_argument("--add-framework", action="append", help="Additional Frameworks to include with this project") args = parser.parse_args(sys.argv[2:]) - filename = args.filename - if not filename.endswith(".xcodeproj"): - # try to find the xcodeproj - from glob import glob - xcodeproj = glob(join(filename, "*.xcodeproj")) - if not xcodeproj: - logger.error("Unable to find a xcodeproj in {}".format(filename)) - sys.exit(1) - filename = xcodeproj[0] - + filename = self.find_xcodeproj(args.filename) filename = join(filename, "project.pbxproj") if not exists(filename): logger.error("{} not found".format(filename)) @@ -1468,15 +1466,7 @@ def xcode(self): parser = argparse.ArgumentParser(description="Open the xcode project") parser.add_argument("filename", help="Path to your project or xcodeproj") args = parser.parse_args(sys.argv[2:]) - filename = args.filename - if not filename.endswith(".xcodeproj"): - # try to find the xcodeproj - from glob import glob - xcodeproj = glob(join(filename, "*.xcodeproj")) - if not xcodeproj: - logger.error("Unable to find a xcodeproj in {}".format(filename)) - sys.exit(1) - filename = xcodeproj[0] + filename = self.find_xcodeproj(args.filename) sh.open(filename) def _xcassets(self, title, command): @@ -1490,16 +1480,7 @@ def _xcassets(self, title, command): logger.error("image path does not exists.") return - filename = args.filename - if not filename.endswith(".xcodeproj"): - # try to find the xcodeproj - from glob import glob - xcodeproj = glob(join(filename, "*.xcodeproj")) - if not xcodeproj: - logger.error("Unable to find a xcodeproj in {}".format(filename)) - sys.exit(1) - filename = xcodeproj[0] - + filename = self.find_xcodeproj(args.filename) project_name = filename.split("/")[-1].replace(".xcodeproj", "") images_xcassets = realpath(join(filename, "..", project_name, "Images.xcassets"))