From 4993f466e98c1fb0cd1d12db00298d87db443c02 Mon Sep 17 00:00:00 2001 From: utahta Date: Sun, 7 Aug 2011 03:48:38 +0900 Subject: [PATCH] add systemwide install support --- pythonbrew-install | 23 +++++++--- pythonbrew/commands/venv.py | 30 ++++++------- pythonbrew/etc/bashrc | 18 ++++++-- pythonbrew/installer/__init__.py | 30 ++++++++++++- pythonbrew/installer/pythonbrewinstaller.py | 50 +++++++++++++++++---- pythonbrew_install.py | 13 +++++- 6 files changed, 126 insertions(+), 38 deletions(-) diff --git a/pythonbrew-install b/pythonbrew-install index f918d47..70594c1 100755 --- a/pythonbrew-install +++ b/pythonbrew-install @@ -66,16 +66,23 @@ if [[ $PYTHON_FOUND != '1' ]] ; then exit fi -ROOT="$HOME/.pythonbrew" -if [[ -n $PYTHONBREW_ROOT ]] ; then - ROOT=$PYTHONBREW_ROOT +systemwide_install=0 +if [[ -n "$PYTHONBREW_ROOT" ]] ; then + ROOT="$PYTHONBREW_ROOT" +else + if (( UID == 0 )) ; then + systemwide_install=1 + ROOT="/usr/local/pythonbrew" + else + ROOT="$HOME/.pythonbrew" + fi fi PATH_DISTS="$ROOT/dists" STABLE_VERSION=`curl -skL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt` STABLE_VERSION=`trim $STABLE_VERSION` -if [[ $STABLE_VERSION = "" ]] ; then - echo 'Could not get stable-version of pythonbrew.' +if [[ -z "$STABLE_VERSION" ]] ; then + echo 'Can not get stable-version of pythonbrew.' exit 1 fi TEMP_FILE="pythonbrew-$STABLE_VERSION" @@ -93,7 +100,11 @@ echo "Extracting $PATH_DISTS/$TEMP_TARBALL" builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL echo "Installing pythonbrew into $ROOT" -$PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py +if (( systemwide_install == 1 )) ; then + PYTHONBREW_ROOT="$ROOT" $PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py --systemwide +else + $PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py +fi if [[ $? == 1 ]] ; then echo "Failed to install pythonbrew." exit diff --git a/pythonbrew/commands/venv.py b/pythonbrew/commands/venv.py index f4a6318..bd8e32e 100644 --- a/pythonbrew/commands/venv.py +++ b/pythonbrew/commands/venv.py @@ -5,7 +5,7 @@ from pythonbrew.define import PATH_PYTHONS, PATH_VENVS, PATH_HOME_ETC_VENV,\ PATH_ETC, VIRTUALENV_DLSITE, PATH_DISTS from pythonbrew.util import Package, \ is_installed, get_installed_pythons_pkgname, get_using_python_pkgname,\ - untar_file + untar_file, Subprocess, rm_r from pythonbrew.log import logger from pythonbrew.downloader import Downloader @@ -92,32 +92,30 @@ class VenvCommand(Command): untar_file(download_file, self._venv_dir) def run_command_create(self, options, args): - virtualenv_options = '' + virtualenv_options = [] if options.no_site_packages: - virtualenv_options += '--no-site-packages' + virtualenv_options.append('--no-site-packages') - output = [] for arg in args[1:]: target_dir = os.path.join(self._workon_home, arg) - output.append("""\ -echo '# Create `%(arg)s` environment into %(workon_home)s' -%(py)s %(venv)s -p '%(target_py)s' %(options)s '%(target_dir)s' -""" % {'arg': arg, 'workon_home': self._workon_home, 'py': self._py, 'venv': self._venv, 'target_py': self._target_py, 'options': virtualenv_options, 'target_dir': target_dir}) - self._write(''.join(output)) + logger.log("# Create `%s` environment into %s" % (arg, self._workon_home)) + # make command + cmd = [self._py, self._venv, '-p', self._target_py] + cmd.extend(virtualenv_options) + cmd.append(target_dir) + # create environment + s = Subprocess(verbose=True) + s.call(cmd) def run_command_delete(self, options, args): - output = [] for arg in args[1:]: target_dir = os.path.join(self._workon_home, arg) if not os.path.isdir(target_dir): logger.error('%s already does not exist.' % target_dir) else: - output.append("""\ -echo '# Delete `%(arg)s` environment in %(workon_home)s' -rm -rf '%(target_dir)s' -""" % {'arg': arg, 'workon_home': self._workon_home, 'target_dir': target_dir}) - if output: - self._write(''.join(output)) + logger.log('# Delete `%s` environment in %s' % (arg, self._workon_home)) + # make command + rm_r(target_dir) def run_command_use(self, options, args): if len(args) < 2: diff --git a/pythonbrew/etc/bashrc b/pythonbrew/etc/bashrc index e6e7e02..da65b41 100644 --- a/pythonbrew/etc/bashrc +++ b/pythonbrew/etc/bashrc @@ -11,8 +11,8 @@ if [ -z "${PATH_HOME}" ] ; then fi PATH_HOME_ETC="$PATH_HOME/etc" -# exec -pythonbrew="$PATH_ROOT/bin/pythonbrew" +# py file +PY_PYTHONBREW="$PATH_ROOT/bin/pythonbrew" # functions __pythonbrew_set_default() @@ -103,7 +103,7 @@ __pythonbrew_find_command() done } -pythonbrew() +__pythonbrew_run() { __pythonbrew_find_command "$@" case $command_name in @@ -117,10 +117,22 @@ pythonbrew() builtin hash -r } +pythonbrew() +{ + pythonbrew=$PY_PYTHONBREW + __pythonbrew_run "$@" +} + pybrew() { pythonbrew "$@" } +sudopybrew() +{ + pythonbrew="sudo PYTHONBREW_ROOT=$PATH_ROOT $PY_PYTHONBREW" + __pythonbrew_run "$@" +} + # main __pythonbrew_set_current_path diff --git a/pythonbrew/installer/__init__.py b/pythonbrew/installer/__init__.py index 8ff349d..0a22bee 100644 --- a/pythonbrew/installer/__init__.py +++ b/pythonbrew/installer/__init__.py @@ -3,7 +3,7 @@ from pythonbrew.log import logger from pythonbrew.define import INSTALLER_ROOT, ROOT, PATH_ETC def install_pythonbrew(): - PythonbrewInstaller().install(INSTALLER_ROOT) + PythonbrewInstaller.install(INSTALLER_ROOT) # for bash shrc = yourshrc = "bashrc" logger.log(""" @@ -33,4 +33,30 @@ Enjoy pythonbrew at %(ROOT)s!! """ % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC}) def upgrade_pythonbrew(): - PythonbrewInstaller().install(INSTALLER_ROOT) + PythonbrewInstaller.install(INSTALLER_ROOT) + +def systemwide_pythonbrew(): + PythonbrewInstaller.install(INSTALLER_ROOT) + PythonbrewInstaller.systemwide_install() + logger.log(""" +Well-done! Congratulations! + +The pythonbrew is installed as: + + %(ROOT)s + +After that, exit this shell, start a new one, and install some fresh +pythons: + + pythonbrew install 2.7.2 + pythonbrew install 3.2 + +For further instructions, run: + + pythonbrew help + +The default help messages will popup and tell you what to do! + +Enjoy pythonbrew at %(ROOT)s!! +""" % {'ROOT':ROOT}) + \ No newline at end of file diff --git a/pythonbrew/installer/pythonbrewinstaller.py b/pythonbrew/installer/pythonbrewinstaller.py index f8dd398..ab6e824 100644 --- a/pythonbrew/installer/pythonbrewinstaller.py +++ b/pythonbrew/installer/pythonbrewinstaller.py @@ -6,15 +6,16 @@ from pythonbrew.util import makedirs, rm_r from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\ PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\ PATH_SCRIPTS_PYTHONBREW_COMMANDS, PATH_BIN_PYTHONBREW,\ - ROOT, PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\ - PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC + PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\ + PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC, ROOT import stat class PythonbrewInstaller(object): """pythonbrew installer: """ - def install(self, installer_root): + @staticmethod + def install(installer_root): # create directories makedirs(PATH_PYTHONS) makedirs(PATH_BUILD) @@ -62,12 +63,43 @@ if __name__ == "__main__": os.chmod(PATH_BIN_PYTHONBREW, stat.S_IRUSR|stat.S_IWUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH) # create a bashrc for pythonbrew - fp = open(os.path.join(PATH_ETC,'bashrc'), 'w') - for line in open(os.path.join(installer_root,'etc','bashrc')): - line = line.replace('@ROOT@', ROOT) - fp.write(line) - fp.close() + shutil.copy(os.path.join(installer_root,'etc','bashrc'), os.path.join(PATH_ETC,'bashrc')) # copy config.cfg shutil.copy(os.path.join(installer_root,'etc','config.cfg'), PATH_ETC_CONFIG) - + + @staticmethod + def systemwide_install(): + profile = """\ +#begin-pythonbrew +if [ -n "${BASH_VERSION:-}" -o -n "${ZSH_VERSION:-}" ] ; then + export PYTHONBREW_ROOT=%(root)s + source "${PYTHONBREW_ROOT}/etc/bashrc" +fi +#end-pythonbrew +""" % {'root': ROOT} + + if os.path.isdir('/etc/profile.d'): + fp = open('/etc/profile.d/pythonbrew.sh', 'w') + fp.write(profile) + fp.close() + elif os.path.isfile('/etc/profile'): + output = [] + is_copy = True + fp = open('/etc/profile', 'r') + for line in fp: + if line.startswith('#begin-pythonbrew'): + is_copy = False + continue + elif line.startswith('#end-pythonbrew'): + is_copy = True + continue + if is_copy: + output.append(line) + fp.close() + output.append(profile) + + fp = open('/etc/profile', 'w') + fp.write(''.join(output)) + fp.close() + diff --git a/pythonbrew_install.py b/pythonbrew_install.py index 0f78ffd..13075c2 100644 --- a/pythonbrew_install.py +++ b/pythonbrew_install.py @@ -1,4 +1,4 @@ -from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew +from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew, systemwide_pythonbrew from optparse import OptionParser if __name__ == "__main__": parser = OptionParser() @@ -9,8 +9,17 @@ if __name__ == "__main__": default=False, help="Upgrade." ) + parser.add_option( + '--systemwide', + dest="systemwide", + action="store_true", + default=False, + help="systemwide install." + ) (opt, arg) = parser.parse_args() - if opt.upgrade: + if opt.systemwide: + systemwide_pythonbrew() + elif opt.upgrade: upgrade_pythonbrew() else: install_pythonbrew()