Sh and py run after installing Python3

I made a python file that can be executed after installing python3 even in an environment without python3.

Plan

It is assumed that it is unlikely that anyone can do anything as root in the development environment.

reference

How to write the correct shebang in Perl, Python and Ruby scripts You can write more than one shebang! ?? I searched for it. It's just Don Pisha, and without this article I wouldn't have done such a stupid thing.

[How to write Ruby that can be executed as PHP] It's a different idea, but I was inspired by the fact that one file is for multiple languages. I want to try this too.

code

install_python3.py


#!/bin/sh
# -*- coding: UTF-8 -*-

""":"

#Write sh here

#Installation & decompression of installation version and compressed files
tmp='/tmp/'
pythonver='3.4.3'
binarypath=$tmp'Python-'$pythonver

#Place of installation
lib='/usr/lib/'
srcpath=$lib'python'$pythonver

#python3 symbolic link installation location
bin='/usr/bin/'
lnname='python3'

if [ -f $bin$lnname ]; then
    #If you have python3, just run python
    echo 'No installation required'
    exec $lnname "$0" ${1+"$@"}
fi

echo 'Installation required'

if [ -f $binarypath'.tgz' ]; then
    echo $binarypath'.tgz already exists'
else
    echo $binarypath'.Download tgz'
    wget https://www.python.org/ftp/python/${pythonver}/Python-${pythonver}.tgz -P ${tmp}
fi

if [ -d $binarypath ]; then
    echo $binarypath'Is already there'
else
    echo $binarypath'Expand to'
    tar zxf $binarypath'.tgz' -C $tmp
fi

if [ -d $srcpath ]; then
    echo $srcpath'Installed in'
else
    echo $srcpath'Install to'
    #If you cd, it will be moss in later python execution, so return after installation
    pwd=`pwd`
    cd $binarypath && ./configure --prefix=${srcpath} && make && make install
    cd $pwd
fi

if [ -f $bin$lnname ]; then
    echo $bin$lnname'Already installed'
else
    echo $bin$lnname'To install'
    ln -s $srcpath'/bin/python3' $bin$lnname
fi

exec $lnname "$0" ${1+"$@"}
"""

#From here python

import sys

version = sys.version_info
if version[0] == 2:
    print('2')
else:
    print(version[0])




At first, if the version of python was 2, I replaced python with 3, but I stopped it because it seemed to affect other things and in fact yum stopped working. The example of failure below is a remnant of that.

Failure example

** Dynamic languages are analyzed and executed line by line with an interpreter, right? ** ** ** Then it's okay if you don't go through the problematic part? ** **

When I use it normally, I always realize that it doesn't happen, but once I can achieve the purpose, I think it's convenient for me.

** Version determined in python and tried to update python in python **


#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import tarfile

version = sys.version_info
if version[0] == 2:
    print 'Update to 3 for version 2'
    if not os.path.isfile('/tmp/Python-3.4.3.tgz'):
        print 'Download'
        os.system('wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz -P /tmp')
    else:
        print '/tmp/Python-3.4.3.tgz already exists'

    # 2.Because it is 4, extract all is nice
    if not os.path.isdir('/tmp/Python-3.4.3'):
        print '/tmp/Python-3.4.Expand to 3 and install'
        tf = tarfile.open('/tmp/Python-3.4.3.tgz', 'r')
        tf.list()
        for item in tf:
            tf.extract(item, '/tmp/')
        #Build
        if not os.path.isdir('/usr/lib/python3.4.3'):
            os.system('cd /tmp/Python-3.4.3/ && ./configure --prefix=/usr/lib/python3.4.3 && make && make install')
        else:
            print '/usr/lib/python3.4.Installed on 3'
    else:
        print '/tmp/Python-3.4.Expanded to 3'


    # /usr/bin/python replacement
    if not os.path.isfile('/usr/bin/python.old'):
        os.rename('/usr/bin/python', '/usr/bin/python.old')
        os.symlink('/usr/lib/python3.4.3/bin/python3.4', '/usr/bin/python')
        #Existing/usr/bin/python2 is a symbolic link to python, so update it
        #Overwriting is unreasonable, so delete it once
        os.remove('/usr/bin/python2')
        os.symlink('/usr/bin/python2.4', '/usr/bin/python2')
    else:
        print '/usr/lib/python has been replaced'

    sys.exit()

#This should be done in case of 3
print('test')

At the time of python2, print ('test') was safe, so I thought "I can go!", But when I raised it to 3, after all it was print and syntax error It seems that both print andprint ()can be used in python2 ... I was spurred on my belief (passing responsibility)

Well, before I started writing, I just read PHP runtime and compile time and realized that it was just impossible.

php Even if it looks like you specify the source code file in a command and execute it directly like hoge.php, modern interpreters parse the source code and then compile it into virtual machine bytecode. Ruby, Python, Perl5, Emacs as well.

** But I just thought that python3, which does not use print on the python2 side and protects syntax, may still work !? **.


I have no plans to use it.

Recommended Posts

Sh and py run after installing Python3
About installing Pwntools and Python2 series
Installing Python 3.4.3.
[Beginner] Installing Python and running programs (Windows)
Install and run Python3.5 + NumPy + SciPy on Windows 10
Notes on installing Python3 and using pip on Windows7
After installing Linux
Let's write a Python program and run it
[Note] Installing Python 3.6 + α on Windows and RHEL
What are python tuples and * args after all?
If you try to install Python2 pip after installing Python3 pip and it is rejected
The process of installing Atom and getting Python running
Installing Python 3 on Mac and checking basic operation Part 1
Python and numpy tips
[Python] pip and wheel
Run Python with VBA
Run prepDE.py with python3
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Run python from excel
Run Blender with python
Installing pandas on python2.6
python input and output
Python and Ruby split
Cloud Run tutorial (python)
Python3, venv and Ansible
Python asyncio and ContextVar
Run iperf with python
Remove double-byte spaces before and after the character string (python)
[Python beginner] How do I develop and execute Python after all?
[python] plot the values ​​before and after the conversion of yeojohnson conversion
Python 3.4 series is safe for installing and executing Python Kivy (macOS)
After enabling the python virtual environment in the batch file, run the python file