mirror of
https://github.com/kennethreitz-archive/python-build.git
synced 2026-06-05 23:30:18 +00:00
update symlink command
This commit is contained in:
@@ -3,7 +3,6 @@ import sys
|
||||
import re
|
||||
from optparse import OptionParser
|
||||
from pythonbrew import commands
|
||||
from pythonbrew.define import PATH_BIN_PYTHONBREW
|
||||
|
||||
command_dict = {}
|
||||
|
||||
@@ -14,7 +13,7 @@ class Command(object):
|
||||
|
||||
def __init__(self):
|
||||
self.parser = OptionParser(usage=self.usage,
|
||||
prog='%s %s' % (PATH_BIN_PYTHONBREW, self.name))
|
||||
prog='%s %s' % ("pythonbrew", self.name))
|
||||
command_dict[self.name] = self
|
||||
|
||||
def run(self, args):
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from optparse import OptionParser
|
||||
from pythonbrew.define import VERSION, PATH_BIN_PYTHONBREW
|
||||
from pythonbrew.define import VERSION
|
||||
|
||||
parser = OptionParser(usage="%prog COMMAND [OPTIONS]",
|
||||
prog=PATH_BIN_PYTHONBREW,
|
||||
prog="pythonbrew",
|
||||
version=VERSION,
|
||||
add_help_option=False)
|
||||
parser.add_option(
|
||||
|
||||
@@ -2,10 +2,11 @@ import os
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_BIN
|
||||
from pythonbrew.util import Package, symlink, unlink
|
||||
from pythonbrew.log import logger
|
||||
|
||||
class SymlinkCommand(Command):
|
||||
name = "symlink"
|
||||
usage = "%prog"
|
||||
usage = "%prog [OPTIONS] [SCRIPT]"
|
||||
summary = "Create/Remove a symbolic link"
|
||||
|
||||
def __init__(self):
|
||||
@@ -26,36 +27,57 @@ class SymlinkCommand(Command):
|
||||
help="Remove the all symbolic link."
|
||||
)
|
||||
self.parser.add_option(
|
||||
"-b", "--bin",
|
||||
dest="bin",
|
||||
action="append",
|
||||
default=[],
|
||||
help="Create a symbolic link to the specified script name in bin directory."
|
||||
"-d", "--default",
|
||||
dest="default",
|
||||
default=None,
|
||||
help="Use as default the specified python version."
|
||||
)
|
||||
|
||||
def run_command(self, options, args):
|
||||
pythons = self._get_pythons(options.pythons)
|
||||
for pkgname in pythons:
|
||||
if options.remove:
|
||||
for bin in os.listdir(PATH_BIN):
|
||||
path = os.path.join(PATH_BIN, bin)
|
||||
if os.path.islink(path):
|
||||
unlink(path)
|
||||
else:
|
||||
self._symlink('python', 'py', pkgname)
|
||||
for bin in options.bin:
|
||||
if options.default:
|
||||
"""create only one instance as default of an application.
|
||||
"""
|
||||
pythons = self._get_pythons([options.default])
|
||||
for pkgname in pythons:
|
||||
if args:
|
||||
bin = args[0]
|
||||
self._symlink(bin, bin, pkgname)
|
||||
else:
|
||||
self._symlink('python', 'py', pkgname)
|
||||
else:
|
||||
pythons = self._get_pythons(options.pythons)
|
||||
for pkgname in pythons:
|
||||
if options.remove:
|
||||
for bin in os.listdir(PATH_BIN):
|
||||
path = os.path.join(PATH_BIN, bin)
|
||||
if os.path.islink(path):
|
||||
unlink(path)
|
||||
else:
|
||||
if args:
|
||||
bin = args[0]
|
||||
self._symlink_version_suffix(bin, bin, pkgname)
|
||||
else:
|
||||
self._symlink_version_suffix('python', 'py', pkgname)
|
||||
|
||||
def _symlink(self, srcbin, dstbin, pkgname):
|
||||
"""Create the symlink
|
||||
def _symlink_version_suffix(self, srcbin, dstbin, pkgname):
|
||||
"""Create a symlink. add version suffix.
|
||||
"""
|
||||
version = Package(pkgname).version
|
||||
dstbin = '%s%s' % (dstbin, version)
|
||||
self._symlink(srcbin, dstbin, pkgname)
|
||||
|
||||
def _symlink(self, srcbin, dstbin, pkgname):
|
||||
"""Create a symlink.
|
||||
"""
|
||||
src = os.path.join(PATH_PYTHONS, pkgname, 'bin', srcbin)
|
||||
dst = os.path.join(PATH_BIN, '%s%s' % (dstbin, version))
|
||||
symlink(src, dst)
|
||||
dst = os.path.join(PATH_BIN, dstbin)
|
||||
if os.path.isfile(src):
|
||||
symlink(src, dst)
|
||||
else:
|
||||
logger.info("%s: File not found" % src)
|
||||
|
||||
def _get_pythons(self, _pythons):
|
||||
"""Get the installed python versions.
|
||||
"""Get the installed python versions list.
|
||||
"""
|
||||
pythons = [Package(p).name for p in _pythons]
|
||||
return [d for d in sorted(os.listdir(PATH_PYTHONS))
|
||||
|
||||
Reference in New Issue
Block a user