dennisvang / tufup-example

Example of a self-updating application using tufup.
MIT License
14 stars 8 forks source link

fix incorrect path to trusted root dir #14

Closed RasmusN closed 1 year ago

RasmusN commented 1 year ago

It's not possible to run the example app in development mode (not frozen):

INFO:__main__:my_app 1.8
Traceback (most recent call last):
  File "c:\Projects\tufup-example_aws\src\main.py", line 13, in <module>      
    main(sys.argv[1:])
  File "C:\Projects\tufup-example_aws\src\myapp\__init__.py", line 64, in main
    shutil.copy(src=settings.TRUSTED_ROOT_SRC, dst=settings.TRUSTED_ROOT_DST) 
  File "C:\Python311\Lib\shutil.py", line 419, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Python311\Lib\shutil.py", line 256, in copyfile
    with open(src, 'rb') as fsrc:
         ^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Projects\\tufup-example_aws\\repo\\deploy\\metadata\\root.json'

The TRUSTED_ROOT_SRC did not point to the path specified by repo_settings.py

RasmusN commented 1 year ago

The problem with that is that main.py doesn't know about the python files in the project root folder. It will have to be something like

if not FROZEN:
    # for development, get the root metadata directly from local repo
    sys.path.insert(0, str(MODULE_DIR.parent.parent))
    from repo_settings import REPO_DIR
    TRUSTED_ROOT_SRC =  REPO_DIR / 'metadata' / 'root.json' 

It is of course possible add the project root folder to your python path but I'm thinking it would be nice if it was working right out of the box without any customizations.

dennisvang commented 1 year ago

@RasmusN True, but, I believe that's not an issue if you run main.py from the project root, using the module (-m) switch:

python -m src.main

From the docs:

[...] As with the -c option, the current directory will be added to the start of sys.path.

RasmusN commented 1 year ago

Hmm... then I get this

$ python -m src.main
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Projects\tufup-example\src\main.py", line 4, in <module>
    from myapp import main, settings
ModuleNotFoundError: No module named 'myapp'
dennisvang commented 1 year ago

@RasmusN Sorry about that: you are absolutely right. Using -m would require imports from src.myapp or relative imports, which, in turn, would break down when running frozen.

On second thought, I guess your sys.path solution would do fine, as proposed above.

RasmusN commented 1 year ago

Another thing: In order to be able to run repo_init.py I need to add these two lines:

import sys
sys.path.insert(0, 'src/')

I can add them to the PR if you want