-
Notifications
You must be signed in to change notification settings - Fork 8
/
eselect-repo-helper
executable file
·163 lines (134 loc) · 4.86 KB
/
eselect-repo-helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# vim:se fileencoding=utf8 :
# Copyright (c) 2017-2022 Michał Górny
# 2-clause BSD license
# This is a Python helper script for eselect-repo that wraps routines
# for repositories.xml & repos.conf file. It helps us avoid ugly sed
# and/or dependency on non-common packages.
import argparse
import configparser
import locale
import lxml.etree
import os
import os.path
import sys
def transform_source(source_el, repo_name):
"""Transform <source/> into (sync-type, sync-uri) pair."""
stype = source_el.get('type')
suri = source_el.text
# pass-through
if stype in ('bzr', 'git', 'rsync', 'svn'):
return (stype, suri)
# map to 'hg' syncer
elif stype == 'mercurial':
return ('hg', suri)
else:
print(f'warning: {repo_name}: unsupported source type {stype}',
file=sys.stderr)
return None
def do_list(args):
repos = {}
for r in args.repositories_xml.getroot().findall('repo'):
name = r.findtext('name')
uris = [transform_source(s, name) for s in r.findall('source')]
if name in args.repos_conf:
sect = args.repos_conf[name]
sync_params = (sect.get('sync-type'), sect.get('sync-uri'))
if sync_params in uris:
status = 'enabled'
else:
status = 'need-update'
else:
status = 'disabled'
repos[name] = {
'status': status,
'url': r.findtext('homepage'),
}
for name, data in args.repos_conf.items():
if name != 'DEFAULT' and (name not in repos or 'sync-uri' not in data):
repos[name] = {
'status': 'local',
'url': '',
}
for name, data in sorted(repos.items(),
key=lambda kv: locale.strxfrm(kv[0])):
print(name, data["status"], (data['url'] or '').strip())
def do_metadata(args):
all_repos = set(
x.findtext('name')
for x in args.repositories_xml.getroot().findall('repo'))
for r in args.repo:
if r not in args.repos_conf:
print(r, "not-exist")
continue
if r not in all_repos:
state = 'local'
elif 'sync-uri' not in args.repos_conf[r]:
state = 'no-sync-uri'
else:
state = 'remote'
local_path = args.repos_conf[r].get('location', '')
print(r, state, local_path)
def do_remote_metadata(args):
all_repos = dict(
(x.findtext('name'), x)
for x in args.repositories_xml.getroot().findall('repo'))
for r in args.repo:
if r in args.repos_conf:
print(r, "enabled", args.repos_conf[r].get('location', ''))
continue
if r not in all_repos:
print(r, "not-exist")
continue
sources = all_repos[r].findall('source')
for s in sources:
sync_data = transform_source(s, r)
if sync_data is not None:
break
else:
print(r, "unsupported")
continue
print(r, "remote", *sync_data)
def make_configparser(path):
cfgp = configparser.ConfigParser(interpolation=None)
if os.path.isdir(path):
paths = [os.path.join(path, x) for x in os.listdir(path)
if not x.startswith('.') and not x.endswith('~')]
else:
paths = [path]
# monkey-patch our path list in
cfgp._erh_paths = paths
cfgp.read(paths)
return cfgp
def main():
locale.setlocale(locale.LC_ALL, '')
p = argparse.ArgumentParser()
paths = p.add_argument_group('paths')
paths.add_argument('--repos-conf', required=True,
type=make_configparser,
help='Location of repos.conf file')
paths.add_argument('--repositories-xml', required=True,
type=lxml.etree.parse,
help='Location of repositories.xml file')
actions = p.add_subparsers(title='actions', dest='action')
actions.add_parser('list',
help='List of remote & local repositories')
metadata_action = actions.add_parser(
'metadata', help='Print metadata for given repos')
metadata_action.add_argument('repo', nargs='+',
help='Repository to print metadata for')
metadata_action = actions.add_parser(
'remote-metadata', help='Print metadata for given remote repos')
metadata_action.add_argument('repo', nargs='+',
help='Repository to print metadata for')
args = p.parse_args()
if args.action == 'list':
do_list(args)
elif args.action == 'metadata':
do_metadata(args)
elif args.action == 'remote-metadata':
do_remote_metadata(args)
else:
raise NotImplementedError(f'Action {args.action} not implemented')
if __name__ == '__main__':
main()