Initial import
[samba] / examples / scripts / shares / python / modify_samba_config.py
1 #!/usr/bin/env python
2 ######################################################################
3 ##
4 ##  Simple add/delete/change share command script for Samba
5 ##
6 ##  Copyright (C) Gerald Carter                2004.
7 ##
8 ##  This program is free software; you can redistribute it and/or modify
9 ##  it under the terms of the GNU General Public License as published by
10 ##  the Free Software Foundation; either version 2 of the License, or
11 ##  (at your option) any later version.
12 ##
13 ##  This program is distributed in the hope that it will be useful,
14 ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ##  GNU General Public License for more details.
17 ##
18 ##  You should have received a copy of the GNU General Public License
19 ##  along with this program; if not, write to the Free Software
20 ##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 ##
22 ######################################################################
23
24 import sys, os
25 from SambaConfig import SambaConf
26
27
28 ##                             ##
29 ## check the command line args ##
30 ##                             ##
31 delete_mode = False
32 if len(sys.argv) == 3:
33         delete_mode = True
34         print "Deleting share..."
35 elif len(sys.argv) == 5:
36         print "Adding/Updating share..."
37 else:
38         print "Usage: %s configfile share [path] [comments]" % sys.argv[0]
39         sys.exit(1)
40         
41         
42 ##                                ##
43 ## read and parse the config file ##
44 ##                                ##
45
46 confFile = SambaConf()
47
48 confFile.ReadConfig( sys.argv[1] )
49 if not confFile.valid:
50         exit( 1 )
51         
52 if delete_mode:
53         if not confFile.isService( sys.argv[2] ):
54                 sys.stderr.write( "Asked to delete non-existent service! [%s]\n" % sys.argv[2] )
55                 sys.exit( 1 )
56                 
57         confFile.DelService( sys.argv[2] )
58 else:
59         ## make the path if it doesn't exist.  Bail out if that fails
60         if ( not os.path.isdir(sys.argv[3]) ):
61                 try:
62                         os.makedirs( sys.argv[3] )
63                         os.chmod( sys.argv[3], 0777 )
64                 except os.error:
65                         sys.exit( 1 )
66
67         ## only add a new service -- if it already exists, then 
68         ## just set the options
69         if not confFile.isService( sys.argv[2] ):
70                 confFile.AddService( sys.argv[2], ['##', '## Added by modify_samba_config.py', '##']  )
71         confFile.SetServiceOption( sys.argv[2], "path", sys.argv[3] )
72         confFile.SetServiceOption( sys.argv[2], "comment", sys.argv[4] )
73         confFile.SetServiceOption( sys.argv[2], "read only", "no" )
74
75 ret = confFile.Flush()
76
77 sys.exit( ret )
78