-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreorganize_patches.py
executable file
·172 lines (148 loc) · 6.28 KB
/
reorganize_patches.py
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
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2018 Nicolas Iooss
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Reorganize patches in several directories
The aim is to make tracking changes to patches easier than plain numbered
patches in one directory (e.g. when a patch got merged upstream, every later
patches got their index decreased).
This script does NOT change the content of patches in any way.
@author: Nicolas Iooss
@license: MIT
"""
import argparse
import re
import os
import os.path
import sys
def get_patch_path(patchdir, name):
"""Get the canonical path to the give file patch"""
directory = ''
# Get the subject line
subject = None
with open(os.path.join(patchdir, name), 'r') as fpatch:
for line in fpatch:
matches = re.match(r'Subject: \[PATCH\] (.*)', line.rstrip())
if matches is not None:
subject = matches.group(1)
if line.startswith('Sent-upstream:'):
directory = 'sent-upstream'
if not subject:
sys.stderr.write("Error: unable to read the subject of patch {}\n"
.format(name))
return None
subject = subject.strip()
# If the subject starts with a drivers/platform/x86/intel/int3472/clk_and_regulator:bracket (eg. [media]), git removes it when
# applying the patch, which leads to information loss.
# This can be fixed by adding tag {SENT} for example.
if subject.startswith('['):
sys.stderr.write("Warning: subject of patch {} starts with a bracket\n"
.format(name))
# Find a suitable directory, for the topic of the patch
KNOWN_TOPICS = {
'{CONSTIFY}': 'constify',
'{For LLVMLinux}': 'llvmlinux',
'{FRAMA-C}': 'frama-c',
'{LLVMLinux}': 'llvmlinux',
'{MAYBE UPS}': 'maybe_upstreamable',
'{NOT UPSTREAMABLE}': 'not_upstreamble',
'{PLUGIN}': 'plugin',
'{PRAGMA}': 'pragma',
'{PRINTF}': 'printf',
'{REJECTED}': 'rejected',
'{SENT}': 'sent-upstream',
'{TYPO}': 'typo',
}
for topic, topicdir in KNOWN_TOPICS.items():
if subject.startswith(topic + ' ') or subject == topic:
directory = topicdir
subject = subject[len(topic) + 1:]
break
# Canonicalize the subject into a patch name
name = re.sub(r'[^._0-9a-zA-Z]', '-', subject).strip('-')
name = re.sub(r'--+', '-', name)
# "BUG" tags have more diversity
if subject.startswith('{BUG') and name.startswith('BUG-'):
directory = 'bug'
name = name[4:]
if not directory:
sys.stderr.write("Warning: patch {} has no specific directory\n"
.format(name))
return os.path.join(directory, name) + '.patch'
def reorganize_patches(patchdir):
"""Reorganize the patches in the given directory"""
# Read the series file
series_file = os.path.join(patchdir, 'series')
with open(series_file, 'r') as fseries:
series = [line.strip() for line in fseries.readlines()]
# Find new names from the patch content
new_series = [get_patch_path(patchdir, name) for name in series]
if None in new_series:
return False
# Run sanity checks on the new patch names
new_to_old = {}
for idx, old_new in enumerate(zip(series, new_series)):
old, new = old_new
# Check that there are no duplicates in new_series
if new in new_to_old:
# There may be duplicated fixup patches between when working on
# some patches. Detect this and work around it by prepending more
# fixups annotations
if new.startswith('fixup-'):
while new in new_to_old:
new = 'fixup-' + new
new_series[idx] = new
else:
sys.stderr.write("Error: patches {} and {} renamed to {}.\n"
.format(new_to_old[new], old, new))
return False
new_to_old[new] = old
if old == new:
continue
# Check that the new name does not exist in the filesystem
new = os.path.join(patchdir, new)
if os.path.exists(new):
sys.stderr.write("Error: {} already exists.\n".format(new))
return False
# Create a new series file
new_series_file = series_file + '.new'
with open(new_series_file, 'w') as fseries:
fseries.write('\n'.join(new_series) + '\n')
# Move the files
for old, new in zip(series, new_series):
if old != new:
print("{} -> {}".format(old, new))
old = os.path.join(patchdir, old)
new = os.path.join(patchdir, new)
# Check that nobody created a file under us
if os.path.exists(new):
sys.stderr.write("Error: {} already exists.\n".format(new))
return False
os.renames(old, new)
os.rename(new_series_file, series_file)
return True
def main(argv=None):
parser = argparse.ArgumentParser(description="Reorganize patches")
parser.add_argument('patchdir', metavar='PATCHDIR', nargs=1,
help="patch directory to reorganize")
args = parser.parse_args(argv)
return 0 if reorganize_patches(args.patchdir[0]) else 1
if __name__ == '__main__':
sys.exit(main())