Skript, Samba seadistamiseks - Python

From ICO wiki
Jump to navigationJump to search

Ülesande leiab siit:
http://lauri.vosandi.eu/wiki/?page=python-kaugope

Materjal, mida kasutatud töö tegemiseks:
1)http://lauri.vosandi.eu/wiki/?page=python-for-beginners (Välise paketi paigaldamist nõudvad näited > python-apt - Ubuntu pakettide paigaldamine)

2)configparser
http://docs.python.org/library/configparser.html
http://www.doughellmann.com/PyMOTW/ConfigParser/
http://bytes.com/topic/python/answers/651758-python-configparser
http://nullege.com/codes/search/ConfigParser.ConfigParser.read

3)sys
http://docs.python.org/library/sys.html

4)subprocess
http://docs.python.org/library/subprocess.html

#!/usr/bin/python
#-*- coding: utf-8 -*-
# Tühikud konfifailis tekitavad probleeme :s

import sys
import ConfigParser
from ConfigParser import RawConfigParser
import subprocess    

config = ConfigParser.RawConfigParser()
config.read('smb.conf')

#1. Kuvama hetkel välja jagatud kaustad
def showSectionsWithPath():
    for section in config.sections():
        if config.has_option(section, 'path'):
            print 'Section ', section, ' has path ', config.get(section, 'path')

#2. Lubama lisada uusi kaustu
#3. Taaskäivitama Samba teenuse peale seadistuste muutmist
def addSection():
    section_name = raw_input('Section name ')
    print 'This is section name: ', section_name
    config.add_section(section_name)

    browseable = raw_input('Is browseable? (yes/no) ')
    config.set(section_name, 'browseable', browseable)

    only_guest = raw_input('Is only guest? (yes/no) ')
    config.set(section_name, 'only guest', only_guest)

    guest_ok = raw_input('Is guest ok? (yes/no) ')
    config.set(section_name, 'guest ok', guest_ok)

    writeable = raw_input('Is writeable? (yes/no) ')
    config.set(section_name, 'writeable', writeable)

    path = raw_input('Give ma a path ')
    config.set(section_name, 'path', path)

    public = raw_input('Is public? (yes/no) ')
    config.set(section_name, 'public', public)

    with open('smb.conf', 'wb') as configfile:
        config.write(configfile)
    subprocess.call('/etc/init.d/samba restart', shell=True)


# Hello my friend, what you want to do today?
if len(sys.argv) != 2: #
    print 'Argument is missing :('
    print 'Choose from: list, add'
elif sys.argv[1] == 'list':
    showSectionsWithPath()
elif 'add' == sys.argv[1]:
    addSection()
else:
    print 'Unknown input'