From e69b344f339c8b66d5cc4259756efef53a18f530 Mon Sep 17 00:00:00 2001 From: utahta Date: Sun, 24 Oct 2010 17:15:58 +0900 Subject: [PATCH] added clean command --- README.rst | 9 ++++++++- pythonbrew | 35 +++++++++++++++++++++++++++++------ pythonbrew.py | 35 +++++++++++++++++++++++++++++------ 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index 3b324a1..9fc5f26 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ If you need to install pythonbrew into somewhere else, you can do that by settin Usage ===== -pythonbrew [options] [init|install|installed|switch|search|uninstall|off|version] +pythonbrew [options] [command] Initialize:: @@ -56,6 +56,10 @@ Uninstall some Pythons:: pythonbrew uninstall Python-2.6.6 +Remove stale source folders and archives:: + + pythonbrew clean + Disable pythonbrew:: pythonbrew off @@ -97,6 +101,9 @@ search Python- uninstall Python- Uninstall the given version of python. +clean + Remove stale source folders and archives. + off Disable pythonbrew. diff --git a/pythonbrew b/pythonbrew index a337d9f..96ffbe5 100755 --- a/pythonbrew +++ b/pythonbrew @@ -80,9 +80,9 @@ def is_archive_file(name): return True return False -def makedirs(name): +def makedirs(path): try: - os.makedirs(name) + os.makedirs(path) except OSError, (e, es): if errno.EEXIST != e: raise @@ -93,20 +93,28 @@ def symlink(src, dst): except: pass -def unlink(name): +def unlink(path): try: - os.unlink(name) + os.unlink(path) except OSError, (e, es): if errno.ENOENT != e: raise + +def rm_r(path): + """like rm -r command.""" + if os.path.isdir(path): + shutil.rmtree(path) + else: + unlink(path) def off(): unlink("%s/current" % PATH_PYTHONS) - for root, dirs, files in os.walk("%s/bin/" % ROOT): + for root, dirs, files in os.walk(PATH_BIN): for f in files: if f == "pythonbrew": continue unlink("%s%s" % (root, f)) + break #---------------------------------------------------- # classes @@ -539,6 +547,7 @@ class SwitchCommand(Command): for root, dirs, files in os.walk("%s/current/bin/" % PATH_PYTHONS): for f in files: symlink("%s%s" % (root, f), "%s/%s" % (PATH_BIN, f)) + break # I want better code if not os.path.isfile("%s/python" % PATH_BIN): if os.path.isfile("%s/python3" % PATH_BIN): @@ -602,10 +611,23 @@ class UninstallCommand(Command): curpath = os.path.realpath("%s/current" % PATH_PYTHONS) if pkgpath == curpath: off() - shutil.rmtree(pkgpath) + rm_r(pkgpath) else: self.parser.print_help() +class CleanCommand(Command): + name = "clean" + usage = "%prog" + summary = "Remove stale source folders and archives" + + def run_command(self, options, args): + self._clean(PATH_BUILD) + self._clean(PATH_DISTS) + + def _clean(self, root): + for dir in os.listdir(root): + rm_r("%s/%s" % (root, dir)) + class Pythonbrew(object): def run(self): options, args = parser.parse_args(sys.argv[1:]) @@ -623,6 +645,7 @@ class Pythonbrew(object): add_command(VersionCommand()) add_command(SearchCommand()) add_command(UninstallCommand()) + add_command(CleanCommand()) command = args[0].lower() if command not in command_dict: diff --git a/pythonbrew.py b/pythonbrew.py index a337d9f..96ffbe5 100755 --- a/pythonbrew.py +++ b/pythonbrew.py @@ -80,9 +80,9 @@ def is_archive_file(name): return True return False -def makedirs(name): +def makedirs(path): try: - os.makedirs(name) + os.makedirs(path) except OSError, (e, es): if errno.EEXIST != e: raise @@ -93,20 +93,28 @@ def symlink(src, dst): except: pass -def unlink(name): +def unlink(path): try: - os.unlink(name) + os.unlink(path) except OSError, (e, es): if errno.ENOENT != e: raise + +def rm_r(path): + """like rm -r command.""" + if os.path.isdir(path): + shutil.rmtree(path) + else: + unlink(path) def off(): unlink("%s/current" % PATH_PYTHONS) - for root, dirs, files in os.walk("%s/bin/" % ROOT): + for root, dirs, files in os.walk(PATH_BIN): for f in files: if f == "pythonbrew": continue unlink("%s%s" % (root, f)) + break #---------------------------------------------------- # classes @@ -539,6 +547,7 @@ class SwitchCommand(Command): for root, dirs, files in os.walk("%s/current/bin/" % PATH_PYTHONS): for f in files: symlink("%s%s" % (root, f), "%s/%s" % (PATH_BIN, f)) + break # I want better code if not os.path.isfile("%s/python" % PATH_BIN): if os.path.isfile("%s/python3" % PATH_BIN): @@ -602,10 +611,23 @@ class UninstallCommand(Command): curpath = os.path.realpath("%s/current" % PATH_PYTHONS) if pkgpath == curpath: off() - shutil.rmtree(pkgpath) + rm_r(pkgpath) else: self.parser.print_help() +class CleanCommand(Command): + name = "clean" + usage = "%prog" + summary = "Remove stale source folders and archives" + + def run_command(self, options, args): + self._clean(PATH_BUILD) + self._clean(PATH_DISTS) + + def _clean(self, root): + for dir in os.listdir(root): + rm_r("%s/%s" % (root, dir)) + class Pythonbrew(object): def run(self): options, args = parser.parse_args(sys.argv[1:]) @@ -623,6 +645,7 @@ class Pythonbrew(object): add_command(VersionCommand()) add_command(SearchCommand()) add_command(UninstallCommand()) + add_command(CleanCommand()) command = args[0].lower() if command not in command_dict: