Skip to content
This repository
Browse code

add systemwide install support

  • Loading branch information...
commit 4993f466e98c1fb0cd1d12db00298d87db443c02 1 parent 7b2fa2f
utahta authored
23 pythonbrew-install
@@ -66,16 +66,23 @@ if [[ $PYTHON_FOUND != '1' ]] ; then
66 66 exit
67 67 fi
68 68
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
72 79 fi
73 80 PATH_DISTS="$ROOT/dists"
74 81
75 82 STABLE_VERSION=`curl -skL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt`
76 83 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.'
79 86 exit 1
80 87 fi
81 88 TEMP_FILE="pythonbrew-$STABLE_VERSION"
@@ -93,7 +100,11 @@ echo "Extracting $PATH_DISTS/$TEMP_TARBALL"
93 100 builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL
94 101
95 102 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
97 108 if [[ $? == 1 ]] ; then
98 109 echo "Failed to install pythonbrew."
99 110 exit
30 pythonbrew/commands/venv.py
@@ -5,7 +5,7 @@
5 5 PATH_ETC, VIRTUALENV_DLSITE, PATH_DISTS
6 6 from pythonbrew.util import Package, \
7 7 is_installed, get_installed_pythons_pkgname, get_using_python_pkgname,\
8   - untar_file
  8 + untar_file, Subprocess, rm_r
9 9 from pythonbrew.log import logger
10 10 from pythonbrew.downloader import Downloader
11 11
@@ -92,32 +92,30 @@ def run_command_init(self):
92 92 untar_file(download_file, self._venv_dir)
93 93
94 94 def run_command_create(self, options, args):
95   - virtualenv_options = ''
  95 + virtualenv_options = []
96 96 if options.no_site_packages:
97   - virtualenv_options += '--no-site-packages'
  97 + virtualenv_options.append('--no-site-packages')
98 98
99   - output = []
100 99 for arg in args[1:]:
101 100 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)
107 109
108 110 def run_command_delete(self, options, args):
109   - output = []
110 111 for arg in args[1:]:
111 112 target_dir = os.path.join(self._workon_home, arg)
112 113 if not os.path.isdir(target_dir):
113 114 logger.error('%s already does not exist.' % target_dir)
114 115 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)
121 119
122 120 def run_command_use(self, options, args):
123 121 if len(args) < 2:
18 pythonbrew/etc/bashrc
@@ -11,8 +11,8 @@ if [ -z "${PATH_HOME}" ] ; then
11 11 fi
12 12 PATH_HOME_ETC="$PATH_HOME/etc"
13 13
14   -# exec
15   -pythonbrew="$PATH_ROOT/bin/pythonbrew"
  14 +# py file
  15 +PY_PYTHONBREW="$PATH_ROOT/bin/pythonbrew"
16 16
17 17 # functions
18 18 __pythonbrew_set_default()
@@ -103,7 +103,7 @@ __pythonbrew_find_command()
103 103 done
104 104 }
105 105
106   -pythonbrew()
  106 +__pythonbrew_run()
107 107 {
108 108 __pythonbrew_find_command "$@"
109 109 case $command_name in
@@ -117,10 +117,22 @@ pythonbrew()
117 117 builtin hash -r
118 118 }
119 119
  120 +pythonbrew()
  121 +{
  122 + pythonbrew=$PY_PYTHONBREW
  123 + __pythonbrew_run "$@"
  124 +}
  125 +
120 126 pybrew()
121 127 {
122 128 pythonbrew "$@"
123 129 }
124 130
  131 +sudopybrew()
  132 +{
  133 + pythonbrew="sudo PYTHONBREW_ROOT=$PATH_ROOT $PY_PYTHONBREW"
  134 + __pythonbrew_run "$@"
  135 +}
  136 +
125 137 # main
126 138 __pythonbrew_set_current_path
30 pythonbrew/installer/__init__.py
@@ -3,7 +3,7 @@
3 3 from pythonbrew.define import INSTALLER_ROOT, ROOT, PATH_ETC
4 4
5 5 def install_pythonbrew():
6   - PythonbrewInstaller().install(INSTALLER_ROOT)
  6 + PythonbrewInstaller.install(INSTALLER_ROOT)
7 7 # for bash
8 8 shrc = yourshrc = "bashrc"
9 9 logger.log("""
@@ -33,4 +33,30 @@ def install_pythonbrew():
33 33 """ % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC})
34 34
35 35 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 +
50 pythonbrew/installer/pythonbrewinstaller.py
@@ -6,15 +6,16 @@
6 6 from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\
7 7 PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\
8 8 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
11 11 import stat
12 12
13 13 class PythonbrewInstaller(object):
14 14 """pythonbrew installer:
15 15 """
16 16
17   - def install(self, installer_root):
  17 + @staticmethod
  18 + def install(installer_root):
18 19 # create directories
19 20 makedirs(PATH_PYTHONS)
20 21 makedirs(PATH_BUILD)
@@ -62,12 +63,43 @@ def install(self, installer_root):
62 63 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)
63 64
64 65 # 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'))
70 67
71 68 # copy config.cfg
72 69 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 +
13 pythonbrew_install.py
... ... @@ -1,4 +1,4 @@
1   -from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew
  1 +from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew, systemwide_pythonbrew
2 2 from optparse import OptionParser
3 3 if __name__ == "__main__":
4 4 parser = OptionParser()
@@ -9,8 +9,17 @@
9 9 default=False,
10 10 help="Upgrade."
11 11 )
  12 + parser.add_option(
  13 + '--systemwide',
  14 + dest="systemwide",
  15 + action="store_true",
  16 + default=False,
  17 + help="systemwide install."
  18 + )
12 19 (opt, arg) = parser.parse_args()
13   - if opt.upgrade:
  20 + if opt.systemwide:
  21 + systemwide_pythonbrew()
  22 + elif opt.upgrade:
14 23 upgrade_pythonbrew()
15 24 else:
16 25 install_pythonbrew()

0 comments on commit 4993f46

Please sign in to comment.
Something went wrong with that request. Please try again.