thp / urlwatch

Watch (parts of) webpages and get notified when something changes via e-mail, on your phone or via other means. Highly configurable.
https://thp.io/2008/urlwatch/
Other
2.81k stars 352 forks source link

urlwatch fails to find example files when launched from /usr/sbin #755

Closed RunningDroid closed 1 year ago

RunningDroid commented 1 year ago

It also opens a blank file instead of opening the example urls.yaml:

$ rm -r .config/urlwatch
$ urlwatch --verbose --edit
2023-04-11 20:22:49,842 cli INFO: turning on verbose logging mode
2023-04-11 20:22:49,843 minidb DEBUG: PRAGMA table_info(CacheEntry)
2023-04-11 20:22:49,843 main INFO: Using /home/runningdroid/.config/urlwatch/urls.yaml as URLs file
2023-04-11 20:22:49,843 main INFO: Using /home/runningdroid/.config/urlwatch/hooks.py for hooks
2023-04-11 20:22:49,843 main INFO: Using /home/runningdroid/.cache/urlwatch/cache.db as cache database

    A default config has been written to /home/runningdroid/.config/urlwatch/urlwatch.yaml.
    Use "urlwatch --edit-config" to customize it.

Traceback (most recent call last):
  File "/usr/sbin/urlwatch", line 33, in <module>
    sys.exit(load_entry_point('urlwatch==2.26', 'console_scripts', 'urlwatch')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/urlwatch/cli.py", line 112, in main
    urlwatch_command.run()
  File "/usr/lib/python3.11/site-packages/urlwatch/command.py", line 458, in run
    self.handle_actions()
  File "/usr/lib/python3.11/site-packages/urlwatch/command.py", line 252, in handle_actions
    sys.exit(self.urlwatcher.urls_storage.edit(self.urlwatch_config.urls_yaml_example))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/urlwatch/storage.py", line 285, in edit
    atomic_rename(file_edit, self.filename)
  File "/usr/lib/python3.11/site-packages/urlwatch/util.py", line 94, in atomic_rename
    os.rename(old_filename, new_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/runningdroid/.config/urlwatch/urls.edit.yaml' -> '/home/runningdroid/.config/urlwatch/urls.yaml'

Doing cp /usr/share/urlwatch/examples/urls.yaml.example ~/.config/urlwatch/urls.yaml works around this for users that have urlwatch installed as a system package.

thp commented 1 year ago

It seems to be that in your case, urlwatch is installed into /usr/sbin/urlwatch instead of /usr/bin/urlwatch (or that /usr/sbin appears first in your $PATH, and on Void Linux, /usr/sbin is a symlink to /usr/bin?).

In order to determine whether the program is run from a source checkout or a system-wide installation, this is used (the assumption here is that if the urlwatch script is in a folder named bin, assume system-wide installation, otherwise assume a source checkout):

https://github.com/thp/urlwatch/blob/d67443983d29cd0100668a8a90a15f02f2a78de3/lib/urlwatch/cli.py#L47-L51

Both prefix and bindir from the code snippet above are then used to construct a path to the examples:

https://github.com/thp/urlwatch/blob/d67443983d29cd0100668a8a90a15f02f2a78de3/lib/urlwatch/config.py#L62-L67

There are multiple ways to fix this:

I'll try to come up with a workaround. This isn't really a regression, but has been like that since ~ 2008 in urlwatch (according to git blame), and probably affects Void Linux since July 2015 (according to the blog post linked above) :)

thp commented 1 year ago

Additional information for the fix in 6b68afbb047060277b00f570345e7e1969266712: Since the urlwatch script that gets installed system-wide (or in $HOME if installed via pip install --user) is different from the urlwatch script in the source checkout, the special-case handling of that can just go into the urlwatch script. Also cleaned up some other things (e.g. sys.path.insert() wasn't necessary, as the urlwatch script was already doing this).

RunningDroid commented 1 year ago

This isn't really a regression, but has been like that since ~ 2008 in urlwatch (according to git blame), and probably affects Void Linux since July 2015 (according to the blog post linked above) :)

My bad, I assumed it was a regression because it worked on one machine with urlwatch 2.25 but not on a different machine that I'd updated to urlwatch 2.26. Apparently urlwatch behaved differently because the machine running urlwatch 2.25 had /usr/bin before /usr/sbin and the one I'd updated had /usr/sbin before /usr/bin.

(I don't know why the paths are ordered differently when all the relevant config files are identical, but that's a separate problem.)