chiark / gitweb /
make-secnet-sites: Move sites file writing into OpBase
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 Dec 2019 16:32:41 +0000 (16:32 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 Feb 2020 21:56:53 +0000 (21:56 +0000)
We provide a callback for the contents.

This is in OpBase rather than OpUserv because we are going to want to
reuse it.

No functional change.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
make-secnet-sites

index c484e4c..4977524 100755 (executable)
@@ -292,11 +292,24 @@ class PkmElide(PkmBase):
 
 class OpBase():
        # Base case is reading a sites file from self.inputfilee.
+       # And writing a sites file to self.sitesfile.
        def read_in(self):
                if self.inputfile is None:
                        pfile("stdin",sys.stdin.readlines())
                else:
                        pfilepath(self.inputfile)
+       def write_out(self):
+               f=open(self.sitesfile+"-tmp",'w')
+               f.write("# sites file autogenerated by make-secnet-sites\n")
+               f.write("# generated %s, invoked by %s\n"%
+                       (time.asctime(time.localtime(time.time())),
+                        self.user))
+               f.write("# use make-secnet-sites to turn this file into a\n")
+               f.write("# valid /etc/secnet/sites.conf file\n\n")
+               self.write_out_contents(f)
+               f.write("# end of sites file\n")
+               f.close()
+               os.rename(self.sitesfile+"-tmp",self.sitesfile)
 
 class OpConf(OpBase):
        def is_service(self): return 0
@@ -363,13 +376,8 @@ class OpUserv(OpBase):
                f.close()
                os.rename(self.groupfiledir+"/T"+self.group.groupname(),
                          self.groupfiledir+"/R"+self.group.groupname())
-               f=open(self.sitesfile+"-tmp",'w')
-               f.write("# sites file autogenerated by make-secnet-sites\n")
-               f.write("# generated %s, invoked by %s\n"%
-                       (time.asctime(time.localtime(time.time())),
-                        self.user))
-               f.write("# use make-secnet-sites to turn this file into a\n")
-               f.write("# valid /etc/secnet/sites.conf file\n\n")
+               OpBase.write_out(self)
+       def write_out_contents(self,f):
                for i in self.headerinput: f.write(i)
                files=os.listdir(self.groupfiledir)
                for i in files:
@@ -377,10 +385,6 @@ class OpUserv(OpBase):
                                j=open(self.groupfiledir+"/"+i)
                                f.write(j.read())
                                j.close()
-               f.write("# end of sites file\n")
-               f.close()
-               os.rename(self.sitesfile+"-tmp",self.sitesfile)
-               
 
 def parse_args():
        global opmode