Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for re-ordering repos #90

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pisi/cli/pisicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import pisi.cli.remove
import pisi.cli.removeorphans
import pisi.cli.removerepo
import pisi.cli.reorderrepo
import pisi.cli.search
import pisi.cli.searchfile
import pisi.cli.updaterepo
Expand Down
73 changes: 73 additions & 0 deletions pisi/cli/reorderrepo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-FileCopyrightText: 2005-2011 TUBITAK/UEKAE, 2013-2017 Ikey Doherty, Solus Project
# SPDX-License-Identifier: GPL-2.0-or-later

from pathlib import Path
import os
import sys
import xml.etree.ElementTree as ET

import optparse

from pisi import translate as _

import pisi.api
import pisi.cli.command as command
import pisi.context as ctx

class ReorderRepo(command.Command, metaclass=command.autocommand):
__doc__ = _(
"""Add a repository

Usage: reorder-repo <repo> <priority>

<repo>: Name of repository to reorder
<priority>: Reorder repository at given position (0 is first)
"""
)

def __init__(self, args):
super(ReorderRepo, self).__init__(args)

name = ("reorder-repo", "rp")

def run(self):
if len(self.args) == 2:
self.init()
repo_name, repo_priority = self.args
self.reorder_repo(repo_name, repo_priority)
else:
self.help()
return

def reorder_repo(self, repo_name, repo_priority):
if repo_priority.isdigit() is False:
raise pisi.Error(_("Priority needs to be a number"))

repos_xml_file = os.path.join(ctx.config.info_dir(), ctx.const.repos)
if not Path(repos_xml_file).is_file():
raise pisi.Error(_("Unable to locate repository file, expected: %s") % repos_xml_file)

tree = ET.parse(repos_xml_file)
root = tree.getroot()

matched_repo = None

for subitem in root.findall('Repo'):
name = subitem.find('Name')
if name is not None and name.text == repo_name:
matched_repo = subitem
break

if matched_repo is None:
raise pisi.Error(_("Repository %s does not exist. Cannot reorder.") % repo_name)

root.remove(matched_repo)
root.insert(int(repo_priority), matched_repo)
ET.indent(root, " ", 0)

try:
tree.write(repos_xml_file)
except IOError as e:
raise pisi.Error(_("Failed to write to repository file"))

ctx.ui.info(_("Repo %s reordered to position %s.") % (repo_name, repo_priority))