Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS Specific Scripts #2848

Open
ameily opened this issue Apr 24, 2024 · 5 comments
Open

OS Specific Scripts #2848

ameily opened this issue Apr 24, 2024 · 5 comments
Labels
⭐ enhancement Improvements for existing features

Comments

@ameily
Copy link

ameily commented Apr 24, 2024

Is your feature/enhancement proposal related to a problem? Please describe.

I have a Python project that runs on both Windows and Linux. I have a lint PDM script that runs pylint. The problem is that I need to ignore Windows-specific source files when running on Linux and ignore Linux-specific source files when running on Windows. I have what I consider a HACK that works:

[tool.pdm.scripts]
# This is ugly but runs the correct lint command based on the OS. This check works on both Windows
# and Linux
lint = {shell = "(python -c \"import sys;import platform;sys.exit(1 if any(platform.win32_ver()) else 0)\" && {pdm} run lint-linux) || {pdm} run lint-windows" }

lint-windows = "pylint --ignore '[LINUX_SPECIFIC_SOURCE_FILES]' my_project"
lint-linux = "pylint --ignore '[WINDOWS_SPECIFIC_SOURCE_FILES]' my_project"

Describe the solution you'd like

I assume there's a better way to do what I need. If not, I think a new script type that selects the script to run based on the OS. Something like:

[tool.pdm.scripts]
lint = { os_match = { win32 = "lint-windows", linux = "lint-linux" } }

lint-windows = "pylint --ignore '[LINUX_SPECIFIC_SOURCE_FILES]' my_project"
lint-linux = "pylint --ignore '[WINDOWS_SPECIFIC_SOURCE_FILES]' my_project"

Looking at TaskRunner.run_task, I think we would add a new kind that behaves something like:

if kind == "os_match":
    assert isinstance(value, dict)
    if sys.platform.startswith("linux") and "linux" in value:
        # generic linux
        script = value["linux"]
    else:
        # match the platform exactly
        script = value[sys.platform]
    
    self.run(...)
@ameily ameily added the ⭐ enhancement Improvements for existing features label Apr 24, 2024
@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Apr 24, 2024

That's IMO better solved with a Python wrapper script that builds the ignore flag value and calls pylint. The main reason being: to avoid scope-creep in PDM :)

@ameily
Copy link
Author

ameily commented Apr 24, 2024

That makes sense. For context, I'm migrating from poetry and was using the poe task runner, which has a switch/case task for selecting the command to run based on some predicate. If that's outside the scope of PDM I understand, although I do think it would have value for cross-platform projects. Looking at the PDM code, I think its something I could add and make a PR for. Otherwise I'll take your advice and make a small Python wrapper.

@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Apr 24, 2024

I'm just a passer-by, @frostming gets the last word 😄

Maybe that's achievable through a PDM plugin by the way?

@frostming
Copy link
Collaborator

frostming commented Apr 25, 2024

IMHO your solution only solves a very specific need (what if I want Python version specific scripts?), and makes the configuration very complicated, which is not worth it.

Good to learn poe supported such a complex configuration to specify conditions, which is impressing. But PDM won't follow this path. If it were up to me I'd consider a Makefile, or a Python script wrapper.

@SealedServant
Copy link

I agree with all of the responses. You will have the best results creating a pylint wrapper script and tossing it in your /scripts top level folder in your repo, then calling that script.

[tool.pdm.scripts]
lint = "scripts/run-pylint.py [--args] DIR"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐ enhancement Improvements for existing features
Projects
None yet
Development

No branches or pull requests

4 participants