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
import os
import sys 

if os.getuid() == 0:
    print 'The current user is not root User, please use root User execute scriptļ½˛'

version = raw_input('Please enter what you want to install python Edition(2.7/3.5)')
if version == '2.7':
    url = ''
elif vesion == '3.5':
    url = ''
    print 'The version number you entered is incorrect, please enter:2.7/3.5'
cmd = 'wget '+url
res = os.system(cmd)
if res != 0:
        print 'Download source package failed, please check the network'

if version == '2.7':
    package_name = 'Python-2.7.12'
    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'
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'

4. Operation results

//Script execution by non root user
[kiosk@python2 shell]$ python 
The current user is not root. Please execute the script as root
//root execute script
[root@python2 shell]# python 
//Please enter the version of python you want to install(2.7/3.5)2.7
--2018-01-30 22:48:54--
Resolving (, 2a04:4e42:6::223
Connecting to (||: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