1515
1616"""The project abstraction."""
1717
18- import collections
1918import configparser
2019import errno
2120import io
2221import os
2322
24- from parsley import makeGrammar
25-
26- from openstack_requirements import requirement
27-
28- # PURE logic from here until the IO marker below.
29-
30-
31- _Comment = collections .namedtuple ('Comment' , ['line' ])
32- _Extra = collections .namedtuple ('Extra' , ['name' , 'content' ])
33-
34-
35- _extras_grammar = """
36- ini = (line*:p extras?:e line*:l final:s) -> (''.join(p), e, ''.join(l+[s]))
37- line = ~extras <(~'\\ n' anything)* '\\ n'>
38- final = <(~'\\ n' anything)* >
39- extras = '[' 'e' 'x' 't' 'r' 'a' 's' ']' '\\ n'+ body*:b -> b
40- body = comment | extra
41- comment = <'#' (~'\\ n' anything)* '\\ n'>:c '\\ n'* -> comment(c)
42- extra = name:n ' '* '=' line:l cont*:c '\\ n'* -> extra(n, ''.join([l] + c))
43- name = <(anything:x ?(x not in '\\ n \\ t='))+>
44- cont = ' '+ <(~'\\ n' anything)* '\\ n'>
45- """
46- _extras_compiled = makeGrammar (
47- _extras_grammar , {"comment" : _Comment , "extra" : _Extra })
48-
49-
50- Error = collections .namedtuple ('Error' , ['message' ])
51- File = collections .namedtuple ('File' , ['filename' , 'content' ])
52- StdOut = collections .namedtuple ('StdOut' , ['message' ])
53- Verbose = collections .namedtuple ('Verbose' , ['message' ])
54-
5523
5624def extras (project ):
5725 """Return a dict of extra-name:content for the extras in setup.cfg."""
@@ -64,41 +32,6 @@ def extras(project):
6432 return dict (c .items ('extras' ))
6533
6634
67- def merge_setup_cfg (old_content , new_extras ):
68- # This is ugly. All the existing libraries handle setup.cfg's poorly.
69- prefix , extras , suffix = _extras_compiled (old_content ).ini ()
70- out_extras = []
71- if extras is not None :
72- for extra in extras :
73- if type (extra ) is _Comment :
74- out_extras .append (extra )
75- elif type (extra ) is _Extra :
76- if extra .name not in new_extras :
77- out_extras .append (extra )
78- continue
79- e = _Extra (
80- extra .name ,
81- requirement .to_content (
82- new_extras [extra .name ], ':' , ' ' , False ))
83- out_extras .append (e )
84- else :
85- raise TypeError ('unknown type %r' % extra )
86- if out_extras :
87- extras_str = ['[extras]\n ' ]
88- for extra in out_extras :
89- if type (extra ) is _Comment :
90- extras_str .append (extra .line )
91- else :
92- extras_str .append (extra .name + ' =' )
93- extras_str .append (extra .content )
94- if suffix :
95- extras_str .append ('\n ' )
96- extras_str = '' .join (extras_str )
97- else :
98- extras_str = ''
99- return prefix + extras_str + suffix
100-
101-
10235# IO from here to the end of the file.
10336
10437def _safe_read (project , filename , output = None ):
@@ -143,47 +76,3 @@ def read(root):
14376 result ['lower-constraints.txt' ] = None
14477 _safe_read (result , 'lower-constraints.txt' )
14578 return result
146-
147-
148- def write (project , actions , stdout , verbose , noop = False ):
149- """Write actions into project.
150-
151- :param project: A project metadata dict.
152- :param actions: A list of action tuples - File or Verbose - that describe
153- what actions are to be taken.
154- Error objects write a message to stdout and trigger an exception at
155- the end of _write_project.
156- File objects describe a file to have content placed in it.
157- StdOut objects describe a message to write to stdout.
158- Verbose objects will write a message to stdout when verbose is True.
159- :param stdout: Where to write content for stdout.
160- :param verbose: If True Verbose actions will be written to stdout.
161- :param noop: If True nothing will be written to disk.
162- :return None:
163- :raises IOError: If the IO operations fail, IOError is raised. If this
164- happens some actions may have been applied and others not.
165- """
166- error = False
167- for action in actions :
168- if type (action ) is Error :
169- error = True
170- stdout .write (action .message + '\n ' )
171- elif type (action ) is File :
172- if noop :
173- continue
174- fullname = os .path .join (project ['root' ], action .filename )
175- tmpname = fullname + '.tmp'
176- with open (tmpname , 'wt' ) as f :
177- f .write (action .content )
178- if os .path .exists (fullname ):
179- os .remove (fullname )
180- os .rename (tmpname , fullname )
181- elif type (action ) is StdOut :
182- stdout .write (action .message )
183- elif type (action ) is Verbose :
184- if verbose :
185- stdout .write (u"%s\n " % (action .message ,))
186- else :
187- raise Exception ("Invalid action %r" % (action ,))
188- if error :
189- raise Exception ("Error occurred processing %s" % (project ['root' ]))
0 commit comments