2019-11-30 22:56:50 +01:00
|
|
|
#!/usr/bin/python3
|
2014-11-03 17:32:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-11-04 06:36:28 +02:00
|
|
|
# repositories.py file is part of slpkg.
|
2014-11-03 17:32:54 +02:00
|
|
|
|
2020-01-30 16:56:07 +01:00
|
|
|
# Copyright 2014-2020 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
|
2014-11-03 17:32:54 +02:00
|
|
|
# All rights reserved.
|
|
|
|
|
2014-12-19 06:57:56 +02:00
|
|
|
# Slpkg is a user-friendly package manager for Slackware installations
|
2014-11-03 17:32:54 +02:00
|
|
|
|
2018-06-09 22:00:52 +02:00
|
|
|
# https://gitlab.com/dslackw/slpkg
|
2014-11-03 17:32:54 +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/>.
|
|
|
|
|
|
|
|
|
2014-11-21 12:51:02 +02:00
|
|
|
import os
|
2014-12-31 10:44:38 +02:00
|
|
|
|
2015-08-21 02:15:43 +03:00
|
|
|
from slpkg.utils import Utils
|
|
|
|
from slpkg.__metadata__ import MetaData as _meta_
|
2014-11-21 12:51:02 +02:00
|
|
|
|
|
|
|
|
2020-02-16 17:49:41 +01:00
|
|
|
class Repo(Utils):
|
2017-02-10 19:04:59 +02:00
|
|
|
"""Manage repositories configuration files
|
2015-08-05 06:58:28 +03:00
|
|
|
"""
|
2014-11-03 18:02:47 +02:00
|
|
|
def __init__(self):
|
2015-06-08 06:28:49 +03:00
|
|
|
self.meta = _meta_
|
2015-09-23 11:52:09 +03:00
|
|
|
self.DEFAULT_REPOS_NAMES = self.meta.default_repositories
|
2015-09-23 08:53:16 +03:00
|
|
|
self.custom_repo_file = "/etc/slpkg/custom-repositories"
|
|
|
|
self.default_repo_file = "/etc/slpkg/default-repositories"
|
2020-02-16 17:49:41 +01:00
|
|
|
self.custom_repositories_list = self.read_file(
|
2019-12-07 17:14:46 +01:00
|
|
|
self.custom_repo_file)
|
2020-02-16 17:49:41 +01:00
|
|
|
self.default_repositories_list = self.read_file(
|
2015-09-23 08:53:16 +03:00
|
|
|
self.default_repo_file)
|
|
|
|
self.default_repository()
|
2014-12-31 10:44:38 +02:00
|
|
|
|
|
|
|
def add(self, repo, url):
|
2015-09-23 08:53:16 +03:00
|
|
|
"""Write custom repository name and url in a file
|
2015-06-05 14:42:52 +03:00
|
|
|
"""
|
2014-12-31 10:44:38 +02:00
|
|
|
repo_name = []
|
2015-06-05 14:42:52 +03:00
|
|
|
if not url.endswith("/"):
|
2020-02-15 19:54:42 +01:00
|
|
|
url += "/"
|
2015-09-23 08:53:16 +03:00
|
|
|
for line in self.custom_repositories_list.splitlines():
|
2014-12-31 10:44:38 +02:00
|
|
|
line = line.lstrip()
|
2015-01-01 10:55:05 +02:00
|
|
|
if line and not line.startswith("#"):
|
2014-12-31 10:44:38 +02:00
|
|
|
repo_name.append(line.split()[0])
|
2015-06-08 06:28:49 +03:00
|
|
|
if (repo in self.meta.repositories or repo in repo_name or
|
|
|
|
repo in self.meta.default_repositories):
|
2020-02-15 19:54:42 +01:00
|
|
|
print(f"\nRepository name '{repo}' exist, select different name.\n"
|
2015-09-16 05:37:58 +03:00
|
|
|
"View all repositories with command 'slpkg "
|
2020-02-15 19:54:42 +01:00
|
|
|
"repo-list'.\n")
|
|
|
|
raise SystemExit(1)
|
2014-12-31 10:44:38 +02:00
|
|
|
elif len(repo) > 6:
|
2015-09-23 11:52:09 +03:00
|
|
|
print("\nslpkg: Error: Maximum repository name length must be "
|
|
|
|
"six (6) characters\n")
|
2020-02-15 19:54:42 +01:00
|
|
|
raise SystemExit(1)
|
2015-09-23 08:53:16 +03:00
|
|
|
with open(self.custom_repo_file, "a") as repos:
|
2020-02-15 19:54:42 +01:00
|
|
|
new_line = f" {repo}{' ' * (10 - len(repo))}{url}\n"
|
2014-12-31 10:44:38 +02:00
|
|
|
repos.write(new_line)
|
2020-02-15 19:54:42 +01:00
|
|
|
print(f"\nRepository '{repo}' successfully added\n")
|
2015-01-01 10:55:05 +02:00
|
|
|
|
|
|
|
def remove(self, repo):
|
2015-09-23 08:53:16 +03:00
|
|
|
"""Remove custom repository
|
2015-06-05 14:42:52 +03:00
|
|
|
"""
|
2015-01-03 07:53:50 +02:00
|
|
|
rem_repo = False
|
2015-09-23 08:53:16 +03:00
|
|
|
with open(self.custom_repo_file, "w") as repos:
|
|
|
|
for line in self.custom_repositories_list.splitlines():
|
2015-01-01 10:55:05 +02:00
|
|
|
repo_name = line.split()[0]
|
|
|
|
if repo_name != repo:
|
2020-02-15 19:54:42 +01:00
|
|
|
repos.write(f"{line}\n")
|
2015-01-03 07:53:50 +02:00
|
|
|
else:
|
2020-02-15 19:54:42 +01:00
|
|
|
print(f"\nRepository '{repo}' successfully "
|
|
|
|
"removed\n")
|
2015-01-03 07:53:50 +02:00
|
|
|
rem_repo = True
|
|
|
|
if not rem_repo:
|
2020-02-15 19:54:42 +01:00
|
|
|
print(f"\nRepository '{repo}' doesn't exist\n")
|
2014-12-31 10:44:38 +02:00
|
|
|
|
2015-01-01 10:55:05 +02:00
|
|
|
def custom_repository(self):
|
2015-09-23 08:53:16 +03:00
|
|
|
"""Return dictionary with repo name and url (used external)
|
2015-06-05 14:42:52 +03:00
|
|
|
"""
|
2015-09-23 11:11:29 +03:00
|
|
|
custom_dict_repo = {}
|
2015-09-23 08:53:16 +03:00
|
|
|
for line in self.custom_repositories_list.splitlines():
|
2014-12-31 10:44:38 +02:00
|
|
|
line = line.lstrip()
|
|
|
|
if not line.startswith("#"):
|
2015-09-23 11:11:29 +03:00
|
|
|
custom_dict_repo[line.split()[0]] = line.split()[1]
|
|
|
|
return custom_dict_repo
|
2014-11-03 18:02:47 +02:00
|
|
|
|
2015-09-23 08:53:16 +03:00
|
|
|
def default_repository(self):
|
|
|
|
"""Return dictionary with default repo name and url
|
2015-06-05 14:42:52 +03:00
|
|
|
"""
|
2015-09-23 11:11:29 +03:00
|
|
|
default_dict_repo = {}
|
2015-09-23 08:53:16 +03:00
|
|
|
for line in self.default_repositories_list.splitlines():
|
|
|
|
line = line.lstrip()
|
|
|
|
if not line.startswith("#"):
|
2015-09-23 11:52:09 +03:00
|
|
|
if line.split()[0] in self.DEFAULT_REPOS_NAMES:
|
|
|
|
default_dict_repo[line.split()[0]] = line.split()[1]
|
|
|
|
else:
|
2020-02-15 19:54:42 +01:00
|
|
|
print(f"\nslpkg: Error: Repository name '{line.split()[0]}'"
|
|
|
|
" is not default.\n Please check file: "
|
|
|
|
"/etc/slpkg/default-repositories\n")
|
|
|
|
raise SystemExit(1)
|
2015-09-23 11:11:29 +03:00
|
|
|
return default_dict_repo
|
2015-09-23 08:53:16 +03:00
|
|
|
|
|
|
|
def slack(self):
|
|
|
|
"""Official slackware repository
|
2015-06-05 14:42:52 +03:00
|
|
|
"""
|
2014-11-21 12:51:02 +02:00
|
|
|
default = "http://mirrors.slackware.com/slackware/"
|
2015-08-10 06:36:07 +03:00
|
|
|
if self.meta.arch.startswith("arm"):
|
|
|
|
default = "http://ftp.arm.slackware.com/slackwarearm/"
|
2014-11-21 12:51:02 +02:00
|
|
|
if os.path.isfile("/etc/slpkg/slackware-mirrors"):
|
2020-02-16 17:49:41 +01:00
|
|
|
mirrors = self.read_file(
|
2020-02-15 19:54:42 +01:00
|
|
|
f"{self.meta.conf_path}slackware-mirrors")
|
2014-11-21 12:51:02 +02:00
|
|
|
for line in mirrors.splitlines():
|
|
|
|
line = line.rstrip()
|
|
|
|
if not line.startswith("#") and line:
|
|
|
|
default = line.split()[-1]
|
2015-08-10 07:11:24 +03:00
|
|
|
if not default.endswith("/"):
|
|
|
|
default += "/"
|
2020-02-15 19:54:42 +01:00
|
|
|
return default
|