-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathseries_sort
More file actions
executable file
·153 lines (136 loc) · 4.86 KB
/
series_sort
File metadata and controls
executable file
·153 lines (136 loc) · 4.86 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 SUSE LLC
#
# This program 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
"""
Script to sort series.conf lines according to the upstream order of commits that
the patches backport.
The script can either read series.conf lines (or a subset thereof) from stdin or
from the file named in the first argument.
A convenient way to use series_sort to filter a subset of lines
within series.conf when using the vim text editor is to visually
select the lines and filter them through the script:
shift-v
j j j j [...] # or ctrl-d or /pattern<enter>
:'<,'>! ~/<path>/series_sort
"""
import argparse
import sys
import os
from git_sort import pygit2_wrapper as pygit2
from git_sort import series_conf
from git_sort import git_sort
from git_sort import lib
from suse_git import exc
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Sort series.conf lines according to the upstream order of "
"commits that the patches backport.")
parser.add_argument("-p", "--prefix", metavar="DIR",
help="Search for patches in this directory. Default: "
"current directory.")
parser.add_argument("-c", "--check", action="store_true",
help="Report via exit status 2 if the series is not "
"sorted. Default: false.")
parser.add_argument("-u", "--upstream", action="store_true",
help="Move patches upstream between subsystem sections "
"as appropriate. Default: false.")
parser.add_argument("series", nargs="?", metavar="series.conf",
help="series.conf file which will be modified in "
"place. Default: if stdin is a terminal, "
"\"series.conf\"; otherwise, read input from stdin.")
args = parser.parse_args()
repo = pygit2.Repository(lib.repo_path())
try:
index = git_sort.SortIndex(repo)
except git_sort.GSMissingUpstream as e:
print('Error: ' + e.args[0])
exit(1)
filter_mode = False
if args.series is None:
if sys.stdin.isatty():
path = "series.conf"
else:
filter_mode = True
else:
path = args.series
if filter_mode:
f = sys.stdin
else:
try:
f = open(path)
except FileNotFoundError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
series_path = os.path.abspath(path)
lines = f.readlines()
if args.prefix is not None:
os.chdir(args.prefix)
try:
before, inside, after = series_conf.split(lines)
except exc.KSNotFound as err:
if filter_mode:
before = []
inside = lines
after = []
elif args.check:
# no sorted section
sys.exit(0)
else:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
try:
input_entries = lib.parse_inside(index, inside, args.upstream)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
try:
sorted_entries = lib.series_sort(index, input_entries)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
new_inside = lib.flatten([
lib.series_header(inside),
lib.series_format(sorted_entries),
lib.series_footer(inside),
])
to_update = list(filter(lib.tag_needs_update, input_entries))
if args.check:
result = 0
if inside != new_inside:
print("Input is not sorted.")
result = 2
if len(to_update):
print("Git-repo tags are outdated.")
result = 2
sys.exit(result)
else:
output = lib.flatten([
before,
new_inside,
after,
])
if filter_mode:
f = sys.stdout
else:
f = open(series_path, mode="w")
f.writelines(output)
try:
lib.update_tags(index, to_update)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)