@@ -46,7 +46,7 @@ def string(self):
4646 """Returns the version string if `run()` found no inconsistencies,
4747 otherwise None is returned."""
4848 return self ._string
49- def include (self , path , func = None , opts = None , ** kwargs ):
49+ def include (self , path , func = None , opts = None , updatable = True , ** kwargs ):
5050 """Includes a file to check.
5151
5252 **Params**:
@@ -55,32 +55,41 @@ def include(self, path, func=None, opts=None, **kwargs):
5555 `path` and `opts`. Must return a list of VerInfo items.
5656 - opts (dict) - Options to pass to the check function. Any additional
5757 key word args will be included.
58+ - updatable (bool) - If true, string can be updated using `update()`.
5859 """
5960 if not opts :
6061 opts = {}
6162 opts .update (copy .deepcopy (kwargs ))
6263 if not func :
6364 func = check_basic
64- c = (path , func , copy .deepcopy (opts ))
65+ c = (path , func , copy .deepcopy (opts ), updatable )
6566 self ._checks .append (c )
66- def run (self , verbose = True , debug = False ):
67- """Runs checks on all included items, reports any inconsistencies."""
68- vprint = get_vprint (verbose )
69- self ._vinfos = []
70- vprint ("%s Version Information:" % (self .name ))
71- # Execute checks and update version information list.
67+ def iter_vinfo (self , get_updatable = False ):
68+ """Iterates over the associated VerInfo objects. Optionally returns if
69+ the associated file is updatable."""
7270 for c in self ._checks :
7371 path = c [0 ]
7472 if not op .isabs (path ):
7573 path = op .join (self .root , path )
7674 path = op .normpath (path )
77- self ._vinfos .extend (c [1 ](path , ** c [2 ]))
78- for vinfo in self ._vinfos :
75+ vinfos = c [1 ](path , ** c [2 ])
76+ if list != type (vinfos ):
77+ vinfos = [vinfos ]
78+ for vi in vinfos :
79+ yield (vi , c [3 ]) if get_updatable else vi
80+ def run (self , verbose = True ):
81+ """Runs checks on all included items, reports any inconsistencies.
82+ Returns version string if consistent else None."""
83+ vprint = get_vprint (verbose )
84+ strings = []
85+ vprint ("%s Version Information:" % (self .name ))
86+ for vinfo in self .iter_vinfo ():
87+ if vinfo .string not in strings :
88+ strings .append (vinfo .string )
7989 vprint (" `%s` (%s:%u)" % (
8090 vinfo .string ,
8191 op .relpath (vinfo .path ),
8292 vinfo .linenum ))
83- strings = set ([v .string for v in self ._vinfos ])
8493 if strings :
8594 self ._string = list (strings )[0 ] if 1 == len (strings ) else None
8695 if not self ._string :
@@ -91,20 +100,25 @@ def run(self, verbose=True, debug=False):
91100 return self ._string
92101 def update (self , newver ):
93102 """Updates all associated version strings to the given new string. Use
94- caution as this will modify file content!"""
95- self .run (verbose = False )
96- for vi in self ._vinfos :
97- with open (vi .path ) as fi :
103+ caution as this will modify file content! Returns number of strings
104+ updated."""
105+ updated = 0
106+ for vinfo ,updatable in self .iter_vinfo (get_updatable = True ):
107+ if not updatable :
108+ continue
109+ with open (vinfo .path ) as fi :
98110 temp = op .join (
99- op .dirname (vi .path ),
100- "__temp-verace-" + op .basename (vi .path ))
111+ op .dirname (vinfo .path ),
112+ "__temp-verace-" + op .basename (vinfo .path ))
101113 with open (temp , "w" ) as fo :
102114 for num ,line in enumerate (fi .readlines (), 1 ):
103- if num == vi .linenum :
104- line = line .replace (vi .string , newver )
115+ if num == vinfo .linenum :
116+ line = line .replace (vinfo .string , newver )
105117 fo .write (line )
106- os .remove (vi .path )
107- os .rename (temp , vi .path )
118+ os .remove (vinfo .path )
119+ os .rename (temp , vinfo .path )
120+ updated += 1
121+ return updated
108122
109123##==============================================================#
110124## SECTION: Function Definitions #
@@ -130,7 +144,7 @@ def check_basic(path, match="version", delim="=", delim2=""):
130144 ver = line .split (delim )[1 ].strip ()
131145 if delim2 :
132146 ver = ver .split (delim2 )[0 ].strip ()
133- return [ VerInfo (path , num + 1 , ver )]
147+ return VerInfo (path , num + 1 , ver )
134148
135149##==============================================================#
136150## SECTION: Main Body #
0 commit comments