I'm writing a library that's dependent on file-magic, which works just fine for most platforms, but in Alpine Linux, file-magic won't work so I need to instead use the python-magic library.
Now I know how to write my own code to handle the different Python library APIs, but what I don't know how to do is to write my setup.cfg
or setup.py
to have a different requirements based on the system on which we're doing the installation.
I figured the best option would be to use the PEP 508 rules, but I can't figure out how to say "libmagic like Alpine" or something in that syntax, let alone if that would work in a package's setup.py. Indeed, I can't even figure out how to tell the difference between the architectures without installing file-magic
and watching it die :-(
Surely, there must be a best practise for this sort of thing?
Update
After some broader understanding from Tim below, I cobbled together this hack to get it working:
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())
This allows for the declarative syntax of setup.cfg
, but tweaks the install_requires
value if the installation target is an Alpine system.