johannesjh / req2flatpak

Convert Python package requirements to Flatpak build manifests
https://johannesjh.github.io/req2flatpak/
MIT License
19 stars 2 forks source link

"generated by" comment includes path to req2flatpak #50

Closed cbm755 closed 1 year ago

cbm755 commented 1 year ago
# Generated by /home/cbm/.local/bin/req2flatpak --requirements Pillow==9.4.0 --target-platforms 310-x86_64 310-aarch64 --outfile python3-Pillow.yaml

That /home/cbm/.local/bin/ is correct but not very professional and not really relevant to almost everyone reading the file.

Some nice way to get just the exe name from argv? or we have to do something like pathlib.Path(argv[...]).name?

I remember also seeing ./ like:

# Generated by ./req2flatpak --requirements

which is probably the same fix...

johannesjh commented 1 year ago

hi!

I agree, this would be a nice improvement.

For the implementation: the Pathlib.Path.name method should do the job, compare, e.g., "Extract file name from path" on stackoverflow.

cbm755 commented 1 year ago

If you run things like python3 -c req2flatpak ... then arg.sysv[0] will be -c. https://docs.python.org/3/library/sys.html#sys.argv

I think pathlib.Path.name is still ok in that case...

real-yfprojects commented 1 year ago

We could use __file__ instead of sys.argv.

cbm755 commented 1 year ago

Things I'm trying:

        a1 = [pathlib.Path(x).name if i == 0 else x for i, x in enumerate(sys.argv)]

        a2 = [pathlib.Path(sys.argv[0]).name]
        a2.extend(sys.argv[1:])

        a3 = [pathlib.Path(__file__).name]
        a3.extend(sys.argv[1:])

        output_stream.write("# Generated by " + " ".join(argv) + "\n")

I was liked a1 but maybe __file__ is closer in semantics to what we want (?)

cbm755 commented 1 year ago

Seems __file__ is not always defined? https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do

real-yfprojects commented 1 year ago

Quoting from the docs:

__file__ is optional. If set, this attribute’s value must be a string. The import system may opt to leave __file__ unset if it has no semantic meaning (e.g. a module loaded from a database).

For sys.argv:

argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

real-yfprojects commented 1 year ago

More docs on __file__:

https://docs.python.org/3.9/library/importlib.html?highlight=__file__#importlib.machinery.ModuleSpec.origin

Name of the place from which the module is loaded, e.g. “builtin” for built-in modules and the filename for modules loaded from source. Normally “origin” should be set, but it may be None (the default) which indicates it is unspecified (e.g. for namespace packages).

https://docs.python.org/3/reference/datamodel.html

The pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter. For extension modules loaded dynamically from a shared library, it’s the pathname of the shared library file.

johannesjh commented 1 year ago

I think you can safely use __file__. in my experience, __file__ is reliably defined if python scripts are loaded from the filesystem. (it is not defined in scenarios where python scripts are compiled into an executable binary... but that does not matter for what we want to do, because compiling to a binary is not at all our intended runtime scenario).

Alternatively, you could print the arguments as parsed by argparse. But there does not seem to be a built-in way for this, compare e.g. https://stackoverflow.com/q/34992524

real-yfprojects commented 1 year ago

I think you can safely use __file__. in my experience, __file__ is reliably defined if python scripts are loaded from the filesystem.

This is my experience to and also what a was able to grasp from the docs.