slpkg/slpkg/repositories.py

127 lines
4.9 KiB
Python
Raw Normal View History

#!/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
# 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
# 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
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"
self.custom_repositories_list = self.read_file(
self.custom_repo_file)
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("/"):
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):
print(f"\nRepository name '{repo}' exist, select different name.\n"
2015-09-16 05:37:58 +03:00
"View all repositories with command 'slpkg "
"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")
raise SystemExit(1)
2015-09-23 08:53:16 +03:00
with open(self.custom_repo_file, "a") as repos:
new_line = f" {repo}{' ' * (10 - len(repo))}{url}\n"
2014-12-31 10:44:38 +02:00
repos.write(new_line)
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:
repos.write(f"{line}\n")
2015-01-03 07:53:50 +02:00
else:
print(f"\nRepository '{repo}' successfully "
"removed\n")
2015-01-03 07:53:50 +02:00
rem_repo = True
if not rem_repo:
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:
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"):
mirrors = self.read_file(
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 += "/"
return default