99from packaging import logger
1010from packaging .errors import PackagingOptionError
1111from packaging .compiler .extension import Extension
12- from packaging .util import check_environ , iglob , resolve_name , strtobool
12+ from packaging .util import (check_environ , iglob , resolve_name , strtobool ,
13+ split_multiline )
1314from packaging .compiler import set_compiler
1415from packaging .command import set_command
1516from packaging .markers import interpret
@@ -60,17 +61,15 @@ def get_resources_dests(resources_root, rules):
6061
6162
6263class Config :
63- """Reads configuration files and work with the Distribution instance
64- """
64+ """Class used to work with configuration files"""
6565 def __init__ (self , dist ):
6666 self .dist = dist
67- self .setup_hook = None
67+ self .setup_hooks = []
6868
69- def run_hook (self , config ):
70- if self .setup_hook is None :
71- return
72- # the hook gets only the config
73- self .setup_hook (config )
69+ def run_hooks (self , config ):
70+ """Run setup hooks in the order defined in the spec."""
71+ for hook in self .setup_hooks :
72+ hook (config )
7473
7574 def find_config_files (self ):
7675 """Find as many configuration files as should be processed for this
@@ -124,29 +123,26 @@ def _convert_metadata(self, name, value):
124123 # XXX
125124 return value
126125
127- def _multiline (self , value ):
128- value = [v for v in
129- [v .strip () for v in value .split ('\n ' )]
130- if v != '' ]
131- return value
132-
133126 def _read_setup_cfg (self , parser , cfg_filename ):
134127 cfg_directory = os .path .dirname (os .path .abspath (cfg_filename ))
135128 content = {}
136129 for section in parser .sections ():
137130 content [section ] = dict (parser .items (section ))
138131
139- # global:setup_hook is called * first*
132+ # global setup hooks are called first
140133 if 'global' in content :
141- if 'setup_hook' in content ['global' ]:
142- setup_hook = content ['global' ]['setup_hook' ]
143- try :
144- self .setup_hook = resolve_name (setup_hook )
145- except ImportError as e :
146- logger .warning ('could not import setup_hook: %s' ,
147- e .args [0 ])
148- else :
149- self .run_hook (content )
134+ if 'setup_hooks' in content ['global' ]:
135+ setup_hooks = split_multiline (content ['global' ]['setup_hooks' ])
136+
137+ for line in setup_hooks :
138+ try :
139+ hook = resolve_name (line )
140+ except ImportError as e :
141+ logger .warning ('cannot find setup hook: %s' , e .args [0 ])
142+ else :
143+ self .setup_hooks .append (hook )
144+
145+ self .run_hooks (content )
150146
151147 metadata = self .dist .metadata
152148
@@ -155,7 +151,7 @@ def _read_setup_cfg(self, parser, cfg_filename):
155151 for key , value in content ['metadata' ].items ():
156152 key = key .replace ('_' , '-' )
157153 if metadata .is_multi_field (key ):
158- value = self . _multiline (value )
154+ value = split_multiline (value )
159155
160156 if key == 'project-url' :
161157 value = [(label .strip (), url .strip ())
@@ -168,21 +164,18 @@ def _read_setup_cfg(self, parser, cfg_filename):
168164 "mutually exclusive" )
169165 raise PackagingOptionError (msg )
170166
171- if isinstance (value , list ):
172- filenames = value
173- else :
174- filenames = value .split ()
167+ filenames = value .split ()
175168
176- # concatenate each files
177- value = ''
169+ # concatenate all files
170+ value = []
178171 for filename in filenames :
179172 # will raise if file not found
180173 with open (filename ) as description_file :
181- value += description_file .read ().strip () + ' \n '
174+ value . append ( description_file .read ().strip ())
182175 # add filename as a required file
183176 if filename not in metadata .requires_files :
184177 metadata .requires_files .append (filename )
185- value = value .strip ()
178+ value = ' \n ' . join ( value ) .strip ()
186179 key = 'description'
187180
188181 if metadata .is_metadata_field (key ):
@@ -192,7 +185,7 @@ def _read_setup_cfg(self, parser, cfg_filename):
192185 files = content ['files' ]
193186 self .dist .package_dir = files .pop ('packages_root' , None )
194187
195- files = dict ((key , self . _multiline (value )) for key , value in
188+ files = dict ((key , split_multiline (value )) for key , value in
196189 files .items ())
197190
198191 self .dist .packages = []
@@ -310,7 +303,7 @@ def parse_config_files(self, filenames=None):
310303 opt = opt .replace ('-' , '_' )
311304
312305 if opt == 'sub_commands' :
313- val = self . _multiline (val )
306+ val = split_multiline (val )
314307 if isinstance (val , str ):
315308 val = [val ]
316309
@@ -348,14 +341,14 @@ def parse_config_files(self, filenames=None):
348341 raise PackagingOptionError (msg )
349342
350343 def _load_compilers (self , compilers ):
351- compilers = self . _multiline (compilers )
344+ compilers = split_multiline (compilers )
352345 if isinstance (compilers , str ):
353346 compilers = [compilers ]
354347 for compiler in compilers :
355348 set_compiler (compiler .strip ())
356349
357350 def _load_commands (self , commands ):
358- commands = self . _multiline (commands )
351+ commands = split_multiline (commands )
359352 if isinstance (commands , str ):
360353 commands = [commands ]
361354 for command in commands :
0 commit comments