Ex46 cleaning up a uninstalled module


#1

I am currently re-doing some exercises I lost in the LMP3THW.

In ex46:
I tend to hack away at stuff to figure it out, and realized on the first uninstall that all the old build, dist, and egg-info directories were left behind after uninstall.
If I want to start from scratch on a module modification and a new setup, I would want these removed yes?

So I made this little file to clean it up, so I can go modify away and not have to go through and delete each thing. I can run clean_up.py from my skeleton dir and it’ll remove the old stuff setup.py placed.
Should this be a common thing to include in modules?

#!usr/bin/python3

# Clean up old version of module in skeleton directory
# sys.argv[1] will be project_name.egg-info
#TO-DO:
# make a clean-up for __pychache__ and *.pyc

import os
import sys
import shutil

#get current directory path
cwd = os.getcwd()
cwd = cwd + "/"

#get module name egg-info
egg_dir = sys.argv[1]

#directories to be removed:
dist_path = cwd + 'dist'
build_path = cwd + 'build'
egg_path = cwd + egg_dir

#----------------------#
# remove egg directory #
#----------------------#

try:
    shutil.rmtree(egg_path)
except OSError as e:
    print("file not found: ", egg_path)
    print("OSERROR:  ", e.strerror)

#-----------------------------------#
# remove build and dist directories #
#-----------------------------------#

try:
    shutil.rmtree(dist_path)
except OSError as e:
    print("OSERROR:  ", e.strerror)
    print("shutil not found, or directory not found:", dist_path)

try:
    shutil.rmtree(build_path)
except OSError as e:
    print("OSERROR:  ", e.strerror)
    print("shutil not found, or directory not found:", build_path)

#2

That might work most of the time but beware that python packaging is a total mess so some projects might do weird stuff. If you really want to be able to restart and totally obliterate packages you just:

  1. create a virtualenv for it
  2. add your packages
  3. do your work
  4. rm -rf the virtualenv

#3

I wanted to be able to uninstall, modify, reinstall.
But with the build, egg, and dist not going away with uninstall, yeah, I’d have to completely obliterate it to change it and test it.
Hence why I thought this would help.


#4

Hey @nellietobey nice script! I like especially the except OSERRor as e: didn’t know that you can do that.