Skip to content

Commit

Permalink
add systemwide install support
Browse files Browse the repository at this point in the history
  • Loading branch information
utahta committed Aug 6, 2011
1 parent 7b2fa2f commit 4993f46
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 38 deletions.
23 changes: 17 additions & 6 deletions pythonbrew-install
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,16 +66,23 @@ if [[ $PYTHON_FOUND != '1' ]] ; then
exit exit
fi fi


ROOT="$HOME/.pythonbrew" systemwide_install=0
if [[ -n $PYTHONBREW_ROOT ]] ; then if [[ -n "$PYTHONBREW_ROOT" ]] ; then
ROOT=$PYTHONBREW_ROOT ROOT="$PYTHONBREW_ROOT"
else
if (( UID == 0 )) ; then
systemwide_install=1
ROOT="/usr/local/pythonbrew"
else
ROOT="$HOME/.pythonbrew"
fi
fi fi
PATH_DISTS="$ROOT/dists" PATH_DISTS="$ROOT/dists"


STABLE_VERSION=`curl -skL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt` STABLE_VERSION=`curl -skL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt`
STABLE_VERSION=`trim $STABLE_VERSION` STABLE_VERSION=`trim $STABLE_VERSION`
if [[ $STABLE_VERSION = "" ]] ; then if [[ -z "$STABLE_VERSION" ]] ; then
echo 'Could not get stable-version of pythonbrew.' echo 'Can not get stable-version of pythonbrew.'
exit 1 exit 1
fi fi
TEMP_FILE="pythonbrew-$STABLE_VERSION" TEMP_FILE="pythonbrew-$STABLE_VERSION"
Expand All @@ -93,7 +100,11 @@ echo "Extracting $PATH_DISTS/$TEMP_TARBALL"
builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL


echo "Installing pythonbrew into $ROOT" 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 if [[ $? == 1 ]] ; then
echo "Failed to install pythonbrew." echo "Failed to install pythonbrew."
exit exit
Expand Down
30 changes: 14 additions & 16 deletions pythonbrew/commands/venv.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PATH_ETC, VIRTUALENV_DLSITE, PATH_DISTS PATH_ETC, VIRTUALENV_DLSITE, PATH_DISTS
from pythonbrew.util import Package, \ from pythonbrew.util import Package, \
is_installed, get_installed_pythons_pkgname, get_using_python_pkgname,\ is_installed, get_installed_pythons_pkgname, get_using_python_pkgname,\
untar_file untar_file, Subprocess, rm_r
from pythonbrew.log import logger from pythonbrew.log import logger
from pythonbrew.downloader import Downloader from pythonbrew.downloader import Downloader


Expand Down Expand Up @@ -92,32 +92,30 @@ def run_command_init(self):
untar_file(download_file, self._venv_dir) untar_file(download_file, self._venv_dir)


def run_command_create(self, options, args): def run_command_create(self, options, args):
virtualenv_options = '' virtualenv_options = []
if options.no_site_packages: if options.no_site_packages:
virtualenv_options += '--no-site-packages' virtualenv_options.append('--no-site-packages')


output = []
for arg in args[1:]: for arg in args[1:]:
target_dir = os.path.join(self._workon_home, arg) target_dir = os.path.join(self._workon_home, arg)
output.append("""\ logger.log("# Create `%s` environment into %s" % (arg, self._workon_home))
echo '# Create `%(arg)s` environment into %(workon_home)s' # make command
%(py)s %(venv)s -p '%(target_py)s' %(options)s '%(target_dir)s' cmd = [self._py, self._venv, '-p', self._target_py]
""" % {'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}) cmd.extend(virtualenv_options)
self._write(''.join(output)) cmd.append(target_dir)
# create environment
s = Subprocess(verbose=True)
s.call(cmd)


def run_command_delete(self, options, args): def run_command_delete(self, options, args):
output = []
for arg in args[1:]: for arg in args[1:]:
target_dir = os.path.join(self._workon_home, arg) target_dir = os.path.join(self._workon_home, arg)
if not os.path.isdir(target_dir): if not os.path.isdir(target_dir):
logger.error('%s already does not exist.' % target_dir) logger.error('%s already does not exist.' % target_dir)
else: else:
output.append("""\ logger.log('# Delete `%s` environment in %s' % (arg, self._workon_home))
echo '# Delete `%(arg)s` environment in %(workon_home)s' # make command
rm -rf '%(target_dir)s' rm_r(target_dir)
""" % {'arg': arg, 'workon_home': self._workon_home, 'target_dir': target_dir})
if output:
self._write(''.join(output))


def run_command_use(self, options, args): def run_command_use(self, options, args):
if len(args) < 2: if len(args) < 2:
Expand Down
18 changes: 15 additions & 3 deletions pythonbrew/etc/bashrc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ if [ -z "${PATH_HOME}" ] ; then
fi fi
PATH_HOME_ETC="$PATH_HOME/etc" PATH_HOME_ETC="$PATH_HOME/etc"


# exec # py file
pythonbrew="$PATH_ROOT/bin/pythonbrew" PY_PYTHONBREW="$PATH_ROOT/bin/pythonbrew"


# functions # functions
__pythonbrew_set_default() __pythonbrew_set_default()
Expand Down Expand Up @@ -103,7 +103,7 @@ __pythonbrew_find_command()
done done
} }


pythonbrew() __pythonbrew_run()
{ {
__pythonbrew_find_command "$@" __pythonbrew_find_command "$@"
case $command_name in case $command_name in
Expand All @@ -117,10 +117,22 @@ pythonbrew()
builtin hash -r builtin hash -r
} }


pythonbrew()
{
pythonbrew=$PY_PYTHONBREW
__pythonbrew_run "$@"
}

pybrew() pybrew()
{ {
pythonbrew "$@" pythonbrew "$@"
} }


sudopybrew()
{
pythonbrew="sudo PYTHONBREW_ROOT=$PATH_ROOT $PY_PYTHONBREW"
__pythonbrew_run "$@"
}

# main # main
__pythonbrew_set_current_path __pythonbrew_set_current_path
30 changes: 28 additions & 2 deletions pythonbrew/installer/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pythonbrew.define import INSTALLER_ROOT, ROOT, PATH_ETC from pythonbrew.define import INSTALLER_ROOT, ROOT, PATH_ETC


def install_pythonbrew(): def install_pythonbrew():
PythonbrewInstaller().install(INSTALLER_ROOT) PythonbrewInstaller.install(INSTALLER_ROOT)
# for bash # for bash
shrc = yourshrc = "bashrc" shrc = yourshrc = "bashrc"
logger.log(""" logger.log("""
Expand Down Expand Up @@ -33,4 +33,30 @@ def install_pythonbrew():
""" % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC}) """ % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC})


def upgrade_pythonbrew(): 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})

50 changes: 41 additions & 9 deletions pythonbrew/installer/pythonbrewinstaller.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\ from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\
PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\ PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\
PATH_SCRIPTS_PYTHONBREW_COMMANDS, PATH_BIN_PYTHONBREW,\ PATH_SCRIPTS_PYTHONBREW_COMMANDS, PATH_BIN_PYTHONBREW,\
ROOT, PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\ PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\
PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC, ROOT
import stat import stat


class PythonbrewInstaller(object): class PythonbrewInstaller(object):
"""pythonbrew installer: """pythonbrew installer:
""" """


def install(self, installer_root): @staticmethod
def install(installer_root):
# create directories # create directories
makedirs(PATH_PYTHONS) makedirs(PATH_PYTHONS)
makedirs(PATH_BUILD) makedirs(PATH_BUILD)
Expand Down Expand Up @@ -62,12 +63,43 @@ def install(self, installer_root):
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) 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 # create a bashrc for pythonbrew
fp = open(os.path.join(PATH_ETC,'bashrc'), 'w') shutil.copy(os.path.join(installer_root,'etc','bashrc'), os.path.join(PATH_ETC,'bashrc'))
for line in open(os.path.join(installer_root,'etc','bashrc')):
line = line.replace('@ROOT@', ROOT)
fp.write(line)
fp.close()


# copy config.cfg # copy config.cfg
shutil.copy(os.path.join(installer_root,'etc','config.cfg'), PATH_ETC_CONFIG) 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()

13 changes: 11 additions & 2 deletions pythonbrew_install.py
Original file line number Original file line Diff line number Diff line change
@@ -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 from optparse import OptionParser
if __name__ == "__main__": if __name__ == "__main__":
parser = OptionParser() parser = OptionParser()
Expand All @@ -9,8 +9,17 @@
default=False, default=False,
help="Upgrade." help="Upgrade."
) )
parser.add_option(
'--systemwide',
dest="systemwide",
action="store_true",
default=False,
help="systemwide install."
)
(opt, arg) = parser.parse_args() (opt, arg) = parser.parse_args()
if opt.upgrade: if opt.systemwide:
systemwide_pythonbrew()
elif opt.upgrade:
upgrade_pythonbrew() upgrade_pythonbrew()
else: else:
install_pythonbrew() install_pythonbrew()

0 comments on commit 4993f46

Please sign in to comment.