blob: afe1a8c238442ff716c9914183231b1dc288a54d [file] [log] [blame]
#!/usr/bin/env python2.7
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import fileinput
import os
import re
import shutil
import subprocess
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
FUCHSIA_ROOT = os.path.dirname( # $root
os.path.dirname( # scripts
SCRIPT_DIR)) # unification
def run_command(command):
return subprocess.check_output(command, cwd=FUCHSIA_ROOT)
def main():
parser = argparse.ArgumentParser(
description='Moves a FIDL library from //zircon to //sdk')
help='Name of the library to migrate')
args = parser.parse_args()
# Accept library names with dots or dashes.
lib = args.lib.replace('-', '.')
lib_with_dash = args.lib.replace('.', '-')
# Check that the fuchsia.git tree is clean.
diff = run_command(['git', 'status', '--porcelain'])
if diff:
print('Please make sure your tree is clean before running this script')
return 1
sdk_base = os.path.join(FUCHSIA_ROOT, 'sdk', 'fidl')
sdk_dir = os.path.join(sdk_base, lib)
fidl_base = os.path.join(FUCHSIA_ROOT, 'zircon', 'system', 'fidl')
source_dir = os.path.join(fidl_base, lib_with_dash)
# Move the sources.
# The destination directory sometimes already exists.
if not os.path.isdir(sdk_dir):
for _, _, files in os.walk(source_dir):
for file in files:
shutil.move(os.path.join(source_dir, file), sdk_dir)
# Edit the build file in its new location.
in_sdk = False
for line in fileinput.FileInput(os.path.join(sdk_dir, ''),
if 'sdk = false' in line:
if 'sdk = true' in line:
in_sdk = True
print(' sdk_category = "partner"')
print(' api = "' + lib + '.api"')
line = line.replace('$zx_build/public/gn/fidl.gni',
line = line.replace('fidl_library',
line = line.replace(lib_with_dash,
line = line.replace('$zx/system/fidl',
# Set up an alias in the old location.
# Fixing references to the library will likely require a soft transition.
with open(os.path.join(source_dir, ''), 'w') as build_file:
'# Copyright 2019 The Fuchsia Authors. All rights reserved.\n',
'# Use of this source code is governed by a BSD-style license that can be\n',
'# found in the LICENSE file.\n',
'fidl_alias("%s") {\n' % lib_with_dash,
' sdk_category = "partner"\n' if in_sdk else '\n',
# Edit references to the library.
# Only editing the ZN file listing all FIDL libraries.
for line in fileinput.FileInput(os.path.join(fidl_base, ''),
if not '"' + lib_with_dash + '"' in line:
# Create a commit.
run_command(['git', 'checkout', '-b', 'fidl-move-' + lib, 'JIRI_HEAD'])
run_command(['git', 'add', sdk_dir])
message = [
'[fidl] Move ' + lib + ' to //sdk/fidl',
'Generated with: //scripts/unification/ ' + lib,
'Bug: 36547'
commit_command = ['git', 'commit', '-a']
for line in message:
commit_command += ['-m', line]
print('Change is ready, use "jiri upload" to start the review process.')
return 0
if __name__ == '__main__':