samba/patch-smb-conf-to-home-sharing.py
2014-10-06 18:49:21 +04:00

56 lines
No EOL
2 KiB
Python

#!/usr/bin/env python
#import iniparse as ip
#import ConfigParser as cp
#smb_conf = ip.INIConfig(open('smb.conf'))
# Note - ConfigParser or Iniparser cannot parse smb.conf because of leading spaces in key values
import sys
import re
class SmbConf(object):
def __init__(self, in_filename):
self.conf = ''
with open(in_filename) as in_file:
self.conf = in_file.read()
def add_after_global_section(self, what):
self.conf = self.conf.replace('[global]', '[global]' + '\n' + what)
pass
def replace_or_add_global_option(self, option, value):
re_ = re.compile('(?m)^\s*%(option)s\s*=' % vars())
expression = '%(option)s=%(value)s' % vars()
if re_.findall(self.conf):
self.conf = re.sub('(?m)^\s*%(option)s\s*=[^$]*' % vars(),
expression, self.conf)
else:
self.add_after_global_section(expression)
def samba_home_user_patch(self):
#https://wiki.archlinux.org/index.php/Samba#Creating_usershare_path
self.replace_or_add_global_option('usershare path', '/var/lib/samba/usershare')
self.replace_or_add_global_option('usershare max shares', '100')
self.replace_or_add_global_option('usershare allow guests', 'yes')
self.replace_or_add_global_option('usershare owner only', 'false')
#self.replace_or_add_global_option('force user', 'nobody')
self.replace_or_add_global_option('guest account', 'nobody')
#essential for anonymous access from windows PC
self.replace_or_add_global_option('map to guest', 'bad user')
self.replace_or_add_global_option('workgroup', 'WORKGROUP')
def output2file(self, out_filename):
with open(out_filename, 'w') as f:
f.write(self.conf)
in_filename = sys.argv[1]
out_filename = in_filename
if len(sys.argv)>2:
out_filename = sys.argv[2]
smb = SmbConf(in_filename)
smb.samba_home_user_patch()
smb.output2file(out_filename)