pytorch

Форк
0
/
build_bundled.py 
194 строки · 7.5 Кб
1
#!/usr/bin/env python3
2
import argparse
3
import os
4

5

6
mydir = os.path.dirname(__file__)
7
licenses = {'LICENSE', 'LICENSE.txt', 'LICENSE.rst', 'COPYING.BSD'}
8

9

10
def 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

40
def 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

71
def 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 squeeze("Clarified Artistic License") in txt:
106
            return 'Clarified Artistic License'
107
        elif all([squeeze(m) in txt.lower() for m in bsd3_txt]):
108
            return 'BSD-3-Clause'
109
        elif all([squeeze(m) in txt.lower() for m in bsd3_v1_txt]):
110
            return 'BSD-3-Clause'
111
        elif all([squeeze(m) in txt.lower() for m in bsd2_txt]):
112
            return 'BSD-2-Clause'
113
        elif all([squeeze(m) in txt.lower() for m in bsd3_src_txt]):
114
            return 'BSD-Source-Code'
115
        elif any([squeeze(m) in txt.lower() for m in mit_txt]):
116
            return 'MIT'
117
        else:
118
            raise ValueError('unknown license')
119

120
mit_txt = ['permission is hereby granted, free of charge, to any person ',
121
           'obtaining a copy of this software and associated documentation ',
122
           'files (the "software"), to deal in the software without ',
123
           'restriction, including without limitation the rights to use, copy, ',
124
           'modify, merge, publish, distribute, sublicense, and/or sell copies ',
125
           'of the software, and to permit persons to whom the software is ',
126
           'furnished to do so, subject to the following conditions:',
127

128
           'the above copyright notice and this permission notice shall be ',
129
           'included in all copies or substantial portions of the software.',
130

131
           'the software is provided "as is", without warranty of any kind, ',
132
           'express or implied, including but not limited to the warranties of ',
133
           'merchantability, fitness for a particular purpose and ',
134
           'noninfringement. in no event shall the authors or copyright holders ',
135
           'be liable for any claim, damages or other liability, whether in an ',
136
           'action of contract, tort or otherwise, arising from, out of or in ',
137
           'connection with the software or the use or other dealings in the ',
138
           'software.',
139
           ]
140

141
bsd3_txt = ['redistribution and use in source and binary forms, with or without '
142
            'modification, are permitted provided that the following conditions '
143
            'are met:',
144

145
            'redistributions of source code',
146

147
            'redistributions in binary form',
148

149
            'neither the name',
150

151
            'this software is provided by the copyright holders and '
152
            'contributors "as is" and any express or implied warranties, '
153
            'including, but not limited to, the implied warranties of '
154
            'merchantability and fitness for a particular purpose are disclaimed.',
155
            ]
156

157
# BSD2 is BSD3 without the "neither the name..." clause
158
bsd2_txt = bsd3_txt[:3] + bsd3_txt[4:]
159

160
# This BSD3 variant leaves "and contributors" out of the last clause of BSD-3,
161
# which is still valid BSD-3
162
v1 = bsd3_txt[4].replace('and contributors', '')
163
bsd3_v1_txt = bsd3_txt[:3] + [v1]
164

165
# This source variant of BSD-3 leaves the "redistributions in binary form" out
166
# which is https://spdx.org/licenses/BSD-Source-Code.html
167
bsd3_src_txt = bsd3_txt[:2] + bsd3_txt[4:]
168

169

170
if __name__ == '__main__':
171
    third_party = os.path.relpath(mydir)
172
    parser = argparse.ArgumentParser(
173
        description="Generate bundled licenses file",
174
    )
175
    parser.add_argument(
176
        "--out-file",
177
        type=str,
178
        default=os.environ.get(
179
            "PYTORCH_THIRD_PARTY_BUNDLED_LICENSE_FILE",
180
            str(os.path.join(third_party, 'LICENSES_BUNDLED.txt'))
181
        ),
182
        help="location to output new bundled licenses file",
183
    )
184
    parser.add_argument(
185
        "--include-files",
186
        action="store_true",
187
        default=False,
188
        help="include actual license terms to the output",
189
    )
190
    args = parser.parse_args()
191
    fname = args.out_file
192
    print(f"+ Writing bundled licenses to {args.out_file}")
193
    with open(fname, 'w') as fid:
194
        create_bundled(third_party, fid, args.include_files)
195

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.