Skip to content

Commit

Permalink
pip installs an entry_point instead of a script
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Jul 25, 2021
1 parent de46f06 commit 15c73a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
80 changes: 40 additions & 40 deletions rmskin_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
quickly.
"""
import os
import sys
import argparse
import configparser
import zipfile
Expand Down Expand Up @@ -88,11 +89,10 @@
"RMSKIN.bmp": False,
}


def discover_components():
def discover_components(path):
"""The method that does priliminary dscovery of rmskin package components."""
for dirpath, dirnames, filenames in os.walk(root_path):
dirpath = dirpath.replace(root_path, "")
for dirpath, dirnames, filenames in os.walk(path):
dirpath = dirpath.replace(path, "")
if dirpath.endswith("Skins"):
HAS_COMPONENTS["Skins"] = len(dirnames)
logger.info("Found %d possible Skin(s)", HAS_COMPONENTS["Skins"])
Expand Down Expand Up @@ -121,13 +121,13 @@ def discover_components():
dirnames.clear()


def parse_rmskin_ini():
def parse_rmskin_ini(args, path, build_dir):
"""Read the RMSKIN.ini and write a copy for building the RMSKIN package."""
arc_name = args.title
version = args.version
config = configparser.ConfigParser()

config.read(root_path + os.sep + "RMSKIN.ini")
config.read(path + os.sep + "RMSKIN.ini")
if "rmskin" in config:
if "Version" in config["rmskin"]:
version = config["rmskin"]["Version"]
Expand Down Expand Up @@ -157,7 +157,7 @@ def parse_rmskin_ini():
if len(load_t): # if a file set to load on-install
# exit early if loaded file does not exist
temp = (
root_path
path
+ os.sep
+ load_t
+ "s"
Expand All @@ -168,22 +168,22 @@ def parse_rmskin_ini():
raise RuntimeError("On-install loaded file does not exits.")
else:
raise RuntimeError("RMSKIN.ini is malformed")
with open(BUILD_DIR + "RMSKIN.ini", "w") as conf:
with open(build_dir + "RMSKIN.ini", "w") as conf:
config.write(conf) # Dump changes/corrections to temp build dir
return (arc_name, version)


def validate_header_image():
def validate_header_image(path, build_dir):
"""Make sure header image (if any) is ready to package"""
if HAS_COMPONENTS["RMSKIN.bmp"]:
with Image.open(root_path + os.sep + "RMSKIN.bmp") as img:
with Image.open(path + os.sep + "RMSKIN.bmp") as img:
if img.width != 400 and img.height != 60:
logger.warning("Resizing header image to 400x60")
img = img.resize((400, 60))
if img.mode != "RGB":
logger.warning("Correcting color space in header image.")
img = img.convert(mode="RGB")
img.save(BUILD_DIR + "RMSKIN.bmp")
img.save(build_dir + "RMSKIN.bmp")


def is_dll_32(dll_file):
Expand All @@ -198,10 +198,10 @@ def is_dll_32(dll_file):
return ret_val


def init_zip_for_package(arch_name):
def init_zip_for_package(arch_name, args, path, build_dir):
"""Create initial archive to use as RMSKIN package"""
output_path_to_archive = (
(root_path if args.dir_out is None else args.dir_out) + os.sep + arch_name
(path if args.dir_out is None else args.dir_out) + os.sep + arch_name
)
with zipfile.ZipFile(
output_path_to_archive,
Expand All @@ -211,11 +211,11 @@ def init_zip_for_package(arch_name):
) as arc_file:
# write RMSKIN.ini and header image (RMSKIN.bmp) first
if HAS_COMPONENTS["RMSKIN.bmp"]:
arc_file.write(BUILD_DIR + "RMSKIN.bmp", arcname="RMSKIN.bmp")
arc_file.write(BUILD_DIR + "RMSKIN.ini", arcname="RMSKIN.ini")
arc_file.write(build_dir + "RMSKIN.bmp", arcname="RMSKIN.bmp")
arc_file.write(build_dir + "RMSKIN.ini", arcname="RMSKIN.ini")
for key, val in HAS_COMPONENTS.items():
if not key.endswith(".ini") and val:
for dirpath, _, filenames in os.walk(root_path + os.sep + key):
for dirpath, _, filenames in os.walk(path + os.sep + key):
for file_name in filenames:
if ( # check bitness of plugins here & archive accordingly
key.endswith("Plugins")
Expand All @@ -234,7 +234,7 @@ def init_zip_for_package(arch_name):
else: # for all other files/folders
arc_file.write(
dirpath + os.sep + file_name,
arcname=dirpath.replace(root_path + os.sep, "")
arcname=dirpath.replace(path + os.sep, "")
+ os.sep
+ file_name,
)
Expand All @@ -243,10 +243,24 @@ def init_zip_for_package(arch_name):

def main():
"""The main execution loop for creating a rmskin package."""
# collect cmd args
args = parser.parse_args()
root_path = args.path
# truncate trailing path seperator
root_path = root_path.rstrip(os.sep)
root_path = os.path.abspath(root_path)

# The temporary build dir for storing altered files
build_dir = root_path + os.sep + "build" + os.sep
if not os.path.isdir(build_dir):
os.mkdir(build_dir)

if args.dir_out is not None and args.dir_out.endswith(os.sep):
args.dir_out = args.dir_out.rstrip(os.sep)
logger.info("Searching path: %s", root_path)

# capture the directory tree
discover_components()
discover_components(root_path)

# quit if bad dir struct
if not (
Expand All @@ -256,30 +270,30 @@ def main():
or HAS_COMPONENTS["@Vault"]
):
raise RuntimeError(
f"repository structure for {root_path} is malformed. Found no Skins,"
f"Repository structure for {root_path} is malformed. Found no Skins,"
" Layouts, Plugins, or @Vault assets."
)

# quit if no RMSKIN.ini
if not HAS_COMPONENTS["RMSKIN.ini"]:
raise RuntimeError(
f"repository structure for {root_path} is malformed. RMSKIN.ini file "
f"Repository structure for {root_path} is malformed. RMSKIN.ini file "
"not found."
)

# read options from RMSKIN.ini
arc_name, version = parse_rmskin_ini()
arc_name, version = parse_rmskin_ini(args, root_path, build_dir)

# make sure header image is correct size (400x60) & correct color space
validate_header_image()
validate_header_image(root_path, build_dir)

# Now creating the archive
archive_name = arc_name + "_" + version + ".rmskin"
path_to_archive = init_zip_for_package(archive_name)
path_to_archive = init_zip_for_package(archive_name, args, root_path, build_dir)

compressed_size = 0
compressed_size = os.path.getsize(path_to_archive)
logger.info("archive size = %d (0x%X)", compressed_size, compressed_size)
logger.info("Archive size = %d (0x%X)", compressed_size, compressed_size)

# convert size to a bytes obj & prepend to custom footer
custom_footer = struct.pack("q", compressed_size) + b"\x00RMSKIN\x00"
Expand All @@ -294,22 +308,8 @@ def main():
if os.getenv("CI", "false").title().startswith("True"):
print("::set-output name=arc_name::{}".format(archive_name))
else:
logger.info("archive name: %s", archive_name)
logger.info("Archive name: %s", archive_name)


if __name__ == "__main__":
# collect cmd args
args = parser.parse_args()
root_path = args.path
# truncate trailing path seperator
root_path = root_path.rstrip(os.sep)
root_path = os.path.abspath(root_path)

# The temporary build dir for storing altered files
BUILD_DIR = root_path + os.sep + "build" + os.sep
if not os.path.isdir(BUILD_DIR):
os.mkdir(BUILD_DIR)

if args.dir_out is not None and args.dir_out.endswith(os.sep):
args.dir_out = args.dir_out.rstrip(os.sep)
main()
sys.exit(main())
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description = f.read()

setup(
name="rmskin-action",
name="rmskin-builder",
use_scm_version=True,
setup_requires=["setuptools_scm"],
description="A script that will attempt to assemble a validating Rainmeter skin "
Expand All @@ -38,7 +38,10 @@
],
keywords="rainmeter rmskin archive builder",
py_modules=["rmskin_builder"],
scripts=["./rmskin_builder.py"],
# scripts=["./rmskin_builder.py"],
entry_points = {
"console_scripts": ["rmskin-builder=rmskin_builder:main"],
},
# Specifiy your homepage URL for your project here
url=REPO,
download_url=f"{REPO}/releases",
Expand Down

0 comments on commit 15c73a2

Please sign in to comment.