Skip to content

Instantly share code, notes, and snippets.

@mrowrpurr
Created February 3, 2024 18:11
Show Gist options
  • Save mrowrpurr/5ed5fcadbe8415bcc5f235d8fa82918a to your computer and use it in GitHub Desktop.
Save mrowrpurr/5ed5fcadbe8415bcc5f235d8fa82918a to your computer and use it in GitHub Desktop.
import argparse
import os
from pathlib import Path
from typing import List
import shutil
# Default output directory placeholder
default_out: str = "xmake_libraries"
# Default package names list
default_packages: List[str] = []
def copy_package_contents(src_dir: Path, dest_dir: Path) -> None:
if src_dir.exists():
for item in src_dir.iterdir():
if item.is_dir():
shutil.copytree(item, dest_dir / item.name, dirs_exist_ok=True)
else:
shutil.copy2(item, dest_dir / item.name)
def select_most_recent_directory(directories: List[Path]) -> Path:
return max(directories, key=os.path.getmtime)
def process_packages(packages_dir: Path, out_dir: Path, packages: List[str]) -> None:
include_dir: Path = out_dir / "include"
lib_dir: Path = out_dir / "lib"
include_dir.mkdir(parents=True, exist_ok=True)
lib_dir.mkdir(parents=True, exist_ok=True)
for package in packages:
package_path: Path = packages_dir / package[0] / package
version_dirs: List[Path] = [d for d in package_path.iterdir() if d.is_dir()]
package_version: Path
# Select @default version if exists, else the most recently added version
default_dir = next((d for d in version_dirs if "@default" in d.name), None)
if default_dir:
package_version = default_dir
else:
package_version = select_most_recent_directory(version_dirs)
# Now select the first hash directory under the selected version
hash_dirs: List[Path] = [d for d in package_version.iterdir() if d.is_dir()]
if hash_dirs:
package_hash = hash_dirs[0] # Select the first hash directory
copy_package_contents(package_hash / "include", include_dir)
copy_package_contents(package_hash / "lib", lib_dir)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Copy xmake package includes and libs to a global directory.")
parser.add_argument("--out", type=str, default=default_out, help="The output directory where libraries will be copied.")
parser.add_argument("--packages-folder", type=str, help="The path to the xmake packages folder.")
parser.add_argument("--packages", type=str, help="Comma-separated list of package names.")
args = parser.parse_args()
# Set up the packages directory
if args.packages_folder:
packages_dir_path = Path(args.packages_folder)
else:
user_profile = os.getenv("USERPROFILE")
home = os.getenv("HOME")
if user_profile:
packages_dir_path = Path(user_profile, "AppData/Local/.xmake/packages")
elif home:
packages_dir_path = Path(home, ".xmake/packages")
else:
print("Error: Cannot determine the packages folder.")
exit(1)
# Verify packages directory exists
if not packages_dir_path.exists():
print("Error: The specified packages folder does not exist.")
exit(1)
# Set up the output directory
out_dir_path = Path(args.out)
if not out_dir_path.exists():
out_dir_path.mkdir(parents=True)
# Parse packages
packages_list = args.packages.split(",") if args.packages else default_packages
if not packages_list:
print("Error: No packages specified.")
exit(1)
# Process packages
process_packages(packages_dir_path, out_dir_path, packages_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment