1+ // Copyright 2017 The Gitea Authors. All rights reserved. 
2+ // SPDX-License-Identifier: MIT 
3+ 
14//go:build ignore 
25
36package  main
47
58import  (
69	"archive/tar" 
710	"compress/gzip" 
11+ 	"crypto/md5" 
12+ 	"encoding/hex" 
813	"flag" 
914	"fmt" 
1015	"io" 
@@ -15,6 +20,8 @@ import (
1520	"path/filepath" 
1621	"strings" 
1722
23+ 	"code.gitea.io/gitea/build/license" 
24+ 	"code.gitea.io/gitea/modules/json" 
1825	"code.gitea.io/gitea/modules/util" 
1926)
2027
@@ -77,7 +84,7 @@ func main() {
7784	}
7885
7986	tr  :=  tar .NewReader (gz )
80- 
87+ 	 aliasesFiles   :=   make ( map [ string ][] string ) 
8188	for  {
8289		hdr , err  :=  tr .Next ()
8390
@@ -97,26 +104,73 @@ func main() {
97104			continue 
98105		}
99106
100- 		if  strings .HasPrefix (filepath .Base (hdr .Name ), "README" ) {
107+ 		fileBaseName  :=  filepath .Base (hdr .Name )
108+ 		licenseName  :=  strings .TrimSuffix (fileBaseName , ".txt" )
109+ 
110+ 		if  strings .HasPrefix (fileBaseName , "README" ) {
101111			continue 
102112		}
103113
104- 		if  strings .HasPrefix (filepath . Base ( hdr . Name ) , "deprecated_" ) {
114+ 		if  strings .HasPrefix (fileBaseName , "deprecated_" ) {
105115			continue 
106116		}
107- 		out , err  :=  os .Create (path .Join (destination , strings . TrimSuffix ( filepath . Base ( hdr . Name ),  ".txt" ) ))
117+ 		out , err  :=  os .Create (path .Join (destination , licenseName ))
108118		if  err  !=  nil  {
109119			log .Fatalf ("Failed to create new file. %s" , err )
110120		}
111121
112122		defer  out .Close ()
113123
114- 		if  _ , err  :=  io .Copy (out , tr ); err  !=  nil  {
124+ 		// some license files have same content, so we need to detect these files and create a convert map into a json file 
125+ 		// Later we use this convert map to avoid adding same license content with different license name 
126+ 		h  :=  md5 .New ()
127+ 		// calculate md5 and write file in the same time 
128+ 		r  :=  io .TeeReader (tr , h )
129+ 		if  _ , err  :=  io .Copy (out , r ); err  !=  nil  {
115130			log .Fatalf ("Failed to write new file. %s" , err )
116131		} else  {
117132			fmt .Printf ("Written %s\n " , out .Name ())
133+ 
134+ 			md5  :=  hex .EncodeToString (h .Sum (nil ))
135+ 			aliasesFiles [md5 ] =  append (aliasesFiles [md5 ], licenseName )
118136		}
119137	}
120138
139+ 	// generate convert license name map 
140+ 	licenseAliases  :=  make (map [string ]string )
141+ 	for  _ , fileNames  :=  range  aliasesFiles  {
142+ 		if  len (fileNames ) >  1  {
143+ 			licenseName  :=  license .GetLicenseNameFromAliases (fileNames )
144+ 			if  licenseName  ==  ""  {
145+ 				// license name should not be empty as expected 
146+ 				// if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases 
147+ 				log .Fatalf ("GetLicenseNameFromAliases: license name is empty" )
148+ 			}
149+ 			for  _ , fileName  :=  range  fileNames  {
150+ 				licenseAliases [fileName ] =  licenseName 
151+ 			}
152+ 		}
153+ 	}
154+ 	// save convert license name map to file 
155+ 	b , err  :=  json .Marshal (licenseAliases )
156+ 	if  err  !=  nil  {
157+ 		log .Fatalf ("Failed to create json bytes. %s" , err )
158+ 	}
159+ 
160+ 	licenseAliasesDestination  :=  filepath .Join (destination , "etc" , "license-aliases.json" )
161+ 	if  err  :=  os .MkdirAll (filepath .Dir (licenseAliasesDestination ), 0o755 ); err  !=  nil  {
162+ 		log .Fatalf ("Failed to create directory for license aliases json file. %s" , err )
163+ 	}
164+ 
165+ 	f , err  :=  os .Create (licenseAliasesDestination )
166+ 	if  err  !=  nil  {
167+ 		log .Fatalf ("Failed to create license aliases json file. %s" , err )
168+ 	}
169+ 	defer  f .Close ()
170+ 
171+ 	if  _ , err  =  f .Write (b ); err  !=  nil  {
172+ 		log .Fatalf ("Failed to write license aliases json file. %s" , err )
173+ 	}
174+ 
121175	fmt .Println ("Done" )
122176}
0 commit comments