github.com/jfrog/build-info-go@v1.9.26/utils/pythonutils/pipdeptree/scriptcreator.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/jfrog/build-info-go/utils"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  const (
    12  	pipDepTreeContentFileName = "deptreescript.go"
    13  	pipDepTreePythonScript    = "pipdeptree.py"
    14  	pythonPackageName         = "pythonutils"
    15  	pythonPackageRelativePath = "utils"
    16  	// The pip-dep-tree script version. The version should be manually incremented following changes to the pipdeptree.py source file.
    17  	pipDepTreeVersion = "6"
    18  )
    19  
    20  // This main function should be executed manually following changes in pipdeptree.py. Running the function generates new 'pipDepTreeContentFileName' from 'pipDepTreePythonScript.
    21  // Make sure to increment the value of the 'pipDepTreeVersion' constant before running this main function.
    22  func main() {
    23  	wd, err := os.Getwd()
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  
    28  	// Check if a pip-dep-tree file of the latest version already exists
    29  	pipDepTreeContentPath := filepath.Join(wd, pythonPackageRelativePath, pythonPackageName, pipDepTreeContentFileName)
    30  	exists, err := utils.IsFileExists(pipDepTreeContentPath, false)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	if exists {
    35  		return
    36  	}
    37  	// Read the script content from the .py file
    38  	pyFile, err := os.ReadFile(path.Join(wd, pythonPackageRelativePath, pythonPackageName, "pipdeptree", pipDepTreePythonScript))
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	// Replace all backticks ( ` ) with a single quote ( ' )
    43  	pyFileString := strings.ReplaceAll(string(pyFile), "`", "'")
    44  
    45  	resourceString := "package " + pythonPackageName + "\n\n"
    46  	// Add a const string with the script's version
    47  	resourceString += "const pipDepTreeVersion = \"" + pipDepTreeVersion + "\"\n\n"
    48  	// Write the script content a byte-slice
    49  	resourceString += "var pipDepTreeContent = []byte(`\n" + pyFileString + "`)"
    50  	// Create .go file with the script content
    51  	err = os.WriteFile(pipDepTreeContentPath, []byte(resourceString), os.ModePerm)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  }