OpenStack Networking (Neutron)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.8 KiB

#!/usr/bin/env python3
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from collections import namedtuple
import contextlib
import os
import re
import sys
import click
import download_gerrit_change
root_dir = os.path.dirname(os.path.realpath(__file__))
Migration = namedtuple('Migration', 'from_repo to_repo')
def read_mapfile(mapfile):
dirmaps = []
with open(mapfile, 'r') as mapfile_fd:
for line_buffer in mapfile_fd.readlines():
# ignore empty lines and anything after #
line_match ="^([^#]+)", line_buffer.strip())
if not line_match:
line_buffer =
# look for tuple of 2 elements
line_match ="^([^\s]+)\s+(.+)", line_buffer.strip())
if not line_match:
ovn_match, neutron_match =,
dirmaps.append(Migration(neutron_match, ovn_match))
return dirmaps
def parse_input(dirmaps, patch_content, output_fd):
for line_buffer in patch_content.splitlines():
# locate markers in patch file for filenames and see if they need
# to me renamed based on dirmaps
filename_replaced = False
line_match ="^\s*---\s+([^\s@]+)[\s@]*", line_buffer)
if not line_match:
line_match ="^\s*\+\+\+\s+([^\s@]+)[\s@]*",
if line_match:
for old, new in dirmaps:
new_line_buffer = line_buffer.replace(old, new)
if new_line_buffer != line_buffer:
filename_replaced = True
if not filename_replaced:
def open_output(filename=None):
if filename and filename != '-':
fh = open(filename, 'w')
fh = sys.stdout
yield fh
if fh is not sys.stdout:
@click.option('-i', '--input_patch', prompt='Input patch file or gerrit id',
help='input_patch patch file or gerrit change')
@click.option('-o', '--output_patch', default='-',
help='Output patch file. Default: stdout')
@click.option('-m', '--mapfile',
default=os.path.join(root_dir, 'migrate_names.txt'),
help='Data file that specifies mapping to be applied to input')
help='Map filenames from networking-ovn to Neutron repo')
def cli(input_patch, output_patch, mapfile, reverse):
dirmaps = read_mapfile(mapfile)
if reverse:
dirmaps = [Migration(two, one) for one, two in dirmaps]
if os.path.isfile(input_patch):
with open(input_patch, 'r') as input_fd:
patch_content = ''.join(input_fd.readlines())
patch_content = download_gerrit_change.fetch(input_patch)
with open_output(output_patch) as output_fd:
parse_input(dirmaps, patch_content, output_fd)
if __name__ == '__main__':