[gdal-dev] moving swig\python\scripts\* into the osgeo package

Sean Gillies sean at mapbox.com
Wed Oct 7 07:54:00 PDT 2020


Hi Idan,

The wrappers are sort of built into Python already. If you declare entry
points for the osgeo.gdal package (see
https://packaging.python.org/specifications/entry-points/) then setuptools
will write the wrappers for you. Here's one as an example. It's defined at
https://github.com/mapbox/rasterio/blob/master/setup.py#L416-L417.

$ cat ~/.local/pipx/venvs/rasterio/bin/rio
#!/home/sean/.local/pipx/venvs/rasterio/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from rasterio.rio.main import main_group
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main_group())



On Wed, Oct 7, 2020 at 6:38 AM Idan Miara <idan at miara.com> wrote:

> Hi all,
>
> I would like to get your opinion about making the code in the python
> scripts more reusable so they can be imported as modules.
>
> I'm working on improving gdal_calc.py:
> https://github.com/OSGeo/gdal/pull/3016
> Eventually I would need to import it as a module, since it's not in a
> package it's not possible without copying it into a package.
>
> From a recent discussion with @Even Rouault <even.rouault at spatialys.com> :
> https://github.com/OSGeo/gdal/pull/3016#issuecomment-704563723
> The thing is that the scripts in swig/python/scripts are typically
> installed in /usr/bin so they must be standalone, or use the content of the
> "osgeo" module where the bindings are installed. If you wanted to use
> helper methods/scripts, I guess they should go in the "osgeo" module as
> well.
>
> We might still need tiny wrapper scripts in swig/python/scripts for
> maintaining backwards compatibility.
>
> So I thought a good approach would be making a main function for each
> script (as many already do have), For most scripts that was
> fairly straightforward*:
>
> https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts_organize
>
> * for gdal_retile.py, gdal_merge.py I needed also to replace all the
> global variables with local variables so it would be easier to reuse these
> functions (apparently I broke some things in  gdal_retile but I'll fix
> that).
>
> Then I made the tiny wrappers automatically using an auxiliary script.
> https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts
> So essentially, each wrapper would be like:
>
> from osgeo.gdal import deprecation_warn
> deprecation_warn('gdal_calc')
> from osgeo.gdal_calc import *
> if __name__ == '__main__':
>     sys.exit(main(sys.argv))
>
> What do you think?
>
> Idan
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev



-- 
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201007/2b071ec9/attachment.html>


More information about the gdal-dev mailing list