Skip to content

Commit 4993f46

Browse files
committed
add systemwide install support
1 parent 7b2fa2f commit 4993f46

6 files changed

Lines changed: 126 additions & 38 deletions

File tree

pythonbrew-install

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,23 @@ if [[ $PYTHON_FOUND != '1' ]] ; then
6666
exit
6767
fi
6868

69-
ROOT="$HOME/.pythonbrew"
70-
if [[ -n $PYTHONBREW_ROOT ]] ; then
71-
ROOT=$PYTHONBREW_ROOT
69+
systemwide_install=0
70+
if [[ -n "$PYTHONBREW_ROOT" ]] ; then
71+
ROOT="$PYTHONBREW_ROOT"
72+
else
73+
if (( UID == 0 )) ; then
74+
systemwide_install=1
75+
ROOT="/usr/local/pythonbrew"
76+
else
77+
ROOT="$HOME/.pythonbrew"
78+
fi
7279
fi
7380
PATH_DISTS="$ROOT/dists"
7481

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

95102
echo "Installing pythonbrew into $ROOT"
96-
$PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py
103+
if (( systemwide_install == 1 )) ; then
104+
PYTHONBREW_ROOT="$ROOT" $PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py --systemwide
105+
else
106+
$PYTHON $PATH_DISTS/$TEMP_FILE/pythonbrew_install.py
107+
fi
97108
if [[ $? == 1 ]] ; then
98109
echo "Failed to install pythonbrew."
99110
exit

pythonbrew/commands/venv.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
PATH_ETC, VIRTUALENV_DLSITE, PATH_DISTS
66
from pythonbrew.util import Package, \
77
is_installed, get_installed_pythons_pkgname, get_using_python_pkgname,\
8-
untar_file
8+
untar_file, Subprocess, rm_r
99
from pythonbrew.log import logger
1010
from pythonbrew.downloader import Downloader
1111

@@ -92,32 +92,30 @@ def run_command_init(self):
9292
untar_file(download_file, self._venv_dir)
9393

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

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

108110
def run_command_delete(self, options, args):
109-
output = []
110111
for arg in args[1:]:
111112
target_dir = os.path.join(self._workon_home, arg)
112113
if not os.path.isdir(target_dir):
113114
logger.error('%s already does not exist.' % target_dir)
114115
else:
115-
output.append("""\
116-
echo '# Delete `%(arg)s` environment in %(workon_home)s'
117-
rm -rf '%(target_dir)s'
118-
""" % {'arg': arg, 'workon_home': self._workon_home, 'target_dir': target_dir})
119-
if output:
120-
self._write(''.join(output))
116+
logger.log('# Delete `%s` environment in %s' % (arg, self._workon_home))
117+
# make command
118+
rm_r(target_dir)
121119

122120
def run_command_use(self, options, args):
123121
if len(args) < 2:

pythonbrew/etc/bashrc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ if [ -z "${PATH_HOME}" ] ; then
1111
fi
1212
PATH_HOME_ETC="$PATH_HOME/etc"
1313

14-
# exec
15-
pythonbrew="$PATH_ROOT/bin/pythonbrew"
14+
# py file
15+
PY_PYTHONBREW="$PATH_ROOT/bin/pythonbrew"
1616

1717
# functions
1818
__pythonbrew_set_default()
@@ -103,7 +103,7 @@ __pythonbrew_find_command()
103103
done
104104
}
105105

106-
pythonbrew()
106+
__pythonbrew_run()
107107
{
108108
__pythonbrew_find_command "$@"
109109
case $command_name in
@@ -117,10 +117,22 @@ pythonbrew()
117117
builtin hash -r
118118
}
119119

120+
pythonbrew()
121+
{
122+
pythonbrew=$PY_PYTHONBREW
123+
__pythonbrew_run "$@"
124+
}
125+
120126
pybrew()
121127
{
122128
pythonbrew "$@"
123129
}
124130

131+
sudopybrew()
132+
{
133+
pythonbrew="sudo PYTHONBREW_ROOT=$PATH_ROOT $PY_PYTHONBREW"
134+
__pythonbrew_run "$@"
135+
}
136+
125137
# main
126138
__pythonbrew_set_current_path

pythonbrew/installer/__init__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pythonbrew.define import INSTALLER_ROOT, ROOT, PATH_ETC
44

55
def install_pythonbrew():
6-
PythonbrewInstaller().install(INSTALLER_ROOT)
6+
PythonbrewInstaller.install(INSTALLER_ROOT)
77
# for bash
88
shrc = yourshrc = "bashrc"
99
logger.log("""
@@ -33,4 +33,30 @@ def install_pythonbrew():
3333
""" % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC})
3434

3535
def upgrade_pythonbrew():
36-
PythonbrewInstaller().install(INSTALLER_ROOT)
36+
PythonbrewInstaller.install(INSTALLER_ROOT)
37+
38+
def systemwide_pythonbrew():
39+
PythonbrewInstaller.install(INSTALLER_ROOT)
40+
PythonbrewInstaller.systemwide_install()
41+
logger.log("""
42+
Well-done! Congratulations!
43+
44+
The pythonbrew is installed as:
45+
46+
%(ROOT)s
47+
48+
After that, exit this shell, start a new one, and install some fresh
49+
pythons:
50+
51+
pythonbrew install 2.7.2
52+
pythonbrew install 3.2
53+
54+
For further instructions, run:
55+
56+
pythonbrew help
57+
58+
The default help messages will popup and tell you what to do!
59+
60+
Enjoy pythonbrew at %(ROOT)s!!
61+
""" % {'ROOT':ROOT})
62+

pythonbrew/installer/pythonbrewinstaller.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\
77
PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\
88
PATH_SCRIPTS_PYTHONBREW_COMMANDS, PATH_BIN_PYTHONBREW,\
9-
ROOT, PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\
10-
PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC
9+
PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\
10+
PATH_SCRIPTS_PYTHONBREW_INSTALLER, PATH_VENVS, PATH_HOME_ETC, ROOT
1111
import stat
1212

1313
class PythonbrewInstaller(object):
1414
"""pythonbrew installer:
1515
"""
1616

17-
def install(self, installer_root):
17+
@staticmethod
18+
def install(installer_root):
1819
# create directories
1920
makedirs(PATH_PYTHONS)
2021
makedirs(PATH_BUILD)
@@ -62,12 +63,43 @@ def install(self, installer_root):
6263
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)
6364

6465
# create a bashrc for pythonbrew
65-
fp = open(os.path.join(PATH_ETC,'bashrc'), 'w')
66-
for line in open(os.path.join(installer_root,'etc','bashrc')):
67-
line = line.replace('@ROOT@', ROOT)
68-
fp.write(line)
69-
fp.close()
66+
shutil.copy(os.path.join(installer_root,'etc','bashrc'), os.path.join(PATH_ETC,'bashrc'))
7067

7168
# copy config.cfg
7269
shutil.copy(os.path.join(installer_root,'etc','config.cfg'), PATH_ETC_CONFIG)
73-
70+
71+
@staticmethod
72+
def systemwide_install():
73+
profile = """\
74+
#begin-pythonbrew
75+
if [ -n "${BASH_VERSION:-}" -o -n "${ZSH_VERSION:-}" ] ; then
76+
export PYTHONBREW_ROOT=%(root)s
77+
source "${PYTHONBREW_ROOT}/etc/bashrc"
78+
fi
79+
#end-pythonbrew
80+
""" % {'root': ROOT}
81+
82+
if os.path.isdir('/etc/profile.d'):
83+
fp = open('/etc/profile.d/pythonbrew.sh', 'w')
84+
fp.write(profile)
85+
fp.close()
86+
elif os.path.isfile('/etc/profile'):
87+
output = []
88+
is_copy = True
89+
fp = open('/etc/profile', 'r')
90+
for line in fp:
91+
if line.startswith('#begin-pythonbrew'):
92+
is_copy = False
93+
continue
94+
elif line.startswith('#end-pythonbrew'):
95+
is_copy = True
96+
continue
97+
if is_copy:
98+
output.append(line)
99+
fp.close()
100+
output.append(profile)
101+
102+
fp = open('/etc/profile', 'w')
103+
fp.write(''.join(output))
104+
fp.close()
105+

pythonbrew_install.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew
1+
from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew, systemwide_pythonbrew
22
from optparse import OptionParser
33
if __name__ == "__main__":
44
parser = OptionParser()
@@ -9,8 +9,17 @@
99
default=False,
1010
help="Upgrade."
1111
)
12+
parser.add_option(
13+
'--systemwide',
14+
dest="systemwide",
15+
action="store_true",
16+
default=False,
17+
help="systemwide install."
18+
)
1219
(opt, arg) = parser.parse_args()
13-
if opt.upgrade:
20+
if opt.systemwide:
21+
systemwide_pythonbrew()
22+
elif opt.upgrade:
1423
upgrade_pythonbrew()
1524
else:
1625
install_pythonbrew()

0 commit comments

Comments
 (0)