MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //scripts.20110531.215904.25158/pythonup
#!/usr/bin/perl
# cpanel - pythonup                               Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel'; }

use Cpanel::Config             ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::Update             ();
use Cpanel::Sys::OS            ();

Cpanel::Update::init_UP_update('python');

my $local_prefix          = '/usr/local/cpanel/3rdparty';
my $local_install         = "$local_prefix/bin/python";
my $stable_version        = '2.4.3';
my $minimum_version_regex = qr/^2\.4/;                                    # 2.5 doesn't exist yet
my $cpconf                = Cpanel::Config::LoadCpConf::loadcpconf();
my $uptodate_path;
my $force = grep { '--force' eq $_ } @ARGV;

unless ( $force ) {
    # Force allows us to override the system python
    if ( exists $cpconf->{'python'} && -x $cpconf->{'python'} ) {
        if ( $cpconf->{'python'} ne $local_install && !-e $local_install ) {
            symlink( $cpconf->{'python'}, $local_install );
        }
        exit;
    }

    $uptodate_path = _find_uptodate_python();
    if ($uptodate_path) {
        if ( $uptodate_path ne $local_install ) {
            symlink( $uptodate_path, $local_install );
            $uptodate_path = $local_install;
        }
        $cpconf->{'python'} = $uptodate_path;
        Cpanel::Config::savecpconf($cpconf);
        exit;
    }
}

# Begin installation of newer version
my $os = Cpanel::Sys::OS::getos();

if ( $os eq 'freebsd' ) {
    system '/scripts/ensurepkg', 'python';
}
else {
    chdir '/usr/local/cpanel/src/3rdparty' or die "Unable to chdir: $!";
    system '/scripts/fetchfile', 'Python-' . $stable_version . '.tar.bz2';
    system 'tar', 'jxf', 'Python-' . $stable_version . '.tar.bz2';
    system "(cd Python-$stable_version && ./configure --prefix=$local_prefix && make && make install)";
    system 'rm', '-rf', 'Python-' . $stable_version;
}

$uptodate_path = _find_uptodate_python();
if ($uptodate_path) {
    if ( $uptodate_path ne $local_install ) {
        symlink( $uptodate_path, $local_install );
        $uptodate_path = $local_install;
    }

    # Refresh the hash
    $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();

    # Update hash
    $cpconf->{'python'} = $uptodate_path;

    # Save hash
    Cpanel::Config::savecpconf($cpconf);
    exit;
}


sub _find_uptodate_python {
    foreach my $python ( @{ _get_installed_paths() } ) {
        next if ( !_is_uptodate($python) );
        return $python;
    }

    return;
}

sub _is_uptodate {
    my $python_bin = shift || return;
    my $version = `$python_bin -V 2>&1`;
    chomp $version;
    if ( $version =~ m/\s+([\d.]+)$/ ) {
        $version = $1;
    }
    else {
        return;
    }

    if ( $version =~ $minimum_version_regex ) {
        return 1;
    }
    return 0;
}

sub _get_installed_paths {
    my @pythons;

    foreach my $python (
        $local_install,
        qw{
        /usr/local/bin/python2.4
        /usr/bin/python2.4
        /usr/local/bin/python2
        /usr/bin/python2
        /usr/local/bin/python
        /usr/bin/python
        }
      ) {
        push @pythons, $python if -x $python;
    }

    return \@pythons;
}