3434import re
3535import traceback
3636
37+ import click
38+
3739from attributecode .util import python2
3840
3941if python2 : # pragma: nocover
@@ -128,11 +130,12 @@ def from_dict(cls, data):
128130
129131 def dump (self , target_dir ):
130132 """
131- Write this license as a .yml data file and a text file in `target_dir`.
133+ Write this license as a .yml data file and a .LICENSE text file in
134+ `target_dir`.
132135 """
133136 data_loc = os .path .join (target_dir , self .key + '.yml' )
134- with io .open (data_loc , 'wb ' ) as out :
135- data_loc .write (saneyaml .dump (self .to_dict ()))
137+ with io .open (data_loc , 'w' , encoding = 'utf-8 ' ) as out :
138+ out .write (saneyaml .dump (self .to_dict ()))
136139
137140 if self .text :
138141 file_loc = self .file_loc (target_dir )
@@ -145,12 +148,13 @@ def load_license_references(reference_dir):
145148 Return a mapping of notices as {notice_file: notice text} and a mapping of
146149 {license key: License} loaded from a `reference_dir`.
147150 In the `reference_dir`, all the files must be UTF-8 encoded text files:
148- - The notice files MUST have a .NOTICE extension.
149151 - license files must be named after their license key and can consist of:
150152 - a text file with .LICENSE extension
151153 - an optional companion .yml YAML data file with extra license data to load
152154 as a License obejct.
153155
156+ - The notice files are any file that is not a license file pair.
157+
154158 For instance, we can have the files foo.LICENSE and foo.yml where foo.yml contains:
155159
156160 key: foo
@@ -159,41 +163,32 @@ def load_license_references(reference_dir):
159163 """
160164 notices_by_name = {}
161165 licenses_by_key = {}
162-
163166 ref_files = os .listdir (reference_dir )
164- for name in ref_files :
165- loc = os .path .join (reference_dir , name )
166-
167- if os .path .isdir (loc ):
168- # TODO: raise some error?
169- continue
170-
171- if name .endswith ('.NOTICE' ):
172- with io .open (loc , encoding = 'utf-8' ) as inp :
173- text = inp .read ()
174- notices_by_name [name ] = text
175-
176-
177- if name .endswith ('.yml' ):
178- loaded = License .load (loc )
179-
180- license_key = name .replace ('.yml' , '' )
181- lic = licenses_by_key .get (license_key )
182-
183- if lic :
184- lic .update (loaded )
185- else :
186- licenses_by_key [license_key ] = loaded
187-
188- if name .endswith ('.LICENSE' ):
189- license_key = name .replace ('.LICENSE' , '' )
190- lic = licenses_by_key .get (license_key ) or License (license_key )
191- licenses_by_key [license_key ] = lic
192-
193- with io .open (loc , encoding = 'utf-8' ) as inp :
167+ data_files = [f for f in ref_files if f .endswith ('.yml' )]
168+ text_files = set ([f for f in ref_files if not f .endswith ('.yml' )])
169+
170+ for data_file in data_files :
171+ loc = os .path .join (reference_dir , data_file )
172+ lic = License .load (loc )
173+ licenses_by_key [lic .key ] = lic
174+
175+ if lic .file not in text_files :
176+ click .echo (
177+ 'WARNING: The reference license: {} does not have a '
178+ 'corresponding text file: {}' .format (lic .key , lic .file ))
179+ else :
180+ with io .open (lic .file_loc (reference_dir ), encoding = 'utf-8' ) as inp :
194181 text = inp .read ()
195182 lic .text = text
196183
184+ text_files .remove (lic .file )
185+
186+ # whatever is left are "notice" files
187+ for notice_file in text_files :
188+ with io .open (loc , encoding = 'utf-8' ) as inp :
189+ text = inp .read ()
190+ notices_by_name [notice_file ] = text
191+
197192 return notices_by_name , licenses_by_key
198193
199194
0 commit comments