Commit d8846274 authored by Xavier Thompson's avatar Xavier Thompson

slapformat: WIP add wrappers

parent 3e5182fe
...@@ -35,6 +35,7 @@ import os ...@@ -35,6 +35,7 @@ import os
import pwd import pwd
import grp import grp
import subprocess import subprocess
import sys
from collections import defaultdict from collections import defaultdict
from netifaces import AF_INET, AF_INET6 from netifaces import AF_INET, AF_INET6
...@@ -351,6 +352,7 @@ class Partition(object): ...@@ -351,6 +352,7 @@ class Partition(object):
# XXX # XXX
def format(self): def format(self):
self.user.createUser()
self.createPath() self.createPath()
def createPath(self): def createPath(self):
...@@ -373,7 +375,7 @@ class User(object): ...@@ -373,7 +375,7 @@ class User(object):
self.path = path self.path = path
self.groups = groups self.groups = groups
def create(self): def createUser(self):
grpname = 'grp_' + self.name if sys.platform == 'cygwin' else self.name grpname = 'grp_' + self.name if sys.platform == 'cygwin' else self.name
if not self.isGroupAvailable(grpname): if not self.isGroupAvailable(grpname):
call(['groupadd', grpname]) call(['groupadd', grpname])
...@@ -393,7 +395,7 @@ class User(object): ...@@ -393,7 +395,7 @@ class User(object):
@classmethod @classmethod
def isGroupAvailable(cls, name): def isGroupAvailable(cls, name):
try: try:
pwd.getgrnam(name) grp.getgrnam(name)
return True return True
except KeyError: except KeyError:
return False return False
...@@ -426,22 +428,20 @@ def call(args, raise_on_error=True): ...@@ -426,22 +428,20 @@ def call(args, raise_on_error=True):
# Wrapping, Tracing & Dry-running # Wrapping, Tracing & Dry-running
class WrappedModule(object): class WrappedModule(object):
def __init__(self, conf, module, *methods): def __init__(self, conf, module, **methods):
self._conf = conf self._conf = conf
self._module = module self._module = module
add = self._addWrapper add = self._addWrapper
for method in methods: for name, fake_result in methods.items():
add(method) add(name, fake_result)
add(method)
add(method)
add(method)
def _addWrapper(self, name): def _addWrapper(self, name, fake_result):
logger = self._conf.logger logger = self._conf.logger
if self._conf.dry_run: if self._conf.dry_run:
def wrapper(*args, **kw): def wrapper(*args, **kw):
l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()] l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()]
logger.debug('%s(%s)' % (name, ', '.join(l))) logger.debug('%s(%s)' % (name, ', '.join(l)))
return fake_result
else: else:
def wrapper(*args, **kw): def wrapper(*args, **kw):
l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()] l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()]
...@@ -463,8 +463,19 @@ class WrappedSystem(object): ...@@ -463,8 +463,19 @@ class WrappedSystem(object):
def __enter__(self): def __enter__(self):
global call, os, pwd global call, os, pwd
conf = self.conf conf = self.conf
os = WrappedModule(conf, os, 'chmod', 'chown', 'makedirs', 'mkdir') os = WrappedModule(
pwd = WrappedModule(conf, pwd, 'getpwnam') # XXX return value of fake conf,
os,
chmod=None,
chown=None,
makedirs=None,
mkdir=None,
)
pwd = WrappedModule(
conf,
pwd,
getpwnam=type('fake_getpwnam', (), {'pw_uid': 12345, 'pw_gid': 54321}),
)
if conf.dry_run: if conf.dry_run:
def dry_call(args, raise_on_error=True): def dry_call(args, raise_on_error=True):
conf.logger.debug(' '.join(args)) conf.logger.debug(' '.join(args))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment