Skip to content

Commit

Permalink
Change path to Python in __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSazonov committed Jul 25, 2024
1 parent 3e33ab6 commit 64663b5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ jobs:
scripts/validate_pyproject_toml.sh
scripts/create_pycfml_python_wheel.sh
scripts/rename_pycfml_python_wheel.sh
scripts/detect_abi3_violations.sh
scripts/check_wheel_contents.sh
- name: Prepare for uploading Python package wheel of pyCFML
shell: bash
Expand Down
72 changes: 66 additions & 6 deletions pybuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,38 @@ def _ifport_lib():
ifport_lib = ''
return ifport_lib

def _initial_python_wheel_tags():
return {
'python_tag': 'py3',
'abi_tag': 'none',
'platform_tag': 'any'
}

def _new_python_wheel_tags():
return {
'python_tag': _python_tag(),
'abi_tag': 'none',
'platform_tag': _platform_tag_github_ci()
}

def _initial_wheel_name():
dist_package_name = PYPROJECT['project']['name']
dist_package_version = PYPROJECT['project']['version']
python_tag = _initial_python_wheel_tags()['python_tag']
abi_tag = _initial_python_wheel_tags()['abi_tag']
platform_tag = _initial_python_wheel_tags()['platform_tag']
name = f'{dist_package_name}-{dist_package_version}-{python_tag}-{abi_tag}-{platform_tag}.whl'
return name

def _new_wheel_name():
dist_package_name = PYPROJECT['project']['name']
dist_package_version = PYPROJECT['project']['version']
python_tag = _new_python_wheel_tags()['python_tag']
abi_tag = _new_python_wheel_tags()['abi_tag']
platform_tag = _new_python_wheel_tags()['platform_tag']
name = f'{dist_package_name}-{dist_package_version}-{python_tag}-{abi_tag}-{platform_tag}.whl'
return name

def _fix_file_permissions(path: str):
os.chmod(path, 0o777)

Expand Down Expand Up @@ -412,6 +444,12 @@ def _compile_executables_script_lines(modules: str,
lines.append(cmd)
return lines

def _run_subprocess(cmd:str):
result = None
p = subprocess.run(cmd, shell=True, text=True, capture_output=True)
result = p.stdout
return result

def parsed_args():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--create-scripts",
Expand Down Expand Up @@ -1238,15 +1276,11 @@ def create_pycfml_python_wheel():

def rename_pycfml_python_wheel():
project_name = CONFIG['pycfml']['log-name']
dist_package_name = PYPROJECT['project']['name']
dist_package_version = PYPROJECT['project']['version']
initial_wheel_name = f'{dist_package_name}-{dist_package_version}-py3-none-any.whl'
new_wheel_name = initial_wheel_name.replace('py3-none-any', f'{_python_tag()}-none-{_platform_tag_github_ci()}')
wheel_dir = CONFIG['pycfml']['dir']['dist-wheel']
wheel_relpath = os.path.join(wheel_dir, initial_wheel_name)
wheel_relpath = os.path.join(wheel_dir, _initial_wheel_name())
wheel_abspath = os.path.join(_project_path(), wheel_relpath)
lines = []
msg = _echo_msg(f"Renaming {project_name} python wheel from '{initial_wheel_name}' to '{new_wheel_name}' in '{wheel_dir}'")
msg = _echo_msg(f"Renaming {project_name} python wheel from '{_initial_wheel_name()}' to '{_new_wheel_name()}' in '{wheel_dir}'")
lines.append(msg)
cmd = CONFIG['template']['rename-wheel']
cmd = cmd.replace('{PYTHON_TAG}', _python_tag()) # https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
Expand All @@ -1257,6 +1291,30 @@ def rename_pycfml_python_wheel():
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)

def detect_abi3_violations():
wheel_dir = CONFIG['pycfml']['dir']['dist-wheel']
wheel_path = os.path.join(wheel_dir, _new_wheel_name())
lines = []
msg = _echo_msg(f"Scanning Python extensions in python wheel '{_new_wheel_name()}' for abi3 violations")
lines.append(msg)
cmd = f'abi3audit {wheel_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)

def check_wheel_contents():
wheel_dir = CONFIG['pycfml']['dir']['dist-wheel']
wheel_path = os.path.join(wheel_dir, _new_wheel_name())
lines = []
msg = _echo_msg(f"Checking content of Python wheel '{_new_wheel_name()}'")
lines.append(msg)
cmd = f'check-wheel-contents {wheel_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)

def install_pycfml_from_wheel():
project_name = CONFIG['pycfml']['log-name']
package_name = PYPROJECT['project']['name']
Expand Down Expand Up @@ -1418,6 +1476,8 @@ def run_pycfml_functional_tests_no_benchmarks():
validate_pyproject_toml()
create_pycfml_python_wheel()
rename_pycfml_python_wheel()
detect_abi3_violations()
check_wheel_contents()

add_main_script_header(f"Install {pyCFML} from Python package wheel")
install_pycfml_from_wheel()
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ dependencies = [

[project.optional-dependencies]
ci = [
'toml', # need this, as tomllib is only available for python >=3.11
'validate-pyproject[all]',
'build',
'wheel',
'colorama',
'toml' # need this, as tomllib is only available for python >=3.11
'check-wheel-contents',
'abi3audit',
'colorama'
]
test = [
'deepdiff',
Expand Down
27 changes: 26 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import importlib.metadata
import os
import platform
import sys
import importlib.metadata


# Set package version
__version__ = importlib.metadata.version("pycrysfml")
Expand All @@ -10,3 +12,26 @@

# Set environment variable CRYSFML_DB to be the path to the Databases directory
os.environ['CRYSFML_DB'] = os.path.join(os.path.dirname(__file__), 'Databases')

# Fix path to Python
if platform.system() == 'Darwin': # macOS
import sysconfig
python_tag = sysconfig.get_config_var('py_version_short')
old_path = f'/Library/Frameworks/Python.framework/Versions/{python_tag}/Python'
new_path = f'`python3-config --prefix`/Python'
if old_path != new_path:
import subprocess
lib_name = 'crysfml08lib.so'
lib_path = os.path.join(os.path.dirname(__file__), lib_name)
cmd = f'otool -L {lib_path}'
p = subprocess.run(cmd, shell=True, text=True, capture_output=True)
result = p.stdout
if old_path in result:
cmd = f'install_name_tool -change {old_path} {new_path} {lib_path}'
p = subprocess.run(cmd, shell=True, text=True, capture_output=True)
#result = p.stdout
#print(result)
#cmd = f'otool -L {lib_path}'
#p = subprocess.run(cmd, shell=True, text=True, capture_output=True)
#result = p.stdout
#print(result)

0 comments on commit 64663b5

Please sign in to comment.