Python script implementation source installation python2/3

Keywords: Python shell ftp network

1. Why python is selected for scripting:

  • python has a large number of third-party libraries, which is very convenient to call
  • python language is simple and clear
Think about it: the comparison shows why large-scale enterprises prefer python to shell
     The syntax of shell implementation is too cumbersome and the amount of code is large

2. Coding ideas

  • 1) Judge whether it is root first, then root can install the software
  • 2) Interactive request, asking which version of python the root user wants to install. Now 2 / 3 is provided
  • 3) Perform if judgment and Analysis on possible problems during source compilation, such as network failure during downloading source package, or dependency package during installation

3. Source code

#!/usr/bin/env python
#coding=utf-8
import os
import sys 

if os.getuid() == 0:
    pass
else:
    print 'The current user is not root User, please use root User execute script~'
    sys.exit(1)

version = raw_input('Please enter what you want to install python Edition(2.7/3.5)')
if version == '2.7':
    url = 'https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz'
elif vesion == '3.5':
    url = 'https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz'
else:
    print 'The version number you entered is incorrect, please enter:2.7/3.5'
    sys.exit(1)
cmd = 'wget '+url
res = os.system(cmd)
if res != 0:
        print 'Download source package failed, please check the network'
        sys.exit(1)

if version == '2.7':
    package_name = 'Python-2.7.12'
else:
    package_name = 'Python-3.5.2'
cmd = 'tar xf '+package_name+'.tgz'
res = os.system(cmd)
if res != 0:
    os.system('rm '+package_name+'tgz')
    print 'Failed to decompress the source package. Please run this script again to download the source package'
    sys.exit(1)
cmd = 'cd '+package_name+' && ./configure --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
if res != 0:
    print 'Compile python Source code failed, please check if the dependency library is missing'
    sys.exit(1)

4. Operation results

//Script execution by non root user
[kiosk@python2 shell]$ python 1.py 
The current user is not root. Please execute the script as root
//root execute script
[root@python2 shell]# python 1.py 
//Please enter the version of python you want to install(2.7/3.5)2.7
--2018-01-30 22:48:54--  https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz
Resolving www.python.org (www.python.org)... 151.101.24.223, 2a04:4e42:6::223
Connecting to www.python.org (www.python.org)|151.101.24.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16935960 (16M) [application/octet-stream]
Saving to: 'Python-2.7.12.tgz'

 8% [======>                                                                          ] 1,521,498    151KB/s  eta 89s
......

Posted by exa_bit on Mon, 04 May 2020 19:29:47 -0700