| 1 |
import os, sys, re, subprocess, argparse |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
default_encoding = 'utf-8' |
| 7 |
|
| 8 |
|
| 9 |
def warn(*msg): |
| 10 |
print(*msg, file=sys.stderr) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
def die(*msg): |
| 16 |
warn(*msg) |
| 17 |
sys.exit(1) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
def set_default_encoding(enc): |
| 22 |
default_encoding = enc |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
def _tweak_opts(cmd, opts, **maybe_set_args): |
| 27 |
def _maybe_set(o, **msa): |
| 28 |
for var, val in msa.items(): |
| 29 |
if var not in o: |
| 30 |
o[var] = val |
| 31 |
|
| 32 |
opts = opts.copy() |
| 33 |
_maybe_set(opts, **maybe_set_args) |
| 34 |
|
| 35 |
if isinstance(cmd, str): |
| 36 |
_maybe_set(opts, shell=True) |
| 37 |
|
| 38 |
want_raw = opts.pop('raw', False) |
| 39 |
if default_encoding and not want_raw: |
| 40 |
_maybe_set(opts, encoding=default_encoding) |
| 41 |
|
| 42 |
capture = opts.pop('capture', None) |
| 43 |
if capture: |
| 44 |
if capture == 'stdout': |
| 45 |
_maybe_set(opts, stdout=subprocess.PIPE) |
| 46 |
elif capture == 'stderr': |
| 47 |
_maybe_set(opts, stderr=subprocess.PIPE) |
| 48 |
elif capture == 'output': |
| 49 |
_maybe_set(opts, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 50 |
elif capture == 'combined': |
| 51 |
_maybe_set(opts, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 52 |
|
| 53 |
discard = opts.pop('discard', None) |
| 54 |
if discard: |
| 55 |
|
| 56 |
if discard == 'stdout' or discard == 'output': |
| 57 |
opts['stdout'] = subprocess.DEVNULL |
| 58 |
if discard == 'stderr' or discard == 'output': |
| 59 |
opts['stderr'] = subprocess.DEVNULL |
| 60 |
|
| 61 |
return opts |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
def cmd_run(cmd, **opts): |
| 66 |
return subprocess.run(cmd, **_tweak_opts(cmd, opts)) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def cmd_chk(cmd, **opts): |
| 71 |
return subprocess.run(cmd, **_tweak_opts(cmd, opts, check=True)) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
def cmd_txt(cmd, **opts): |
| 78 |
input = opts.pop('input', None) |
| 79 |
if input is not None: |
| 80 |
opts['stdin'] = subprocess.PIPE |
| 81 |
proc = subprocess.Popen(cmd, **_tweak_opts(cmd, opts, capture='stdout')) |
| 82 |
out, err = proc.communicate(input=input) |
| 83 |
return argparse.Namespace(out=out, err=err, rc=proc.returncode) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
def cmd_txt_chk(cmd, **opts): |
| 89 |
ct = cmd_txt(cmd, **opts) |
| 90 |
if ct.rc != 0: |
| 91 |
cmd_err = f'Command "{cmd}" returned non-0 exit status "{ct.rc}" and output:\n{ct.out}{ct.err}' |
| 92 |
raise Exception(cmd_err) |
| 93 |
return ct |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
def cmd_pipe(cmd, **opts): |
| 99 |
return subprocess.Popen(cmd, **_tweak_opts(cmd, opts, capture='stdout')) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
def check_git_status(fatal_unless_clean=True, subdir='.'): |
| 106 |
status_txt = cmd_txt_chk(f"cd '{subdir}' && git status").out |
| 107 |
is_clean = re.search(r'\nnothing to commit.+working (directory|tree) clean', status_txt) != None |
| 108 |
|
| 109 |
if not is_clean and fatal_unless_clean: |
| 110 |
if subdir == '.': |
| 111 |
subdir = '' |
| 112 |
else: |
| 113 |
subdir = f" *{subdir}*" |
| 114 |
die(f"The{subdir} checkout is not clean:\n" + status_txt) |
| 115 |
|
| 116 |
m = re.match(r'^(?:# )?On branch (.+)\n', status_txt) |
| 117 |
cur_branch = m[1] if m else None |
| 118 |
|
| 119 |
return (cur_branch, is_clean, status_txt) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
def check_git_state(master_branch, fatal_unless_clean=True, check_extra_dir=None): |
| 128 |
cur_branch = check_git_status(fatal_unless_clean)[0] |
| 129 |
branch = re.sub(r'^patch/([^/]+)/[^/]+$', r'\1', cur_branch) |
| 130 |
if branch != master_branch: |
| 131 |
print(f"The checkout is not on the {master_branch} branch.") |
| 132 |
if master_branch != 'master': |
| 133 |
sys.exit(1) |
| 134 |
ans = input(f"Do you want me to continue with --branch={branch}? [n] ") |
| 135 |
if not ans or not re.match(r'^y', ans, flags=re.I): |
| 136 |
sys.exit(1) |
| 137 |
master_branch = branch |
| 138 |
|
| 139 |
if check_extra_dir and os.path.isdir(os.path.join(check_extra_dir, '.git')): |
| 140 |
branch = check_git_status(fatal_unless_clean, check_extra_dir)[0] |
| 141 |
if branch != master_branch: |
| 142 |
print(f"The *{check_extra_dir}* checkout is on branch {branch}, not branch {master_branch}.") |
| 143 |
ans = input(f"Do you want to change it to branch {master_branch}? [n] ") |
| 144 |
if not ans or not re.match(r'^y', ans, flags=re.I): |
| 145 |
sys.exit(1) |
| 146 |
subdir.check_call(f"cd {check_extra_dir} && git checkout '{master_branch}'", shell=True) |
| 147 |
|
| 148 |
return (cur_branch, master_branch) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
def latest_git_hash(branch): |
| 153 |
out = cmd_txt_chk(['git', 'log', '-1', '--no-color', branch]).out |
| 154 |
m = re.search(r'^commit (\S+)', out, flags=re.M) |
| 155 |
if not m: |
| 156 |
die(f"Unable to determine commit hash for master branch: {branch}") |
| 157 |
return m[1] |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
def get_patch_branches(base_branch): |
| 163 |
branches = set() |
| 164 |
proc = cmd_pipe('git branch -l'.split()) |
| 165 |
for line in proc.stdout: |
| 166 |
m = re.search(r' patch/([^/]+)/(.+)', line) |
| 167 |
if m and m[1] == base_branch: |
| 168 |
branches.add(m[2]) |
| 169 |
proc.communicate() |
| 170 |
return branches |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
def get_gen_files(want_dir_plus_list=False): |
| 175 |
cont_re = re.compile(r'\\\n') |
| 176 |
|
| 177 |
gen_files = [ ] |
| 178 |
|
| 179 |
auto_dir = os.path.join('auto-build-save', cmd_txt('git rev-parse --abbrev-ref HEAD').out.strip().replace('/', '%')) |
| 180 |
|
| 181 |
with open(auto_dir + '/Makefile', 'r', encoding='utf-8') as fh: |
| 182 |
for line in fh: |
| 183 |
if not gen_files: |
| 184 |
chk = re.sub(r'^GENFILES=', '', line) |
| 185 |
if line == chk: |
| 186 |
continue |
| 187 |
line = chk |
| 188 |
m = re.search(r'\\$', line) |
| 189 |
line = re.sub(r'^\s+|\s*\\\n?$|\s+$', '', line) |
| 190 |
gen_files += line.split() |
| 191 |
if not m: |
| 192 |
break |
| 193 |
|
| 194 |
if want_dir_plus_list: |
| 195 |
return (auto_dir, gen_files) |
| 196 |
|
| 197 |
return [ os.path.join(auto_dir, fn) for fn in gen_files ] |
| 198 |
|
| 199 |
|
| 200 |
def get_rsync_version(): |
| 201 |
with open('version.h', 'r', encoding='utf-8') as fh: |
| 202 |
txt = fh.read() |
| 203 |
m = re.match(r'^#define\s+RSYNC_VERSION\s+"(\d.+?)"', txt) |
| 204 |
if m: |
| 205 |
return m[1] |
| 206 |
die("Unable to find RSYNC_VERSION define in version.h") |
| 207 |
|
| 208 |
|
| 209 |
def get_NEWS_version_info(skip_version=None): |
| 210 |
"""Return (last_version, its protocol version, {version: protocol-change date}). |
| 211 |
|
| 212 |
skip_version lets the caller exclude the version it is about to release. |
| 213 |
Its NEWS entry may already carry a release date -- dated by hand, or by an |
| 214 |
earlier run of --step-3-tweak -- and would otherwise be reported as the |
| 215 |
PREVIOUS release, which is both wrong and fatal when it has no table row yet. |
| 216 |
""" |
| 217 |
rel_re = re.compile(r'^\| \S{2} \w{3} \d{4}\s+\|\s+(?P<ver>\d+\.\d+\.\d+)\s+\|\s+(?P<pdate>\d{2} \w{3} \d{4})?\s+\|\s+(?P<pver>\d+)\s+\|') |
| 218 |
last_version = last_protocol_version = None |
| 219 |
pdate = { } |
| 220 |
|
| 221 |
with open('NEWS.md', 'r', encoding='utf-8') as fh: |
| 222 |
for line in fh: |
| 223 |
if not last_version: |
| 224 |
m = re.search(r'rsync (\d+\.\d+\.\d+) .*\d\d\d\d', line) |
| 225 |
if m and m[1] != skip_version: |
| 226 |
last_version = m[1] |
| 227 |
m = rel_re.match(line) |
| 228 |
if m: |
| 229 |
if m['pdate']: |
| 230 |
pdate[m['ver']] = m['pdate'] |
| 231 |
if m['ver'] == last_version: |
| 232 |
last_protocol_version = m['pver'] |
| 233 |
|
| 234 |
if not last_protocol_version: |
| 235 |
die(f"Unable to determine protocol_version for {last_version}.") |
| 236 |
|
| 237 |
return last_version, last_protocol_version, pdate |
| 238 |
|
| 239 |
|
| 240 |
def get_protocol_versions(): |
| 241 |
protocol_version = subprotocol_version = None |
| 242 |
|
| 243 |
with open('rsync.h', 'r', encoding='utf-8') as fh: |
| 244 |
for line in fh: |
| 245 |
m = re.match(r'^#define\s+PROTOCOL_VERSION\s+(\d+)', line) |
| 246 |
if m: |
| 247 |
protocol_version = m[1] |
| 248 |
continue |
| 249 |
m = re.match(r'^#define\s+SUBPROTOCOL_VERSION\s+(\d+)', line) |
| 250 |
if m: |
| 251 |
subprotocol_version = m[1] |
| 252 |
break |
| 253 |
|
| 254 |
if not protocol_version: |
| 255 |
die("Unable to determine the current PROTOCOL_VERSION.") |
| 256 |
|
| 257 |
if not subprotocol_version: |
| 258 |
die("Unable to determine the current SUBPROTOCOL_VERSION.") |
| 259 |
|
| 260 |
return protocol_version, subprotocol_version |
| 261 |
|
| 262 |
|