1#!/usr/bin/env python3 2import argparse 3import os 4 5 6mydir = os.path.dirname(__file__) 7licenses = {'LICENSE', 'LICENSE.txt', 'LICENSE.rst', 'COPYING.BSD'} 8 9 10def collect_license(current): 11 collected = {} 12 for root, dirs, files in os.walk(current): 13 license = list(licenses & set(files)) 14 if license: 15 name = root.split('/')[-1] 16 license_file = os.path.join(root, license[0]) 17 try: 18 ident = identify_license(license_file) 19 except ValueError: 20 raise ValueError('could not identify license file ' 21 f'for {root}') from None 22 val = { 23 'Name': name, 24 'Files': [root], 25 'License': ident, 26 'License_file': [license_file], 27 } 28 if name in collected: 29 # Only add it if the license is different 30 if collected[name]['License'] == ident: 31 collected[name]['Files'].append(root) 32 collected[name]['License_file'].append(license_file) 33 else: 34 collected[name + f' ({root})'] = val 35 else: 36 collected[name] = val 37 return collected 38 39 40def create_bundled(d, outstream, include_files=False): 41 """Write the information to an open outstream""" 42 collected = collect_license(d) 43 sorted_keys = sorted(collected.keys()) 44 outstream.write('The PyTorch repository and source distributions bundle ' 45 'several libraries that are \n') 46 outstream.write('compatibly licensed. We list these here.') 47 files_to_include = [] 48 for k in sorted_keys: 49 c = collected[k] 50 files = ',\n '.join(c['Files']) 51 license_file = ',\n '.join(c['License_file']) 52 outstream.write('\n\n') 53 outstream.write(f"Name: {c['Name']}\n") 54 outstream.write(f"License: {c['License']}\n") 55 outstream.write(f"Files: {files}\n") 56 outstream.write(' For details, see') 57 if include_files: 58 outstream.write(' the files concatenated below: ') 59 files_to_include += c['License_file'] 60 else: 61 outstream.write(': ') 62 outstream.write(license_file) 63 for fname in files_to_include: 64 outstream.write('\n\n') 65 outstream.write(fname) 66 outstream.write('\n' + '-' * len(fname) + '\n') 67 with open(fname, 'r') as fid: 68 outstream.write(fid.read()) 69 70 71def identify_license(f, exception=''): 72 """ 73 Read f and try to identify the license type 74 This is __very__ rough and probably not legally binding, it is specific for 75 this repo. 76 """ 77 def squeeze(t): 78 """Remove 'n and ' ', normalize quotes 79 """ 80 t = t.replace('\n', '').replace(' ', '') 81 t = t.replace('``', '"').replace("''", '"') 82 return t 83 84 with open(f) as fid: 85 txt = fid.read() 86 if not exception and 'exception' in txt: 87 license = identify_license(f, 'exception') 88 return license + ' with exception' 89 txt = squeeze(txt) 90 if 'ApacheLicense' in txt: 91 # Hmm, do we need to check the text? 92 return 'Apache-2.0' 93 elif 'MITLicense' in txt: 94 # Hmm, do we need to check the text? 95 return 'MIT' 96 elif 'BSD-3-ClauseLicense' in txt: 97 # Hmm, do we need to check the text? 98 return 'BSD-3-Clause' 99 elif 'BSD3-ClauseLicense' in txt: 100 # Hmm, do we need to check the text? 101 return 'BSD-3-Clause' 102 elif 'BoostSoftwareLicense-Version1.0' in txt: 103 # Hmm, do we need to check the text? 104 return 'BSL-1.0' 105 elif 'gettimeofday' in txt: 106 # Used in opentelemetry-cpp/tools/vcpkg/ports/gettimeofday 107 return 'Apache-2.0' 108 elif 'libhungarian' in txt: 109 # Used in opentelemetry-cpp/tools/vcpkg/ports/hungarian 110 return 'Permissive (free to use)' 111 elif 'PDCurses' in txt: 112 # Used in opentelemetry-cpp/tools/vcpkg/ports/pdcurses 113 return 'Public Domain for core' 114 elif 'Copyright1999UniversityofNorthCarolina' in txt: 115 # Used in opentelemetry-cpp/tools/vcpkg/ports/pqp 116 return 'Apache-2.0' 117 elif 'sigslot' in txt: 118 # Used in opentelemetry-cpp/tools/vcpkg/ports/sigslot 119 return 'Public Domain' 120 elif squeeze("Clarified Artistic License") in txt: 121 return 'Clarified Artistic License' 122 elif all([squeeze(m) in txt.lower() for m in bsd3_txt]): 123 return 'BSD-3-Clause' 124 elif all([squeeze(m) in txt.lower() for m in bsd3_v1_txt]): 125 return 'BSD-3-Clause' 126 elif all([squeeze(m) in txt.lower() for m in bsd2_txt]): 127 return 'BSD-2-Clause' 128 elif all([squeeze(m) in txt.lower() for m in bsd3_src_txt]): 129 return 'BSD-Source-Code' 130 elif any([squeeze(m) in txt.lower() for m in mit_txt]): 131 return 'MIT' 132 else: 133 raise ValueError('unknown license') 134 135mit_txt = ['permission is hereby granted, free of charge, to any person ', 136 'obtaining a copy of this software and associated documentation ', 137 'files (the "software"), to deal in the software without ', 138 'restriction, including without limitation the rights to use, copy, ', 139 'modify, merge, publish, distribute, sublicense, and/or sell copies ', 140 'of the software, and to permit persons to whom the software is ', 141 'furnished to do so, subject to the following conditions:', 142 143 'the above copyright notice and this permission notice shall be ', 144 'included in all copies or substantial portions of the software.', 145 146 'the software is provided "as is", without warranty of any kind, ', 147 'express or implied, including but not limited to the warranties of ', 148 'merchantability, fitness for a particular purpose and ', 149 'noninfringement. in no event shall the authors or copyright holders ', 150 'be liable for any claim, damages or other liability, whether in an ', 151 'action of contract, tort or otherwise, arising from, out of or in ', 152 'connection with the software or the use or other dealings in the ', 153 'software.', 154 ] 155 156bsd3_txt = ['redistribution and use in source and binary forms, with or without ' 157 'modification, are permitted provided that the following conditions ' 158 'are met:', 159 160 'redistributions of source code', 161 162 'redistributions in binary form', 163 164 'neither the name', 165 166 'this software is provided by the copyright holders and ' 167 'contributors "as is" and any express or implied warranties, ' 168 'including, but not limited to, the implied warranties of ' 169 'merchantability and fitness for a particular purpose are disclaimed.', 170 ] 171 172# BSD2 is BSD3 without the "neither the name..." clause 173bsd2_txt = bsd3_txt[:3] + bsd3_txt[4:] 174 175# This BSD3 variant leaves "and contributors" out of the last clause of BSD-3, 176# which is still valid BSD-3 177v1 = bsd3_txt[4].replace('and contributors', '') 178bsd3_v1_txt = bsd3_txt[:3] + [v1] 179 180# This source variant of BSD-3 leaves the "redistributions in binary form" out 181# which is https://spdx.org/licenses/BSD-Source-Code.html 182bsd3_src_txt = bsd3_txt[:2] + bsd3_txt[4:] 183 184 185if __name__ == '__main__': 186 third_party = os.path.relpath(mydir) 187 parser = argparse.ArgumentParser( 188 description="Generate bundled licenses file", 189 ) 190 parser.add_argument( 191 "--out-file", 192 type=str, 193 default=os.environ.get( 194 "PYTORCH_THIRD_PARTY_BUNDLED_LICENSE_FILE", 195 str(os.path.join(third_party, 'LICENSES_BUNDLED.txt')) 196 ), 197 help="location to output new bundled licenses file", 198 ) 199 parser.add_argument( 200 "--include-files", 201 action="store_true", 202 default=False, 203 help="include actual license terms to the output", 204 ) 205 args = parser.parse_args() 206 fname = args.out_file 207 print(f"+ Writing bundled licenses to {args.out_file}") 208 with open(fname, 'w') as fid: 209 create_bundled(third_party, fid, args.include_files) 210