Commit 1ef1daf7 authored by Godefroid Chapelle's avatar Godefroid Chapelle

Refactor: move top level code to function

parent 80006c77
...@@ -29,173 +29,177 @@ import os, shutil, subprocess, tempfile ...@@ -29,173 +29,177 @@ import os, shutil, subprocess, tempfile
os.environ["SETUPTOOLS_USE_DISTUTILS"] = "stdlib" os.environ["SETUPTOOLS_USE_DISTUTILS"] = "stdlib"
for d in 'eggs', 'develop-eggs', 'bin', 'parts': def main():
if not os.path.exists(d): for d in 'eggs', 'develop-eggs', 'bin', 'parts':
os.mkdir(d) if not os.path.exists(d):
os.mkdir(d)
bin_buildout = os.path.join('bin', 'buildout')
if os.path.isfile(bin_buildout): bin_buildout = os.path.join('bin', 'buildout')
os.remove(bin_buildout) if os.path.isfile(bin_buildout):
os.remove(bin_buildout)
if os.path.isdir('build'):
shutil.rmtree('build') if os.path.isdir('build'):
shutil.rmtree('build')
print("Current directory %s" % os.getcwd())
print("Current directory %s" % os.getcwd())
#######################################################################
def install_pip():
print('')
print('Install pip')
print('')
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
tmp = tempfile.mkdtemp(prefix='buildout-dev-')
try:
get_pip = os.path.join(tmp, 'get-pip.py')
if sys.version_info < (3, ):
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/2.7/get-pip.py'
elif (sys.version_info.major, sys.version_info.minor) == (3, 5):
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/3.5/get-pip.py'
else:
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/get-pip.py'
with open(get_pip, 'wb') as f:
f.write(urlopen(GET_PIP_URL).read())
sys.stdout.flush()
if subprocess.call([sys.executable, get_pip]):
raise RuntimeError("Failed to install pip.")
finally:
shutil.rmtree(tmp)
print("Restart")
sys.stdout.flush()
return_code = subprocess.call(
[sys.executable] + sys.argv
)
sys.exit(return_code)
#######################################################################
def install_pip():
print('')
print('Install pip')
print('')
try: try:
from urllib.request import urlopen import pip
print('')
try:
print(subprocess.check_output(
[sys.executable] + ['-m', 'pip', '--version'],
stderr=subprocess.STDOUT,
).decode('utf8'))
print('is installed.')
except subprocess.CalledProcessError as e:
# some debian/ubuntu based machines
# have broken pip installs
# that cannot import distutils or html5lib
# thus try to install via get-pip
if (b"ImportError" in e.output or
b"ModuleNotFoundError" in e.output):
install_pip()
raise e
except ImportError: except ImportError:
from urllib2 import urlopen install_pip()
tmp = tempfile.mkdtemp(prefix='buildout-dev-') ######################################################################
try: def check_upgrade(package):
get_pip = os.path.join(tmp, 'get-pip.py') print('')
if sys.version_info < (3, ): print('Try to upgrade %s' % package)
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/2.7/get-pip.py' print('')
elif (sys.version_info.major, sys.version_info.minor) == (3, 5):
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/3.5/get-pip.py' try:
else: sys.stdout.flush()
GET_PIP_URL = 'https://bootstrap.pypa.io/pip/get-pip.py' output = subprocess.check_output(
with open(get_pip, 'wb') as f: [sys.executable] + ['-m', 'pip', 'install', '--upgrade', package],
f.write(urlopen(GET_PIP_URL).read()) stderr=subprocess.STDOUT,
)
was_up_to_date = b"up-to-date" in output or b"already satisfied" in output
if not was_up_to_date:
print(output.decode('utf8'))
return not was_up_to_date
except subprocess.CalledProcessError as e:
# some debian/ubuntu based machines
# have broken pip installs
# that cannot import distutils or html5lib
# thus try to install via get-pip
if (b"ImportError" in e.output or
b"ModuleNotFoundError" in e.output) :
install_pip()
return False
raise RuntimeError("Upgrade %s failed." % package)
def show(package):
try:
sys.stdout.flush()
output = subprocess.check_output(
[sys.executable, '-m', 'pip', 'show', package],
)
for line in output.splitlines():
if line.startswith(b'Name') or line.startswith(b'Version'):
print(line.decode('utf8'))
except subprocess.CalledProcessError:
raise RuntimeError("Upgrade %s failed." % package)
need_restart = False
for package in ['pip', 'setuptools', 'wheel']:
did_upgrade = check_upgrade(package)
show(package)
need_restart = need_restart or did_upgrade
if need_restart:
print("Restart")
sys.stdout.flush() sys.stdout.flush()
if subprocess.call([sys.executable, get_pip]): return_code = subprocess.call(
raise RuntimeError("Failed to install pip.") [sys.executable] + sys.argv
finally: )
shutil.rmtree(tmp) sys.exit(return_code)
print("Restart") ######################################################################
sys.stdout.flush()
return_code = subprocess.call(
[sys.executable] + sys.argv
)
sys.exit(return_code)
try:
import pip
print('')
try:
print(subprocess.check_output(
[sys.executable] + ['-m', 'pip', '--version'],
stderr=subprocess.STDOUT,
).decode('utf8'))
print('is installed.')
except subprocess.CalledProcessError as e:
# some debian/ubuntu based machines
# have broken pip installs
# that cannot import distutils or html5lib
# thus try to install via get-pip
if (b"ImportError" in e.output or
b"ModuleNotFoundError" in e.output):
install_pip()
raise e
except ImportError:
install_pip()
######################################################################
def check_upgrade(package):
print('') print('')
print('Try to upgrade %s' % package) print('Install buildout')
print('') print('')
sys.stdout.flush()
if subprocess.call(
[sys.executable] +
['setup.py', '-q', 'develop', '-m', '-x', '-d', 'develop-eggs'],
):
raise RuntimeError("buildout build failed.")
try: import pkg_resources
sys.stdout.flush()
output = subprocess.check_output(
[sys.executable] + ['-m', 'pip', 'install', '--upgrade', package],
stderr=subprocess.STDOUT,
)
was_up_to_date = b"up-to-date" in output or b"already satisfied" in output
if not was_up_to_date:
print(output.decode('utf8'))
return not was_up_to_date
except subprocess.CalledProcessError as e:
# some debian/ubuntu based machines
# have broken pip installs
# that cannot import distutils or html5lib
# thus try to install via get-pip
if (b"ImportError" in e.output or
b"ModuleNotFoundError" in e.output) :
install_pip()
return False
raise RuntimeError("Upgrade %s failed." % package)
def show(package):
try:
sys.stdout.flush()
output = subprocess.check_output(
[sys.executable, '-m', 'pip', 'show', package],
)
for line in output.splitlines():
if line.startswith(b'Name') or line.startswith(b'Version'):
print(line.decode('utf8'))
except subprocess.CalledProcessError:
raise RuntimeError("Upgrade %s failed." % package)
pkg_resources.working_set.add_entry('src')
need_restart = False import zc.buildout.easy_install
for package in ['pip', 'setuptools', 'wheel']: zc.buildout.easy_install.scripts(
did_upgrade = check_upgrade(package) ['zc.buildout'], pkg_resources.working_set, sys.executable, 'bin')
show(package)
need_restart = need_restart or did_upgrade
if need_restart: ######################################################################
print("Restart") def install_coverage():
sys.stdout.flush() print('')
return_code = subprocess.call( print('Install coverage')
[sys.executable] + sys.argv print('')
) bin_pip = os.path.join('bin', 'pip')
sys.exit(return_code) if subprocess.call(
###################################################################### [sys.executable] +
print('') ['-m', 'pip', 'install', 'coverage'],
print('Install buildout') ):
print('') raise RuntimeError("coverage install failed.")
sys.stdout.flush()
if subprocess.call( try:
[sys.executable] + import coverage
['setup.py', '-q', 'develop', '-m', '-x', '-d', 'develop-eggs'], except ImportError:
): install_coverage()
raise RuntimeError("buildout build failed.")
######################################################################
import pkg_resources
pkg_resources.working_set.add_entry('src')
import zc.buildout.easy_install
zc.buildout.easy_install.scripts(
['zc.buildout'], pkg_resources.working_set, sys.executable, 'bin')
######################################################################
def install_coverage():
print('') print('')
print('Install coverage') print('Run buildout')
print('') print('')
bin_pip = os.path.join('bin', 'pip') bin_buildout = os.path.join('bin', 'buildout')
if subprocess.call(
[sys.executable] +
['-m', 'pip', 'install', 'coverage'],
):
raise RuntimeError("coverage install failed.")
try: if sys.platform.startswith('java'):
import coverage # Jython needs the script to be called twice via sys.executable
except ImportError: assert subprocess.Popen([sys.executable, bin_buildout, '-N']).wait() == 0
install_coverage()
###################################################################### sys.stdout.flush()
print('') sys.exit(subprocess.Popen(bin_buildout).wait())
print('Run buildout')
print('')
bin_buildout = os.path.join('bin', 'buildout')
if sys.platform.startswith('java'):
# Jython needs the script to be called twice via sys.executable
assert subprocess.Popen([sys.executable, bin_buildout, '-N']).wait() == 0
sys.stdout.flush() if __name__ == '__main__':
sys.exit(subprocess.Popen(bin_buildout).wait()) main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment