2014-09-27 22:54:30 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# downloader.py file is part of slpkg.
|
|
|
|
|
2015-05-21 01:00:07 +02:00
|
|
|
# Copyright 2014-2015 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
|
2014-09-27 22:54:30 +02:00
|
|
|
# All rights reserved.
|
|
|
|
|
2014-12-19 05:57:56 +01:00
|
|
|
# Slpkg is a user-friendly package manager for Slackware installations
|
2014-09-27 22:54:30 +02:00
|
|
|
|
|
|
|
# https://github.com/dslackw/slpkg
|
|
|
|
|
|
|
|
# Slpkg is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-05-28 05:07:13 +02:00
|
|
|
|
2015-01-30 23:04:51 +01:00
|
|
|
import os
|
2014-10-06 21:44:44 +02:00
|
|
|
import sys
|
2014-09-27 22:54:30 +02:00
|
|
|
import subprocess
|
|
|
|
|
2015-01-30 23:04:51 +01:00
|
|
|
from messages import Msg
|
2015-06-07 07:26:42 +02:00
|
|
|
from __metadata__ import MetaData as _meta_
|
2014-09-27 22:54:30 +02:00
|
|
|
|
2014-10-06 21:44:44 +02:00
|
|
|
|
|
|
|
class Download(object):
|
|
|
|
|
2015-06-17 16:20:42 +02:00
|
|
|
def __init__(self, path, url, repo):
|
2014-10-06 21:44:44 +02:00
|
|
|
self.path = path
|
|
|
|
self.url = url
|
2015-06-17 16:20:42 +02:00
|
|
|
self.repo = repo
|
2015-06-20 05:15:54 +02:00
|
|
|
self.file_name = ""
|
2015-06-08 05:28:49 +02:00
|
|
|
self.meta = _meta_
|
2015-06-20 01:15:26 +02:00
|
|
|
self.dir_prefix = ""
|
|
|
|
self.downder = self.meta.downder
|
|
|
|
self.downder_options = self.meta.downder_options
|
2014-10-06 21:44:44 +02:00
|
|
|
|
|
|
|
def start(self):
|
2015-06-20 01:55:58 +02:00
|
|
|
"""Download files using wget or other downloader.
|
|
|
|
Optional curl and aria2c"""
|
2015-02-26 08:48:30 +01:00
|
|
|
dwn_count = 1
|
2015-06-20 01:15:26 +02:00
|
|
|
self._directory_prefix()
|
2014-12-03 02:42:55 +01:00
|
|
|
for dwn in self.url:
|
2015-01-30 23:04:51 +01:00
|
|
|
self.file_name = dwn.split("/")[-1]
|
2015-06-17 07:13:08 +02:00
|
|
|
self._check_certificate()
|
2015-02-26 08:48:30 +01:00
|
|
|
print("\n[{0}/{1}][ {2}Download{3} ] --> {4}\n".format(
|
2015-06-08 05:28:49 +02:00
|
|
|
dwn_count, len(self.url), self.meta.color["GREEN"],
|
|
|
|
self.meta.color["ENDC"],
|
2015-02-26 08:48:30 +01:00
|
|
|
self.file_name))
|
2014-12-03 02:42:55 +01:00
|
|
|
try:
|
2015-06-20 01:15:26 +02:00
|
|
|
if self.downder in ["wget", "aria2c"]:
|
|
|
|
subprocess.call("{0} {1} {2}{3} {4}".format(
|
|
|
|
self.downder, self.downder_options,
|
|
|
|
self.dir_prefix, self.path, dwn),
|
|
|
|
shell=True)
|
|
|
|
elif self.downder == "curl":
|
2015-06-20 05:05:15 +02:00
|
|
|
subprocess.call("{0} {1} {2}{3} {4}".format(
|
2015-06-20 01:15:26 +02:00
|
|
|
self.downder, self.downder_options,
|
2015-06-20 05:05:15 +02:00
|
|
|
self.path, self.file_name, dwn), shell=True)
|
2015-02-09 10:48:59 +01:00
|
|
|
self._check_if_downloaded()
|
2015-02-26 08:48:30 +01:00
|
|
|
dwn_count += 1
|
2014-12-03 02:42:55 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print # new line at cancel
|
|
|
|
sys.exit(0)
|
2015-01-30 23:04:51 +01:00
|
|
|
|
2015-06-20 01:15:26 +02:00
|
|
|
def _directory_prefix(self):
|
2015-06-20 01:55:58 +02:00
|
|
|
"""Downloader options for specific directory
|
|
|
|
"""
|
2015-06-20 01:15:26 +02:00
|
|
|
if self.downder == "wget":
|
|
|
|
self.dir_prefix = "--directory-prefix="
|
|
|
|
elif self.downder == "aria2c":
|
|
|
|
self.dir_prefix = "--dir="
|
|
|
|
|
2015-02-09 10:48:59 +01:00
|
|
|
def _check_if_downloaded(self):
|
2015-06-20 01:15:26 +02:00
|
|
|
"""Check if file downloaded
|
|
|
|
"""
|
2015-01-30 23:04:51 +01:00
|
|
|
if not os.path.isfile(self.path + self.file_name):
|
2015-06-05 13:42:52 +02:00
|
|
|
print("")
|
2015-01-30 23:04:51 +01:00
|
|
|
Msg().template(78)
|
|
|
|
print("| Download '{0}' file {1}[ FAILED ]{2}".format(
|
2015-06-08 05:28:49 +02:00
|
|
|
self.file_name, self.meta.color["RED"],
|
|
|
|
self.meta.color["ENDC"]))
|
2015-01-30 23:04:51 +01:00
|
|
|
Msg().template(78)
|
2015-06-05 13:42:52 +02:00
|
|
|
print("")
|
|
|
|
if not Msg().answer() in ["y", "Y"]:
|
2015-01-31 07:31:50 +01:00
|
|
|
sys.exit(0)
|
2015-06-17 07:13:08 +02:00
|
|
|
|
|
|
|
def _check_certificate(self):
|
2015-06-17 16:20:42 +02:00
|
|
|
"""Check for certificates options for wget
|
2015-06-17 07:13:08 +02:00
|
|
|
"""
|
2015-06-20 01:15:26 +02:00
|
|
|
if (self.file_name.startswith("jdk-") and self.repo == "sbo" and
|
|
|
|
self.downder == "wget"):
|
|
|
|
certificate = (' --no-check-certificate --header="Cookie: '
|
|
|
|
'oraclelicense=accept-securebackup-cookie"')
|
|
|
|
Msg().template(78)
|
|
|
|
print("| '{0}' need to go ahead downloading".format(
|
|
|
|
certificate[:23]))
|
|
|
|
Msg().template(78)
|
2015-06-20 02:37:06 +02:00
|
|
|
print("")
|
2015-06-20 01:15:26 +02:00
|
|
|
self.downder_options += certificate
|
|
|
|
if not Msg().answer() in ["y", "Y"]:
|
|
|
|
sys.exit(0)
|