chiark / gitweb /
make-secnet-sites: Tainted.name: take `what'
[secnet.git] / make-secnet-sites
index c26cab0922e3f308f30c2dbff637150be59a3b33..2fd182e29dcb3119cc625aba873a4bf071f0d60b 100755 (executable)
@@ -105,19 +105,19 @@ class Tainted:
                assert(self._ok is not True)
                self._ok=False
                complain('bad parameter: %s: %s' % (what, why))
-               return self
+               return False
 
        def _max_ok(self,what,maxlen):
                if len(self._s) > maxlen:
-                       self._bad(what,'too long (max %d)' % maxlen)
-               return self
+                       return self._bad(what,'too long (max %d)' % maxlen)
+               return True
 
        def _re_ok(self,bad,what,maxlen=None):
                if maxlen is None: maxlen=max[what]
                self._max_ok(what,maxlen)
-               if self._ok is False: return self
+               if self._ok is False: return False
                if bad.search(self._s): return self._bad(what,'bad syntax')
-               return self
+               return True
 
        def _rtnval(self, is_ok, ifgood, ifbad=''):
                if is_ok:
@@ -149,8 +149,8 @@ class Tainted:
        bad_name=re.compile(r'^[^a-zA-Z]|[^-_0-9a-zA-Z]')
        # secnet accepts _ at start of names, but we reserve that
        bad_name_counter=0
-       def name(self):
-               ok=self._re_ok(Tainted.bad_name,'name')
+       def name(self,what='name'):
+               ok=self._re_ok(Tainted.bad_name,what)
                return self._rtn(ok,
                                 '_line%d_%s' % (self._line, id(self)))
 
@@ -211,6 +211,7 @@ def parse_args():
        global header
        global groupfiledir
        global sitesfile
+       global outputfile
        global group
        global user
        global of
@@ -258,8 +259,6 @@ def parse_args():
                        print("Too many arguments")
                        sys.exit(1)
                (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
-               if outputfile is None: of=sys.stdout
-               else: of=open(outputfile,'w')
 
 parse_args()
 
@@ -538,13 +537,12 @@ levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
 
 def complain(msg):
        "Complain about a particular input line"
-       global complaints
-       print(("%s line %d: "%(file,line))+msg)
-       complaints=complaints+1
+       moan(("%s line %d: "%(file,line))+msg)
 def moan(msg):
        "Complain about something in general"
        global complaints
        print(msg);
+       if complaints is None: sys.exit(1)
        complaints=complaints+1
 
 class UntaintedRoot():
@@ -777,4 +775,11 @@ if service:
        f.close()
        os.rename(sitesfile+"-tmp",sitesfile)
 else:
+       if outputfile is None:
+               of=sys.stdout
+       else:
+               tmp_outputfile=outputfile+'~tmp~'
+               of=open(tmp_outputfile,'w')
        outputsites(of)
+       if outputfile is not None:
+               os.rename(tmp_outputfile,outputfile)