ghanshyam | e73744b | 2015-11-25 14:16:53 +0900 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Use this script to use interfaces/files from tempest-lib. |
| 4 | # Many files have been migrated to tempest-lib and tempest has |
| 5 | # its own copy too. |
| 6 | # This script helps to remove those from tempest and make use of tempest-lib. |
| 7 | # It adds the change-id of each file on which they were migrated in lib. |
| 8 | # This should only be done for files which were migrated to lib with |
| 9 | # "Migrated" in commit message as done by tempest-lib/tools/migrate_from_tempest.sh script. |
| 10 | # "Migrated" keyword is used to fetch the migration commit history from lib. |
| 11 | # To use: |
| 12 | # 1. Create a new branch in the tempest repo so not to destroy your current |
| 13 | # working branch |
| 14 | # 2. Run the script from the repo dir and specify the file paths relative to |
| 15 | # the root tempest dir(only code and unit tests): |
| 16 | # |
| 17 | # tools/use_tempest_lib.sh.sh tempest/file1.py tempest/file2.py |
| 18 | |
| 19 | |
| 20 | function usage { |
| 21 | echo "Usage: $0 [OPTION] file1 file2 .." |
| 22 | echo "Use files from tempest-lib" |
| 23 | echo -e "Input files should be tempest files with path. \n Example- tempest/file1.py tempest/file2.py .." |
| 24 | echo "" |
| 25 | echo "-s, --service_client Specify if files are service clients." |
| 26 | echo "-u, --tempest_lib_git_url Specify the repo to clone tempest-lib from." |
| 27 | } |
| 28 | |
| 29 | function check_all_files_valid { |
| 30 | failed=0 |
| 31 | for file in $files; do |
| 32 | # Get the latest change-id for each file |
| 33 | latest_commit_id=`git log -n1 -- $file | grep "^commit" | awk '{print $2}'` |
| 34 | cd $tmpdir |
| 35 | filename=`basename $file` |
| 36 | lib_path=`find ./ -name $filename` |
| 37 | if [ -z $lib_path ]; then |
| 38 | echo "ERROR: $filename does not exist in tempest-lib." |
| 39 | failed=$(( failed + 1)) |
| 40 | cd - > /dev/null |
| 41 | continue |
| 42 | fi |
| 43 | # Get the CHANGE_ID of tempest-lib patch where file was migrated |
| 44 | migration_change_id=`git log -n1 --grep "Migrated" -- $lib_path | grep "Change-Id: " | awk '{print $2}'` |
| 45 | MIGRATION_IDS=`echo -e "$MIGRATION_IDS\n * $filename: $migration_change_id"` |
| 46 | # Get tempest CHANGE_ID of file which was migrated to lib |
| 47 | migrated_change_id=`git log -n1 --grep "Migrated" -- $lib_path | grep "* $filename"` |
| 48 | migrated_change_id=${migrated_change_id#*:} |
| 49 | cd - > /dev/null |
| 50 | # Get the commit-id of tempest which was migrated to tempest-lib |
| 51 | migrated_commit_id=`git log --grep "$migrated_change_id" -- $file | grep "^commit" | awk '{print $2}'` |
| 52 | DIFF=$(git diff $latest_commit_id $migrated_commit_id $file) |
| 53 | if [ "$DIFF" != "" ]; then |
| 54 | echo "ERROR: $filename in tempest has been updated after migration to tempest-lib. First sync the file to tempest-lib." |
| 55 | failed=$(( failed + 1)) |
| 56 | fi |
| 57 | done |
| 58 | if [[ $failed -gt 0 ]]; then |
| 59 | echo "$failed files had issues" |
| 60 | exit $failed |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | set -e |
| 65 | |
| 66 | service_client=0 |
| 67 | file_list='' |
| 68 | |
| 69 | while [ $# -gt 0 ]; do |
| 70 | case "$1" in |
| 71 | -h|--help) usage; exit;; |
| 72 | -u|--tempest_lib_git_url) tempest_lib_git_url="$2"; shift;; |
| 73 | -s|--service_client) service_client=1;; |
| 74 | *) files="$files $1";; |
| 75 | esac |
| 76 | shift |
| 77 | done |
| 78 | |
| 79 | if [ -z "$files" ]; then |
| 80 | usage; exit |
| 81 | fi |
| 82 | |
| 83 | TEMPEST_LIB_GIT_URL=${tempest_lib_git_url:-git://git.openstack.org/openstack/tempest-lib} |
| 84 | |
| 85 | tmpdir=$(mktemp -d -t use-tempest-lib.XXXX) |
| 86 | |
| 87 | # Clone tempest-lib |
| 88 | git clone $TEMPEST_LIB_GIT_URL $tmpdir |
| 89 | |
| 90 | # Checks all provided files are present in lib and |
| 91 | # not updated in tempest after migration to lib. |
| 92 | check_all_files_valid |
| 93 | |
| 94 | for file in $files; do |
| 95 | rm -f $file |
| 96 | tempest_dir=`pwd` |
| 97 | tempest_dir="$tempest_dir/tempest/" |
| 98 | tempest_dirname=`dirname $file` |
| 99 | lib_dirname=`echo $tempest_dirname | sed s@tempest\/@tempest_lib/\@` |
| 100 | # Convert tempest dirname to import string |
| 101 | tempest_import="${tempest_dirname//\//.}" |
| 102 | tempest_import=${tempest_import:2:${#tempest_import}} |
| 103 | if [ $service_client -eq 1 ]; then |
| 104 | # Remove /json path because tempest-lib supports JSON only without XML |
| 105 | lib_dirname=`echo $lib_dirname | sed s@\/json@@` |
| 106 | fi |
| 107 | # Convert tempest-lib dirname to import string |
| 108 | tempest_lib_import="${lib_dirname//\//.}" |
| 109 | tempest_lib_import=${tempest_lib_import:2:${#tempest_lib_import}} |
| 110 | module_name=`basename $file .py` |
| 111 | tempest_import1="from $tempest_import.$module_name" |
| 112 | tempest_lib_import1="from $tempest_lib_import.$module_name" |
| 113 | tempest_import2="from $tempest_import import $module_name" |
| 114 | tempest_lib_import2="from $tempest_lib_import import $module_name" |
| 115 | set +e |
| 116 | grep -rl "$tempest_import1" $tempest_dir | xargs sed -i'' s/"$tempest_import1"/"$tempest_lib_import1"/g 2> /dev/null |
| 117 | grep -rl "$tempest_import2" $tempest_dir | xargs sed -i'' s/"$tempest_import2"/"$tempest_lib_import2"/g 2> /dev/null |
| 118 | set -e |
| 119 | if [[ -z "$file_list" ]]; then |
| 120 | file_list="$module_name" |
| 121 | else |
| 122 | tmp_file_list="$file_list, $module_name" |
| 123 | char_size=`echo $tmp_file_list | wc -c` |
| 124 | if [ $char_size -lt 27 ]; then |
| 125 | file_list="$file_list, $module_name" |
| 126 | fi |
| 127 | fi |
| 128 | done |
| 129 | |
| 130 | rm -rf $tmpdir |
| 131 | echo "Completed. Run pep8 and fix error if any" |
| 132 | |
| 133 | git add -A tempest/ |
| 134 | # Generate a migration commit |
| 135 | commit_message="Use $file_list from tempest-lib" |
| 136 | pre_list=$"The files below have been migrated to tempest-lib\n" |
| 137 | pre_list=`echo -e $pre_list` |
| 138 | post_list=$"Now Tempest-lib provides those as stable interfaces. So Tempest should\nstart using those from lib and remove its own copy." |
| 139 | post_list=`echo -e $post_list` |
| 140 | |
| 141 | git commit -m "$commit_message" -m "$pre_list" -m "$MIGRATION_IDS" -m "$post_list" |