ほとんどのプラットフォームで問題なく動作するfile-magicに依存するライブラリを作成していますが、Alpine Linux では file-magic が動作しないため、代わりにpython-magicライブラリを使用する必要があります。
これで、さまざまな Python ライブラリ API を処理する独自のコードを作成する方法がわかりましたが、インストールを実行しているシステムに基づいて独自のコードを作成しsetup.cfg
たり、別の要件を設定したりする方法がわかりません。setup.py
最善の選択肢はPEP 508ルールを使用することだと考えましたが、パッケージの setup.py で機能するかどうかは言うまでもなく、「Alpine のような libmagic」またはその構文で何かを言う方法がわかりません。実際、インストールして死ぬのを見ないと、アーキテクチャの違いを見分ける方法さえわかりませんfile-magic
:-(
確かに、この種のことにはベストプラクティスが必要ですか?
アップデート
以下のティムからの幅広い理解の後、私はこのハックをまとめて機能させました。
def get_requirements():
"""
Alpine is problematic in how it doesn't play nice with file-magic -- a
module that appears to be the standard for most other Linux distros. As a
work-around for this, we swap out file-magic for python-magic in the Alpine
case.
"""
config = configparser.ConfigParser()
config.read("setup.cfg")
requirements = config["options"]["install_requires"].split()
os_id = None
try:
with open("/etc/os-release") as f:
os_id = [_ for _ in f.readlines() if _.startswith("ID=")][0] \
.strip() \
.replace("ID=", "")
except (FileNotFoundError, OSError, IndexError):
pass
if os_id == "alpine":
requirements[1] = "python-magic>=0.4.15"
return requirements
setuptools.setup(install_requires=get_requirements())
これにより、 の宣言構文が可能になりますが、インストール先が Alpine システムの場合は値setup.cfg
が微調整されます。install_requires