reposync
/
reposync.py
82 строки · 2.8 Кб
1#!/usr/bin/env python3
2
3#
4# RepoSync
5#
6# Copyright (c) 2023-2024 Владислав Щапов aka Vladislav Shchapov <vladislav@shchapov.ru>
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20
21#
22# python3.12 -m venv --system-site-packages /Users/phprus/bin/venv/reposync
23# source /Users/phprus/bin/venv/reposync/bin/activate
24#
25# python3.11 -m venv --system-site-packages /home/phprus/bin/venv/reposync
26# source /home/phprus/bin/venv/reposync/bin/activate
27#
28# pip install urllib3
29#
30
31#
32# ./reposync.py -vvvv --action=check https://mirror.yandex.ru/almalinux/9/SAPHANA/x86_64/os ../al9-saphana-test
33# ./reposync.py -vvvv https://mirror.yandex.ru/almalinux/9/SAPHANA/x86_64/os ../al9-saphana-test
34
35#
36
37import argparse38import urllib.parse39
40import provider41
42
43class ValidateUrl(argparse.Action):44def is_url(self, url):45min_attributes = ('scheme', 'netloc')46try:47tokens = urllib.parse.urlparse(url)48return all([getattr(tokens, attr) for attr in min_attributes])49except ValueError:50return False51
52def __call__(self, parser, namespace, values, option_string=None):53if not self.is_url(values):54parser.error(f"Please enter a valid URL. Got: {values}")55setattr(namespace, self.dest, values)56
57
58parser = argparse.ArgumentParser(59add_help=True,60description='Repository mirror tools'61)
62
63# Параметры
64parser.add_argument('-t', '--type', type=str, default="yum", choices=['yum'], help='repository type (default: %(default)s)')65parser.add_argument('-a', '--action', type=str, default="mirror", choices=['mirror', 'check'], help='action (default: %(default)s)')66parser.add_argument('-v', '--verbose', action='count', default=0, help='increase verbosity')67parser.add_argument('--delete', action=argparse.BooleanOptionalAction, default=True, help='delete extraneous files from repository mirror directory')68
69
70# Позиционные аргументы
71parser.add_argument('url', type=str, action=ValidateUrl, help='Repository URL')72parser.add_argument('dir', type=str, help='Repository mirror directory')73
74
75args = parser.parse_args()76
77repo = provider.repo(args.type, args.url, args.dir, provider.options(args.verbose, delete=args.delete))78
79if args.action == 'check':80repo.check()81elif args.action == 'mirror':82repo.mirror()83