2019-11-30 22:56:50 +01:00
|
|
|
#!/usr/bin/python3
|
2014-09-27 22:54:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# downloader.py file is part of slpkg.
|
|
|
|
|
2020-01-30 16:56:07 +01:00
|
|
|
# Copyright 2014-2020 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
|
|
|
|
2018-06-09 22:00:52 +02:00
|
|
|
# https://gitlab.com/dslackw/slpkg
|
2014-09-27 22:54:30 +02:00
|
|
|
|
|
|
|
# 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
|
2016-04-13 18:57:39 +02:00
|
|
|
import tarfile
|
2014-09-27 22:54:30 +02:00
|
|
|
import subprocess
|
|
|
|
|
2019-05-19 16:19:18 +02:00
|
|
|
from slpkg.utils import Utils
|
2019-05-19 16:35:55 +02:00
|
|
|
from slpkg.messages import Msg
|
2016-04-13 18:57:39 +02:00
|
|
|
from slpkg.slack.slack_version import slack_ver
|
2015-08-21 01:15:43 +02:00
|
|
|
from slpkg.__metadata__ import MetaData as _meta_
|
2014-09-27 22:54:30 +02:00
|
|
|
|
2014-10-06 21:44:44 +02:00
|
|
|
|
2020-02-17 13:50:20 +01:00
|
|
|
class Download(Utils):
|
2015-08-05 05:58:28 +02:00
|
|
|
"""Downloader manager. Slpkg use wget by default but support
|
2016-10-19 23:52:33 +02:00
|
|
|
curl, aria2 and httpie
|
2015-08-05 05:58:28 +02:00
|
|
|
"""
|
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_
|
2020-01-11 14:12:21 +01:00
|
|
|
self.green = _meta_.color["GREEN"]
|
|
|
|
self.red = _meta_.color["RED"]
|
|
|
|
self.endc = _meta_.color["ENDC"]
|
2015-08-21 06:46:33 +02:00
|
|
|
self.msg = Msg()
|
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.
|
2016-10-19 23:52:33 +02:00
|
|
|
Optional curl, aria2c and httpie
|
2015-09-23 04:17:33 +02:00
|
|
|
"""
|
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:
|
2020-02-17 13:50:20 +01:00
|
|
|
self.file_name = self.fix_file_name(dwn.split("/")[-1])
|
2016-04-13 18:57:39 +02:00
|
|
|
|
|
|
|
if dwn.startswith("file:///"):
|
2016-04-26 05:34:24 +02:00
|
|
|
source_dir = dwn[7:-7].replace(slack_ver(), "")
|
2016-04-13 18:57:39 +02:00
|
|
|
self._make_tarfile(self.file_name, source_dir)
|
|
|
|
|
2015-06-17 07:13:08 +02:00
|
|
|
self._check_certificate()
|
2020-01-30 17:57:44 +01:00
|
|
|
print(f"\n[{dwn_count}/{len(self.url)}][ {self.green}"
|
|
|
|
f"Download{self.endc} ] --> {self.file_name}\n")
|
2018-04-29 12:17:26 +02:00
|
|
|
if self.downder in ["wget"]:
|
2020-01-30 17:57:44 +01:00
|
|
|
subprocess.call(f"{self.downder} {self.downder_options}"
|
|
|
|
f" {self.dir_prefix}{self.path} {dwn}",
|
|
|
|
shell=True)
|
2018-04-29 12:17:26 +02:00
|
|
|
if self.downder in ["aria2c"]:
|
2020-01-30 17:57:44 +01:00
|
|
|
subprocess.call(f"{self.downder} {self.downder_options}"
|
|
|
|
f" {self.dir_prefix}{self.path[:-1]} {dwn}",
|
|
|
|
shell=True)
|
2015-09-15 15:01:06 +02:00
|
|
|
elif self.downder in ["curl", "http"]:
|
2020-01-30 17:57:44 +01:00
|
|
|
subprocess.call(f"{self.downder} {self.downder_options}"
|
|
|
|
f" {self.path}{self.file_name} {dwn}",
|
|
|
|
shell=True)
|
2015-09-15 15:01:06 +02:00
|
|
|
self._check_if_downloaded()
|
|
|
|
dwn_count += 1
|
2015-01-30 23:04:51 +01:00
|
|
|
|
2016-04-13 18:57:39 +02:00
|
|
|
def _make_tarfile(self, output_filename, source_dir):
|
|
|
|
"""Create .tar.gz file
|
|
|
|
"""
|
|
|
|
with tarfile.open(output_filename, "w:gz") as tar:
|
|
|
|
tar.add(source_dir, arcname=os.path.basename(source_dir))
|
|
|
|
|
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):
|
2019-12-02 18:36:32 +01:00
|
|
|
print()
|
2015-08-21 06:46:33 +02:00
|
|
|
self.msg.template(78)
|
2020-01-30 17:57:44 +01:00
|
|
|
print(f"| Download '{self.file_name}' file"
|
|
|
|
f" [ {self.red}FAILED{self.endc} ]")
|
2015-08-21 06:46:33 +02:00
|
|
|
self.msg.template(78)
|
2019-12-02 18:36:32 +01:00
|
|
|
print()
|
2015-08-21 06:46:33 +02:00
|
|
|
if not self.msg.answer() in ["y", "Y"]:
|
2015-08-05 05:20:04 +02:00
|
|
|
raise SystemExit()
|
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"')
|
2015-08-21 06:46:33 +02:00
|
|
|
self.msg.template(78)
|
2020-01-30 17:57:44 +01:00
|
|
|
print(f"| '{certificate[:23].strip()}' need to go"
|
|
|
|
f" ahead downloading")
|
2015-08-21 06:46:33 +02:00
|
|
|
self.msg.template(78)
|
2019-12-02 18:36:32 +01:00
|
|
|
print()
|
2015-06-20 01:15:26 +02:00
|
|
|
self.downder_options += certificate
|
2015-08-21 06:46:33 +02:00
|
|
|
if not self.msg.answer() in ["y", "Y"]:
|
2020-01-30 17:57:44 +01:00
|
|
|
raise SystemExit()
|