Because of the relatively loose coupling between a source file and the package it resides it, I don't think it is easy to find out the package name from within the source file. You could do it by looking for specific parent directories, but this will need to be different depending on if the file has been installed or not (if not, then look for the nearest parent with a package.xml file, if so and installed like a binary then look for the immediate parent directory of the executable). I think this is very fragile, however.