Commit d8846274 authored by Xavier Thompson's avatar Xavier Thompson

slapformat: WIP add wrappers

parent 3e5182fe
......@@ -35,6 +35,7 @@ import os
import pwd
import grp
import subprocess
import sys
from collections import defaultdict
from netifaces import AF_INET, AF_INET6
......@@ -351,6 +352,7 @@ class Partition(object):
# XXX
def format(self):
self.user.createUser()
self.createPath()
def createPath(self):
......@@ -373,7 +375,7 @@ class User(object):
self.path = path
self.groups = groups
def create(self):
def createUser(self):
grpname = 'grp_' + self.name if sys.platform == 'cygwin' else self.name
if not self.isGroupAvailable(grpname):
call(['groupadd', grpname])
......@@ -393,7 +395,7 @@ class User(object):
@classmethod
def isGroupAvailable(cls, name):
try:
pwd.getgrnam(name)
grp.getgrnam(name)
return True
except KeyError:
return False
......@@ -426,22 +428,20 @@ def call(args, raise_on_error=True):
# Wrapping, Tracing & Dry-running
class WrappedModule(object):
def __init__(self, conf, module, *methods):
def __init__(self, conf, module, **methods):
self._conf = conf
self._module = module
add = self._addWrapper
for method in methods:
add(method)
add(method)
add(method)
add(method)
for name, fake_result in methods.items():
add(name, fake_result)
def _addWrapper(self, name):
def _addWrapper(self, name, fake_result):
logger = self._conf.logger
if self._conf.dry_run:
def wrapper(*args, **kw):
l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()]
logger.debug('%s(%s)' % (name, ', '.join(l)))
return fake_result
else:
def wrapper(*args, **kw):
l = [repr(x) for x in args] + ['%s=%r' % item for item in kw.items()]
......@@ -463,8 +463,19 @@ class WrappedSystem(object):
def __enter__(self):
global call, os, pwd
conf = self.conf
os = WrappedModule(conf, os, 'chmod', 'chown', 'makedirs', 'mkdir')
pwd = WrappedModule(conf, pwd, 'getpwnam') # XXX return value of fake
os = WrappedModule(
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:
def dry_call(args, raise_on_error=True):
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