Skip to content

Commit a696654

Browse files
author
utahta
committed
Refactoring: update command
1 parent 828f488 commit a696654

6 files changed

Lines changed: 118 additions & 122 deletions

File tree

pythonbrew-install

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
PYTHON=`command -v python`
4+
CURL=`command -v curl`
45

56
usage()
67
{
@@ -31,9 +32,12 @@ parse_arguments()
3132
parse_arguments $@
3233

3334
if [[ ! -x $PYTHON ]] ; then
34-
echo "I think $PYTHON is not Python interpreter."
35+
echo "Python (2.4, 2.5 or 2.6) is required."
3536
exit
3637
fi
38+
if [[ ! -x $CURL ]] ; then
39+
echo "curl is required."
40+
fi
3741

3842
PYTHON_VERSION=`$PYTHON -V 2>&1`
3943
PYTHON_VERSION=${PYTHON_VERSION/"Python "/""}
@@ -61,7 +65,7 @@ TEMP_TARBALL="pythonbrew.tar.gz"
6165
DOWNLOAD_URL="http://github.com/utahta/pythonbrew/tarball/master"
6266

6367
echo "Downloading $DOWNLOAD_URL"
64-
builtin cd $PATH_DISTS ; curl -L $DOWNLOAD_URL -o "$TEMP_TARBALL" > /dev/null 2>&1
68+
builtin cd $PATH_DISTS ; curl -sL $DOWNLOAD_URL -o "$TEMP_TARBALL"
6569

6670
echo "Extracting $PATH_DISTS/$TEMP_TARBALL"
6771
builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL
@@ -74,51 +78,3 @@ if [[ $? == 1 ]] ; then
7478
echo "Failed to install pythonbrew."
7579
exit
7680
fi
77-
78-
case $SHELL in
79-
*tcsh)
80-
shrc="cshrc"
81-
yourshrc="tcshrc"
82-
;;
83-
84-
*csh)
85-
shrc="cshrc"
86-
yourshrc=$shrc
87-
;;
88-
89-
*)
90-
shrc="bashrc"
91-
yourshrc=$shrc
92-
;;
93-
esac
94-
95-
printf "
96-
The pythonbrew is installed as:
97-
98-
$ROOT/bin/pythonbrew
99-
100-
You may trash the downloaded $0 from now on.
101-
102-
Pythonbrew environment initiated, required directories are created under
103-
104-
$ROOT
105-
106-
Well-done! Congratulations! Please add the following line to the end
107-
of your ~/.$yourshrc
108-
109-
source $PATH_ETC/$shrc
110-
111-
After that, exit this shell, start a new one, and install some fresh
112-
pythons:
113-
114-
pythonbrew install 2.6.6
115-
pythonbrew install 2.5.5
116-
117-
For further instructions, run:
118-
119-
pythonbrew help
120-
121-
The default help messages will popup and tell you what to do!
122-
123-
Enjoy pythonbrew at $ROOT!!
124-
"

pythonbrew/commands/uninstall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run_command(self, options, args):
1616
pkgname = pkg.name
1717
pkgpath = "%s/%s" % (PATH_PYTHONS, pkgname)
1818
if not os.path.isdir(pkgpath):
19-
logger.error("`%s` is not installed." % pkgname)
19+
logger.info("`%s` is not installed." % pkgname)
2020
sys.exit(1)
2121
if os.path.islink("%s/current" % PATH_PYTHONS):
2222
curpath = os.path.realpath("%s/current" % PATH_PYTHONS)

pythonbrew/commands/update.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import sys
3-
import re
43
from pythonbrew.basecommand import Command
5-
from pythonbrew.define import PATH_DISTS, VERSION, PYTHONBREW_DIRNAME, ROOT
4+
from pythonbrew.define import PATH_DISTS, VERSION, ROOT,\
5+
PATH_BUILD
66
from pythonbrew.log import logger
7-
from pythonbrew.downloader import Downloader, get_pythonbrew_update_url
8-
from pythonbrew.util import Subprocess, rm_r
7+
from pythonbrew.downloader import Downloader, get_pythonbrew_update_url,\
8+
get_response_from_url
9+
from pythonbrew.util import rm_r, is_html, unpack_downloadfile
10+
from pythonbrew.installer import PythonbrewInstaller
911

1012
class UpdateCommand(Command):
1113
name = "update"
@@ -26,39 +28,34 @@ def run_command(self, options, args):
2628
if not download_url:
2729
logger.error("`%s` of pythonbrew not found." % version)
2830
sys.exit(1)
31+
resp = get_response_from_url(download_url)
32+
content_type = resp.info()['content-type']
33+
if is_html(content_type):
34+
logger.error("Invalid content-type: `%s`" % content_type)
35+
sys.exit(1)
2936

3037
distname = "pythonbrew.tgz"
31-
download_path = "%s/%s" % (PATH_DISTS, distname)
38+
download_file = os.path.join(PATH_DISTS, distname)
3239
try:
3340
d = Downloader()
34-
d.download(distname, download_url, download_path)
41+
d.download(distname, download_url, download_file)
3542
except:
3643
logger.error("Failed to download. `%s`" % download_url)
3744
sys.exit(1)
38-
39-
_re = re.compile("^%s.*" % PYTHONBREW_DIRNAME)
40-
for name in os.listdir(PATH_DISTS):
41-
if _re.match(name):
42-
rm_r("%s/%s" % (PATH_DISTS, name))
45+
46+
extract_dir = os.path.join(PATH_BUILD, "pythonbrew")
47+
rm_r(extract_dir)
48+
if not unpack_downloadfile(content_type, download_file, extract_dir):
49+
sys.exit(1)
50+
4351
try:
44-
s = Subprocess(shell=True, cwd=PATH_DISTS, print_cmd=False)
45-
logger.info("Extracting %s" % download_path)
46-
s.check_call("tar zxf %s" % download_path)
52+
installer_path = "%s/pythonbrew" % (extract_dir)
53+
logger.info("Installing %s into %s" % (extract_dir, ROOT))
54+
PythonbrewInstaller().install(installer_path)
4755
except:
4856
logger.error("Failed to update pythonbrew.")
57+
raise
4958
sys.exit(1)
50-
51-
for name in os.listdir(PATH_DISTS):
52-
if _re.match(name):
53-
try:
54-
installer_path = "%s/%s" % (PATH_DISTS, name)
55-
s = Subprocess(shell=True, cwd=PATH_DISTS, print_cmd=False)
56-
logger.info("Installing %s into %s" % (installer_path, ROOT))
57-
s.check_call("%s %s/pythonbrew_install.py" % (sys.executable, installer_path))
58-
except:
59-
logger.error("Failed to update pythonbrew.")
60-
sys.exit(1)
61-
break
6259
logger.info("The pythonbrew has been updated.")
6360

6461
UpdateCommand()

pythonbrew/define.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"0.5": "https://github.com/utahta/pythonbrew/tarball/0.5",
3737
"0.6": "https://github.com/utahta/pythonbrew/tarball/0.6",
3838
}
39-
PYTHONBREW_DIRNAME = "utahta-pythonbrew"
4039

4140
# download Python package url
4241
PYTHON_PACKAGE_URL = {}

pythonbrew/installer.py

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import shutil
55
import re
66
from pythonbrew.util import makedirs, symlink, Package, is_url, splitext, Link,\
7-
unlink, is_gzip, is_html, untar_file, Subprocess, rm_r,\
8-
is_macosx_snowleopard, is_python25, is_python24, is_python26
7+
unlink, is_html, Subprocess, rm_r,\
8+
is_macosx_snowleopard, is_python25, is_python24, is_python26,\
9+
unpack_downloadfile
910
from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\
1011
PATH_ETC, PATH_SCRIPTS, PATH_SCRIPTS_PYTHONBREW,\
1112
PATH_SCRIPTS_PYTHONBREW_COMMANDS, INSTALLER_ROOT, PATH_BIN_PYTHONBREW,\
@@ -16,42 +17,79 @@
1617
from pythonbrew.log import logger
1718

1819
def install_pythonbrew():
19-
makedirs(PATH_PYTHONS)
20-
makedirs(PATH_BUILD)
21-
makedirs(PATH_DISTS)
22-
makedirs(PATH_ETC)
23-
makedirs(PATH_BIN)
24-
makedirs(PATH_LOG)
25-
makedirs(PATH_SCRIPTS)
26-
makedirs(PATH_SCRIPTS_PYTHONBREW)
27-
makedirs(PATH_SCRIPTS_PYTHONBREW_COMMANDS)
28-
29-
for path in glob.glob(os.path.join(INSTALLER_ROOT,"*.py")):
30-
shutil.copy(path, PATH_SCRIPTS_PYTHONBREW)
31-
32-
for path in glob.glob(os.path.join(INSTALLER_ROOT,"commands","*.py")):
33-
shutil.copy(path, PATH_SCRIPTS_PYTHONBREW_COMMANDS)
20+
PythonbrewInstaller().install(INSTALLER_ROOT)
3421

35-
rm_r(PATH_PATCHES)
36-
shutil.copytree(os.path.join(INSTALLER_ROOT,"patches"), PATH_PATCHES)
22+
m = re.search("(t?csh)", os.environ.get("SHELL"))
23+
if m:
24+
shrc = "cshrc"
25+
yourshrc = m.group(1)+"rc"
26+
else:
27+
shrc = yourshrc = "bashrc"
3728

38-
fp = open("%s/pythonbrew_main.py" % PATH_SCRIPTS, "w")
39-
fp.write("""import pythonbrew
29+
logger.info("""
30+
Well-done! Congratulations!
31+
32+
The pythonbrew is installed as:
33+
34+
%(ROOT)s
35+
36+
Please add the following line to the end of your ~/.%(yourshrc)s
37+
38+
source %(PATH_ETC)s/%(shrc)s
39+
40+
After that, exit this shell, start a new one, and install some fresh
41+
pythons:
42+
43+
pythonbrew install 2.6.6
44+
pythonbrew install 2.5.5
45+
46+
For further instructions, run:
47+
48+
pythonbrew help
49+
50+
The default help messages will popup and tell you what to do!
51+
52+
Enjoy pythonbrew at %(ROOT)s!!
53+
""" % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC})
54+
55+
class PythonbrewInstaller(object):
56+
def install(self, installer_root):
57+
makedirs(PATH_PYTHONS)
58+
makedirs(PATH_BUILD)
59+
makedirs(PATH_DISTS)
60+
makedirs(PATH_ETC)
61+
makedirs(PATH_BIN)
62+
makedirs(PATH_LOG)
63+
makedirs(PATH_SCRIPTS)
64+
makedirs(PATH_SCRIPTS_PYTHONBREW)
65+
makedirs(PATH_SCRIPTS_PYTHONBREW_COMMANDS)
66+
67+
for path in glob.glob(os.path.join(installer_root,"*.py")):
68+
shutil.copy(path, PATH_SCRIPTS_PYTHONBREW)
69+
70+
for path in glob.glob(os.path.join(installer_root,"commands","*.py")):
71+
shutil.copy(path, PATH_SCRIPTS_PYTHONBREW_COMMANDS)
72+
73+
rm_r(PATH_PATCHES)
74+
shutil.copytree(os.path.join(installer_root,"patches"), PATH_PATCHES)
75+
76+
fp = open("%s/pythonbrew_main.py" % PATH_SCRIPTS, "w")
77+
fp.write("""import pythonbrew
4078
if __name__ == "__main__":
4179
pythonbrew.main()
4280
""")
43-
fp.close()
44-
45-
fp = open(PATH_BIN_PYTHONBREW, "w")
46-
fp.write("""#!/usr/bin/env bash
81+
fp.close()
82+
83+
fp = open(PATH_BIN_PYTHONBREW, "w")
84+
fp.write("""#!/usr/bin/env bash
4785
%s %s/pythonbrew_main.py "$@"
4886
""" % (sys.executable, PATH_SCRIPTS))
49-
fp.close()
50-
os.chmod(PATH_BIN_PYTHONBREW, 0755)
51-
symlink(PATH_BIN_PYTHONBREW, PATH_BIN_PYBREW) # pyb as pythonbrew
52-
53-
os.system("echo 'export PATH=%s/bin:%s/current/bin:${PATH}' > %s/bashrc" % (ROOT, PATH_PYTHONS, PATH_ETC))
54-
os.system("echo 'setenv PATH %s/bin:%s/current/bin:$PATH' > %s/cshrc" % (ROOT, PATH_PYTHONS, PATH_ETC))
87+
fp.close()
88+
os.chmod(PATH_BIN_PYTHONBREW, 0755)
89+
symlink(PATH_BIN_PYTHONBREW, PATH_BIN_PYBREW) # pybrew is symbolic pythonbrew
90+
91+
os.system("echo 'export PATH=%s/bin:%s/current/bin:${PATH}' > %s/bashrc" % (ROOT, PATH_PYTHONS, PATH_ETC))
92+
os.system("echo 'setenv PATH %s/bin:%s/current/bin:$PATH' > %s/cshrc" % (ROOT, PATH_PYTHONS, PATH_ETC))
5593

5694
class PythonInstaller(object):
5795
def __init__(self, arg, options):
@@ -132,16 +170,13 @@ def download(self):
132170
sys.exit(1)
133171

134172
def unpack(self):
135-
logger.info("Extracting %s" % os.path.basename(self.download_file))
136-
if is_gzip(self.content_type, self.download_file):
137-
untar_file(self.download_file, self.build_dir)
138-
else:
139-
logger.error("Cannot determine archive format of %s" % self.download_file)
173+
if not unpack_downloadfile(self.content_type, self.download_file, self.build_dir):
174+
sys.exit(1)
140175

141176
def patch(self):
142177
version = self.pkg.version
143178
try:
144-
s = Subprocess(log=self.logfile, shell=True, cwd=self.build_dir, print_cmd=False)
179+
s = Subprocess(log=self.logfile, cwd=self.build_dir)
145180
patches = []
146181
if is_macosx_snowleopard():
147182
if is_python24(version):
@@ -179,11 +214,11 @@ def configure(self):
179214
elif is_python26(version):
180215
configure_option = '--with-universal-archs="intel" MACOSX_DEPLOYMENT_TARGET=10.6'
181216

182-
s = Subprocess(log=self.logfile, shell=True, cwd=self.build_dir, print_cmd=False)
217+
s = Subprocess(log=self.logfile, cwd=self.build_dir)
183218
s.check_call("./configure --prefix=%s %s %s" % (self.install_dir, self.options.configure, configure_option))
184219

185220
def make(self):
186-
s = Subprocess(log=self.logfile, shell=True, cwd=self.build_dir, print_cmd=False)
221+
s = Subprocess(log=self.logfile, cwd=self.build_dir)
187222
if self.options.force:
188223
s.check_call("make")
189224
else:
@@ -194,7 +229,7 @@ def make_install(self):
194229
version = self.pkg.version
195230
if version == "1.5.2" or version == "1.6.1":
196231
makedirs(self.install_dir)
197-
s = Subprocess(log=self.logfile, shell=True, cwd=self.build_dir, print_cmd=False)
232+
s = Subprocess(log=self.logfile, cwd=self.build_dir)
198233
s.check_call("make install")
199234

200235
def install_setuptools(self):
@@ -227,12 +262,12 @@ def install_setuptools(self):
227262
pyexec = os.path.join(install_dir,"bin","python")
228263

229264
try:
230-
s = Subprocess(log=self.logfile, shell=True, cwd=PATH_DISTS, print_cmd=False)
265+
s = Subprocess(log=self.logfile, cwd=PATH_DISTS)
231266
logger.info("Installing distribute into %s" % install_dir)
232267
s.check_call("%s %s" % (pyexec, filename))
233268
if os.path.isfile("%s/bin/easy_install" % (install_dir)) and not is_python3:
234269
logger.info("Installing pip into %s" % install_dir)
235-
s.check_call("%s/bin/easy_install pip" % (install_dir), cwd=None)
270+
s.check_call("%s/bin/easy_install pip" % (install_dir))
236271
except:
237272
logger.error("Failed to install setuptools. See %s/build.log to see why." % (ROOT))
238273
logger.info("Skip install setuptools.")

pythonbrew/util.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def rm_r(path):
8989
"""like rm -r command."""
9090
if os.path.isdir(path):
9191
shutil.rmtree(path)
92-
else:
92+
elif os.path.isfile(path):
9393
unlink(path)
9494

9595
def off():
@@ -175,8 +175,17 @@ def untar_file(filename, location):
175175
finally:
176176
tar.close()
177177

178+
def unpack_downloadfile(content_type, download_file, target_dir):
179+
logger.info("Extracting %s" % os.path.basename(download_file))
180+
if is_gzip(content_type, download_file):
181+
untar_file(download_file, target_dir)
182+
else:
183+
logger.error("Cannot determine archive format of %s" % download_file)
184+
return False
185+
return True
186+
178187
class Subprocess(object):
179-
def __init__(self, log=None, shell=False, cwd=None, print_cmd=True):
188+
def __init__(self, log=None, shell=True, cwd=None, print_cmd=False):
180189
self._log = log
181190
self._shell = shell
182191
self._cwd = cwd

0 commit comments

Comments
 (0)