The problem: If the last image is a jpeg/raw pair, the code to remove the jpeg from the import list is not run since it is the last image. This is solved by adding an empty line to the images array. It will be counted as new base and therefore the jpeg removal code will be run one more time. I don't know if this is a good solution but I think it solves the problem without adding complexity.
lua-scripts
The Lua scripts in this repository are meant to be used together with darktable. Either copy them individually to ~/.config/darktable/lua (you might have to create that folder) or just copy/symlink the whole repository there. That allows to update all your scripts with a simple call to git pull.
To enable one of the scripts you have to add a line like require "official/hello_world" to your ~/.config/darktable/luarc file which will enable the example script in official/hello_world.lua (note the lack of the .lua suffix).
Each script includes its own documentation and usage in its header, please refer to them.
In order to have your own scripts added here they have to be under a free license (GPL2+ will definitely work, others can be discussed). Scripts in the official/ subfolder are maintained by the darktable community, those under contrib/ are meant to have an "owner" who maintains them.