From ea64553c0315676503ab1c652ba6ab4abe2f6781 Mon Sep 17 00:00:00 2001 From: utahta Date: Sun, 24 Oct 2010 00:57:51 +0900 Subject: [PATCH] Added uninstall command --- README.rst | 11 +++++++++-- pythonbrew | 41 +++++++++++++++++++++++++++++------------ pythonbrew.py | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/README.rst b/README.rst index 2e16ec4..3b324a1 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|off|version] +pythonbrew [options] [init|install|installed|switch|search|uninstall|off|version] Initialize:: @@ -52,6 +52,10 @@ Search python packages:: pythonbrew search Python-2.6 pythonbrew search Python-3 +Uninstall some Pythons:: + + pythonbrew uninstall Python-2.6.6 + Disable pythonbrew:: pythonbrew off @@ -87,8 +91,11 @@ switch /path/to/Python-dir/ switch /path/to/python Switch to the given version of python. -search [Python-] +search Python- Search Python packages. + +uninstall Python- + Uninstall the given version of python. off Disable pythonbrew. diff --git a/pythonbrew b/pythonbrew index 04153e7..a337d9f 100755 --- a/pythonbrew +++ b/pythonbrew @@ -100,7 +100,8 @@ def unlink(name): if errno.ENOENT != e: raise -def clean_switch_symlink(): +def off(): + unlink("%s/current" % PATH_PYTHONS) for root, dirs, files in os.walk("%s/bin/" % ROOT): for f in files: if f == "pythonbrew": @@ -520,7 +521,7 @@ class SwitchCommand(Command): if re.search( ".*python(\d(\.\d)?)?$", dist ): self._switch_file(dist) else: - print "Invalid binary: `%s`" % dist + print "`%s` is not python binary." % dist return elif not os.path.isdir( "%s/%s" % (PATH_PYTHONS, dist) ): print "`%s` is not installed." % dist @@ -528,17 +529,13 @@ class SwitchCommand(Command): self._switch_dir( distdir ) def _switch_file(self, dist): - unlink("%s/current" % PATH_PYTHONS) - unlink("%s/bin/python" % ROOT) - clean_switch_symlink() - symlink(dist, "%s/bin/python" % ROOT) + off() + symlink(dist, "%s/python" % PATH_BIN) print "Switched to "+dist def _switch_dir(self, dist): - unlink("%s/current" % PATH_PYTHONS) - unlink("%s/bin/python" % ROOT) + off() symlink(dist, "%s/current" % PATH_PYTHONS) - clean_switch_symlink() 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)) @@ -556,8 +553,7 @@ class OffCommand(Command): summary = "Disable pythonbrew" def run_command(self, options, args): - unlink("%s/current" % PATH_PYTHONS) - clean_switch_symlink() + off() class VersionCommand(Command): name = "version" @@ -569,7 +565,7 @@ class VersionCommand(Command): class SearchCommand(Command): name = "search" - usage = "%prog [Python-]" + usage = "%prog Python-" summary = "Search Python packages" def run_command(self, options, args): @@ -590,6 +586,26 @@ class SearchCommand(Command): for pkgname in pkgs.get_packages(): print pkgname +class UninstallCommand(Command): + name = "uninstall" + usage = "%prog Python-" + summary = "Uninstall the given version of python" + + def run_command(self, options, args): + if args: + pkgname = args[0] + pkgpath = "%s/%s" % (PATH_PYTHONS, pkgname) + if not os.path.isdir(pkgpath): + print "%s is not installed." % pkgname + sys.exit(1) + if os.path.islink("%s/current" % PATH_PYTHONS): + curpath = os.path.realpath("%s/current" % PATH_PYTHONS) + if pkgpath == curpath: + off() + shutil.rmtree(pkgpath) + else: + self.parser.print_help() + class Pythonbrew(object): def run(self): options, args = parser.parse_args(sys.argv[1:]) @@ -606,6 +622,7 @@ class Pythonbrew(object): add_command(OffCommand()) add_command(VersionCommand()) add_command(SearchCommand()) + add_command(UninstallCommand()) command = args[0].lower() if command not in command_dict: diff --git a/pythonbrew.py b/pythonbrew.py index 04153e7..a337d9f 100755 --- a/pythonbrew.py +++ b/pythonbrew.py @@ -100,7 +100,8 @@ def unlink(name): if errno.ENOENT != e: raise -def clean_switch_symlink(): +def off(): + unlink("%s/current" % PATH_PYTHONS) for root, dirs, files in os.walk("%s/bin/" % ROOT): for f in files: if f == "pythonbrew": @@ -520,7 +521,7 @@ class SwitchCommand(Command): if re.search( ".*python(\d(\.\d)?)?$", dist ): self._switch_file(dist) else: - print "Invalid binary: `%s`" % dist + print "`%s` is not python binary." % dist return elif not os.path.isdir( "%s/%s" % (PATH_PYTHONS, dist) ): print "`%s` is not installed." % dist @@ -528,17 +529,13 @@ class SwitchCommand(Command): self._switch_dir( distdir ) def _switch_file(self, dist): - unlink("%s/current" % PATH_PYTHONS) - unlink("%s/bin/python" % ROOT) - clean_switch_symlink() - symlink(dist, "%s/bin/python" % ROOT) + off() + symlink(dist, "%s/python" % PATH_BIN) print "Switched to "+dist def _switch_dir(self, dist): - unlink("%s/current" % PATH_PYTHONS) - unlink("%s/bin/python" % ROOT) + off() symlink(dist, "%s/current" % PATH_PYTHONS) - clean_switch_symlink() 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)) @@ -556,8 +553,7 @@ class OffCommand(Command): summary = "Disable pythonbrew" def run_command(self, options, args): - unlink("%s/current" % PATH_PYTHONS) - clean_switch_symlink() + off() class VersionCommand(Command): name = "version" @@ -569,7 +565,7 @@ class VersionCommand(Command): class SearchCommand(Command): name = "search" - usage = "%prog [Python-]" + usage = "%prog Python-" summary = "Search Python packages" def run_command(self, options, args): @@ -590,6 +586,26 @@ class SearchCommand(Command): for pkgname in pkgs.get_packages(): print pkgname +class UninstallCommand(Command): + name = "uninstall" + usage = "%prog Python-" + summary = "Uninstall the given version of python" + + def run_command(self, options, args): + if args: + pkgname = args[0] + pkgpath = "%s/%s" % (PATH_PYTHONS, pkgname) + if not os.path.isdir(pkgpath): + print "%s is not installed." % pkgname + sys.exit(1) + if os.path.islink("%s/current" % PATH_PYTHONS): + curpath = os.path.realpath("%s/current" % PATH_PYTHONS) + if pkgpath == curpath: + off() + shutil.rmtree(pkgpath) + else: + self.parser.print_help() + class Pythonbrew(object): def run(self): options, args = parser.parse_args(sys.argv[1:]) @@ -606,6 +622,7 @@ class Pythonbrew(object): add_command(OffCommand()) add_command(VersionCommand()) add_command(SearchCommand()) + add_command(UninstallCommand()) command = args[0].lower() if command not in command_dict: