go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/util/extractor_util.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package util contains utility functions
    16  package util
    17  
    18  import (
    19  	"fmt"
    20  	"path/filepath"
    21  	"strings"
    22  )
    23  
    24  /*
    25  	 	NormalizeFilePath returns the normalized the file path.
    26  		Strips leading "/" or "\"
    27  		Converts "\\" and "//" to "/"
    28  		Resolves ".." and "." from the file path
    29  		e.g.
    30  		//BUILD.gn  -> BUILD.gn
    31  		../a/b/c.cc -> a/b/c.cc
    32  		a/b/./c.cc  -> a/b/c.cc
    33  */
    34  func NormalizeFilePath(fp string) string {
    35  	fp = strings.TrimLeft(fp, "\\/")
    36  	fp = strings.ReplaceAll(fp, "\\", "/")
    37  	fp = strings.ReplaceAll(fp, "//", "/")
    38  
    39  	// path.Clean cannot handle the case like ../c.cc, so
    40  	// we need to do it manually
    41  	parts := strings.Split(fp, "/")
    42  	filteredParts := []string{}
    43  	for _, part := range parts {
    44  		if part == "." {
    45  			continue
    46  		} else if part == ".." {
    47  			if len(filteredParts) > 0 {
    48  				filteredParts = filteredParts[:len(filteredParts)-1]
    49  			}
    50  		} else {
    51  			filteredParts = append(filteredParts, part)
    52  		}
    53  	}
    54  	return strings.Join(filteredParts, "/")
    55  }
    56  
    57  // GetCanonicalFileName return the file name without extension and common suffixes.
    58  func GetCanonicalFileName(fp string) string {
    59  	fp = NormalizeFilePath(fp)
    60  	name := filepath.Base(fp)
    61  	return StripExtensionAndCommonSuffixFromFileName(name)
    62  }
    63  
    64  // StripExtensionAndCommonSuffix extension and common suffixes from file name.
    65  // Examples:
    66  // file_impl.cc, file_unittest.cc, file_impl_mac.h -> file
    67  func StripExtensionAndCommonSuffixFromFileName(fileName string) string {
    68  	fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
    69  	commonSuffixes := []string{
    70  		"impl",
    71  		"browser_tests", // Those test suffixes are here for completeness, in compile analysis we will not use them
    72  		"browser_test",
    73  		"browsertest",
    74  		"browsertests",
    75  		"unittests",
    76  		"unittest",
    77  		"tests",
    78  		"test",
    79  		"gcc",
    80  		"msvc",
    81  		"arm",
    82  		"arm64",
    83  		"mips",
    84  		"portable",
    85  		"x86",
    86  		"android",
    87  		"ios",
    88  		"linux",
    89  		"mac",
    90  		"ozone",
    91  		"posix",
    92  		"win",
    93  		"aura",
    94  		"x",
    95  		"x11",
    96  	}
    97  	for {
    98  		found := false
    99  		for _, suffix := range commonSuffixes {
   100  			suffix = "_" + suffix
   101  			if strings.HasSuffix(fileName, suffix) {
   102  				found = true
   103  				fileName = strings.TrimSuffix(fileName, suffix)
   104  			}
   105  		}
   106  		if !found {
   107  			break
   108  		}
   109  	}
   110  	return fileName
   111  }
   112  
   113  // StripExtensionAndCommonSuffix extension and common suffixes from file path.
   114  // Same as StripExtensionAndCommonSuffixFromFileName, but maintain the path.
   115  // If the path is ".", just return the name.
   116  func StripExtensionAndCommonSuffixFromFilePath(fp string) string {
   117  	fp = NormalizeFilePath(fp)
   118  	name := filepath.Base(fp)
   119  	name = StripExtensionAndCommonSuffixFromFileName(name)
   120  	dir := filepath.Dir(fp)
   121  	if dir == "." {
   122  		return name
   123  	}
   124  	return fmt.Sprintf("%s/%s", dir, name)
   125  }