From bfac59d6724384974292a20ddda0e18dad714f6b Mon Sep 17 00:00:00 2001 From: TheRealKeto Date: Sat, 19 Feb 2022 20:16:44 -0500 Subject: [PATCH 1/5] Don't be a noob: Use f-strings Signed-off-by: TheRealKeto --- index.py | 76 +++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/index.py b/index.py index 8c6fb43..0c25b36 100644 --- a/index.py +++ b/index.py @@ -22,13 +22,13 @@ def main(): # Step 0: Clean up "docs" and "temp" folder ########### - root = os.path.dirname(os.path.abspath(__file__)) + "/" + root = os.path.dirname(os.path.abspath(__file__)) # Remove everything except for the DEBs. DepictionGenerator.CleanUp() try: - shutil.rmtree(root + "temp/") + shutil.rmtree(f"{root}/temp/") except Exception: pass @@ -53,7 +53,7 @@ def main(): # Create folder for each tweak for tweak in tweak_release: - PackageLister.CreateFolder("docs/assets/" + tweak['bundle_id']) + PackageLister.CreateFolder(f"docs/assets/{tweak['bundle_id']}") ########### # Step 2: Copy all images @@ -62,54 +62,54 @@ def main(): package_bundle_id = PackageLister.DirNameToBundleID(package_name) try: - shutil.copy(root + "Packages/" + package_name + "/silica_data/icon.png", - root + "docs/assets/" + package_bundle_id + "/icon.png") + shutil.copy(f"{root}/Packages/{package_name}/silica_data/icon.png", + f"{root}/docs/assets/{package_bundle_id}/icon.png") except Exception: category = PackageLister.ResolveCategory(tweak_release, package_bundle_id) category = re.sub(r'\([^)]*\)', '', category).strip() try: - shutil.copy(root + "Styles/Generic/Icon/" + category + ".png", - root + "docs/assets/" + package_bundle_id + "/icon.png") + shutil.copy(f"{root}/Styles/Generic/Icon/{category}.png", + f"{root}/docs/assets/{package_bundle_id}/icon.png") except Exception: try: - shutil.copy(root + "Styles/Generic/Icon/Generic.png", - root + "docs/assets/" + package_bundle_id + "/icon.png") + shutil.copy(f"{root}/Styles/Generic/Icon/Generic.png", + f"{root}/docs/assets/{package_bundle_id}/icon.png") except Exception: PackageLister.ErrorReporter("Configuration Error!", "You are missing a file at " + root + - "Styles/Generic/Icon/Generic.png. Please place an icon here to be the repo's default.") + "/Styles/Generic/Icon/Generic.png. Please place an icon here to be the repo's default.") try: - shutil.copy(root + "Packages/" + package_name + "/silica_data/banner.png", - root + "docs/assets/" + package_bundle_id + "/banner.png") + shutil.copy(f"{root}/Packages/{package_name}/silica_data/banner.png", + f"{root}/docs/assets/{package_bundle_id}/banner.png") except Exception: category = PackageLister.ResolveCategory(tweak_release, package_bundle_id) category = re.sub(r'\([^)]*\)', '', category).strip() try: - shutil.copy(root + "Styles/Generic/Banner/" + category + ".png", - root + "docs/assets/" + package_bundle_id + "/banner.png") + shutil.copy(f"{root}/Styles/Generic/Banner/{category}.png", + f"{root}/docs/assets/{package_bundle_id}/banner.png") except Exception: try: - shutil.copy(root + "Styles/Generic/Banner/Generic.png", - root + "docs/assets/" + package_bundle_id + "/banner.png") + shutil.copy(f"{root}/Styles/Generic/Banner/Generic.png", + f"{root}/docs/assets/{package_bundle_id}/banner.png") except Exception: PackageLister.ErrorReporter("Configuration Error!", "You are missing a file at " + root + - "Styles/Generic/Banner/Generic.png. Please place a banner here to be the repo's default.") + "/Styles/Generic/Banner/Generic.png. Please place a banner here to be the repo's default.") try: - shutil.copy(root + "Packages/" + package_name + "/silica_data/description.md", - root + "docs/assets/" + package_bundle_id + "/description.md") + shutil.copy(f"{root}/Packages/{package_name}/silica_data/description.md", + f"{root}/docs/assets/{package_bundle_id}/description.md") except Exception: pass try: - shutil.copytree(root + "Packages/" + package_name + "/silica_data/screenshots", - root + "docs/assets/" + package_bundle_id + "/screenshot") + shutil.copytree(f"{root}/Packages/{package_name}/silica_data/screenshots", + f"{root}/docs/assets/{package_bundle_id}/screenshot") except Exception: pass try: - shutil.copy(root + "Styles/icon.png", root + "docs/CydiaIcon.png") + shutil.copy(f"{root}/Styles/icon.png", f"{root}/docs/CydiaIcon.png") except Exception: - PackageLister.ErrorReporter("Configuration Error!", "You are missing a file at " + root + "Styles/icon.png. " + PackageLister.ErrorReporter("Configuration Error!", "You are missing a file at " + root + "/Styles/icon.png. " "Please add a PNG here to act as the repo's icon.") ########### @@ -117,8 +117,8 @@ def main(): ########### # Copy CSS and JS over - shutil.copy(root + "Styles/index.css", root + "docs/web/index.css") - shutil.copy(root + "Styles/index.js", root + "docs/web/index.js") + shutil.copy(f"{root}/Styles/index.css", f"{root}/docs/web/index.css") + shutil.copy(f"{root}/Styles/index.js", f"{root}/docs/web/index.js") # Generate index.html index_html = DepictionGenerator.RenderIndexHTML() @@ -128,7 +128,7 @@ def main(): # Generate per-tweak depictions for tweak_data in tweak_release: tweak_html = DepictionGenerator.RenderPackageHTML(tweak_data) - PackageLister.CreateFile("docs/depiction/web/" + tweak_data['bundle_id'] + ".html", tweak_html) + PackageLister.CreateFile(f"docs/depiction/web/{tweak_data['bundle_id']}.html", tweak_html) if "github.io" not in repo_settings['cname'].lower(): PackageLister.CreateFile("docs/CNAME", repo_settings['cname']) @@ -144,9 +144,9 @@ def main(): # Generate per-tweak depictions for tweak_data in tweak_release: tweak_json = DepictionGenerator.RenderPackageNative(tweak_data) - PackageLister.CreateFile("docs/depiction/native/" + tweak_data['bundle_id'] + ".json", tweak_json) + PackageLister.CreateFile(f"docs/depiction/native/{tweak_data['bundle_id']}.json", tweak_json) help_depiction = DepictionGenerator.RenderNativeHelp(tweak_data) - PackageLister.CreateFile("docs/depiction/native/help/" + tweak_data['bundle_id'] + ".json", help_depiction) + PackageLister.CreateFile(f"docs/depiction/native/help/{tweak_data['bundle_id']}.json", help_depiction) ########### # Step 5: Generate Release file from settings.json. @@ -164,20 +164,19 @@ def main(): for package_name in PackageLister.ListDirNames(): bundle_id = PackageLister.DirNameToBundleID(package_name) try: - shutil.copytree(root + "Packages/" + package_name, root + "temp/" + bundle_id) - shutil.rmtree(root + "temp/" + bundle_id + "/silica_data") + shutil.copytree(f"{root}/Packages/{package_name}", f"{root}/temp/{bundle_id}") + shutil.rmtree(f"{root}/temp/{bundle_id}/silica_data") except Exception: try: - shutil.rmtree(root + "temp/" + bundle_id + "/silica_data") + shutil.rmtree(f"{root}/temp/{bundle_id}/silica_data") except Exception: pass - script_check = Path(root + "Packages/" + package_name + "/silica_data/scripts/") + script_check = Path(f"{root}/Packages/{package_name}/silica_data/scripts/") if script_check.is_dir(): - shutil.copytree(root + "Packages/" + package_name + "/silica_data/scripts", root + "temp/" + bundle_id - + "/DEBIAN") + shutil.copytree(f"{root}/Packages/{package_name}/silica_data/scripts", f"{root}/temp/{bundle_id}/DEBIAN") else: - PackageLister.CreateFolder("temp/" + bundle_id + "/DEBIAN") + PackageLister.CreateFolder(f"temp/{bundle_id}/DEBIAN") ########### # Step 7: Generate CONTROL and DEB files and move them to docs/ @@ -185,10 +184,9 @@ def main(): for tweak_data in tweak_release: control_file = DebianPackager.CompileControl(tweak_data, repo_settings) - PackageLister.CreateFile("temp/" + tweak_data['bundle_id'] + "/DEBIAN/control", control_file) + PackageLister.CreateFile(f"temp/{tweak_data['bundle_id']}/DEBIAN/control", control_file) DebianPackager.CreateDEB(tweak_data['bundle_id'], tweak_data['version']) - shutil.copy(root + "temp/" + tweak_data['bundle_id'] + ".deb", root + "docs/pkg/" + tweak_data['bundle_id'] - + ".deb") + shutil.copy(f"{root}/temp/{tweak_data['bundle_id']}.deb", f"{root}/docs/pkg/{tweak_data['bundle_id']}.deb") ########### # Step 8: Generate Package file and hash/sign the Release file. @@ -209,7 +207,7 @@ def main(): # Step 10: Push to GitHub ########### - shutil.rmtree(root + "temp/") # Clean-up the now-unneeded temp folder. + shutil.rmtree(f"{root}/temp/") # Clean-up the now-unneeded temp folder. try: if repo_settings['automatic_git'].lower() == "true": From b74fcfd345234e60f62cfdf8aded69ebfdcf9615 Mon Sep 17 00:00:00 2001 From: TheRealKeto Date: Sat, 19 Feb 2022 20:17:57 -0500 Subject: [PATCH 2/5] Ignore Python byte-compiled files Signed-off-by: TheRealKeto --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..749ccda --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class From 6986538673134854a5e03ac295ea0365f104a886 Mon Sep 17 00:00:00 2001 From: TheRealKeto Date: Sat, 19 Feb 2022 20:20:50 -0500 Subject: [PATCH 3/5] Remove standard library requirements Signed-off-by: TheRealKeto --- requirements.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index be753ae..d8ff253 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,5 @@ -pathlib pystache -datetime mistune arpy Pillow -pydpkg \ No newline at end of file +pydpkg From 4991a7d36e1d3c3af2d7c9751f8cef4e52e3f396 Mon Sep 17 00:00:00 2001 From: TheRealKeto Date: Sat, 19 Feb 2022 20:38:06 -0500 Subject: [PATCH 4/5] Use pathlib over os.path Signed-off-by: TheRealKeto --- index.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.py b/index.py index 0c25b36..22733db 100644 --- a/index.py +++ b/index.py @@ -2,7 +2,6 @@ # Import stuff. import shutil # Used to copy files -import os # Used for identifying project root from pathlib import Path # Identify if folder exists. import re # Regular expressions import json # API endpoint generation @@ -22,7 +21,7 @@ def main(): # Step 0: Clean up "docs" and "temp" folder ########### - root = os.path.dirname(os.path.abspath(__file__)) + root = Path(Path(__file__)).parent # Remove everything except for the DEBs. DepictionGenerator.CleanUp() From 1562f60b668985e8a229142362abbc15b91f89d0 Mon Sep 17 00:00:00 2001 From: TheRealKeto Date: Sat, 19 Feb 2022 21:12:56 -0500 Subject: [PATCH 5/5] Python's Path is not hard to use Signed-off-by: TheRealKeto --- util/PackageLister.py | 45 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/util/PackageLister.py b/util/PackageLister.py index 0b1a2c2..e64a8b9 100644 --- a/util/PackageLister.py +++ b/util/PackageLister.py @@ -1,5 +1,6 @@ import json # Used to parse various JSON files -import os # Used to navigate files so we know what tweak folders exist. + +from pathlib import Path # Used to navigate files so we know what tweak folders exist. from PIL import Image # Used to get image screenshot size. @@ -12,7 +13,7 @@ class PackageLister: def __init__(self, version): super(PackageLister, self).__init__() self.version = version - self.root = os.path.dirname(os.path.abspath(__file__)) + "/../" + self.root = f"{Path(Path(__file__)).parent}/.." def CreateFile(self, path, contents): """ @@ -21,7 +22,7 @@ def CreateFile(self, path, contents): String path: A file location to create a file at. Is relative to project root. String contents: The contents of the file to be created. """ - with open(self.root + path, "w") as text_file: + with open(f"{self.root}/{path}", "w") as text_file: text_file.write(contents) def CreateFolder(self, path): @@ -30,8 +31,8 @@ def CreateFolder(self, path): String path: A file location to create a folder at. Is relative to project root. """ - if not os.path.exists(self.root + path): - os.makedirs(self.root + path) + if not Path(f"{self.root}/{path}").exists(): + Path(f"{self.root}/{path}").mkdir() else: pass @@ -40,7 +41,7 @@ def ListDirNames(self): List the file names for package entries. """ package_list = [] - for folder in os.listdir(self.root + "Packages"): + for folder in Path(f"{self.root}/Packages").iterdir(): if folder.lower() != ".ds_store": package_list.append(folder) return package_list @@ -52,12 +53,12 @@ def GetTweakRelease(self): """ tweak_release = [] for tweakEntry in PackageLister.ListDirNames(self): - with open(self.root + "Packages/" + tweakEntry + "/silica_data/index.json", "r") as content_file: + with open(f"{self.root}/Packages/{tweakEntry}/silica_data/index.json", "r") as content_file: try: data = json.load(content_file) except Exception: PackageLister.ErrorReporter(self, "Configuration Error!", "The package configuration file at \"" + - self.root + "Packages/" + tweakEntry + "/silica_data/index.json\" is malformatted. Please check" + self.root + f"/Packages/{tweakEntry}/silica_data/index.json\" is malformatted. Please check" " for any syntax errors in a JSON linter and run Silica again.") tweak_release.append(data) return tweak_release @@ -70,7 +71,7 @@ def GetScreenshots(self, tweak_data): """ image_list = [] try: - for folder in os.listdir(self.root + "docs/assets/" + tweak_data['bundle_id'] + "/screenshot/"): + for folder in Path(f"{self.root}/docs/assets/{tweak_data.get('bundle_id')}/screenshot/").iterdir(): if folder.lower() != ".ds_store": image_list.append(folder) except: @@ -84,9 +85,9 @@ def GetScreenshotSize(self, tweak_data): Object tweak_data: A single index of a "tweak release" object. """ try: - for folder in os.listdir(self.root + "docs/assets/" + tweak_data['bundle_id'] + "/screenshot/"): + for folder in Path(f"{self.root}/docs/assets/{tweak_data.get('bundle_id')}/screenshot/").iterdir(): if folder.lower() != ".ds_store": - with Image.open(self.root + "docs/assets/" + tweak_data['bundle_id'] + "/screenshot/" + folder) as img: + with Image.open(f"{self.root}/docs/assets/{tweak_data['bundle_id']}/screenshot/{folder}") as img: width, height = img.size # Make sure it's not too big. # If height > width, make height 300, width proportional. @@ -108,9 +109,9 @@ def DirNameToBundleID(self, package_name): String package_name: The name of a folder in Packages/ that holds tweak information. """ - with open(self.root + "Packages/" + package_name + "/silica_data/index.json", "r") as content_file: + with open(f"{self.root}/Packages/{package_name}/silica_data/index.json", "r") as content_file: data = json.load(content_file) - return data['bundle_id'] + return data.get('bundle_id') def BundleIdToDirName(self, bundle_id): """ @@ -125,7 +126,7 @@ def BundleIdToDirName(self, bundle_id): return None def GetRepoSettings(self): - with open(self.root + "Styles/settings.json", "r") as content_file: + with open(f"{self.root}/Styles/settings.json", "r") as content_file: try: return json.load(content_file) except Exception: @@ -141,8 +142,8 @@ def FullPathCname(self, repo_settings): Object repo_settings: An object of repo settings. """ try: - if repo_settings['subfolder'] != "": - subfolder = "/" + repo_settings['subfolder'] + if repo_settings.get('subfolder') != "": + subfolder = f"/{repo_settings.get('subfolder')}" except Exception: subfolder = "" return subfolder @@ -155,8 +156,8 @@ def ResolveCategory(self, tweak_release, bundle_id): String bundle_id: The bundle ID of the tweak. """ for tweak in tweak_release: - if tweak['bundle_id'] == bundle_id: - return tweak['section'] + if tweak.get('bundle_id') == bundle_id: + return tweak.get('section') def ResolveVersion(self, tweak_release, bundle_id): """ @@ -166,9 +167,9 @@ def ResolveVersion(self, tweak_release, bundle_id): String bundle_id: The bundle ID of the tweak. """ for tweak in tweak_release: - if tweak['bundle_id'] == bundle_id: - return tweak['version'] + if tweak.get('bundle_id') == bundle_id: + return tweak.get('version') def ErrorReporter(self, title, message): - print('\033[91m- {0} -\n{1}\033[0m'.format(title, message)) - quit() \ No newline at end of file + print(f'\033[91m- {title} -\n{message}\033[0m') + quit()