neutron/tools/migrate_names.py
Flavio Fernandes 303937f3ae [OVN] scripts for networking-ovn code migration
In an effort to help on migrating changes from neutron to networking-ovn,
these 3 scripts are being introduced under tools.

Also adding documentation about these under OVN folder.

1) files_in_patch.py
Use this to show files that are changed in a patch file.

   $ # Make a patch to use as example
   $ git show > /tmp/commit.patch

   $ ./tools/files_in_patch.py /tmp/commit.patch | grep .py
   tools/download_gerrit_change.py
   tools/files_in_patch.py
   tools/migrate_names.py

2) download_gerrit_change.py
Given a gerrit change id, it will fetch the latest patchset of the change
from review.opendev.org as a patch file.

   $ ./tools/download_gerrit_change.py --help
   Usage: download_gerrit_change.py [OPTIONS] GERRIT_CHANGE

   Options:
     -o, --output_patch TEXT  Output patch file  [default: stdout]
     -g, --gerrit_url TEXT    The url to Gerrit server  [default:
                              https://review.opendev.org/]
     -t, --timeout INTEGER    Timeout, in seconds  [default: 10]
     --help                   Show this message and exit.

   $ ./tools/download_gerrit_change.py 698863 -o /tmp/change.patch
   $ ./tools/files_in_patch.py /tmp/change.patch
   networking_ovn/ml2/mech_driver.py
   networking_ovn/ml2/trunk_driver.py
   networking_ovn/tests/unit/ml2/test_mech_driver.py
   networking_ovn/tests/unit/ml2/test_trunk_driver.py

3) migrate_names.py
Use this tool to modify the name of the files in a patchfile so it can
be converted to/from the legacy networking-ovn and neutron repositories.

   $ ./tools/migrate_names.py --help
   Usage: migrate_names.py [OPTIONS]

   Options:
     -i, --input_patch TEXT    input_patch patch file or gerrit change
     -o, --output_patch TEXT   Output patch file. Default: stdout
     -m, --mapfile PATH        Data file that specifies mapping to be applied to
                               input  [default: /home/user/openstack/neutron.git
                               /tools/migrate_names.txt]
     --reverse / --no-reverse  Map filenames from networking-ovn to Neutron repo
     --help                    Show this message and exit.
   $ ./tools/migrate_names.py -i 701646 > /tmp/ovn_change.patch
   $ ./tools/migrate_names.py -o /tmp/reverse.patch -i /tmp/ovn_change.patch --reverse
   $ diff /tmp/reverse.patch /tmp/ovn_change.patch | grep .py
   < --- a/neutron/plugins/ml2/drivers/ovn/mech_driver/mech_driver.py
   < +++ b/neutron/plugins/ml2/drivers/ovn/mech_driver/mech_driver.py
   > --- a/networking_ovn/ml2/mech_driver.py
   > +++ b/networking_ovn/ml2/mech_driver.py
   <... snip ...>

   $ ./tools/files_in_patch.py /tmp/ovn_change.patch
   networking_ovn/ml2/mech_driver.py
   networking_ovn/ml2/trunk_driver.py
   networking_ovn/tests/unit/ml2/test_mech_driver.py
   networking_ovn/tests/unit/ml2/test_trunk_driver.py

Change-Id: I17904c996e1357f7292d25aab4d448edb052f44c
Related-Blueprint: neutron-ovn-merge
2020-01-26 20:19:43 +00:00

112 lines
3.8 KiB
Python
Executable File

#!/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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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 = re.search("^([^#]+)", line_buffer.strip())
if not line_match:
continue
line_buffer = line_match.group(1)
# look for tuple of 2 elements
line_match = re.search(r"^([^\s]+)\s+(.+)", line_buffer.strip())
if not line_match:
continue
ovn_match, neutron_match = line_match.group(1), line_match.group(2)
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 = re.search(r"^\s*---\s+([^\s@]+)[\s@]*", line_buffer)
if not line_match:
line_match = re.search(r"^\s*\+\+\+\s+([^\s@]+)[\s@]*",
line_buffer)
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
output_fd.write("{}\n".format(new_line_buffer))
break
if not filename_replaced:
output_fd.write("{}\n".format(line_buffer))
@contextlib.contextmanager
def open_output(filename=None):
if filename and filename != '-':
fh = open(filename, 'w')
else:
fh = sys.stdout
try:
yield fh
finally:
if fh is not sys.stdout:
fh.close()
@click.command()
@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'),
show_default=True,
type=click.Path(),
help='Data file that specifies mapping to be applied to input')
@click.option('--reverse/--no-reverse',
default=False,
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())
else:
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__':
cli()