99import sys
1010
1111
12- def check_metadata (filenames , headers_spec , quiet = False ):
12+ def check_metadata (filenames , headers_spec , no_metadata = False , quiet = False ):
1313 """Check that metadata headers and values match a set of requirements.
1414
1515 Parameters
@@ -22,6 +22,11 @@ def check_metadata(filenames, headers_spec, quiet=False):
2222 Name of headers as keys and regular expressions as values to match
2323 in the metadata of each file.
2424
25+ no_metadata : bool, optional
26+ When this option is set to ``True``, the hook instead checks that there
27+ is no metadata in the files, so it will return 1 if metadata is found
28+ and 0 exitcode otherwise.
29+
2530 quiet : bool, optional
2631 Enabled, don't print output to stderr when a wrong metadata is found.
2732
@@ -44,21 +49,35 @@ def check_metadata(filenames, headers_spec, quiet=False):
4449 if line .startswith ('msgid ""' ) and content_lines [i + 1 ].startswith (
4550 'msgstr ""'
4651 ):
47- if content_lines [i + 2 ].startswith ('"' ):
52+ if (len (content_lines ) > i + 2 ) and content_lines [i + 2 ].startswith (
53+ '"'
54+ ):
4855 _first_metadata_line = i + 2
56+
57+ if no_metadata :
58+ exitcode = 1
59+ if not quiet :
60+ sys .stderr .write (
61+ f"Found unexpected metadata at { filename } :{ i + 3 } \n "
62+ )
4963 else :
50- sys .stderr .write (f"No metadata found in the file { filename } \n " )
51- exitcode = 1
64+ if not no_metadata :
65+ exitcode = 1
66+ if not quiet :
67+ sys .stderr .write (
68+ f"No metadata found in the file { filename } \n "
69+ )
70+
5271 break
5372
54- if _first_metadata_line is None :
73+ if ( no_metadata and exitcode ) or _first_metadata_line is None :
5574 continue
5675
5776 for i , line in enumerate (content_lines [_first_metadata_line :]):
5877 if not line .strip ():
5978 break
6079
61- header , value = line .split (": " )
80+ header , value = line .split (": " , maxsplit = 1 )
6281 header = header .lstrip ('"' )
6382
6483 if header in headers_spec_regex :
@@ -99,10 +118,31 @@ def main():
99118 parser .add_argument (
100119 "filenames" , nargs = "*" , help = "Filenames to check for obsolete messages"
101120 )
121+ parser .add_argument (
122+ "-n" ,
123+ "--no-metadata" ,
124+ action = "store_true" ,
125+ dest = "no_metadata" ,
126+ help = (
127+ "The files shouldn't have metadata. If a file has metadata"
128+ " information, exits with code 1."
129+ ),
130+ )
102131 parser .add_argument ("-q" , "--quiet" , action = "store_true" , help = "Supress output" )
103132 args = parser .parse_args ()
104133
105- return check_metadata (args .filenames , headers_spec , quiet = args .quiet )
134+ if args .no_metadata and len (headers_spec .keys ()):
135+ raise ValueError (
136+ "You must pass either '--no-metadata' or headers regexes specification,"
137+ " but both can't be non false."
138+ )
139+
140+ return check_metadata (
141+ args .filenames ,
142+ headers_spec ,
143+ no_metadata = args .no_metadata ,
144+ quiet = args .quiet ,
145+ )
106146
107147
108148if __name__ == "__main__" :
0 commit comments