Sunday, December 3, 2017

Introducing python module symcopy

Have you ever had a directory that you wished to copy the contents to another but you did not want to duplicate the file data? You use a symlink.

Now, what happens if you have migrated an entire directory to another drive and faked it with symlinks but then a Windows Update comes through and gets confused by your setup and refreshes your whole system?!

Oh look! Windows.old
I woke this morning to find that Windows Update had "fixed" my system. My symlinks were saved off in Windows.old and both Program Files directories were restored to fresh. Thanks, Windows.

To avoid reinstalling EVERYTHING, I decided to fix my problem from Windows rejecting my symlinks with... symlinks!
This would have been awful to type out and typing that many commands in an admin prompt can be a bit dangerous so I turned to my totally safe friend python. I needed to compare two directories, identify the items in the target that were not in the source, and create symlinks to them.
Don't worry about it being an image, I'm linking the Github.
The actual magic happens in that mklink function. I first implemented that as its own function because it was the first code I wrote this morning. Having it like that allowed me to easily test it before moving on. Once my problem was solved I decided to pip it up and share in case anyone (myself in the future...) has similar use cases, so the final version selects the correct command: ln or mklink.
I had never submitted code to pypi before so I prepared the package with the help of @mart0903.

And now: `pip install symcopy`
Demonstrated in docker: docker run --rm -it --entrypoint=/bin/bash python
Source Code:

1 comment: