Skip to content

Commit

Permalink
fix: Simplify build-system and support editable install
Browse files Browse the repository at this point in the history
* Switch to using "FetchContent_Populate" instead of custom logic
  for checking archive integrity

* Disambiguate executable location installing it into `s5cmd/bin`

* Add support for editable install using `importlib.metadata` to lookup
  location of executable.
  • Loading branch information
jcfr committed Feb 20, 2024
1 parent 4c5588b commit 9445a57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 57 deletions.
68 changes: 13 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,73 +1,31 @@
cmake_minimum_required(VERSION 3.15...3.26)
project(${SKBUILD_PROJECT_NAME} LANGUAGES NONE)

function(_check_archive dest_file expected_sha256 output_var)
get_filename_component(filename ${dest_file} NAME)
message(STATUS "Checking ${filename}")

if(NOT EXISTS ${dest_file})
message(STATUS "Checking ${filename} - nonexistent")
set(${output_var} "nonexistent" PARENT_SCOPE)
return()
endif()

file(SHA256 ${dest_file} current_hash)
if(NOT ${current_hash} STREQUAL ${expected_sha256})
message(STATUS "Checking ${filename} - expired")
set(${output_var} "expired" PARENT_SCOPE)
return()
endif()

message(STATUS "Checking ${filename} - up-to-date")
set(${output_var} "ok" PARENT_SCOPE)
endfunction()

# Set in the current scope the following variables:
# - s5cmd_archive_url
# - s5cmd_archive_sha256
include(${CMAKE_CURRENT_SOURCE_DIR}/s5cmdUrls.cmake)

#
# Download archive
# Download & extract archive
#
cmake_path(GET s5cmd_archive_url FILENAME archive_filename)
set(destination_file "${CMAKE_CURRENT_BINARY_DIR}/${archive_filename}")

_check_archive(${destination_file} ${s5cmd_archive_sha256} result)

if(result MATCHES "^(nonexistent|expired)$")
message(STATUS "Downloading ${s5cmd_archive_url}")
file(
DOWNLOAD
${s5cmd_archive_url}
${destination_file}
EXPECTED_HASH SHA256=${s5cmd_archive_sha256}
)
elseif(result STREQUAL "ok")
# ok
else()
message(FATAL_ERROR "Unknown result value: ${result}")
endif()
set(download_dir "${PROJECT_BINARY_DIR}")
set(extract_dir "${PROJECT_BINARY_DIR}/s5cmd-binary-distribution")
include(FetchContent)
FetchContent_Populate(s5cmd
URL ${s5cmd_archive_url}
URL_HASH SHA256=${s5cmd_archive_sha256}
DOWNLOAD_DIR ${download_dir}
SOURCE_DIR "${extract_dir}"
)

#
# Extract archive
# Install executable
#
set(executable_name "s5cmd${CMAKE_EXECUTABLE_SUFFIX}")

string(MAKE_C_IDENTIFIER ${archive_filename} extract_subdir)
set(extract_dir "${CMAKE_CURRENT_BINARY_DIR}/${extract_subdir}")
message(STATUS "Extracting ${archive_filename} into ${extract_dir}")
file(ARCHIVE_EXTRACT
INPUT ${destination_file}
DESTINATION ${extract_dir}
PATTERNS "${executable_name}"
VERBOSE
)

set(_permissions PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)

install(PROGRAMS ${extract_dir}/${executable_name} DESTINATION "s5cmd" ${_permissions})
set(executable_name "s5cmd${CMAKE_EXECUTABLE_SUFFIX}")
install(PROGRAMS ${extract_dir}/${executable_name} DESTINATION "s5cmd/bin" ${_permissions})
13 changes: 11 additions & 2 deletions src/s5cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import subprocess
import sys
from importlib.metadata import distribution
from pathlib import Path
from typing import NoReturn

Expand All @@ -17,11 +18,19 @@
__all__ = ["__version__", "s5cmd"]


S5CMD_BIN_DIR: Path = Path(__file__).parent
def _lookup(name: str) -> Path:
executable_path = f"s5cmd/bin/{name}"
files = distribution("s5cmd").files
if files is not None:
for _file in files:
if str(_file).startswith(executable_path):
return Path(_file.locate())
msg = f"Failed to lookup '{executable_path}` directory."
raise FileNotFoundError(msg)


def _program(name: str, args: list[str]) -> int:
return subprocess.call([S5CMD_BIN_DIR / name, *args], close_fds=False)
return subprocess.call([_lookup(name).resolve(strict=True), *args], close_fds=False)


def s5cmd() -> NoReturn:
Expand Down

0 comments on commit 9445a57

Please sign in to comment.