I have the following situation: I am working on several projects which make use of library modules that I have written. The library modules contain several classes and functions. In each project, some subset of the code of the libraries is used.
However, when I publish a project for other users, I only want to give away the code that is used by that project rather than the whole modules. This means I would like, for a given project, to remove unused library functions from the library code (i.e. create a new reduced library). Is there any tool that can do this automatically?
EDIT
Some clarifications/replies:
My first advice to you would be to design your code with a stronger modularity, so that you can have all the functions you want to keep in as many python modules/eggs as you have to make it flexible to have just what you need for each of your projects. And I think that would be the only way to keep your code easily manageable and readable.
That said, if you really want to go the way you describe in your question, to my knowledge there's no tool that does exactly what you say, because it's an uncommon usage pattern.
But I don't think it would be hard to code a tool that does it using rope. It does static and dynamic code analysis (so you can find what imported objects are being used thus guess what is not used from your imported modules), and also gives many refactoring tools to move or remove code.
Anyway, I think to be able to really make a tool that find accurately all code that is being used in your current code, you need to make a full unit test coverage of your code or you shall be really methodical in how you import your module's code (using only from foo import bar
and avoiding chaining imports between the modules).
I agree with @zmo - one way to avoid future problems like this is to plan ahead and make your code as modular as possible. I would have suggested putting the classes and functions in much smaller files. This would mean that for every project you make, you would have to hand-select which of these smaller files to include. I'm not sure if that's feasible with the size of your projects right now. But for future projects it's a practice you may consider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With