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

Hidden dependency on which program #259

Open
Zaczero opened this issue Feb 22, 2024 · 3 comments
Open

Hidden dependency on which program #259

Zaczero opened this issue Feb 22, 2024 · 3 comments

Comments

@Zaczero
Copy link

Zaczero commented Feb 22, 2024

There seems to be a hidden dependency on the which program.

['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]

The way the binary gets discovered is unnecessarily complicated. Instead of starting an external binary (a whole new process), I recommend that the script simply checks the $PATH env variable. The syntax for it is very simple.

Sample solution:

    for directory in os.environ.get('PATH', '').split(os.pathsep):
        potential_path = os.path.join(directory, 'wkhtmltopdf')  # perhaps .exe on windows?
        if os.path.isfile(potential_path) and os.access(potential_path, os.X_OK):
            return potential_path
@stefan6419846
Copy link

It is even easier with recent Python versions by relying on the stdlib shutil.which.

@sscotti
Copy link

sscotti commented Mar 26, 2024

Is that the same as ?

`root@ubuntu-s-2vcpu-4gb-amd-sfo3-01:~# which which

/usr/bin/which`

I always laugh when I do that because it reminds me of the Wizard of Oz.

"Wicked Witch of the East - Wikipedia

The Wicked Witch of the East
The Wicked Witch of the East was featured in the film The Wizard of Oz (1939), in which she is the sister of the Wicked Witch of the West. As in the book, she is killed when Dorothy's house falls on her."

@stefan6419846
Copy link

It should be mostly identical to the known which tool, but implemented in pure Python: https://github.com/python/cpython/blob/1c72265a31eaf8724a5dd16391b951366460b64e/Lib/shutil.py#L1525-L1605

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

No branches or pull requests

3 participants