From 15a9cbb94b92dd00848a2e8b77a3b53ca4d65113 Mon Sep 17 00:00:00 2001 Message-Id: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 29 Nov 2013 09:49:01 +0100 Subject: [PATCH 01/33] Bug #21585: Fix listing policy references Organization: Univention GmbH, Bremen, Germany Rename and set variable "policies" -> "list_policies". --- .../modules/univention/admincli/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 1607760..24d7ba7 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -476,7 +476,7 @@ def _doit(arglist): elif opt == '--logfile': logfile=val elif opt == '--policies': - policies = True + list_policies = True if val=="1": policies_with_DN = True else: -- 1.8.4.rc3 From efa54e3554cb9fe5b59c96655b41f3e7252c0cdb Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 02/33] Bug #33224: Fix typo in comment Organization: Univention GmbH, Bremen, Germany goal -> global --- .../modules/univention/admincli/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 24d7ba7..e7256e4 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -934,7 +934,7 @@ def _doit(arglist): out.append("need new position for moving object") else: res = '' - try: # check if goal-position exists + try: # check if global-position exists res = lo.get(position_dn) except: pass -- 1.8.4.rc3 From 1c4de0ad8099e61c3043bbb17463410e557989b2 Mon Sep 17 00:00:00 2001 Message-Id: <1c4de0ad8099e61c3043bbb17463410e557989b2.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:29:15 +0100 Subject: [PATCH 03/33] Bug #33224: Remove arg handling Organization: Univention GmbH, Bremen, Germany Unused (since a very long time?) --- .../modules/univention/admincli/admin.py | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index e7256e4..c404f05 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -95,7 +95,6 @@ def usage(): out.append(' --%-30s %s' % ('bindpwd', 'bind password')) out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) out.append(' --%-30s %s' % ('dn', 'Edit object with DN')) - out.append(' --%-30s %s' % ('arg', 'Edit object with ARG')) out.append(' --%-30s %s' % ('set', 'Set variable to value, e.g. foo=bar')) out.append(' --%-30s %s' % ('append', 'Append value to variable, e.g. foo=bar')) out.append(' --%-30s %s' % ('remove', 'Remove value from variable, e.g. foo=bar')) @@ -113,7 +112,6 @@ def usage(): out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) out.append(' --%-30s %s' % ('dn', 'Remove object with DN')) out.append(' --%-30s %s' % ('superordinate', 'Use superordinate module')) - out.append(' --%-30s %s' % ('arg', 'Remove object with ARG')) out.append(' --%-30s %s' % ('filter', 'Lookup filter e.g. foo=bar')) out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) out.append(' --%-30s %s' % ('remove_referring', 'remove referring objects')) @@ -435,7 +433,30 @@ def _doit(arglist): remove_referring=0 recursive=1 # parse options - longopts=['position=', 'dn=', 'arg=', 'set=', 'append=', 'remove=', 'superordinate=', 'option=', 'append-option=', 'filter=', 'tls=', 'ignore_exists', 'logfile=', 'policies=', 'binddn=', 'bindpwd=', 'bindpwdfile=', 'customattribute=', 'customattribute-remove=','policy-reference=','policy-dereference=','remove_referring','recursive'] + longopts = [ + 'position=', + 'dn=', + 'set=', + 'append=', + 'remove=', + 'superordinate=', + 'option=', + 'append-option=', + 'filter=', + 'tls=', + 'ignore_exists', + 'logfile=', + 'policies=', + 'binddn=', + 'bindpwd=', + 'bindpwdfile=', + 'customattribute=', + 'customattribute-remove=', + 'policy-reference=', + 'policy-dereference=', + 'remove_referring', + 'recursive' + ] try: opts, args=getopt.getopt(arglist[3:], '', longopts) except getopt.error, msg: @@ -450,7 +471,6 @@ def _doit(arglist): position_dn='' dn='' - arg=None binddn=None bindpwd=None list_policies=False @@ -494,8 +514,6 @@ def _doit(arglist): return out + ['OPERATION FAILED'] elif opt == '--dn': dn = _2utf8( val ) - elif opt == '--arg': - arg=val elif opt == '--tls': tls=val elif opt == '--ignore_exists': @@ -1121,7 +1139,6 @@ def _doit(arglist): try: for object in univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter): out.append( 'DN: %s' % _2utf8( univention.admin.objects.dn (object ) ) ) - out.append( 'ARG: %s' % univention.admin.objects.arg( object ) ) if (hasattr(module,'virtual') and not module.virtual) or not hasattr(module,'virtual'): object.open() -- 1.8.4.rc3 From 823154ef65d7633660c0f54d86e19fa11c1277d8 Mon Sep 17 00:00:00 2001 Message-Id: <823154ef65d7633660c0f54d86e19fa11c1277d8.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:29:15 +0100 Subject: [PATCH 04/33] Bug #33224: Remove custom attribute handling Organization: Univention GmbH, Bremen, Germany Unsupported since UCS-3.0-0. --- .../modules/univention/admincli/admin.py | 132 --------------------- 1 file changed, 132 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index c404f05..f21610c 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -85,7 +85,6 @@ def usage(): out.append(' --%-30s %s' % ('set', 'Set variable to value, e.g. foo=bar')) out.append(' --%-30s %s' % ('superordinate', 'Use superordinate module')) out.append(' --%-30s %s' % ('option', 'Use only given module options')) - out.append(' --%-30s %s' % ('customattribute', 'Set custom attribute foo=bar')) out.append(' --%-30s %s' % ('policy-reference', 'Reference to policy given by DN')) out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) out.append(' --%-30s ' % ('ignore_exists')) @@ -100,8 +99,6 @@ def usage(): out.append(' --%-30s %s' % ('remove', 'Remove value from variable, e.g. foo=bar')) out.append(' --%-30s %s' % ('option', 'Use only given module options')) out.append(' --%-30s %s' % ('append-option', 'Append the module options')) - out.append(' --%-30s %s' % ('customattribute', 'Set custom attribute foo=bar')) - out.append(' --%-30s %s' % ('customattribute-remove', 'Remove custom attribute')) out.append(' --%-30s %s' % ('policy-reference', 'Reference to policy given by DN')) out.append(' --%-30s %s' % ('policy-dereference', 'Remove reference to policy given by DN')) out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) @@ -450,8 +447,6 @@ def _doit(arglist): 'binddn=', 'bindpwd=', 'bindpwdfile=', - 'customattribute=', - 'customattribute-remove=', 'policy-reference=', 'policy-dereference=', 'remove_referring', @@ -486,8 +481,6 @@ def _doit(arglist): input={} append={} remove={} - customattribute={} - customattribute_remove=[] policy_reference=[] policy_dereference=[] for opt, val in opts: @@ -526,21 +519,6 @@ def _doit(arglist): parsed_append_options.append(val) elif opt == '--filter': filter=val - elif opt == '--customattribute': - pos=val.find('=') - name=val[:pos] - value= _2utf8( val[ pos + 1 : ] ) - if not customattribute.has_key(name): - customattribute[name]=[] - customattribute[name].append(value) - elif opt == '--customattribute-remove': - pos=val.find('=') - if pos == -1: - customattribute_remove.append((val,None)) - else: - name=val[:pos] - value = _2utf8( val[ pos + 1 : ] ) - customattribute_remove.append((name,value)) elif opt == '--policy-reference': policy_reference.append(val) elif opt == '--policy-dereference': @@ -725,53 +703,7 @@ def _doit(arglist): recursive=1 - extraOC=[] - extraAttributes=[] - customattributes_set =[] - if hasattr(module, 'ldap_extra_objectclasses') and action in ['modify','edit','create','new']: - for oc, pname, syntax, ldapMapping, deleteValues, deleteObjectClass in module.ldap_extra_objectclasses: - if customattribute.has_key(module.property_descriptions[pname].short_description): - customattributes_set.append(module.property_descriptions[pname].short_description) - extraOC.append(oc); - # check multivalue - if module.property_descriptions[pname].multivalue: - if action in ['create','new'] or not dn or dn == '': - values_found=[] - else: - values_found=lo.search(base=dn, attr=[ldapMapping]) - for i in customattribute[module.property_descriptions[pname].short_description]: - value_already_set=0 - for tmp,val in values_found: - if val.has_key(ldapMapping): - if i in val[ldapMapping]: - value_already_set=1 - if value_already_set: - out.append('WARNING: customattribute %s is already set to %s'%(module.property_descriptions[pname].short_description,i)) - else: - extraAttributes.append((ldapMapping,'',[i])) - else: - if len(customattribute[module.property_descriptions[pname].short_description])>1: - out.append('WARNING: cannot set singlevalue customattribute "%s" with more than one entry'%module.property_descriptions[pname].short_description) - else: - replaced=0 - if action in ['create','new'] or not dn or dn == '': - values_found=[] - else: - values_found=lo.search(base=dn, attr=[ldapMapping]) - for tmp,val in values_found: - if val.has_key(ldapMapping): - extraAttributes.append((ldapMapping,val[ldapMapping][0],[customattribute[module.property_descriptions[pname].short_description][0]])) - replaced = 1 - if len(val[ldapMapping]) > 1: - out.append("WARNING: singlevalue customattribute %s has more than one value set, replace first"%customattribute[module.property_descriptions[pname].short_description]) - if not replaced: - extraAttributes.append((ldapMapping,'',[customattribute[module.property_descriptions[pname].short_description][0]])) - if action in ['modify','edit','create','new']: - for i in customattribute.keys(): - if not i in customattributes_set: - out.append("WARNING: customattribute %s not found, value not set"%i) - if policy_reference: for el in policy_reference: oc = lo.get(el,['objectClass']) @@ -885,24 +817,6 @@ def _doit(arglist): out.append('E: circular group dependency detected: %s' % e) return out + ["OPERATION FAILED"] - if extraOC or extraAttributes: - if extraOC: - oc=lo.search(base=dn, scope='base', attr=['objectClass']) - - noc=[] - for i in range(len(oc[0][1]['objectClass'])): - noc.append(oc[0][1]['objectClass'][i]) - - for i in range(len(extraOC)): - if extraOC[i] not in noc: - noc.append(extraOC[i]) - - if oc != noc: - extraAttributes.append(('objectClass',oc,noc)) - - if extraAttributes: - lo.modify(dn,extraAttributes) - if policy_reference: lo.modify(dn,[('objectClass','','univentionPolicyReference')]) modlist=[] @@ -1010,52 +924,6 @@ def _doit(arglist): out.append('E: Invalid Syntax: %s' % e) return out + ["OPERATION FAILED"] - if extraOC or extraAttributes: - if extraOC: - oc=lo.search(base=dn, scope='base', attr=['objectClass']) - - noc=[] - for i in range(len(oc[0][1]['objectClass'])): - noc.append(oc[0][1]['objectClass'][i]) - - for i in range(len(extraOC)): - if not extraOC[i] in noc: - noc.append(extraOC[i]) - - if noc != oc[0][1]['objectClass']: - extraAttributes.append(('objectClass',oc[0][1]['objectClass'],noc)) - if extraAttributes: - try: - lo.modify(dn,extraAttributes) - object_modified+=1 - except univention.admin.uexceptions.ldapError, msg: - out.append("ldap Error: %s"%msg) - - if customattribute_remove: - extraAttributes=[] - removed_attributes=[] - if hasattr(module, 'ldap_extra_objectclasses'): - for oc, pname, syntax, ldapMapping, deleteValues, deleteObjectClass in module.ldap_extra_objectclasses: - for index in range(0,len(customattribute_remove)): - if customattribute_remove[index][0] == module.property_descriptions[pname].short_description: - for tmp,val in lo.search(base=dn, attr=[ldapMapping]): - if val.has_key(ldapMapping): - for i in range(0, len(val[ldapMapping])): - if (not customattribute_remove[index][1]) or customattribute_remove[index][1] == val[ldapMapping][i]: - extraAttributes.append((ldapMapping,val[ldapMapping][i],'')) - removed_attributes.append(module.property_descriptions[pname].short_description) - else: - out.append("customattribute %s not set"%module.property_descriptions[pname].short_description) - removed_attributes.append(module.property_descriptions[pname].short_description) - - if extraAttributes: - lo.modify(dn,extraAttributes) - object_modified+=1 - - for n,v in customattribute_remove: - if not n in removed_attributes: - out.append("WARNING: customattribute %s not found"%n) - if policy_reference: if 'univentionPolicyReference' not in lo.get(dn,['objectClass'])['objectClass']: lo.modify(dn,[('objectClass','','univentionPolicyReference')]) -- 1.8.4.rc3 From 9116db8297cf2be389c4f61b0e1a46d8a3cece79 Mon Sep 17 00:00:00 2001 Message-Id: <9116db8297cf2be389c4f61b0e1a46d8a3cece79.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:06:24 +0100 Subject: [PATCH 05/33] Bug #33224: Convert array to streaming to stdout Organization: Univention GmbH, Bremen, Germany Stream output instead of collecting it first as a list of lines and then transferring them. This improves the responsibility with large outputs and simplifies debuggability, as previous output is no longer lost and a complete stack trace is printed. Throw exception in case of an error so simplify error handling: The CLI front-end process expects the exact string "OPERATION FAILED" in the output to terminate the process with an exit status unequal zero, which was sprinkled over a lot of different places. --- .../modules/univention/admincli/admin.py | 582 +++++++++++---------- .../univention-cli-server | 9 +- 2 files changed, 300 insertions(+), 291 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index f21610c..c983006 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -30,7 +30,8 @@ # /usr/share/common-licenses/AGPL-3; if not, see # . - +from textwrap import dedent +import sys import getopt import re import string @@ -54,93 +55,92 @@ univention.admin.modules.update() # update choices-lists which are defined in LDAP univention.admin.syntax.update_choices() -# usage information -def usage(): - out=[] - out.append('univention-directory-manager: command line interface for managing UCS') - out.append('copyright (c) 2001-@%@copyright_lastyear@%@ Univention GmbH, Germany') - out.append('') - out.append('Syntax:') - out.append(' univention-directory-manager module action [options]') - out.append(' univention-directory-manager [--help] [--version]') - out.append('') - out.append('actions:') - out.append(' %-32s %s' % ('create:', 'Create a new object')) - out.append(' %-32s %s' % ('modify:', 'Modify an existing object')) - out.append(' %-32s %s' % ('remove:', 'Remove an existing object')) - out.append(' %-32s %s' % ('list:', 'List objects')) - out.append(' %-32s %s' % ('move:', 'Move object in directory tree')) - out.append('') - out.append(' %-32s %s' % ('-h | --help | -?:', 'print this usage message')) - out.append(' %-32s %s' % ('--version:', 'print version information')) - out.append('') - out.append('general options:') - out.append(' --%-30s %s' % ('logfile', 'path and name of the logfile to be used')) - out.append('') - out.append('create options:') - out.append(' --%-30s %s' % ('binddn', 'bind DN')) - out.append(' --%-30s %s' % ('bindpwd', 'bind password')) - out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) - out.append(' --%-30s %s' % ('position', 'Set position in tree')) - out.append(' --%-30s %s' % ('set', 'Set variable to value, e.g. foo=bar')) - out.append(' --%-30s %s' % ('superordinate', 'Use superordinate module')) - out.append(' --%-30s %s' % ('option', 'Use only given module options')) - out.append(' --%-30s %s' % ('policy-reference', 'Reference to policy given by DN')) - out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) - out.append(' --%-30s ' % ('ignore_exists')) - out.append('') - out.append('modify options:') - out.append(' --%-30s %s' % ('binddn', 'bind DN')) - out.append(' --%-30s %s' % ('bindpwd', 'bind password')) - out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) - out.append(' --%-30s %s' % ('dn', 'Edit object with DN')) - out.append(' --%-30s %s' % ('set', 'Set variable to value, e.g. foo=bar')) - out.append(' --%-30s %s' % ('append', 'Append value to variable, e.g. foo=bar')) - out.append(' --%-30s %s' % ('remove', 'Remove value from variable, e.g. foo=bar')) - out.append(' --%-30s %s' % ('option', 'Use only given module options')) - out.append(' --%-30s %s' % ('append-option', 'Append the module options')) - out.append(' --%-30s %s' % ('policy-reference', 'Reference to policy given by DN')) - out.append(' --%-30s %s' % ('policy-dereference', 'Remove reference to policy given by DN')) - out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) - out.append('') - out.append('remove options:') - out.append(' --%-30s %s' % ('binddn', 'bind DN')) - out.append(' --%-30s %s' % ('bindpwd', 'bind password')) - out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) - out.append(' --%-30s %s' % ('dn', 'Remove object with DN')) - out.append(' --%-30s %s' % ('superordinate', 'Use superordinate module')) - out.append(' --%-30s %s' % ('filter', 'Lookup filter e.g. foo=bar')) - out.append(' --%-30s %s' % ('tls', '0 (no); 1 (try); 2 (must)')) - out.append(' --%-30s %s' % ('remove_referring', 'remove referring objects')) - out.append('') - out.append('list options:') - out.append(' --%-30s %s' % ('filter', 'Lookup filter e.g. foo=bar')) - out.append(' --%-30s %s' % ('policies', 'List policy-based settings:')) - out.append(' %-30s %s' % ('', '0:short, 1:long (with policy-DN)')) - out.append('') - out.append('move options:') - out.append(' --%-30s %s' % ('binddn', 'bind DN')) - out.append(' --%-30s %s' % ('bindpwd', 'bind password')) - out.append(' --%-30s %s' % ('bindpwdfile', 'file containing bind password')) - out.append(' --%-30s %s' % ('dn', 'Move object with DN')) - out.append(' --%-30s %s' % ('position', 'Move to position in tree')) - out.append('') - out.append('Description:') - out.append(' univention-directory-manager is a tool to handle the configuration for UCS') - out.append(' on command line level.') - out.append(' Use "univention-directory-manager modules" for a list of available modules.') - out.append('') - return out - -def version(): - o=[] - o.append('univention-directory-manager @%@package_version@%@') - return o - -def _print_property( module, action, name, output ): + +class Failed(Exception): + def __str__(self): + return "OPERATION FAILED" + + +def usage(out=sys.stdout): + print >> out, dedent("""\ + %(prog)s command line interface for managing UCS + copyright (c) 2001-%(year)s Univention GmbH, Germany + + Syntax: + %(prog)s module action [options] + %(prog)s [--help] [--version] + + actions: + create: Create a new object + modify: Modify an existing object + remove: Remove an existing object + list: List objects + move: Move object in directory tree + + -h | --help | -?: Print this usage message + --version: Print version information + + general options: + --logfile Path and name of the logfile to be used + --binddn Bind DN + --bindpwd Bind password + --bindpwdfile file containing bind password + --tls 0 (no); 1 (try); 2 (must) + + create options: + --position Set position in tree + --set Set variable to value, e.g. foo=bar + --superordinate Use superordinate module + --option Use only given module options + --policy-reference Reference to policy given by DN + --ignore_exists + + modify options: + --dn Edit object with DN + --set Set variable to value, e.g. foo=bar + --append Append value to variable, e.g. foo=bar + --remove Remove value from variable, e.g. foo=bar + --option Use only given module options + --append-option Append the module options + --policy-reference Reference to policy given by DN + --policy-dereference Remove reference to policy given by DN + + remove options: + --dn Remove object with DN + --superordinate Use superordinate module + --filter Lookup filter e.g. foo=bar + --remove_referring Remove referring objects + + list options: + --filter Lookup filter e.g. foo=bar + --policies List policy-based settings: + 0:short, 1:long (with policy-DN) + + move options: + --dn Move object with DN + --position Move to position in tree + + Description: + %(prog)s is a tool to handle the configuration for UCS + on command line level. + Use "%(prog)s modules" for a list of available modules. + """) % { + "prog": "univention-directory-manager", + "year": "@%@copyright_lastyear@%@", + } + + +def version(out=sys.stdout): + print >> out, 'univention-directory-manager @%@package_version@%@' + + +def _print_property(module, action, name, out=sys.stdout): property = module.property_descriptions.get( name ) if property is None: - output.append( 'E: unknown property %s of module %s' % ( name, univention.admin.modules.name( module ) ) ) + print >> out, 'E: unknown property %s of module %s' % ( + name, + univention.admin.modules.name(module), + ) return required = { @@ -181,32 +181,35 @@ def _print_property( module, action, name, output ): flags += ',' flags += '[]' if flags: - flags = '(' + flags + ')' + name += ' (%s)' % (flags,) + + print >> out, ' %-40s %s' % ( + name, + property.short_description + ) - output.append( ' %-40s %s' % ( name + ' ' + flags, property.short_description ) ) -def module_usage(information, action=''): - out=[] +def module_usage(information, action='', out=sys.stdout): for module, l in information.items(): properties, options = l if options: - out.append('') - out.append('%s options:' % module.module) + print >> out, '' + print >> out, '%s options:' % module.module for name, option in options.items(): - out.append(' %-32s %s' % (name, option.short_description)) + print >> out, ' %-32s %s' % (name, option.short_description) - out.append('') - out.append('%s variables:' % module.module) + print >> out, '' + print >> out, '%s variables:' % module.module if not hasattr(module,"layout"): continue for moduletab in module.layout: - out.append(' %s:' % (moduletab.label)) + print >> out, ' %s:' % (moduletab.label) for row in moduletab.layout: if isinstance( row, Group ): - out.append( ' %s' % row.label ) + print >> out, ' %s' % row.label for row in row.layout: if isinstance( row, basestring ): _print_property( module, action, row, out ) @@ -220,7 +223,6 @@ def module_usage(information, action=''): for item in row: _print_property( module, action, item, out ) - return out def module_information(module, identifies_only=0): information={module:[{},{}]} @@ -243,8 +245,7 @@ def _2utf8( text ): except: return text.decode( 'iso-8859-1' ) -def object_input(module, object, input, append=None, remove=None): - out=[] +def object_input(module, object, input, append=None, remove=None, out=sys.stdout): if append: for key, value in append.items(): if module.property_descriptions[key].syntax.name == 'file': @@ -256,7 +257,7 @@ def object_input(module, object, input, append=None, remove=None): object[key] = content fh.close() else: - out.append('WARNING: file not found: %s' % value) + print >> out, 'WARNING: file not found: %s' % value elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): for i in range(0,len(value)): @@ -272,7 +273,7 @@ def object_input(module, object, input, append=None, remove=None): if not object.has_key(key): object[key]=[] if val in object[key]: - out.append('WARNING: cannot append %s to %s, value exists'%(val,key)) + print >> out, 'WARNING: cannot append %s to %s, value exists' % (val, key) elif object[key] == [''] or object[key] == []: object[key]=[val] else: @@ -280,7 +281,7 @@ def object_input(module, object, input, append=None, remove=None): else: for val in value: if val in object[key]: - out.append('WARNING: cannot append %s to %s, value exists'%(val,key)) + print >> out, 'WARNING: cannot append %s to %s, value exists' % (val, key) elif object[key] == [''] or object[key] == []: object[key]=[val] else: @@ -289,7 +290,7 @@ def object_input(module, object, input, append=None, remove=None): tmp.append(val) object[key] = list(tmp) except univention.admin.uexceptions.valueInvalidSyntax, errmsg: - out.append('E: Invalid Syntax: %s' % str(errmsg)) + print >> out, 'E: Invalid Syntax: %s' % (errmsg,) if remove: for key, value in remove.items(): if univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): @@ -300,7 +301,7 @@ def object_input(module, object, input, append=None, remove=None): val=value[i].split(' ') else: val=[] - out.append('test_val=%s' % test_val) + print >> out, 'test_val=%s' % (test_val,) for j in test_val: if j and j.rstrip().lstrip(): val.append(j.rstrip().lstrip()) @@ -311,7 +312,7 @@ def object_input(module, object, input, append=None, remove=None): if val and val in object[key]: object[key].remove(val) else: - out.append("WARNING: cannot remove %s from %s, value does not exist"%(val,key)) + print >> out, "WARNING: cannot remove %s from %s, value does not exist" % (val, key) else: object[key]=[] @@ -326,7 +327,7 @@ def object_input(module, object, input, append=None, remove=None): if val in object[key]: object[key].remove(val) else: - out.append("WARNING: cannot remove %s from %s, value does not exist"%(val,key)) + print >> out, "WARNING: cannot remove %s from %s, value does not exist" % (val, key) if input: for key, value in input.items(): if module.property_descriptions[key].syntax.name == 'binaryfile': @@ -343,7 +344,7 @@ def object_input(module, object, input, append=None, remove=None): object[key]= content fh.close() else: - out.append('WARNING: file not found: %s' % value) + print >> out, 'WARNING: file not found: %s' % value elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): if type(value) == type([]): @@ -366,26 +367,24 @@ def object_input(module, object, input, append=None, remove=None): try: object[key]=value except univention.admin.uexceptions.ipOverridesNetwork, e: - out.append('WARNING: %s' % e.message) + print >> out, 'WARNING: %s' % e.message except univention.admin.uexceptions.valueMayNotChange, e: raise univention.admin.uexceptions.valueMayNotChange, "%s: %s"%(e.message, key) - return out -def list_available_modules(o=[]): - o.append("Available Modules are:") +def list_available_modules(out=sys.stdout): + print >> out, "Available Modules are:" avail_modules = [] for mod in univention.admin.modules.modules.keys(): avail_modules.append(mod) avail_modules.sort() for mod in avail_modules: - o.append(" %s"%mod) - return o + print >> out, " %s" % mod -def doit(arglist): - out=[] + +def doit(arglist, out=sys.stdout): try: - out=_doit(arglist) + _doit(arglist, out) except univention.admin.uexceptions.base, e: univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, traceback.format_exc()) @@ -406,26 +405,28 @@ def doit(arglist): msg[0] = '%s:' % msg[0].strip(':.') # append to the output - out.append(' '.join(msg)) - return out + ["OPERATION FAILED"] - return out + print >> out, ' '.join(msg) + print >> out, Failed() + except Failed as ex: + print >> out, ex -def _doit(arglist): - out=[] +def _doit(arglist, out=sys.stdout): # parse module and action + if set(arglist) & set(('-h', '--help', '-?')): + return usage(out=out) + + if '--version' in arglist: + return version(out=out) + if len(arglist) < 2: - return usage() + ["OPERATION FAILED"] + usage(out=out) + raise Failed() module_name=arglist[1] - if module_name in ['-h', '--help', '-?']: - return usage() - - if module_name == '--version': - return version() if module_name == 'modules': - return list_available_modules() + return list_available_modules(out=out) remove_referring=0 recursive=1 @@ -455,14 +456,13 @@ def _doit(arglist): try: opts, args=getopt.getopt(arglist[3:], '', longopts) except getopt.error, msg: - out.append(str(msg)) - return out + ["OPERATION FAILED"] + print >> out, msg + raise Failed() if not args == [] and type(args) == type([]): - msg = "WARNING: the following arguments are ignored:" - for argument in args: - msg = '%s "%s"' % (msg, argument) - out.append(msg) + print >> out, "WARNING: the following arguments are ignored: %s" % ( + " ".join(('"%s"' % _ for _ in args)), + ) position_dn='' dn='' @@ -503,8 +503,8 @@ def _doit(arglist): with open(val) as fp: bindpwd=fp.read().strip() except IOError as e: - out.append( 'E: could not read bindpwd from file (%s)' % str(e) ) - return out + ['OPERATION FAILED'] + print >> out, 'E: could not read bindpwd from file (%s)' % (e,) + raise Failed() elif opt == '--dn': dn = _2utf8( val ) elif opt == '--tls': @@ -527,7 +527,7 @@ def _doit(arglist): if logfile: univention.debug.init(logfile, 1, 0) else: - out.append("WARNING: no logfile specified") + print >> out, "WARNING: no logfile specified" configRegistry=univention.config_registry.ConfigRegistry() configRegistry.load() @@ -553,8 +553,8 @@ def _doit(arglist): lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, start_tls=tls, bindpw=bindpwd) except Exception, e: univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) - out.append('authentication error: %s' % str(e)) - return out + ["OPERATION FAILED"] + print >> out, 'authentication error: %s' % (e,) + raise Failed() policyOptions.extend(['-D', binddn, '-w', bindpwd]) ## FIXME not so nice else: @@ -568,12 +568,15 @@ def _doit(arglist): secretFileName='/etc/machine.secret' binddn=configRegistry['ldap/hostdn'] policyOptions.extend(['-D', binddn, '-y', secretFileName]) + else: + print >> out, "E: Missing LDAP credentials" + raise Failed() try: secretFile=open(secretFileName,'r') except IOError: - out.append('E: Permission denied, try --binddn and --bindpwd') - return out + ["OPERATION FAILED"] + print >> out, 'E: Permission denied, try --binddn and --bindpwd' + raise Failed() pwdLine=secretFile.readline() pwd=re.sub('\n','',pwdLine) @@ -581,8 +584,8 @@ def _doit(arglist): lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, bindpw=pwd, start_tls=tls) except Exception, e: univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) - out.append('authentication error: %s' % str(e)) - return out + ["OPERATION FAILED"] + print >> out, 'authentication error: %s' % (e,) + raise Failed() if not position_dn and superordinate_dn: position_dn=superordinate_dn @@ -593,20 +596,22 @@ def _doit(arglist): position=univention.admin.uldap.position(baseDN) position.setDn(position_dn) except univention.admin.uexceptions.noObject: - out.append('E: Invalid position') - return out + ["OPERATION FAILED"] + print >> out, 'E: Invalid position' + raise Failed() try: module=univention.admin.modules.get(module_name) except: - out.append("failed to get module %s."%module_name) - out.append("") - return list_available_modules(out) + ["OPERATION FAILED"] + print >> out, "failed to get module %s." % module_name + print >> out, "" + list_available_modules(out) + raise Failed() if not module: - out.append("unknown module %s." % module_name) - out.append("") - return list_available_modules(out) + ["OPERATION FAILED"] + print >> out, "unknown module %s." % module_name + print >> out, "" + list_available_modules(out) + raise Failed() # initialise modules univention.admin.modules.init(lo,position,module) @@ -617,21 +622,21 @@ def _doit(arglist): try: superordinate=univention.admin.objects.get(univention.admin.modules.superordinate(module), co, lo, '', dn=superordinate_dn) except univention.admin.uexceptions.insufficientInformation, e: - out.append('Insufficient Information: %s' % str(e)) - return out + ["OPERATION FAILED"] + print >> out, 'Insufficient Information: %s' % (e,) + raise Failed() else: superordinate=None if len(arglist) == 2: - out = usage() + module_usage(information) - return out + ["OPERATION FAILED"] + module_usage(information, out=out) + raise Failed() action=arglist[2] if len(arglist) == 3 and action != 'list': - out = usage() + module_usage(information, action) - return out + ["OPERATION FAILED"] + module_usage(information, action, out=out) + raise Failed() for opt, val in opts: @@ -655,7 +660,7 @@ def _doit(arglist): was_set=1 if not was_set: - out.append("WARNING: No attribute with name '%s' in this module, value not set."%name) + print >> out, "WARNING: No attribute with name '%s' in this module, value not set." % name elif opt == '--append': pos=val.find('=') name=val[:pos] @@ -673,7 +678,7 @@ def _doit(arglist): append[name]=value was_set=1 if not was_set: - out.append("WARNING: No attribute with name %s in this module, value not appended."%name) + print >> out, "WARNING: No attribute with name %s in this module, value not appended." % name elif opt == '--remove': pos=val.find('=') @@ -696,7 +701,7 @@ def _doit(arglist): remove[name]=value was_set=1 if not was_set: - out.append("WARNING: No attribute with name %s in this module, value not removed."%name) + print >> out, "WARNING: No attribute with name %s in this module, value not removed." % name elif opt == '--remove_referring': remove_referring=1 elif opt == '--recursive': @@ -704,49 +709,48 @@ def _doit(arglist): if action in ['modify','edit','create','new']: - if policy_reference: for el in policy_reference: - oc = lo.get(el,['objectClass']) + oc = lo.get(el, ['objectClass']) if not oc: - out.append("Object to be referenced does not exist:"+el) - return out + ["OPERATION FAILED"] + print >> out, "Object to be referenced does not exist: %s" % (el,) + raise Failed() if not 'univentionPolicy' in oc['objectClass']: - out.append("Object to be referenced is no valid Policy:"+el) - return out + ["OPERATION FAILED"] + print >> out, "Object to be referenced is no valid Policy: %s" % (el,) + raise Failed() #+++# ACTION CREATE #+++# if action == 'create' or action == 'new': if hasattr(module,'operations') and module.operations: if not 'add' in module.operations: - out.append('Create %s not allowed' % module_name) - return out + ["OPERATION FAILED"] + print >> out, 'Create %s not allowed' % module_name + raise Failed() try: object=module.object(co, lo, position=position, superordinate=superordinate) except univention.admin.uexceptions.insufficientInformation: - out.append('E: Insufficient information') - out.append('Superordinate object is missing') - return out + ["OPERATION FAILED"] + print >> out, 'E: Insufficient information' + print >> out, 'Superordinate object is missing' + raise Failed() if parsed_options: object.options=parsed_options object.open() if hasattr(object,' open_warning') and object.open_warning: - out.append('WAR NING:%s'%object.open_warning) + print >> out, 'WARNING:%s' % object.open_warning exists=0 try: - out.extend(object_input(module, object, input, append=append)) + object_input(module, object, input, append=append, out=out) except univention.admin.uexceptions.nextFreeIp: if not ignore_exists: - out.append('E: No free IP address found') - return out + ['OPERATION FAILED'] + print >> out, 'E: No free IP address found' + raise Failed() except univention.admin.uexceptions.valueInvalidSyntax, err: - out.append('E: Invalid Syntax: %s' % err) - return out + ["OPERATION FAILED"] + print >> out, 'E: Invalid Syntax: %s' % err + raise Failed() except Exception, err: - out.append('E: Option %s is not valid' %err) - return out + ['OPERATION FAILED'] + print >> out, 'E: Option %s is not valid' % err + raise Failed() exists=0 exists_msg=None @@ -755,67 +759,67 @@ def _doit(arglist): except univention.admin.uexceptions.objectExists, dn: exists_msg = dn if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.uidAlreadyUsed, user: exists_msg = '(uid) %s' % user if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.groupNameAlreadyUsed, group: exists_msg = '(group) %s' % group if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.dhcpServerAlreadyUsed, name: exists_msg = '(dhcpserver) %s' % name if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.macAlreadyUsed, mac: exists_msg = '(mac) %s' % mac if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.noLock, e: exists_msg = '(nolock) %s' % str(e) if not ignore_exists: - out.append('E: Object exists: %s' % exists_msg) - return out + ["OPERATION FAILED"] + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() else: exists=1 except univention.admin.uexceptions.invalidDhcpEntry: - out.append('E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.') - return out + ["OPERATION FAILED"] + print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' + raise Failed() except univention.admin.uexceptions.invalidOptions, e: if not ignore_exists: - out.append('E: invalid Options: %s' % e) - return out + ["OPERATION FAILED"] + print >> out, 'E: invalid Options: %s' % e + raise Failed() except univention.admin.uexceptions.insufficientInformation: - out.append('E: Insufficient information') - out.append('The following parameters are missing:') + print >> out, 'E: Insufficient information' + print >> out, 'The following parameters are missing:' for i in module.property_descriptions: property=module.property_descriptions.get(i) if property.required: if not object.has_key(i) or (not object[i] or (type(object[i]) == list and object[i]==[''])): - out.append(i) - return out + ["OPERATION FAILED"] + print >> out, i + raise Failed() except univention.admin.uexceptions.noObject, e: - out.append('E: object not found: %s' % e) - return out + ["OPERATION FAILED"] + print >> out, 'E: object not found: %s' % e + raise Failed() except univention.admin.uexceptions.circularGroupDependency, e: - out.append('E: circular group dependency detected: %s' % e) - return out + ["OPERATION FAILED"] + print >> out, 'E: circular group dependency detected: %s' % e + raise Failed() if policy_reference: lo.modify(dn,[('objectClass','','univentionPolicyReference')]) @@ -826,44 +830,44 @@ def _doit(arglist): if exists == 1: if exists_msg: - out.append('Object exists: %s' % exists_msg) + print >> out, 'Object exists: %s' % exists_msg else: - out.append('Object exists') + print >> out, 'Object exists' else: if not dn: dn=object.dn - out.append('Object created: %s' % _2utf8( dn ) ) + print >> out, 'Object created: %s' % _2utf8(dn) #+++# ACTION MODIFY #+++# elif action == 'modify' or action == 'edit' or action == 'move': if not dn: - out.append('E: DN is missing') - return out + ["OPERATION FAILED"] + print >> out, 'E: DN is missing' + raise Failed() object_modified = 0 if hasattr(module,'operations') and module.operations: if not 'edit' in module.operations: - out.append('Modify %s not allowed' % module_name) - return out + ["OPERATION FAILED"] + print >> out, 'Modify %s not allowed' % module_name + raise Failed() try: object=univention.admin.objects.get(module, co, lo, position='', dn=dn) except univention.admin.uexceptions.noObject: - out.append('E: object not found') - return out + ["OPERATION FAILED"] + print >> out, 'E: object not found' + raise Failed() object.open() if hasattr(object,'open_warning') and object.open_warning: - out.append('WARNING:%s'%object.open_warning) + print >> out, 'WARNING:%s' % object.open_warning if action == 'move': if hasattr(module,'operations') and module.operations: if not 'move' in module.operations: - out.append('Move %s not allowed' % module_name) - return out + ["OPERATION FAILED"] + print >> out, 'Move %s not allowed' % module_name + raise Failed() if not position_dn: - out.append("need new position for moving object") + print >> out, "need new position for moving object" else: res = '' try: # check if global-position exists @@ -871,28 +875,28 @@ def _doit(arglist): except: pass if not res: - out.append("position does not exsist: %s"%position_dn) - return out + ["OPERATION FAILED"] + print >> out, "position does not exsist: %s" % position_dn + raise Failed() rdn = dn[:string.find(dn,',')] newdn="%s,%s" % (rdn,position_dn) try: object.move(newdn) object_modified+=1 except univention.admin.uexceptions.noObject: - out.append('E: object not found') - return out + ["OPERATION FAILED"] + print >> out, 'E: object not found' + raise Failed() except univention.admin.uexceptions.ldapError, msg: - out.append("ldap Error: %s"%msg) - return out + ["OPERATION FAILED"] + print >> out, "ldap Error: %s" % msg + raise Failed() except univention.admin.uexceptions.nextFreeIp: - out.append('E: No free IP address found') - return out + ['OPERATION FAILED'] + print >> out, 'E: No free IP address found' + raise Failed() except univention.admin.uexceptions.valueInvalidSyntax, err: - out.append('E: Invalid Syntax: %s' % err) - return out + ["OPERATION FAILED"] + print >> out, 'E: Invalid Syntax: %s' % err + raise Failed() except univention.admin.uexceptions.invalidOperation, msg: - out.append(str(msg)) - return out + ["OPERATION FAILED"] + print >> out, str(msg) + raise Failed() else: # modify @@ -903,26 +907,26 @@ def _doit(arglist): for option in parsed_append_options: object.options.append(option) try: - out.extend(object_input(module, object, input, append, remove)) + object_input(module, object, input, append, remove, out=out) except univention.admin.uexceptions.valueMayNotChange,e: - out.append(unicode(e[0])) - return out + ["OPERATION FAILED"] + print >> out, unicode(e[0]) + raise Failed() if object.hasChanged(input.keys()) or object.hasChanged(append.keys()) or object.hasChanged(remove.keys()) or parsed_append_options or parsed_options: try: dn=object.modify() object_modified+=1 except univention.admin.uexceptions.noObject: - out.append('E: object not found') - return out + ["OPERATION FAILED"] + print >> out, 'E: object not found' + raise Failed() except univention.admin.uexceptions.invalidDhcpEntry: - out.append('E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.') - return out + ["OPERATION FAILED"] + print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' + raise Failed() except univention.admin.uexceptions.circularGroupDependency, e: - out.append('E: circular group dependency detected: %s' % e) - return out + ["OPERATION FAILED"] + print >> out, 'E: circular group dependency detected: %s' % e + raise Failed() except univention.admin.uexceptions.valueInvalidSyntax, e: - out.append('E: Invalid Syntax: %s' % e) - return out + ["OPERATION FAILED"] + print >> out, 'E: Invalid Syntax: %s' % e + raise Failed() if policy_reference: if 'univentionPolicyReference' not in lo.get(dn,['objectClass'])['objectClass']: @@ -934,7 +938,7 @@ def _doit(arglist): upr['univentionPolicyReference'] = [] for el in policy_reference: if val in upr['univentionPolicyReference']: - out.append('WARNING: cannot append %s to univentionPolicyReference, value exists' % val) + print >> out, 'WARNING: cannot append %s to univentionPolicyReference, value exists' % val else: modlist.append(('univentionPolicyReference','',el)) if modlist: @@ -949,16 +953,16 @@ def _doit(arglist): object_modified+=1 if object_modified > 0: - out.append( 'Object modified: %s'% _2utf8( dn ) ) + print >> out, 'Object modified: %s' % _2utf8(dn) else: - out.append( 'No modification: %s'% _2utf8( dn ) ) + print >> out, 'No modification: %s' % _2utf8(dn) elif action == 'remove' or action == 'delete': if hasattr(module,'operations') and module.operations: if not 'remove' in module.operations: - out.append('Remove %s not allowed' % module_name) - return out + ["OPERATION FAILED"] + print >> out, 'Remove %s not allowed' % module_name + raise Failed() try: if dn and filter: @@ -968,15 +972,15 @@ def _doit(arglist): elif filter: object=univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter, required=1, unique=1)[0] else: - out.append('E: dn or filter needed') - return out + ["OPERATION FAILED"] + print >> out, 'E: dn or filter needed' + raise Failed() except (univention.admin.uexceptions.noObject, IndexError): - out.append('E: object not found') - return out + ["OPERATION FAILED"] + print >> out, 'E: object not found' + raise Failed() object.open() if hasattr(object,'open_warning') and object.open_warning: - out.append('WARNING:%s'%object.open_warning) + print >> out, 'WARNING:%s' % object.open_warning if remove_referring and univention.admin.objects.wantsCleanup(object): univention.admin.objects.performCleanup(object) @@ -985,67 +989,69 @@ def _doit(arglist): try: object.remove(recursive) except univention.admin.uexceptions.ldapError,msg: - out.append(str(msg)) - return out + ["OPERATION FAILED"] + print >> out, str(msg) + raise Failed() else: try: object.remove() except univention.admin.uexceptions.primaryGroupUsed: - out.append('E: object in use') - return out + ["OPERATION FAILED"] - out.append( 'Object removed: %s'% _2utf8( dn ) ) + print >> out, 'E: object in use' + raise Failed() + print >> out, 'Object removed: %s' % _2utf8(dn) elif action == 'list' or action == 'lookup': if hasattr(module,'operations') and module.operations: if not 'search' in module.operations: - out.append('Search %s not allowed' % module_name) - return out + ["OPERATION FAILED"] + print >> out, 'Search %s not allowed' % module_name + raise Failed() - out.append( _2utf8( filter ) ) + print >> out, _2utf8(filter) try: for object in univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter): - out.append( 'DN: %s' % _2utf8( univention.admin.objects.dn (object ) ) ) + print >> out, 'DN: %s' % _2utf8(univention.admin.objects.dn(object)) if (hasattr(module,'virtual') and not module.virtual) or not hasattr(module,'virtual'): object.open() if hasattr(object,'open_warning') and object.open_warning: - out.append('WARNING: %s'%object.open_warning) + print >> out, 'WARNING: %s' % object.open_warning for key, value in object.items(): s=module.property_descriptions[key].syntax if module.property_descriptions[key].multivalue: for v in value: if s.tostring(v): - out.append(' %s: %s' % ( _2utf8( key ), _2utf8( s.tostring( v ) ) ) ) + print >> out, ' %s: %s' % (_2utf8(key), _2utf8(s.tostring(v))) else: - out.append(' %s: %s' % ( _2utf8( key ), None ) ) + print >> out, ' %s: %s' % (_2utf8(key), None) else: if s.tostring(value): - out.append(' %s: %s' % ( _2utf8( key ), _2utf8( s.tostring( value ) ) ) ) + print >> out, ' %s: %s' % (_2utf8(key), _2utf8(s.tostring(value))) else: - out.append(' %s: %s' % ( _2utf8( key ), None ) ) + print >> out, ' %s: %s' % (_2utf8(key), None) if 'univentionPolicyReference' in lo.get(univention.admin.objects.dn(object),['objectClass'])['objectClass']: references = lo.get( _2utf8(univention.admin.objects.dn( object ) ), [ 'univentionPolicyReference' ] ) if references: for el in references['univentionPolicyReference']: - out.append(' %s: %s' % ( 'univentionPolicyReference', - _2utf8( s.tostring( el ) ) ) ) + print >> out, ' %s: %s' % ( + 'univentionPolicyReference', + _2utf8(s.tostring(el)) + ) if list_policies: utf8_objectdn = _2utf8( univention.admin.objects.dn( object ) ) p1 = subprocess.Popen(['univention_policy_result'] + policyOptions + [utf8_objectdn], stdout=subprocess.PIPE) policyResults = p1.communicate()[0].split('\n') - out.append(" Policy-based Settings:") + print >> out, " Policy-based Settings:" policy='' value=[] client={} for line in policyResults: if not (line.strip() == "" or line.strip()[:4]=="DN: " or line.strip()[:7]=="POLICY "): - out.append(" %s"%line.strip()) + print >> out, " %s" % line.strip() if policies_with_DN: clsplit=string.split(line.strip(), ': ') if clsplit[0] == 'Policy': @@ -1067,7 +1073,7 @@ def _doit(arglist): client[attribute]=[policy, value] value=[] - out.append('') + print >> out, '' if module_name == 'dhcp/host': subnet_module=univention.admin.modules.get('dhcp/subnet') @@ -1077,13 +1083,13 @@ def _doit(arglist): utf8_subnet_dn = _2utf8( subnet.dn ) p1 = subprocess.Popen(['univention_policy_result'] + policyOptions + [utf8_subnet_dn], stdout=subprocess.PIPE) policyResults = p1.communicate()[0].split('\n') - out.append(" Subnet-based Settings:") + print >> out, " Subnet-based Settings:" ddict={} policy='' value=[] for line in policyResults: if not (line.strip() == "" or line.strip()[:4]=="DN: " or line.strip()[:7]=="POLICY "): - out.append(" %s"%line.strip()) + print >> out, " %s" % line.strip() if policies_with_DN: subsplit=string.split(line.strip(), ': ') if subsplit[0] == 'Policy': @@ -1101,13 +1107,12 @@ def _doit(arglist): ddict[subsplit[0]] = [] ddict[subsplit[0]].append(subsplit[1]) - out.append('') + print >> out, '' if policies_with_DN: ddict[attribute]=[policy, value] - value=[] - out.append(" Merged Settings:") + print >> out, " Merged Settings:" for key in ddict.keys(): if not client.has_key(key): @@ -1115,27 +1120,24 @@ def _doit(arglist): if policies_with_DN: for key in client.keys(): - out.append(" Policy: "+client[key][0]) - out.append(" Attribute: "+key) + print >> out, " Policy: " + client[key][0] + print >> out, " Attribute: " + key for i in range(0, len(client[key][1])): - out.append(" Value: "+client[key][1][i]) + print >> out, " Value: " + client[key][1][i] else: for key in client.keys(): for i in range(0, len(client[key])): - out.append(" %s=%s" % (key, client[key][i])) - out.append('') + print >> out, " %s=%s" % (key, client[key][i]) + print >> out, '' - out.append('') + print >> out, '' except univention.admin.uexceptions.ldapError, errmsg: - out.append('%s' %str(errmsg)) - return out + ["OPERATION FAILED"] + print >> out, '%s' % (errmsg,) + raise Failed() except univention.admin.uexceptions.valueInvalidSyntax, errmsg: - out.append('%s' %str(errmsg.message)) - return out + ["OPERATION FAILED"] + print >> out, '%s' % (errmsg.message,) + raise Failed() else: - out.append("Unknown or no action defined") - out.append('') - usage() - return out + ["OPERATION FAILED"] - - return out # nearly the only successfull return + print >> out, "Unknown or no action defined" + print >> out, '' + raise Failed() diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server index 908bd36..df4af1f 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server @@ -48,6 +48,10 @@ try: licenseImportError = False except ImportError: licenseImportError = True +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO logfile = '' @@ -219,7 +223,10 @@ def doit(sarglist, conn): if cmdfile in ('univention-admin', 'univention-directory-manager', 'udm'): ud.debug(ud.ADMIN, ud.PROCESS, 'daemon [%s] [%s] Calling univention-directory-manager' % (os.getppid(), os.getpid())) ud.debug(ud.ADMIN, ud.ALL, 'daemon [%s] [%s] arglist: %s' % (os.getppid(), os.getpid(), arglist)) - output = univention.admincli.admin.doit(arglist) + stream = StringIO() + univention.admincli.admin.doit(arglist, stream) + value = stream.getvalue() + output = value.splitlines() elif cmdfile == 'univention-passwd': ud.debug(ud.ADMIN, ud.PROCESS, 'daemon [%s] [%s] Calling univention-passwd' % (os.getppid(), os.getpid())) ud.debug(ud.ADMIN, ud.ALL, 'daemon [%s] [%s] arglist: %s' % (os.getppid(), os.getpid(), arglist)) -- 1.8.4.rc3 From 5f914eeabf90f35fb90e43357c554d8e3665a0c9 Mon Sep 17 00:00:00 2001 Message-Id: <5f914eeabf90f35fb90e43357c554d8e3665a0c9.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 06/33] Bug #33224: Make module callable Organization: Univention GmbH, Bremen, Germany Add __main__ handling to enable calling the server directly for easier debugging. --- .../modules/univention/admincli/admin.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index c983006..8539f4c 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -1141,3 +1141,8 @@ def _doit(arglist, out=sys.stdout): print >> out, "Unknown or no action defined" print >> out, '' raise Failed() + + +if __name__ == '__main__': + import sys + doit(sys.argv, sys.stdout) -- 1.8.4.rc3 From ee7ed2c287512c9dc744999e3df5b98313badec4 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 07/33] Bug #33224: Strip both sides Organization: Univention GmbH, Bremen, Germany Simplify .rstrip().lstrip() to .strip(). --- .../modules/univention/admincli/admin.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 8539f4c..18b478c 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -267,8 +267,9 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: val=[] for j in test_val: - if j and j.rstrip().lstrip(): - val.append(j.rstrip().lstrip()) + j = j.strip() + if j: + val.append(j) if not object.has_key(key): object[key]=[] @@ -303,8 +304,9 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout val=[] print >> out, 'test_val=%s' % (test_val,) for j in test_val: - if j and j.rstrip().lstrip(): - val.append(j.rstrip().lstrip()) + j = j.strip() + if j: + val.append(j) for j in range(0,len(val)): val[j]='"%s"' % val[j] @@ -355,8 +357,9 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: val=[] for j in test_val: - if j and j.rstrip().lstrip(): - val.append(j.rstrip().lstrip()) + j = j.strip() + if j: + val.append(j) else: val=value.split(' ') if module.property_descriptions[key].multivalue: -- 1.8.4.rc3 From 2e2659c89cf1f4d91ae5abbce01e8400efcfbf71 Mon Sep 17 00:00:00 2001 Message-Id: <2e2659c89cf1f4d91ae5abbce01e8400efcfbf71.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:00:46 +0100 Subject: [PATCH 08/33] Bug #33224: Simplify buffer concatenation Organization: Univention GmbH, Bremen, Germany --- .../univention-cli-server | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server index df4af1f..8df48ce 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server @@ -60,15 +60,10 @@ class MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): ud.debug(ud.ADMIN, ud.INFO, 'daemon [%s] new connection [%s]' % (os.getppid(), os.getpid())) sarglist = '' - while True: + while not sarglist.endswith('\0'): buf = self.request.recv(1024) - if buf[-1] == '\0': - buf = buf[:-1] - sarglist += buf - break - else: - sarglist += buf - doit(sarglist, self.request) + sarglist += buf + doit(sarglist.rstrip('\0'), self.request) ud.debug(ud.ADMIN, ud.INFO, 'daemon [%s] connection closed [%s]' % (os.getppid(), os.getpid())) def finish(self): -- 1.8.4.rc3 From 2eb110b61f43f0aac1171b6b7d64da89eee9033a Mon Sep 17 00:00:00 2001 Message-Id: <2eb110b61f43f0aac1171b6b7d64da89eee9033a.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:03:42 +0100 Subject: [PATCH 09/33] Bug #33224: Convert UCR type early Organization: Univention GmbH, Bremen, Germany This moves any trace-back closer to the position causing a problem. --- .../modules/univention/admincli/admin.py | 9 +++------ .../univention-directory-manager-modules/univention-cli-server | 10 +++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 18b478c..943db3b 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -542,13 +542,10 @@ def _doit(arglist, out=sys.stdout): baseDN=configRegistry['ldap/base'] - if configRegistry.has_key('directory/manager/cmd/debug/level'): - debug_level=configRegistry['directory/manager/cmd/debug/level'] - else: - debug_level=0 + debug_level = int(configRegistry.get('directory/manager/cmd/debug/level', 0)) - univention.debug.set_level(univention.debug.LDAP, int(debug_level)) - univention.debug.set_level(univention.debug.ADMIN, int(debug_level)) + univention.debug.set_level(univention.debug.LDAP, debug_level) + univention.debug.set_level(univention.debug.ADMIN, debug_level) if binddn and bindpwd: univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, "using %s account" % binddn) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server index 8df48ce..3f7a55b 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server @@ -114,8 +114,8 @@ def server_main(): configRegistry = ConfigRegistry() configRegistry.load() - debug_level = configRegistry.get('directory/manager/cmd/debug/level', 1) - ud.set_level(ud.ADMIN, int(debug_level)) + debug_level = int(configRegistry.get('directory/manager/cmd/debug/level', 1)) + ud.set_level(ud.ADMIN, debug_level) ud.debug(ud.ADMIN, ud.INFO, 'daemon [%s] forked to background' % os.getpid()) try: @@ -128,9 +128,9 @@ def server_main(): else: print >> sys.stderr, 'E: socket directory exists (%s)' % socket_dir - timeout = configRegistry.get('directory/manager/cmd/timeout') + timeout = float(configRegistry.get('directory/manager/cmd/timeout')) if timeout: - if int(timeout) > 2147483647: + if timeout > 2147483647: timeout = 2147483647 else: timeout = 300 @@ -154,7 +154,7 @@ def server_main(): try: while True: - rlist, _wlist, _xlist = select([sock], [], [], float(timeout)) + rlist, _wlist, _xlist = select([sock], [], [], timeout) for handler in rlist: handler.handle_request() if not rlist: -- 1.8.4.rc3 From 263262408fd8621b22861325cb5f64cefd263224 Mon Sep 17 00:00:00 2001 Message-Id: <263262408fd8621b22861325cb5f64cefd263224.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:08:22 +0100 Subject: [PATCH 10/33] Bug #33224: Use path concatenation Organization: Univention GmbH, Bremen, Germany Use os.path.join() instead of simple string concatenations. This shows the intent more clearly. --- .../univention-directory-manager-modules/univention-cli-server | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server index 3f7a55b..e59f515 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server @@ -91,7 +91,7 @@ def server_main(): socket_dir = '/tmp/admincli_%s/' % os.getuid() socket_filename = 'sock' - socket_path = (socket_dir+socket_filename) + socket_path = os.path.join(socket_dir, socket_filename) ud.init(logfile, ud.FLUSH, ud.NO_FUNCTION) -- 1.8.4.rc3 From e4a464e52fd5f0f532502cb583e139df166d91bd Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 11/33] Bug #33224: Just read the whole file Organization: Univention GmbH, Bremen, Germany Use .read() instead of reading line-by-line and building a concatenation by hand. --- .../modules/univention/admincli/admin.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 943db3b..e9a8702 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -250,12 +250,8 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout for key, value in append.items(): if module.property_descriptions[key].syntax.name == 'file': if os.path.exists(value): - fh = open(value, 'r') - content='' - for line in fh.readlines(): - content += line - object[key] = content - fh.close() + with open(value, 'r') as fh: + object[key] = fh.read() else: print >> out, 'WARNING: file not found: %s' % value -- 1.8.4.rc3 From dea7fac40dfa07bd18bfd7586c00572ff7e2da68 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 12/33] Bug #33224: Use instanceof() Organization: Univention GmbH, Bremen, Germany Replace fragile type() checking with using instanceof() to handle lists and unicode strings. --- .../modules/univention/admincli/admin.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index e9a8702..65b415b 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -315,10 +315,10 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout object[key]=[] else: - if type(object[key]) is str: + if isinstance(object[key], basestring): object[key] = [ object[key] ] vallist = value - if type(value) is str: + if isinstance(value, basestring): vallist = [ value ] for val in vallist: @@ -345,7 +345,7 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout print >> out, 'WARNING: file not found: %s' % value elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): - if type(value) == type([]): + if isinstance(value, list): for i in range(0,len(value)): test_val=value[i].split('"') if test_val[0] and test_val[0] == value[i]: @@ -458,7 +458,7 @@ def _doit(arglist, out=sys.stdout): print >> out, msg raise Failed() - if not args == [] and type(args) == type([]): + if args and isinstance(args, list): print >> out, "WARNING: the following arguments are ignored: %s" % ( " ".join(('"%s"' % _ for _ in args)), ) @@ -807,7 +807,7 @@ def _doit(arglist, out=sys.stdout): for i in module.property_descriptions: property=module.property_descriptions.get(i) if property.required: - if not object.has_key(i) or (not object[i] or (type(object[i]) == list and object[i]==[''])): + if not object.has_key(i) or (not object[i] or (isinstance(object[i], list) and object[i]==[''])): print >> out, i raise Failed() except univention.admin.uexceptions.noObject, e: -- 1.8.4.rc3 From 84ac60a24e12973d07ee02ac6fc75c3f23ea874f Mon Sep 17 00:00:00 2001 Message-Id: <84ac60a24e12973d07ee02ac6fc75c3f23ea874f.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 13/33] Bug #33224: Catch specific exception Organization: Univention GmbH, Bremen, Germany Only handle UnicodeErrors. --- .../modules/univention/admincli/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 65b415b..7c2ecc0 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -242,7 +242,7 @@ def module_information(module, identifies_only=0): def _2utf8( text ): try: return text.encode( 'utf-8' ) - except: + except UnicodeError: return text.decode( 'iso-8859-1' ) def object_input(module, object, input, append=None, remove=None, out=sys.stdout): -- 1.8.4.rc3 From aca0007ee550fb6c5352c5853775d17e5f76a0f1 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 14/33] Bug #33224: Just use .keys() directly Organization: Univention GmbH, Bremen, Germany Use .keys() directly instead of iterating through all keys and adding them to a list by hand. --- .../modules/univention/admincli/admin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 7c2ecc0..ed30e16 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -373,9 +373,7 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout def list_available_modules(out=sys.stdout): print >> out, "Available Modules are:" - avail_modules = [] - for mod in univention.admin.modules.modules.keys(): - avail_modules.append(mod) + avail_modules = univention.admin.modules.modules.keys() avail_modules.sort() for mod in avail_modules: print >> out, " %s" % mod -- 1.8.4.rc3 From e7e4fb867138c92957953f8a1d07194fcc3bd662 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 15/33] Bug #33224: Use getattr() Organization: Univention GmbH, Bremen, Germany Use getattr() instead of hasttr() following a seconds check if the attribute is set. --- .../modules/univention/admincli/admin.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index ed30e16..10d9bf2 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -226,7 +226,7 @@ def module_usage(information, action='', out=sys.stdout): def module_information(module, identifies_only=0): information={module:[{},{}]} - if 'superordinate' in dir(module) and module.superordinate: + if getattr(module, 'superordinate', None): superordinate=univention.admin.modules.get(module.superordinate) information.update(module_information(superordinate, identifies_only=1)) @@ -715,7 +715,7 @@ def _doit(arglist, out=sys.stdout): #+++# ACTION CREATE #+++# if action == 'create' or action == 'new': - if hasattr(module,'operations') and module.operations: + if getattr(module, 'operations', None): if not 'add' in module.operations: print >> out, 'Create %s not allowed' % module_name raise Failed() @@ -730,7 +730,7 @@ def _doit(arglist, out=sys.stdout): object.options=parsed_options object.open() - if hasattr(object,' open_warning') and object.open_warning: + if getattr(object, 'open_warning', None): print >> out, 'WARNING:%s' % object.open_warning exists=0 try: @@ -840,7 +840,7 @@ def _doit(arglist, out=sys.stdout): object_modified = 0 - if hasattr(module,'operations') and module.operations: + if getattr(module, 'operations', None): if not 'edit' in module.operations: print >> out, 'Modify %s not allowed' % module_name raise Failed() @@ -852,11 +852,11 @@ def _doit(arglist, out=sys.stdout): raise Failed() object.open() - if hasattr(object,'open_warning') and object.open_warning: + if getattr(object, 'open_warning', None): print >> out, 'WARNING:%s' % object.open_warning if action == 'move': - if hasattr(module,'operations') and module.operations: + if getattr(module, 'operations', None): if not 'move' in module.operations: print >> out, 'Move %s not allowed' % module_name raise Failed() @@ -953,7 +953,7 @@ def _doit(arglist, out=sys.stdout): elif action == 'remove' or action == 'delete': - if hasattr(module,'operations') and module.operations: + if getattr(module, 'operations', None): if not 'remove' in module.operations: print >> out, 'Remove %s not allowed' % module_name raise Failed() @@ -973,7 +973,7 @@ def _doit(arglist, out=sys.stdout): raise Failed() object.open() - if hasattr(object,'open_warning') and object.open_warning: + if getattr(object, 'open_warning', None): print >> out, 'WARNING:%s' % object.open_warning if remove_referring and univention.admin.objects.wantsCleanup(object): @@ -995,7 +995,7 @@ def _doit(arglist, out=sys.stdout): elif action == 'list' or action == 'lookup': - if hasattr(module,'operations') and module.operations: + if getattr(module, 'operations', None): if not 'search' in module.operations: print >> out, 'Search %s not allowed' % module_name raise Failed() @@ -1006,9 +1006,9 @@ def _doit(arglist, out=sys.stdout): for object in univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter): print >> out, 'DN: %s' % _2utf8(univention.admin.objects.dn(object)) - if (hasattr(module,'virtual') and not module.virtual) or not hasattr(module,'virtual'): + if not getattr(module, 'virtual', None): object.open() - if hasattr(object,'open_warning') and object.open_warning: + if getattr(object, 'open_warning', None): print >> out, 'WARNING: %s' % object.open_warning for key, value in object.items(): s=module.property_descriptions[key].syntax -- 1.8.4.rc3 From 3ac7f7ea8b3ebb18e263c5251b6e558b9fb97924 Mon Sep 17 00:00:00 2001 Message-Id: <3ac7f7ea8b3ebb18e263c5251b6e558b9fb97924.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 16/33] Bug #33224: Replace .has_key() Organization: Univention GmbH, Bremen, Germany Use dict.get(key) or "key in dict" instead of deprecated dist.has_key(). --- .../modules/univention/admincli/admin.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 10d9bf2..8a9f3fa 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -529,7 +529,7 @@ def _doit(arglist, out=sys.stdout): configRegistry=univention.config_registry.ConfigRegistry() configRegistry.load() - if configRegistry.has_key('ldap/master') and configRegistry['ldap/master']: + if configRegistry.get('ldap/master'): co=univention.admin.config.config(configRegistry['ldap/master']) else: co=univention.admin.config.config() @@ -641,7 +641,7 @@ def _doit(arglist, out=sys.stdout): was_set=0 for mod, (properties,options) in information.items(): - if properties.has_key(name): + if name in properties: if properties[name].multivalue: if not input.has_key(name): input[name]=[] @@ -661,7 +661,7 @@ def _doit(arglist, out=sys.stdout): value = _2utf8( val[ pos + 1 : ] ) was_set=0 for mod, (properties,options) in information.items(): - if properties.has_key(name): + if name in properties: if properties[name].multivalue: if not append.has_key(name): append[name]=[] @@ -684,7 +684,7 @@ def _doit(arglist, out=sys.stdout): value = _2utf8( val[ pos + 1 : ] ) was_set=0 for mod, (properties,options) in information.items(): - if properties.has_key(name): + if name in properties: if properties[name].multivalue: if not remove.has_key(name): remove[name]=[] @@ -805,7 +805,7 @@ def _doit(arglist, out=sys.stdout): for i in module.property_descriptions: property=module.property_descriptions.get(i) if property.required: - if not object.has_key(i) or (not object[i] or (isinstance(object[i], list) and object[i]==[''])): + if i not in object or (not object[i] or (isinstance(object[i], list) and object[i]==[''])): print >> out, i raise Failed() except univention.admin.uexceptions.noObject, e: @@ -928,7 +928,7 @@ def _doit(arglist, out=sys.stdout): object_modified+=1 modlist=[] upr = lo.search(base=dn, scope='base', attr=['univentionPolicyReference'])[0][1] - if not upr.has_key('univentionPolicyReference'): + if 'univentionPolicyReference' not in upr: upr['univentionPolicyReference'] = [] for el in policy_reference: if val in upr['univentionPolicyReference']: @@ -1109,7 +1109,7 @@ def _doit(arglist, out=sys.stdout): print >> out, " Merged Settings:" for key in ddict.keys(): - if not client.has_key(key): + if key not in client: client[key]=ddict[key] if policies_with_DN: -- 1.8.4.rc3 From 18214dc734814f461c4d9b91f70c3af1cec66e7a Mon Sep 17 00:00:00 2001 Message-Id: <18214dc734814f461c4d9b91f70c3af1cec66e7a.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 17/33] Bug #33224: Use else after loop Organization: Univention GmbH, Bremen, Germany to handle unmatched values. --- .../modules/univention/admincli/admin.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 8a9f3fa..6d1497c 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -639,27 +639,22 @@ def _doit(arglist, out=sys.stdout): name=val[:pos] value = _2utf8( val[ pos + 1 : ] ) - was_set=0 for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: if not input.has_key(name): input[name]=[] - was_set=1 if value: input[name].append(value) - was_set=1 else: input[name]=value - was_set=1 - - if not was_set: + break + else: print >> out, "WARNING: No attribute with name '%s' in this module, value not set." % name elif opt == '--append': pos=val.find('=') name=val[:pos] value = _2utf8( val[ pos + 1 : ] ) - was_set=0 for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: @@ -667,11 +662,10 @@ def _doit(arglist, out=sys.stdout): append[name]=[] if value: append[name].append(value) - was_set=1 else: append[name]=value - was_set=1 - if not was_set: + break + else: print >> out, "WARNING: No attribute with name %s in this module, value not appended." % name elif opt == '--remove': @@ -682,7 +676,6 @@ def _doit(arglist, out=sys.stdout): else: name=val[:pos] value = _2utf8( val[ pos + 1 : ] ) - was_set=0 for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: @@ -690,11 +683,10 @@ def _doit(arglist, out=sys.stdout): remove[name]=[] if value: remove[name].append(value) - was_set=1 else: remove[name]=value - was_set=1 - if not was_set: + break + else: print >> out, "WARNING: No attribute with name %s in this module, value not removed." % name elif opt == '--remove_referring': remove_referring=1 -- 1.8.4.rc3 From 531d495daf3af82ef46a4a88d63800541331f449 Mon Sep 17 00:00:00 2001 Message-Id: <531d495daf3af82ef46a4a88d63800541331f449.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 18/33] Bug #33224: Use dict.setdefault() Organization: Univention GmbH, Bremen, Germany Use dict.setdefault() instead of an explicit check for the first instance. --- .../modules/univention/admincli/admin.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 6d1497c..2bd5aff 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -642,10 +642,9 @@ def _doit(arglist, out=sys.stdout): for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: - if not input.has_key(name): - input[name]=[] + values = input.setdefault(name, []) if value: - input[name].append(value) + values.append(value) else: input[name]=value break @@ -658,10 +657,9 @@ def _doit(arglist, out=sys.stdout): for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: - if not append.has_key(name): - append[name]=[] + values = append.setdefault(name, []) if value: - append[name].append(value) + values.append(value) else: append[name]=value break @@ -679,10 +677,9 @@ def _doit(arglist, out=sys.stdout): for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: - if not remove.has_key(name): - remove[name]=[] + values = remove.setdefault(name, []) if value: - remove[name].append(value) + values.append(value) else: remove[name]=value break -- 1.8.4.rc3 From 4b3d6a11232316ef7cb04df844fa854a9a5609fc Mon Sep 17 00:00:00 2001 Message-Id: <4b3d6a11232316ef7cb04df844fa854a9a5609fc.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 19/33] Bug #33224: Iterate directly through list Organization: Univention GmbH, Bremen, Germany Use for-loop to directly iterate through the list element instead of using an indirect index into the list. Use dict.items() instead of explicitly dereferencing the values. Also use dict.setdefault() instead of an explicit check for the first instance. --- .../modules/univention/admincli/admin.py | 58 +++++++++++----------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 2bd5aff..1cb5a9e 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -256,10 +256,10 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout print >> out, 'WARNING: file not found: %s' % value elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): - for i in range(0,len(value)): - test_val=value[i].split('"') - if test_val[0] and test_val[0] == value[i]: - val=value[i].split(' ') + for i in value: + test_val = i.split('"') + if test_val[0] and test_val[0] == i: + val = i.split(' ') else: val=[] for j in test_val: @@ -267,23 +267,23 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout if j: val.append(j) - if not object.has_key(key): - object[key]=[] - if val in object[key]: + values = object.setdefault(key, []) + if val in values: print >> out, 'WARNING: cannot append %s to %s, value exists' % (val, key) - elif object[key] == [''] or object[key] == []: - object[key]=[val] + elif values == [''] or values == []: + values[:] = [val] else: - object[key].append(val) + values.append(val) else: for val in value: - if val in object[key]: + values = object[key] + if val in values: print >> out, 'WARNING: cannot append %s to %s, value exists' % (val, key) - elif object[key] == [''] or object[key] == []: - object[key]=[val] + elif values == [''] or values == []: + values[:] = [val] else: try: - tmp = list(object[key]) + tmp = list(values) tmp.append(val) object[key] = list(tmp) except univention.admin.uexceptions.valueInvalidSyntax, errmsg: @@ -292,10 +292,10 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout for key, value in remove.items(): if univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): if value: - for i in range(0,len(value)): - test_val=value[i].split('"') - if test_val[0] and test_val[0] == value[i]: - val=value[i].split(' ') + for i in value: + test_val = i.split('"') + if test_val[0] and test_val[0] == i: + val = i.split(' ') else: val=[] print >> out, 'test_val=%s' % (test_val,) @@ -346,8 +346,8 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): if isinstance(value, list): - for i in range(0,len(value)): - test_val=value[i].split('"') + for i in value: + test_val = i.split('"') if test_val[0] and test_val[0] == value[i]: val=value[i].split(' ') else: @@ -1097,20 +1097,20 @@ def _doit(arglist, out=sys.stdout): print >> out, " Merged Settings:" - for key in ddict.keys(): + for key, values in ddict.items(): if key not in client: - client[key]=ddict[key] + client[key] = values if policies_with_DN: - for key in client.keys(): - print >> out, " Policy: " + client[key][0] + for key, (policy, values) in client.items(): + print >> out, " Policy: " + policy print >> out, " Attribute: " + key - for i in range(0, len(client[key][1])): - print >> out, " Value: " + client[key][1][i] + for value in values: + print >> out, " Value: " + value else: - for key in client.keys(): - for i in range(0, len(client[key])): - print >> out, " %s=%s" % (key, client[key][i]) + for key, values in client.items(): + for value in values: + print >> out, " %s=%s" % (key, value) print >> out, '' print >> out, '' -- 1.8.4.rc3 From 9303ce1f52440cd87a4e9cd9261fbff3b076c097 Mon Sep 17 00:00:00 2001 Message-Id: <9303ce1f52440cd87a4e9cd9261fbff3b076c097.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 20/33] Bug #33224: Use list comprehension Organization: Univention GmbH, Bremen, Germany Use more compact list comprehension. --- .../modules/univention/admincli/admin.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 1cb5a9e..d3c2729 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -304,8 +304,7 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout if j: val.append(j) - for j in range(0,len(val)): - val[j]='"%s"' % val[j] + val = ['"%s"' % _ for _ in val] if val and val in object[key]: object[key].remove(val) @@ -395,7 +394,7 @@ def doit(arglist, out=sys.stdout): msg.extend(e.args) # strip elements and make sure that a ':' is printed iff further information follows - msg = [i.strip() for i in msg] + msg = [_.strip() for _ in msg] if len(msg) == 1: msg[0] = '%s.' % msg[0].strip(':.') elif len(msg) > 1: @@ -806,9 +805,7 @@ def _doit(arglist, out=sys.stdout): if policy_reference: lo.modify(dn,[('objectClass','','univentionPolicyReference')]) - modlist=[] - for el in policy_reference: - modlist.append(('univentionPolicyReference','',el)) + modlist = [('univentionPolicyReference', '', el) for el in policy_reference] lo.modify(dn,modlist) if exists == 1: @@ -929,9 +926,7 @@ def _doit(arglist, out=sys.stdout): object_modified+=1 if policy_dereference: - modlist=[] - for el in policy_dereference: - modlist.append(('univentionPolicyReference',el,'')) + modlist = [('univentionPolicyReference', el, '') for el in policy_dereference] lo.modify(dn,modlist) object_modified+=1 -- 1.8.4.rc3 From 6282db54fc215da640cd17d940fed63c78493f5a Mon Sep 17 00:00:00 2001 Message-Id: <6282db54fc215da640cd17d940fed63c78493f5a.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 21/33] Bug #33224: Use True/False Organization: Univention GmbH, Bremen, Germany instead of 1/0. Drop "exists" completely and directly evaluate "exists_msg". --- .../modules/univention/admincli/admin.py | 35 ++++++---------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index d3c2729..201a828 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -224,11 +224,11 @@ def module_usage(information, action='', out=sys.stdout): _print_property( module, action, item, out ) -def module_information(module, identifies_only=0): +def module_information(module, identifies_only=False): information={module:[{},{}]} if getattr(module, 'superordinate', None): superordinate=univention.admin.modules.get(module.superordinate) - information.update(module_information(superordinate, identifies_only=1)) + information.update(module_information(superordinate, identifies_only=True)) if not identifies_only: for name, property in module.property_descriptions.items(): @@ -424,8 +424,8 @@ def _doit(arglist, out=sys.stdout): if module_name == 'modules': return list_available_modules(out=out) - remove_referring=0 - recursive=1 + remove_referring = False + recursive = True # BUG: if this is the default, there's no way to disable it # parse options longopts = [ 'position=', @@ -469,7 +469,7 @@ def _doit(arglist, out=sys.stdout): policyOptions=[] logfile='/var/log/univention/directory-manager-cmd.log' tls=2 - ignore_exists=0 + ignore_exists = False superordinate_dn='' parsed_append_options=[] parsed_options=[] @@ -506,7 +506,7 @@ def _doit(arglist, out=sys.stdout): elif opt == '--tls': tls=val elif opt == '--ignore_exists': - ignore_exists=1 + ignore_exists = True elif opt == '--superordinate': superordinate_dn=val elif opt == '--option': @@ -685,9 +685,9 @@ def _doit(arglist, out=sys.stdout): else: print >> out, "WARNING: No attribute with name %s in this module, value not removed." % name elif opt == '--remove_referring': - remove_referring=1 + remove_referring = True elif opt == '--recursive': - recursive=1 + recursive = True if action in ['modify','edit','create','new']: @@ -720,7 +720,6 @@ def _doit(arglist, out=sys.stdout): object.open() if getattr(object, 'open_warning', None): print >> out, 'WARNING:%s' % object.open_warning - exists=0 try: object_input(module, object, input, append=append, out=out) except univention.admin.uexceptions.nextFreeIp: @@ -734,7 +733,6 @@ def _doit(arglist, out=sys.stdout): print >> out, 'E: Option %s is not valid' % err raise Failed() - exists=0 exists_msg=None try: dn=object.create() @@ -743,43 +741,31 @@ def _doit(arglist, out=sys.stdout): if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.uidAlreadyUsed, user: exists_msg = '(uid) %s' % user if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.groupNameAlreadyUsed, group: exists_msg = '(group) %s' % group if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.dhcpServerAlreadyUsed, name: exists_msg = '(dhcpserver) %s' % name if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.macAlreadyUsed, mac: exists_msg = '(mac) %s' % mac if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.noLock, e: exists_msg = '(nolock) %s' % str(e) if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - else: - exists=1 except univention.admin.uexceptions.invalidDhcpEntry: print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' raise Failed() @@ -808,11 +794,8 @@ def _doit(arglist, out=sys.stdout): modlist = [('univentionPolicyReference', '', el) for el in policy_reference] lo.modify(dn,modlist) - if exists == 1: - if exists_msg: + if exists_msg: print >> out, 'Object exists: %s' % exists_msg - else: - print >> out, 'Object exists' else: if not dn: dn=object.dn -- 1.8.4.rc3 From 11ec3075c84f5d2228f13d5ad756d925558fe484 Mon Sep 17 00:00:00 2001 Message-Id: <11ec3075c84f5d2228f13d5ad756d925558fe484.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 22/33] Bug #33224: Refactore duplicate policy code Organization: Univention GmbH, Bremen, Germany Extract common implementation to get policy into separate function. --- .../modules/univention/admincli/admin.py | 98 +++++++++------------- 1 file changed, 40 insertions(+), 58 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 201a828..b5fd68d 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -407,6 +407,44 @@ def doit(arglist, out=sys.stdout): print >> out, ex +def get_policy(dn, out=sys.stdout, policyOptions=None, policies_with_DN=False): + cmd = ['univention_policy_result'] + if policyOptions: + cmd.extend(policyOptions) + cmd.append(dn) + + policy = attribute = '' + value = [] + client = {} + + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) + for line in proc.stdout: + line = line.strip() + if not line or line.startswith("DN: ") or line.startswith("POLICY "): + continue + + print >> out, " %s" % line + if policies_with_DN: + key, val = line.split(': ', 1) + if key == 'Policy': + if attribute and policy: + client[attribute] = [policy, value] + value = [] + policy = val + elif key == 'Attribute': + attribute = val + elif key == 'Value': + value.append(val) + else: + key, val = line.split('=', 1) + client.setdefault(key, []).append(val) + proc.wait() + if policies_with_DN and attribute and policy: + client[attribute] = [policy, value] + + return client + + def _doit(arglist, out=sys.stdout): # parse module and action if set(arglist) & set(('-h', '--help', '-?')): @@ -1003,37 +1041,8 @@ def _doit(arglist, out=sys.stdout): if list_policies: utf8_objectdn = _2utf8( univention.admin.objects.dn( object ) ) - p1 = subprocess.Popen(['univention_policy_result'] + policyOptions + [utf8_objectdn], stdout=subprocess.PIPE) - policyResults = p1.communicate()[0].split('\n') - print >> out, " Policy-based Settings:" - policy='' - value=[] - client={} - for line in policyResults: - if not (line.strip() == "" or line.strip()[:4]=="DN: " or line.strip()[:7]=="POLICY "): - print >> out, " %s" % line.strip() - if policies_with_DN: - clsplit=string.split(line.strip(), ': ') - if clsplit[0] == 'Policy': - if policy: - client[attribute]=[policy, value] - value=[] - policy=clsplit[1] - elif clsplit[0] == 'Attribute': - attribute=clsplit[1] - elif clsplit[0] == 'Value': - value.append(clsplit[1]) - else: - clsplit=string.split(line.strip(), '=') - if not client.has_key(clsplit[0]): - client[clsplit[0]] = [] - client[clsplit[0]].append(clsplit[1]) - - if policies_with_DN: - client[attribute]=[policy, value] - value=[] - + client = get_policy(utf8_objectdn, out, policyOptions, policies_with_DN) print >> out, '' if module_name == 'dhcp/host': @@ -1042,37 +1051,10 @@ def _doit(arglist, out=sys.stdout): if univention.admin.ipaddress.ip_is_in_network(subnet['subnet'], subnet['subnetmask'], object['fixedaddress'][0]): utf8_subnet_dn = _2utf8( subnet.dn ) - p1 = subprocess.Popen(['univention_policy_result'] + policyOptions + [utf8_subnet_dn], stdout=subprocess.PIPE) - policyResults = p1.communicate()[0].split('\n') print >> out, " Subnet-based Settings:" - ddict={} - policy='' - value=[] - for line in policyResults: - if not (line.strip() == "" or line.strip()[:4]=="DN: " or line.strip()[:7]=="POLICY "): - print >> out, " %s" % line.strip() - if policies_with_DN: - subsplit=string.split(line.strip(), ': ') - if subsplit[0] == 'Policy': - if policy: - ddict[attribute]=[policy, value] - value=[] - policy=subsplit[1] - elif subsplit[0] == 'Attribute': - attribute=subsplit[1] - elif subsplit[0] == 'Value': - value.append(subsplit[1]) - else: - subsplit=string.split(line.strip(), '=') - if not ddict.has_key(subsplit[0]): - ddict[subsplit[0]] = [] - ddict[subsplit[0]].append(subsplit[1]) - + ddict = get_policy(utf8_subnet_dn, out, policyOptions, policies_with_DN) print >> out, '' - if policies_with_DN: - ddict[attribute]=[policy, value] - print >> out, " Merged Settings:" for key, values in ddict.items(): -- 1.8.4.rc3 From e780925a46a252df4adcd74e92173fea4756589a Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 29 Nov 2013 13:31:01 +0100 Subject: [PATCH 23/33] Bug #33224: Remove unused imports Organization: Univention GmbH, Bremen, Germany univention.admin.layout.Tab is unused. --- .../modules/univention/admincli/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index b5fd68d..250ec96 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -46,7 +46,7 @@ import univention.admin.uexceptions import univention.admin.uldap import univention.admin.modules import univention.admin.objects -from univention.admin.layout import Tab, Group +from univention.admin.layout import Group import univention.config_registry import univention.admin.ipaddress -- 1.8.4.rc3 From 494aad499be7095224d24090c11e375f8bfc8b99 Mon Sep 17 00:00:00 2001 Message-Id: <494aad499be7095224d24090c11e375f8bfc8b99.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 24/33] Bug #33224: Shorten imports Organization: Univention GmbH, Bremen, Germany Alias long import names. Only import ConfigRegistry --- .../modules/univention/admincli/admin.py | 100 ++++++++++----------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 250ec96..fc334fa 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -40,14 +40,14 @@ import os import subprocess import traceback -import univention.debug +import univention.debug as ud -import univention.admin.uexceptions +import univention.admin.uexceptions as uex import univention.admin.uldap import univention.admin.modules import univention.admin.objects from univention.admin.layout import Group -import univention.config_registry +from univention.config_registry import ConfigRegistry import univention.admin.ipaddress univention.admin.modules.update() @@ -286,7 +286,7 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout tmp = list(values) tmp.append(val) object[key] = list(tmp) - except univention.admin.uexceptions.valueInvalidSyntax, errmsg: + except uex.valueInvalidSyntax, errmsg: print >> out, 'E: Invalid Syntax: %s' % (errmsg,) if remove: for key, value in remove.items(): @@ -364,10 +364,10 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: try: object[key]=value - except univention.admin.uexceptions.ipOverridesNetwork, e: + except uex.ipOverridesNetwork, e: print >> out, 'WARNING: %s' % e.message - except univention.admin.uexceptions.valueMayNotChange, e: - raise univention.admin.uexceptions.valueMayNotChange, "%s: %s"%(e.message, key) + except uex.valueMayNotChange, e: + raise uex.valueMayNotChange, "%s: %s"%(e.message, key) def list_available_modules(out=sys.stdout): @@ -381,8 +381,8 @@ def list_available_modules(out=sys.stdout): def doit(arglist, out=sys.stdout): try: _doit(arglist, out) - except univention.admin.uexceptions.base, e: - univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, traceback.format_exc()) + except uex.base, e: + ud.debug(ud.ADMIN, ud.WARN, traceback.format_exc()) # collect error information msg = [] @@ -559,11 +559,11 @@ def _doit(arglist, out=sys.stdout): policy_dereference.append(val) if logfile: - univention.debug.init(logfile, 1, 0) + ud.init(logfile, 1, 0) else: print >> out, "WARNING: no logfile specified" - configRegistry=univention.config_registry.ConfigRegistry() + configRegistry = ConfigRegistry() configRegistry.load() if configRegistry.get('ldap/master'): @@ -575,27 +575,27 @@ def _doit(arglist, out=sys.stdout): debug_level = int(configRegistry.get('directory/manager/cmd/debug/level', 0)) - univention.debug.set_level(univention.debug.LDAP, debug_level) - univention.debug.set_level(univention.debug.ADMIN, debug_level) + ud.set_level(ud.LDAP, debug_level) + ud.set_level(ud.ADMIN, debug_level) if binddn and bindpwd: - univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, "using %s account" % binddn) + ud.debug(ud.ADMIN, ud.INFO, "using %s account" % binddn) try: lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, start_tls=tls, bindpw=bindpwd) except Exception, e: - univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) + ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) print >> out, 'authentication error: %s' % (e,) raise Failed() policyOptions.extend(['-D', binddn, '-w', bindpwd]) ## FIXME not so nice else: if os.path.exists('/etc/ldap.secret'): - univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, "using cn=admin,%s account" % baseDN) + ud.debug(ud.ADMIN, ud.INFO, "using cn=admin,%s account" % baseDN) secretFileName='/etc/ldap.secret' binddn='cn=admin,'+baseDN policyOptions.extend(['-D', binddn, '-y', secretFileName]) elif os.path.exists('/etc/machine.secret'): - univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, "using %s account" % configRegistry['ldap/hostdn']) + ud.debug(ud.ADMIN, ud.INFO, "using %s account" % configRegistry['ldap/hostdn']) secretFileName='/etc/machine.secret' binddn=configRegistry['ldap/hostdn'] policyOptions.extend(['-D', binddn, '-y', secretFileName]) @@ -614,7 +614,7 @@ def _doit(arglist, out=sys.stdout): try: lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, bindpw=pwd, start_tls=tls) except Exception, e: - univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) + ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) print >> out, 'authentication error: %s' % (e,) raise Failed() @@ -626,7 +626,7 @@ def _doit(arglist, out=sys.stdout): try: position=univention.admin.uldap.position(baseDN) position.setDn(position_dn) - except univention.admin.uexceptions.noObject: + except uex.noObject: print >> out, 'E: Invalid position' raise Failed() @@ -652,7 +652,7 @@ def _doit(arglist, out=sys.stdout): if superordinate_dn and univention.admin.modules.superordinate(module): try: superordinate=univention.admin.objects.get(univention.admin.modules.superordinate(module), co, lo, '', dn=superordinate_dn) - except univention.admin.uexceptions.insufficientInformation, e: + except uex.insufficientInformation, e: print >> out, 'Insufficient Information: %s' % (e,) raise Failed() else: @@ -747,7 +747,7 @@ def _doit(arglist, out=sys.stdout): raise Failed() try: object=module.object(co, lo, position=position, superordinate=superordinate) - except univention.admin.uexceptions.insufficientInformation: + except uex.insufficientInformation: print >> out, 'E: Insufficient information' print >> out, 'Superordinate object is missing' raise Failed() @@ -760,11 +760,11 @@ def _doit(arglist, out=sys.stdout): print >> out, 'WARNING:%s' % object.open_warning try: object_input(module, object, input, append=append, out=out) - except univention.admin.uexceptions.nextFreeIp: + except uex.nextFreeIp: if not ignore_exists: print >> out, 'E: No free IP address found' raise Failed() - except univention.admin.uexceptions.valueInvalidSyntax, err: + except uex.valueInvalidSyntax, err: print >> out, 'E: Invalid Syntax: %s' % err raise Failed() except Exception, err: @@ -774,44 +774,44 @@ def _doit(arglist, out=sys.stdout): exists_msg=None try: dn=object.create() - except univention.admin.uexceptions.objectExists, dn: + except uex.objectExists, dn: exists_msg = dn if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.uidAlreadyUsed, user: + except uex.uidAlreadyUsed, user: exists_msg = '(uid) %s' % user if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.groupNameAlreadyUsed, group: + except uex.groupNameAlreadyUsed, group: exists_msg = '(group) %s' % group if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.dhcpServerAlreadyUsed, name: + except uex.dhcpServerAlreadyUsed, name: exists_msg = '(dhcpserver) %s' % name if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.macAlreadyUsed, mac: + except uex.macAlreadyUsed, mac: exists_msg = '(mac) %s' % mac if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.noLock, e: + except uex.noLock, e: exists_msg = '(nolock) %s' % str(e) if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except univention.admin.uexceptions.invalidDhcpEntry: + except uex.invalidDhcpEntry: print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' raise Failed() - except univention.admin.uexceptions.invalidOptions, e: + except uex.invalidOptions, e: if not ignore_exists: print >> out, 'E: invalid Options: %s' % e raise Failed() - except univention.admin.uexceptions.insufficientInformation: + except uex.insufficientInformation: print >> out, 'E: Insufficient information' print >> out, 'The following parameters are missing:' for i in module.property_descriptions: @@ -820,10 +820,10 @@ def _doit(arglist, out=sys.stdout): if i not in object or (not object[i] or (isinstance(object[i], list) and object[i]==[''])): print >> out, i raise Failed() - except univention.admin.uexceptions.noObject, e: + except uex.noObject, e: print >> out, 'E: object not found: %s' % e raise Failed() - except univention.admin.uexceptions.circularGroupDependency, e: + except uex.circularGroupDependency, e: print >> out, 'E: circular group dependency detected: %s' % e raise Failed() @@ -854,7 +854,7 @@ def _doit(arglist, out=sys.stdout): try: object=univention.admin.objects.get(module, co, lo, position='', dn=dn) - except univention.admin.uexceptions.noObject: + except uex.noObject: print >> out, 'E: object not found' raise Failed() @@ -883,19 +883,19 @@ def _doit(arglist, out=sys.stdout): try: object.move(newdn) object_modified+=1 - except univention.admin.uexceptions.noObject: + except uex.noObject: print >> out, 'E: object not found' raise Failed() - except univention.admin.uexceptions.ldapError, msg: + except uex.ldapError, msg: print >> out, "ldap Error: %s" % msg raise Failed() - except univention.admin.uexceptions.nextFreeIp: + except uex.nextFreeIp: print >> out, 'E: No free IP address found' raise Failed() - except univention.admin.uexceptions.valueInvalidSyntax, err: + except uex.valueInvalidSyntax, err: print >> out, 'E: Invalid Syntax: %s' % err raise Failed() - except univention.admin.uexceptions.invalidOperation, msg: + except uex.invalidOperation, msg: print >> out, str(msg) raise Failed() @@ -909,23 +909,23 @@ def _doit(arglist, out=sys.stdout): object.options.append(option) try: object_input(module, object, input, append, remove, out=out) - except univention.admin.uexceptions.valueMayNotChange,e: + except uex.valueMayNotChange,e: print >> out, unicode(e[0]) raise Failed() if object.hasChanged(input.keys()) or object.hasChanged(append.keys()) or object.hasChanged(remove.keys()) or parsed_append_options or parsed_options: try: dn=object.modify() object_modified+=1 - except univention.admin.uexceptions.noObject: + except uex.noObject: print >> out, 'E: object not found' raise Failed() - except univention.admin.uexceptions.invalidDhcpEntry: + except uex.invalidDhcpEntry: print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' raise Failed() - except univention.admin.uexceptions.circularGroupDependency, e: + except uex.circularGroupDependency, e: print >> out, 'E: circular group dependency detected: %s' % e raise Failed() - except univention.admin.uexceptions.valueInvalidSyntax, e: + except uex.valueInvalidSyntax, e: print >> out, 'E: Invalid Syntax: %s' % e raise Failed() @@ -973,7 +973,7 @@ def _doit(arglist, out=sys.stdout): else: print >> out, 'E: dn or filter needed' raise Failed() - except (univention.admin.uexceptions.noObject, IndexError): + except (uex.noObject, IndexError): print >> out, 'E: object not found' raise Failed() @@ -987,13 +987,13 @@ def _doit(arglist, out=sys.stdout): if recursive: try: object.remove(recursive) - except univention.admin.uexceptions.ldapError,msg: + except uex.ldapError,msg: print >> out, str(msg) raise Failed() else: try: object.remove() - except univention.admin.uexceptions.primaryGroupUsed: + except uex.primaryGroupUsed: print >> out, 'E: object in use' raise Failed() print >> out, 'Object removed: %s' % _2utf8(dn) @@ -1074,10 +1074,10 @@ def _doit(arglist, out=sys.stdout): print >> out, '' print >> out, '' - except univention.admin.uexceptions.ldapError, errmsg: + except uex.ldapError, errmsg: print >> out, '%s' % (errmsg,) raise Failed() - except univention.admin.uexceptions.valueInvalidSyntax, errmsg: + except uex.valueInvalidSyntax, errmsg: print >> out, '%s' % (errmsg.message,) raise Failed() else: -- 1.8.4.rc3 From 57ddb3b002ab86e402de0c5405b1dbe485b12610 Mon Sep 17 00:00:00 2001 Message-Id: <57ddb3b002ab86e402de0c5405b1dbe485b12610.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 25/33] Bug #33224: Use named constants Organization: Univention GmbH, Bremen, Germany instead of magic values. --- .../modules/univention/admincli/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index fc334fa..b7fc898 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -559,7 +559,7 @@ def _doit(arglist, out=sys.stdout): policy_dereference.append(val) if logfile: - ud.init(logfile, 1, 0) + ud.init(logfile, ud.FLUSH, ud.NO_FUNCTION) else: print >> out, "WARNING: no logfile specified" -- 1.8.4.rc3 From 11bae0c1d1a5261fecddf51fcc65e9834670e3a9 Mon Sep 17 00:00:00 2001 Message-Id: <11bae0c1d1a5261fecddf51fcc65e9834670e3a9.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 26/33] Bug #33224: Rework splitting Organization: Univention GmbH, Bremen, Germany Use string splitting instead of string.find() --- .../modules/univention/admincli/admin.py | 25 +++++++++------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index b7fc898..b1360c2 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -34,7 +34,6 @@ from textwrap import dedent import sys import getopt import re -import string import base64 import os import subprocess @@ -672,9 +671,8 @@ def _doit(arglist, out=sys.stdout): for opt, val in opts: if opt == '--set': - pos=val.find('=') - name=val[:pos] - value = _2utf8( val[ pos + 1 : ] ) + name, value = val.split('=', 1) + value = _2utf8(value) for mod, (properties,options) in information.items(): if name in properties: @@ -688,9 +686,8 @@ def _doit(arglist, out=sys.stdout): else: print >> out, "WARNING: No attribute with name '%s' in this module, value not set." % name elif opt == '--append': - pos=val.find('=') - name=val[:pos] - value = _2utf8( val[ pos + 1 : ] ) + name, value = val.split('=', 1) + value = _2utf8(value) for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: @@ -704,13 +701,11 @@ def _doit(arglist, out=sys.stdout): print >> out, "WARNING: No attribute with name %s in this module, value not appended." % name elif opt == '--remove': - pos=val.find('=') - if pos == -1: - name=val - value=None - else: - name=val[:pos] - value = _2utf8( val[ pos + 1 : ] ) + try: + name, value = val.split('=', 1) + value = _2utf8(value) + except ValueError: + name, value = val, None for mod, (properties,options) in information.items(): if name in properties: if properties[name].multivalue: @@ -878,7 +873,7 @@ def _doit(arglist, out=sys.stdout): if not res: print >> out, "position does not exsist: %s" % position_dn raise Failed() - rdn = dn[:string.find(dn,',')] + rdn, _old_base = dn.split(',', 1) newdn="%s,%s" % (rdn,position_dn) try: object.move(newdn) -- 1.8.4.rc3 From d8fe2738eb579a4c6ab4ec63d4658e816461c572 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 27/33] Bug #33224: Check for multiple conditions Organization: Univention GmbH, Bremen, Germany Use modern any() and tuple() containment test instead of multiple equal comparisons. --- .../modules/univention/admincli/admin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index b1360c2..8fc1316 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -723,7 +723,7 @@ def _doit(arglist, out=sys.stdout): recursive = True - if action in ['modify','edit','create','new']: + if action in ('modify', 'edit', 'create', 'new'): for el in policy_reference: oc = lo.get(el, ['objectClass']) if not oc: @@ -735,7 +735,7 @@ def _doit(arglist, out=sys.stdout): #+++# ACTION CREATE #+++# - if action == 'create' or action == 'new': + if action in ('create', 'new'): if getattr(module, 'operations', None): if not 'add' in module.operations: print >> out, 'Create %s not allowed' % module_name @@ -896,7 +896,7 @@ def _doit(arglist, out=sys.stdout): else: # modify - if (len(input)+len(append)+len(remove)+len(parsed_append_options)+len(parsed_options))>0: + if any((input, append, remove, parsed_append_options, parsed_options)): if parsed_options: object.options=parsed_options if parsed_append_options: -- 1.8.4.rc3 From fe2900355096b075ec48b1d403421e26c3e09aa2 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 28/33] Bug #33224: Refactore credential check Organization: Univention GmbH, Bremen, Germany Use for-loop to check for usable credential to reduce code duplication. --- .../modules/univention/admincli/admin.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 8fc1316..f863386 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -588,19 +588,17 @@ def _doit(arglist, out=sys.stdout): policyOptions.extend(['-D', binddn, '-w', bindpwd]) ## FIXME not so nice else: - if os.path.exists('/etc/ldap.secret'): - ud.debug(ud.ADMIN, ud.INFO, "using cn=admin,%s account" % baseDN) - secretFileName='/etc/ldap.secret' - binddn='cn=admin,'+baseDN - policyOptions.extend(['-D', binddn, '-y', secretFileName]) - elif os.path.exists('/etc/machine.secret'): - ud.debug(ud.ADMIN, ud.INFO, "using %s account" % configRegistry['ldap/hostdn']) - secretFileName='/etc/machine.secret' - binddn=configRegistry['ldap/hostdn'] - policyOptions.extend(['-D', binddn, '-y', secretFileName]) + for binddn, secretFileName in ( + ('cn=admin,' + baseDN, '/etc/ldap.secret'), + (configRegistry['ldap/hostdn'], '/etc/machine.secret'), + ): + if os.path.exists(secretFileName): + break else: print >> out, "E: Missing LDAP credentials" raise Failed() + ud.debug(ud.ADMIN, ud.INFO, "using %s account" % binddn) + policyOptions.extend(['-D', binddn, '-y', secretFileName]) try: secretFile=open(secretFileName,'r') -- 1.8.4.rc3 From 5133a8078c2f922e7ff17542b54cef1429f8f0e3 Mon Sep 17 00:00:00 2001 Message-Id: <5133a8078c2f922e7ff17542b54cef1429f8f0e3.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 29/33] Bug #33224: Small Python code cleanup Organization: Univention GmbH, Bremen, Germany Add comma for easier future extension. Convert string into number. Move variable initialization into else case. --- .../modules/univention/admincli/admin.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index f863386..ff9d82a 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -484,7 +484,7 @@ def _doit(arglist, out=sys.stdout): 'policy-reference=', 'policy-dereference=', 'remove_referring', - 'recursive' + 'recursive', ] try: opts, args=getopt.getopt(arglist[3:], '', longopts) @@ -580,7 +580,14 @@ def _doit(arglist, out=sys.stdout): if binddn and bindpwd: ud.debug(ud.ADMIN, ud.INFO, "using %s account" % binddn) try: - lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, start_tls=tls, bindpw=bindpwd) + lo = univention.admin.uldap.access( + host=configRegistry['ldap/master'], + port=int(configRegistry.get('ldap/master/port', 7389)), + base=baseDN, + binddn=binddn, + start_tls=tls, + bindpw=bindpwd + ) except Exception, e: ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) print >> out, 'authentication error: %s' % (e,) @@ -863,11 +870,10 @@ def _doit(arglist, out=sys.stdout): if not position_dn: print >> out, "need new position for moving object" else: - res = '' try: # check if global-position exists res = lo.get(position_dn) except: - pass + res = '' if not res: print >> out, "position does not exsist: %s" % position_dn raise Failed() -- 1.8.4.rc3 From 5a555f398305f8b30105fa6ff206ab915f9b486e Mon Sep 17 00:00:00 2001 Message-Id: <5a555f398305f8b30105fa6ff206ab915f9b486e.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 30/33] Bug #33224: Convert string concatenation to list Organization: Univention GmbH, Bremen, Germany String concatenation is somehow expensive in Python, as the Python interpreter hashes strings to detect duplication. Collect data in lists first and only convert them to strings at the end. --- .../modules/univention/admincli/admin.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index ff9d82a..2d4ea22 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -160,7 +160,7 @@ def _print_property(module, action, name, out=sys.stdout): required[ 'editable' ] = False flags = '' - if action in required and required[ action ]: + if required.get(action): flags='*' elif not action in required: if required[ 'create' ]: @@ -171,16 +171,13 @@ def _print_property(module, action, name, out=sys.stdout): flags += 'r' if not required[ 'editable' ]: flags += 'e' + flags = [flags] if flags else [] if property.options: - if flags: - flags += ',' - flags += ','.join(property.options) + flags.extend(property.options) if property.multivalue: - if flags: - flags += ',' - flags += '[]' + flags.append('[]') if flags: - name += ' (%s)' % (flags,) + name += ' (%s)' % (','.join(flags),) print >> out, ' %-40s %s' % ( name, -- 1.8.4.rc3 From 35b471f7f09af73675f11295fcb234e5d47cf51c Mon Sep 17 00:00:00 2001 Message-Id: <35b471f7f09af73675f11295fcb234e5d47cf51c.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:26:36 +0100 Subject: [PATCH 31/33] Bug #33224: pep8 compliance Organization: Univention GmbH, Bremen, Germany Blank lines. White space. Re-indent two sections to use one tab for indention level instead of two. --- .../modules/univention/admincli/admin.py | 533 +++++++++++---------- .../univention-cli-server | 8 +- 2 files changed, 275 insertions(+), 266 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 2d4ea22..3ae7ee1 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -49,6 +49,7 @@ from univention.admin.layout import Group from univention.config_registry import ConfigRegistry import univention.admin.ipaddress + univention.admin.modules.update() # update choices-lists which are defined in LDAP @@ -134,7 +135,7 @@ def version(out=sys.stdout): def _print_property(module, action, name, out=sys.stdout): - property = module.property_descriptions.get( name ) + property = module.property_descriptions.get(name) if property is None: print >> out, 'E: unknown property %s of module %s' % ( name, @@ -143,33 +144,33 @@ def _print_property(module, action, name, out=sys.stdout): return required = { - 'create' : False, - 'modify' : False, - 'remove' : False, + 'create': False, + 'modify': False, + 'remove': False, 'editable': True, - } + } if property.required: - required [ 'create' ] = True + required['create'] = True if property.identifies: - required[ 'modify' ] = True - required[ 'remove' ] = True + required['modify'] = True + required['remove'] = True if not property.editable: - required[ 'modify' ] = False - required[ 'remove' ] = False - required[ 'editable' ] = False + required['modify'] = False + required['remove'] = False + required['editable'] = False flags = '' if required.get(action): - flags='*' + flags = '*' elif not action in required: - if required[ 'create' ]: + if required['create']: flags += 'c' - if required[ 'modify' ]: + if required['modify']: flags += 'm' - if required[ 'remove' ]: + if required['remove']: flags += 'r' - if not required[ 'editable' ]: + if not required['editable']: flags += 'e' flags = [flags] if flags else [] if property.options: @@ -198,48 +199,52 @@ def module_usage(information, action='', out=sys.stdout): print >> out, '' print >> out, '%s variables:' % module.module - if not hasattr(module,"layout"): + if not hasattr(module, "layout"): continue for moduletab in module.layout: print >> out, ' %s:' % (moduletab.label) for row in moduletab.layout: - if isinstance( row, Group ): + if isinstance(row, Group): print >> out, ' %s' % row.label for row in row.layout: - if isinstance( row, basestring ): - _print_property( module, action, row, out ) + if isinstance(row, basestring): + _print_property(module, action, row, out) continue for item in row: - _print_property( module, action, item, out ) + _print_property(module, action, item, out) else: - if isinstance( row, basestring ): - _print_property( module, action, row, out ) + if isinstance(row, basestring): + _print_property(module, action, row, out) continue for item in row: - _print_property( module, action, item, out ) + _print_property(module, action, item, out) def module_information(module, identifies_only=False): - information={module:[{},{}]} + information = { + module: [{}, {}], + } if getattr(module, 'superordinate', None): - superordinate=univention.admin.modules.get(module.superordinate) + superordinate = univention.admin.modules.get(module.superordinate) information.update(module_information(superordinate, identifies_only=True)) if not identifies_only: for name, property in module.property_descriptions.items(): - information[module][0][name]=property - if hasattr(module,'options'): + information[module][0][name] = property + if hasattr(module, 'options'): for name, option in module.options.items(): - information[module][1][name]=option + information[module][1][name] = option return information -def _2utf8( text ): + +def _2utf8(text): try: - return text.encode( 'utf-8' ) + return text.encode('utf-8') except UnicodeError: - return text.decode( 'iso-8859-1' ) + return text.decode('iso-8859-1') + def object_input(module, object, input, append=None, remove=None, out=sys.stdout): if append: @@ -251,13 +256,13 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: print >> out, 'WARNING: file not found: %s' % value - elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): + elif univention.admin.syntax.is_syntax(module.property_descriptions[key].syntax, univention.admin.syntax.complex): for i in value: test_val = i.split('"') if test_val[0] and test_val[0] == i: val = i.split(' ') else: - val=[] + val = [] for j in test_val: j = j.strip() if j: @@ -286,14 +291,14 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout print >> out, 'E: Invalid Syntax: %s' % (errmsg,) if remove: for key, value in remove.items(): - if univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): + if univention.admin.syntax.is_syntax(module.property_descriptions[key].syntax, univention.admin.syntax.complex): if value: for i in value: test_val = i.split('"') if test_val[0] and test_val[0] == i: val = i.split(' ') else: - val=[] + val = [] print >> out, 'test_val=%s' % (test_val,) for j in test_val: j = j.strip() @@ -307,14 +312,14 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: print >> out, "WARNING: cannot remove %s from %s, value does not exist" % (val, key) else: - object[key]=[] + object[key] = [] else: if isinstance(object[key], basestring): - object[key] = [ object[key] ] + object[key] = [object[key]] vallist = value if isinstance(value, basestring): - vallist = [ value ] + vallist = [value] for val in vallist: if val in object[key]: @@ -325,45 +330,45 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout for key, value in input.items(): if module.property_descriptions[key].syntax.name == 'binaryfile': if value == '': - object[key]=value + object[key] = value elif os.path.exists(value): fh = open(value, 'r') - content=fh.read() + content = fh.read() if "----BEGIN CERTIFICATE-----" in content: - content = content.replace('----BEGIN CERTIFICATE-----','') - content = content.replace('----END CERTIFICATE-----','') - object[key]=base64.decodestring(content) + content = content.replace('----BEGIN CERTIFICATE-----', '') + content = content.replace('----END CERTIFICATE-----', '') + object[key] = base64.decodestring(content) else: - object[key]= content + object[key] = content fh.close() else: print >> out, 'WARNING: file not found: %s' % value - elif univention.admin.syntax.is_syntax( module.property_descriptions[key].syntax, univention.admin.syntax.complex ): + elif univention.admin.syntax.is_syntax(module.property_descriptions[key].syntax, univention.admin.syntax.complex): if isinstance(value, list): for i in value: test_val = i.split('"') if test_val[0] and test_val[0] == value[i]: - val=value[i].split(' ') + val = value[i].split(' ') else: - val=[] + val = [] for j in test_val: j = j.strip() if j: val.append(j) else: - val=value.split(' ') + val = value.split(' ') if module.property_descriptions[key].multivalue: - object[key]=[val] + object[key] = [val] else: - object[key]=val + object[key] = val else: try: - object[key]=value + object[key] = value except uex.ipOverridesNetwork, e: print >> out, 'WARNING: %s' % e.message except uex.valueMayNotChange, e: - raise uex.valueMayNotChange, "%s: %s"%(e.message, key) + raise uex.valueMayNotChange, "%s: %s" % (e.message, key) def list_available_modules(out=sys.stdout): @@ -453,8 +458,7 @@ def _doit(arglist, out=sys.stdout): usage(out=out) raise Failed() - module_name=arglist[1] - + module_name = arglist[1] if module_name == 'modules': return list_available_modules(out=out) @@ -484,7 +488,7 @@ def _doit(arglist, out=sys.stdout): 'recursive', ] try: - opts, args=getopt.getopt(arglist[3:], '', longopts) + opts, args = getopt.getopt(arglist[3:], '', longopts) except getopt.error, msg: print >> out, msg raise Failed() @@ -494,61 +498,61 @@ def _doit(arglist, out=sys.stdout): " ".join(('"%s"' % _ for _ in args)), ) - position_dn='' - dn='' - binddn=None - bindpwd=None - list_policies=False - policies_with_DN=False - policyOptions=[] - logfile='/var/log/univention/directory-manager-cmd.log' - tls=2 + position_dn = '' + dn = '' + binddn = None + bindpwd = None + list_policies = False + policies_with_DN = False + policyOptions = [] + logfile = '/var/log/univention/directory-manager-cmd.log' + tls = 2 ignore_exists = False - superordinate_dn='' - parsed_append_options=[] - parsed_options=[] - filter='' - input={} - append={} - remove={} - policy_reference=[] - policy_dereference=[] + superordinate_dn = '' + parsed_append_options = [] + parsed_options = [] + filter = '' + input = {} + append = {} + remove = {} + policy_reference = [] + policy_dereference = [] for opt, val in opts: if opt == '--position': - position_dn = _2utf8( val ) + position_dn = _2utf8(val) elif opt == '--logfile': - logfile=val + logfile = val elif opt == '--policies': list_policies = True - if val=="1": + if val == "1": policies_with_DN = True else: policyOptions = ['-s'] elif opt == '--binddn': - binddn=val + binddn = val elif opt == '--bindpwd': - bindpwd=val + bindpwd = val elif opt == '--bindpwdfile': try: with open(val) as fp: - bindpwd=fp.read().strip() + bindpwd = fp.read().strip() except IOError as e: print >> out, 'E: could not read bindpwd from file (%s)' % (e,) raise Failed() elif opt == '--dn': - dn = _2utf8( val ) + dn = _2utf8(val) elif opt == '--tls': - tls=val + tls = val elif opt == '--ignore_exists': ignore_exists = True elif opt == '--superordinate': - superordinate_dn=val + superordinate_dn = val elif opt == '--option': parsed_options.append(val) elif opt == '--append-option': parsed_append_options.append(val) elif opt == '--filter': - filter=val + filter = val elif opt == '--policy-reference': policy_reference.append(val) elif opt == '--policy-dereference': @@ -563,11 +567,11 @@ def _doit(arglist, out=sys.stdout): configRegistry.load() if configRegistry.get('ldap/master'): - co=univention.admin.config.config(configRegistry['ldap/master']) + co = univention.admin.config.config(configRegistry['ldap/master']) else: - co=univention.admin.config.config() + co = univention.admin.config.config() - baseDN=configRegistry['ldap/base'] + baseDN = configRegistry['ldap/base'] debug_level = int(configRegistry.get('directory/manager/cmd/debug/level', 0)) @@ -589,7 +593,7 @@ def _doit(arglist, out=sys.stdout): ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) print >> out, 'authentication error: %s' % (e,) raise Failed() - policyOptions.extend(['-D', binddn, '-w', bindpwd]) ## FIXME not so nice + policyOptions.extend(['-D', binddn, '-w', bindpwd]) # FIXME not so nice else: for binddn, secretFileName in ( @@ -605,34 +609,41 @@ def _doit(arglist, out=sys.stdout): policyOptions.extend(['-D', binddn, '-y', secretFileName]) try: - secretFile=open(secretFileName,'r') + secretFile = open(secretFileName, 'r') except IOError: print >> out, 'E: Permission denied, try --binddn and --bindpwd' raise Failed() - pwdLine=secretFile.readline() - pwd=re.sub('\n','',pwdLine) + pwdLine = secretFile.readline() + pwd = re.sub('\n', '', pwdLine) try: - lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=baseDN, binddn=binddn, bindpw=pwd, start_tls=tls) + lo = univention.admin.uldap.access( + host=configRegistry['ldap/master'], + port=int(configRegistry.get('ldap/master/port', '7389')), + base=baseDN, + binddn=binddn, + bindpw=pwd, + start_tls=tls + ) except Exception, e: ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) print >> out, 'authentication error: %s' % (e,) raise Failed() if not position_dn and superordinate_dn: - position_dn=superordinate_dn + position_dn = superordinate_dn elif not position_dn: - position_dn=baseDN + position_dn = baseDN try: - position=univention.admin.uldap.position(baseDN) + position = univention.admin.uldap.position(baseDN) position.setDn(position_dn) except uex.noObject: print >> out, 'E: Invalid position' raise Failed() try: - module=univention.admin.modules.get(module_name) + module = univention.admin.modules.get(module_name) except: print >> out, "failed to get module %s." % module_name print >> out, "" @@ -646,58 +657,56 @@ def _doit(arglist, out=sys.stdout): raise Failed() # initialise modules - univention.admin.modules.init(lo,position,module) + univention.admin.modules.init(lo, position, module) - information=module_information(module) + information = module_information(module) if superordinate_dn and univention.admin.modules.superordinate(module): try: - superordinate=univention.admin.objects.get(univention.admin.modules.superordinate(module), co, lo, '', dn=superordinate_dn) + superordinate = univention.admin.objects.get(univention.admin.modules.superordinate(module), co, lo, '', dn=superordinate_dn) except uex.insufficientInformation, e: print >> out, 'Insufficient Information: %s' % (e,) raise Failed() else: - superordinate=None - + superordinate = None if len(arglist) == 2: module_usage(information, out=out) raise Failed() - action=arglist[2] + action = arglist[2] if len(arglist) == 3 and action != 'list': module_usage(information, action, out=out) raise Failed() - for opt, val in opts: if opt == '--set': name, value = val.split('=', 1) value = _2utf8(value) - - for mod, (properties,options) in information.items(): + for mod, (properties, options) in information.items(): if name in properties: if properties[name].multivalue: values = input.setdefault(name, []) if value: values.append(value) else: - input[name]=value + input[name] = value break else: print >> out, "WARNING: No attribute with name '%s' in this module, value not set." % name + elif opt == '--append': name, value = val.split('=', 1) value = _2utf8(value) - for mod, (properties,options) in information.items(): + for mod, (properties, options) in information.items(): if name in properties: if properties[name].multivalue: values = append.setdefault(name, []) if value: values.append(value) else: - append[name]=value + append[name] = value break else: print >> out, "WARNING: No attribute with name %s in this module, value not appended." % name @@ -708,135 +717,135 @@ def _doit(arglist, out=sys.stdout): value = _2utf8(value) except ValueError: name, value = val, None - for mod, (properties,options) in information.items(): + for mod, (properties, options) in information.items(): if name in properties: if properties[name].multivalue: values = remove.setdefault(name, []) if value: values.append(value) else: - remove[name]=value + remove[name] = value break else: print >> out, "WARNING: No attribute with name %s in this module, value not removed." % name + elif opt == '--remove_referring': remove_referring = True + elif opt == '--recursive': recursive = True - if action in ('modify', 'edit', 'create', 'new'): - for el in policy_reference: - oc = lo.get(el, ['objectClass']) - if not oc: - print >> out, "Object to be referenced does not exist: %s" % (el,) - raise Failed() - if not 'univentionPolicy' in oc['objectClass']: - print >> out, "Object to be referenced is no valid Policy: %s" % (el,) - raise Failed() - + for el in policy_reference: + oc = lo.get(el, ['objectClass']) + if not oc: + print >> out, "Object to be referenced does not exist: %s" % (el,) + raise Failed() + if not 'univentionPolicy' in oc['objectClass']: + print >> out, "Object to be referenced is no valid Policy: %s" % (el,) + raise Failed() -#+++# ACTION CREATE #+++# + #+++# ACTION CREATE #+++# if action in ('create', 'new'): - if getattr(module, 'operations', None): - if not 'add' in module.operations: - print >> out, 'Create %s not allowed' % module_name - raise Failed() - try: - object=module.object(co, lo, position=position, superordinate=superordinate) - except uex.insufficientInformation: - print >> out, 'E: Insufficient information' - print >> out, 'Superordinate object is missing' + if getattr(module, 'operations', None): + if not 'add' in module.operations: + print >> out, 'Create %s not allowed' % module_name raise Failed() + try: + object = module.object(co, lo, position=position, superordinate=superordinate) + except uex.insufficientInformation: + print >> out, 'E: Insufficient information' + print >> out, 'Superordinate object is missing' + raise Failed() - if parsed_options: - object.options=parsed_options + if parsed_options: + object.options = parsed_options - object.open() - if getattr(object, 'open_warning', None): - print >> out, 'WARNING:%s' % object.open_warning - try: - object_input(module, object, input, append=append, out=out) - except uex.nextFreeIp: - if not ignore_exists: - print >> out, 'E: No free IP address found' - raise Failed() - except uex.valueInvalidSyntax, err: - print >> out, 'E: Invalid Syntax: %s' % err - raise Failed() - except Exception, err: - print >> out, 'E: Option %s is not valid' % err + object.open() + if getattr(object, 'open_warning', None): + print >> out, 'WARNING:%s' % object.open_warning + try: + object_input(module, object, input, append=append, out=out) + except uex.nextFreeIp: + if not ignore_exists: + print >> out, 'E: No free IP address found' raise Failed() + except uex.valueInvalidSyntax, err: + print >> out, 'E: Invalid Syntax: %s' % err + raise Failed() + except Exception, err: + print >> out, 'E: Option %s is not valid' % err + raise Failed() - exists_msg=None - try: - dn=object.create() - except uex.objectExists, dn: - exists_msg = dn - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.uidAlreadyUsed, user: - exists_msg = '(uid) %s' % user - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.groupNameAlreadyUsed, group: - exists_msg = '(group) %s' % group - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.dhcpServerAlreadyUsed, name: - exists_msg = '(dhcpserver) %s' % name - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.macAlreadyUsed, mac: - exists_msg = '(mac) %s' % mac - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.noLock, e: - exists_msg = '(nolock) %s' % str(e) - if not ignore_exists: - print >> out, 'E: Object exists: %s' % exists_msg - raise Failed() - except uex.invalidDhcpEntry: - print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' + exists_msg = None + try: + dn = object.create() + except uex.objectExists, dn: + exists_msg = dn + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except uex.invalidOptions, e: - if not ignore_exists: - print >> out, 'E: invalid Options: %s' % e - raise Failed() - except uex.insufficientInformation: - print >> out, 'E: Insufficient information' - print >> out, 'The following parameters are missing:' - for i in module.property_descriptions: - property=module.property_descriptions.get(i) - if property.required: - if i not in object or (not object[i] or (isinstance(object[i], list) and object[i]==[''])): - print >> out, i + except uex.uidAlreadyUsed, user: + exists_msg = '(uid) %s' % user + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except uex.noObject, e: - print >> out, 'E: object not found: %s' % e + except uex.groupNameAlreadyUsed, group: + exists_msg = '(group) %s' % group + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except uex.circularGroupDependency, e: - print >> out, 'E: circular group dependency detected: %s' % e + except uex.dhcpServerAlreadyUsed, name: + exists_msg = '(dhcpserver) %s' % name + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg raise Failed() + except uex.macAlreadyUsed, mac: + exists_msg = '(mac) %s' % mac + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() + except uex.noLock, e: + exists_msg = '(nolock) %s' % str(e) + if not ignore_exists: + print >> out, 'E: Object exists: %s' % exists_msg + raise Failed() + except uex.invalidDhcpEntry: + print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' + raise Failed() + except uex.invalidOptions, e: + if not ignore_exists: + print >> out, 'E: invalid Options: %s' % e + raise Failed() + except uex.insufficientInformation: + print >> out, 'E: Insufficient information' + print >> out, 'The following parameters are missing:' + for i in module.property_descriptions: + property = module.property_descriptions.get(i) + if property.required: + if i not in object or (not object[i] or (isinstance(object[i], list) and object[i] == [''])): + print >> out, i + raise Failed() + except uex.noObject, e: + print >> out, 'E: object not found: %s' % e + raise Failed() + except uex.circularGroupDependency, e: + print >> out, 'E: circular group dependency detected: %s' % e + raise Failed() - if policy_reference: - lo.modify(dn,[('objectClass','','univentionPolicyReference')]) - modlist = [('univentionPolicyReference', '', el) for el in policy_reference] - lo.modify(dn,modlist) + if policy_reference: + lo.modify(dn, [('objectClass', '', 'univentionPolicyReference')]) + modlist = [('univentionPolicyReference', '', el) for el in policy_reference] + lo.modify(dn, modlist) - if exists_msg: - print >> out, 'Object exists: %s' % exists_msg - else: - if not dn: - dn=object.dn - print >> out, 'Object created: %s' % _2utf8(dn) + if exists_msg: + print >> out, 'Object exists: %s' % exists_msg + else: + if not dn: + dn = object.dn + print >> out, 'Object created: %s' % _2utf8(dn) -#+++# ACTION MODIFY #+++# + #+++# ACTION MODIFY #+++# elif action == 'modify' or action == 'edit' or action == 'move': if not dn: print >> out, 'E: DN is missing' @@ -850,7 +859,7 @@ def _doit(arglist, out=sys.stdout): raise Failed() try: - object=univention.admin.objects.get(module, co, lo, position='', dn=dn) + object = univention.admin.objects.get(module, co, lo, position='', dn=dn) except uex.noObject: print >> out, 'E: object not found' raise Failed() @@ -875,10 +884,10 @@ def _doit(arglist, out=sys.stdout): print >> out, "position does not exsist: %s" % position_dn raise Failed() rdn, _old_base = dn.split(',', 1) - newdn="%s,%s" % (rdn,position_dn) + newdn = "%s,%s" % (rdn, position_dn) try: object.move(newdn) - object_modified+=1 + object_modified += 1 except uex.noObject: print >> out, 'E: object not found' raise Failed() @@ -895,23 +904,22 @@ def _doit(arglist, out=sys.stdout): print >> out, str(msg) raise Failed() - else: # modify - + else: # modify if any((input, append, remove, parsed_append_options, parsed_options)): if parsed_options: - object.options=parsed_options + object.options = parsed_options if parsed_append_options: for option in parsed_append_options: object.options.append(option) try: object_input(module, object, input, append, remove, out=out) - except uex.valueMayNotChange,e: + except uex.valueMayNotChange, e: print >> out, unicode(e[0]) raise Failed() if object.hasChanged(input.keys()) or object.hasChanged(append.keys()) or object.hasChanged(remove.keys()) or parsed_append_options or parsed_options: try: - dn=object.modify() - object_modified+=1 + dn = object.modify() + object_modified += 1 except uex.noObject: print >> out, 'E: object not found' raise Failed() @@ -926,10 +934,10 @@ def _doit(arglist, out=sys.stdout): raise Failed() if policy_reference: - if 'univentionPolicyReference' not in lo.get(dn,['objectClass'])['objectClass']: - lo.modify(dn,[('objectClass','','univentionPolicyReference')]) - object_modified+=1 - modlist=[] + if 'univentionPolicyReference' not in lo.get(dn, ['objectClass'])['objectClass']: + lo.modify(dn, [('objectClass', '', 'univentionPolicyReference')]) + object_modified += 1 + modlist = [] upr = lo.search(base=dn, scope='base', attr=['univentionPolicyReference'])[0][1] if 'univentionPolicyReference' not in upr: upr['univentionPolicyReference'] = [] @@ -937,15 +945,15 @@ def _doit(arglist, out=sys.stdout): if val in upr['univentionPolicyReference']: print >> out, 'WARNING: cannot append %s to univentionPolicyReference, value exists' % val else: - modlist.append(('univentionPolicyReference','',el)) + modlist.append(('univentionPolicyReference', '', el)) if modlist: - lo.modify(dn,modlist) - object_modified+=1 + lo.modify(dn, modlist) + object_modified += 1 if policy_dereference: modlist = [('univentionPolicyReference', el, '') for el in policy_dereference] - lo.modify(dn,modlist) - object_modified+=1 + lo.modify(dn, modlist) + object_modified += 1 if object_modified > 0: print >> out, 'Object modified: %s' % _2utf8(dn) @@ -953,7 +961,6 @@ def _doit(arglist, out=sys.stdout): print >> out, 'No modification: %s' % _2utf8(dn) elif action == 'remove' or action == 'delete': - if getattr(module, 'operations', None): if not 'remove' in module.operations: print >> out, 'Remove %s not allowed' % module_name @@ -961,11 +968,11 @@ def _doit(arglist, out=sys.stdout): try: if dn and filter: - object=univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=dn, filter=filter, required=1, unique=1)[0] + object = univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=dn, filter=filter, required=1, unique=1)[0] elif dn: - object=univention.admin.modules.lookup(module, co, lo, scope='base', superordinate=superordinate, base=dn, filter=filter, required=1, unique=1)[0] + object = univention.admin.modules.lookup(module, co, lo, scope='base', superordinate=superordinate, base=dn, filter=filter, required=1, unique=1)[0] elif filter: - object=univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter, required=1, unique=1)[0] + object = univention.admin.modules.lookup(module, co, lo, scope='sub', superordinate=superordinate, base=position.getDn(), filter=filter, required=1, unique=1)[0] else: print >> out, 'E: dn or filter needed' raise Failed() @@ -983,7 +990,7 @@ def _doit(arglist, out=sys.stdout): if recursive: try: object.remove(recursive) - except uex.ldapError,msg: + except uex.ldapError, msg: print >> out, str(msg) raise Failed() else: @@ -995,7 +1002,6 @@ def _doit(arglist, out=sys.stdout): print >> out, 'Object removed: %s' % _2utf8(dn) elif action == 'list' or action == 'lookup': - if getattr(module, 'operations', None): if not 'search' in module.operations: print >> out, 'Search %s not allowed' % module_name @@ -1012,7 +1018,7 @@ def _doit(arglist, out=sys.stdout): if getattr(object, 'open_warning', None): print >> out, 'WARNING: %s' % object.open_warning for key, value in object.items(): - s=module.property_descriptions[key].syntax + s = module.property_descriptions[key].syntax if module.property_descriptions[key].multivalue: for v in value: if s.tostring(v): @@ -1025,9 +1031,11 @@ def _doit(arglist, out=sys.stdout): else: print >> out, ' %s: %s' % (_2utf8(key), None) - if 'univentionPolicyReference' in lo.get(univention.admin.objects.dn(object),['objectClass'])['objectClass']: - references = lo.get( _2utf8(univention.admin.objects.dn( object ) ), - [ 'univentionPolicyReference' ] ) + if 'univentionPolicyReference' in lo.get(univention.admin.objects.dn(object), ['objectClass'])['objectClass']: + references = lo.get( + _2utf8(univention.admin.objects.dn(object)), + ['univentionPolicyReference'] + ) if references: for el in references['univentionPolicyReference']: print >> out, ' %s: %s' % ( @@ -1036,38 +1044,37 @@ def _doit(arglist, out=sys.stdout): ) if list_policies: - utf8_objectdn = _2utf8( univention.admin.objects.dn( object ) ) + utf8_objectdn = _2utf8(univention.admin.objects.dn(object)) print >> out, " Policy-based Settings:" client = get_policy(utf8_objectdn, out, policyOptions, policies_with_DN) print >> out, '' if module_name == 'dhcp/host': - subnet_module=univention.admin.modules.get('dhcp/subnet') - for subnet in univention.admin.modules.lookup(subnet_module, co, lo, scope='sub', superordinate=superordinate, base='', filter=''): - - if univention.admin.ipaddress.ip_is_in_network(subnet['subnet'], subnet['subnetmask'], object['fixedaddress'][0]): - utf8_subnet_dn = _2utf8( subnet.dn ) - print >> out, " Subnet-based Settings:" - ddict = get_policy(utf8_subnet_dn, out, policyOptions, policies_with_DN) - print >> out, '' - - print >> out, " Merged Settings:" - - for key, values in ddict.items(): - if key not in client: - client[key] = values - - if policies_with_DN: - for key, (policy, values) in client.items(): - print >> out, " Policy: " + policy - print >> out, " Attribute: " + key - for value in values: - print >> out, " Value: " + value - else: - for key, values in client.items(): - for value in values: - print >> out, " %s=%s" % (key, value) - print >> out, '' + subnet_module = univention.admin.modules.get('dhcp/subnet') + for subnet in univention.admin.modules.lookup(subnet_module, co, lo, scope='sub', superordinate=superordinate, base='', filter=''): + if univention.admin.ipaddress.ip_is_in_network(subnet['subnet'], subnet['subnetmask'], object['fixedaddress'][0]): + utf8_subnet_dn = _2utf8(subnet.dn) + print >> out, " Subnet-based Settings:" + ddict = get_policy(utf8_subnet_dn, out, policyOptions, policies_with_DN) + print >> out, '' + + print >> out, " Merged Settings:" + + for key, values in ddict.items(): + if key not in client: + client[key] = values + + if policies_with_DN: + for key, (policy, values) in client.items(): + print >> out, " Policy: " + policy + print >> out, " Attribute: " + key + for value in values: + print >> out, " Value: " + value + else: + for key, values in client.items(): + for value in values: + print >> out, " %s=%s" % (key, value) + print >> out, '' print >> out, '' except uex.ldapError, errmsg: diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server index e59f515..284f77d 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/univention-cli-server @@ -55,6 +55,7 @@ except ImportError: logfile = '' + class MyRequestHandler(SocketServer.BaseRequestHandler): """Handle request on listeneing socket to open new connection.""" def handle(self): @@ -74,10 +75,11 @@ class ForkingTCPServer(SocketServer.ForkingTCPServer): """UDM server listening on UNIX socket.""" address_family = socket.AF_UNIX allow_reuse_address = 1 + def server_bind(self): SocketServer.TCPServer.server_bind(self) _host, port = self.socket.getsockname()[:2] - self.server_name = 'localhost' #socket.getfqdn(host) + self.server_name = 'localhost' # socket.getfqdn(host) self.server_port = port @@ -104,7 +106,7 @@ def server_main(): os.kill(pid, signal.SIGCONT) except (ValueError, OSError): pid = 0 - if not pid: # no pid found or no server running + if not pid: # no pid found or no server running os.unlink(socket_path) os.unlink(runfilename) os.rmdir(socket_dir) @@ -160,7 +162,6 @@ def server_main(): if not rlist: ud.debug(ud.ADMIN, ud.INFO, 'daemon [%s] stopped after %s seconds idle' % (os.getpid(), timeout)) sys.exit(0) - finally: os.unlink(socket_path) os.unlink(runfilename) @@ -259,5 +260,6 @@ def main(): else: # parent os.waitpid(pid, os.P_NOWAIT) + if __name__ == "__main__": main() -- 1.8.4.rc3 From 11500099eeb19681a4e6f93c759110c02eedc51a Mon Sep 17 00:00:00 2001 Message-Id: <11500099eeb19681a4e6f93c759110c02eedc51a.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:09:22 +0100 Subject: [PATCH 32/33] Bug #33224: pep8 exception renaming Organization: Univention GmbH, Bremen, Germany Use "ex" consistently to name exception instances. --- .../modules/univention/admincli/admin.py | 108 ++++++++++----------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 3ae7ee1..53f3fd1 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -287,8 +287,8 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout tmp = list(values) tmp.append(val) object[key] = list(tmp) - except uex.valueInvalidSyntax, errmsg: - print >> out, 'E: Invalid Syntax: %s' % (errmsg,) + except uex.valueInvalidSyntax, ex: + print >> out, 'E: Invalid Syntax: %s' % (ex,) if remove: for key, value in remove.items(): if univention.admin.syntax.is_syntax(module.property_descriptions[key].syntax, univention.admin.syntax.complex): @@ -365,10 +365,10 @@ def object_input(module, object, input, append=None, remove=None, out=sys.stdout else: try: object[key] = value - except uex.ipOverridesNetwork, e: - print >> out, 'WARNING: %s' % e.message - except uex.valueMayNotChange, e: - raise uex.valueMayNotChange, "%s: %s" % (e.message, key) + except uex.ipOverridesNetwork, ex: + print >> out, 'WARNING: %s' % ex.message + except uex.valueMayNotChange, ex: + raise uex.valueMayNotChange, "%s: %s" % (ex.message, key) def list_available_modules(out=sys.stdout): @@ -382,17 +382,17 @@ def list_available_modules(out=sys.stdout): def doit(arglist, out=sys.stdout): try: _doit(arglist, out) - except uex.base, e: + except uex.base, ex: ud.debug(ud.ADMIN, ud.WARN, traceback.format_exc()) # collect error information msg = [] - if getattr(e, 'message', None): - msg.append(e.message) - if getattr(e, 'args', None): + if getattr(ex, 'message', None): + msg.append(ex.message) + if getattr(ex, 'args', None): #avoid duplicate messages - if not len(msg) or len(e.args) > 1 or e.args[0] != msg[0]: - msg.extend(e.args) + if not len(msg) or len(ex.args) > 1 or ex.args[0] != msg[0]: + msg.extend(ex.args) # strip elements and make sure that a ':' is printed iff further information follows msg = [_.strip() for _ in msg] @@ -489,8 +489,8 @@ def _doit(arglist, out=sys.stdout): ] try: opts, args = getopt.getopt(arglist[3:], '', longopts) - except getopt.error, msg: - print >> out, msg + except getopt.error, ex: + print >> out, ex raise Failed() if args and isinstance(args, list): @@ -536,8 +536,8 @@ def _doit(arglist, out=sys.stdout): try: with open(val) as fp: bindpwd = fp.read().strip() - except IOError as e: - print >> out, 'E: could not read bindpwd from file (%s)' % (e,) + except IOError as ex: + print >> out, 'E: could not read bindpwd from file (%s)' % (ex,) raise Failed() elif opt == '--dn': dn = _2utf8(val) @@ -589,9 +589,9 @@ def _doit(arglist, out=sys.stdout): start_tls=tls, bindpw=bindpwd ) - except Exception, e: - ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) - print >> out, 'authentication error: %s' % (e,) + except Exception, ex: + ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % (ex,)) + print >> out, 'authentication error: %s' % (ex,) raise Failed() policyOptions.extend(['-D', binddn, '-w', bindpwd]) # FIXME not so nice @@ -625,9 +625,9 @@ def _doit(arglist, out=sys.stdout): bindpw=pwd, start_tls=tls ) - except Exception, e: - ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % str(e)) - print >> out, 'authentication error: %s' % (e,) + except Exception, ex: + ud.debug(ud.ADMIN, ud.WARN, 'authentication error: %s' % (ex,)) + print >> out, 'authentication error: %s' % (ex,) raise Failed() if not position_dn and superordinate_dn: @@ -664,8 +664,8 @@ def _doit(arglist, out=sys.stdout): if superordinate_dn and univention.admin.modules.superordinate(module): try: superordinate = univention.admin.objects.get(univention.admin.modules.superordinate(module), co, lo, '', dn=superordinate_dn) - except uex.insufficientInformation, e: - print >> out, 'Insufficient Information: %s' % (e,) + except uex.insufficientInformation, ex: + print >> out, 'Insufficient Information: %s' % (ex,) raise Failed() else: superordinate = None @@ -770,11 +770,11 @@ def _doit(arglist, out=sys.stdout): if not ignore_exists: print >> out, 'E: No free IP address found' raise Failed() - except uex.valueInvalidSyntax, err: - print >> out, 'E: Invalid Syntax: %s' % err + except uex.valueInvalidSyntax, ex: + print >> out, 'E: Invalid Syntax: %s' % ex raise Failed() - except Exception, err: - print >> out, 'E: Option %s is not valid' % err + except Exception, ex: + print >> out, 'E: Option %s is not valid' % ex raise Failed() exists_msg = None @@ -805,17 +805,17 @@ def _doit(arglist, out=sys.stdout): if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() - except uex.noLock, e: - exists_msg = '(nolock) %s' % str(e) + except uex.noLock, ex: + exists_msg = '(nolock) %s' % (ex,) if not ignore_exists: print >> out, 'E: Object exists: %s' % exists_msg raise Failed() except uex.invalidDhcpEntry: print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' raise Failed() - except uex.invalidOptions, e: + except uex.invalidOptions, ex: if not ignore_exists: - print >> out, 'E: invalid Options: %s' % e + print >> out, 'E: invalid Options: %s' % ex raise Failed() except uex.insufficientInformation: print >> out, 'E: Insufficient information' @@ -826,11 +826,11 @@ def _doit(arglist, out=sys.stdout): if i not in object or (not object[i] or (isinstance(object[i], list) and object[i] == [''])): print >> out, i raise Failed() - except uex.noObject, e: - print >> out, 'E: object not found: %s' % e + except uex.noObject, ex: + print >> out, 'E: object not found: %s' % ex raise Failed() - except uex.circularGroupDependency, e: - print >> out, 'E: circular group dependency detected: %s' % e + except uex.circularGroupDependency, ex: + print >> out, 'E: circular group dependency detected: %s' % ex raise Failed() if policy_reference: @@ -891,17 +891,17 @@ def _doit(arglist, out=sys.stdout): except uex.noObject: print >> out, 'E: object not found' raise Failed() - except uex.ldapError, msg: - print >> out, "ldap Error: %s" % msg + except uex.ldapError, ex: + print >> out, "ldap Error: %s" % ex raise Failed() except uex.nextFreeIp: print >> out, 'E: No free IP address found' raise Failed() - except uex.valueInvalidSyntax, err: - print >> out, 'E: Invalid Syntax: %s' % err + except uex.valueInvalidSyntax, ex: + print >> out, 'E: Invalid Syntax: %s' % ex raise Failed() - except uex.invalidOperation, msg: - print >> out, str(msg) + except uex.invalidOperation, ex: + print >> out, ex raise Failed() else: # modify @@ -913,8 +913,8 @@ def _doit(arglist, out=sys.stdout): object.options.append(option) try: object_input(module, object, input, append, remove, out=out) - except uex.valueMayNotChange, e: - print >> out, unicode(e[0]) + except uex.valueMayNotChange, ex: + print >> out, unicode(ex[0]) raise Failed() if object.hasChanged(input.keys()) or object.hasChanged(append.keys()) or object.hasChanged(remove.keys()) or parsed_append_options or parsed_options: try: @@ -926,11 +926,11 @@ def _doit(arglist, out=sys.stdout): except uex.invalidDhcpEntry: print >> out, 'E: The DHCP entry for this host should contain the zone dn, the ip address and the mac address.' raise Failed() - except uex.circularGroupDependency, e: - print >> out, 'E: circular group dependency detected: %s' % e + except uex.circularGroupDependency, ex: + print >> out, 'E: circular group dependency detected: %s' % ex raise Failed() - except uex.valueInvalidSyntax, e: - print >> out, 'E: Invalid Syntax: %s' % e + except uex.valueInvalidSyntax, ex: + print >> out, 'E: Invalid Syntax: %s' % ex raise Failed() if policy_reference: @@ -990,8 +990,8 @@ def _doit(arglist, out=sys.stdout): if recursive: try: object.remove(recursive) - except uex.ldapError, msg: - print >> out, str(msg) + except uex.ldapError, ex: + print >> out, ex raise Failed() else: try: @@ -1077,11 +1077,11 @@ def _doit(arglist, out=sys.stdout): print >> out, '' print >> out, '' - except uex.ldapError, errmsg: - print >> out, '%s' % (errmsg,) + except uex.ldapError, ex: + print >> out, ex raise Failed() - except uex.valueInvalidSyntax, errmsg: - print >> out, '%s' % (errmsg.message,) + except uex.valueInvalidSyntax, ex: + print >> out, ex.message raise Failed() else: print >> out, "Unknown or no action defined" -- 1.8.4.rc3 From 0fe04d98118249ee602c088ba8cf3810ddef6c77 Mon Sep 17 00:00:00 2001 Message-Id: <0fe04d98118249ee602c088ba8cf3810ddef6c77.1385969664.git.hahn@univention.de> In-Reply-To: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> References: <15a9cbb94b92dd00848a2e8b77a3b53ca4d65113.1385969664.git.hahn@univention.de> From: Philipp Hahn Date: Fri, 22 Nov 2013 07:11:31 +0100 Subject: [PATCH 33/33] Bug #33224: WIP Convert to argparse Organization: Univention GmbH, Bremen, Germany Use argpase to handle command line parsing instead of using the old getopt interface and a lot of hand-rolled code. --- .../modules/univention/admincli/admin.py | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py index 53f3fd1..c358556 100755 --- a/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py +++ b/branches/ucs-3.2/ucs-3.2-0/management/univention-directory-manager-modules/modules/univention/admincli/admin.py @@ -32,6 +32,7 @@ from textwrap import dedent import sys +from argparse import ArgumentParser import getopt import re import base64 @@ -56,6 +57,153 @@ univention.admin.modules.update() univention.admin.syntax.update_choices() +def parse_commandline(): + usage = dedent("""\ + %(prog)s module action [arguments] + %(prog)s [--help] [--version] + """) + description = dedent("""\ + univention-directory-manager is a tool to handle the configuration for UCS + on command line level. + Use "univention-directory-manager modules" for a list of available modules. + """) + version = "%(prog)s @%@package_version@%@" + parser = ArgumentParser( + usage=usage, + description=description, + prog="univention-directory-manager") + parser.add_argument("-?", action="help") + parser.add_argument("--version", action="version", version=version) + + general_opt = parser.add_argument_group("General options") + general_opt.add_argument( + "--logfile", + dest="logfile", + default='/var/log/univention/directory-manager-cmd.log', + help="path and name of the logfile to be used") + general_opt.add_argument( + "--binddn", + dest="binddn", + help="bind DN") + general_opt.add_argument( + "--bindpwd", + dest="bindpwd", + help="bind password") + general_opt.add_argument( + "--bindpwdfile", + dest="bindpwdfile", + help="file containing bind password") + general_opt.add_argument( + "--tls", + dest="tls", type=int, + default=2, choices=range(3), + help="0 (no); 1 (try); 2 (must)") + + subparsers = parser.add_subparsers( + title="sub-commands", + description="valid subcommands", + help="additional help") + + create_opt = subparsers.add_parser("modules", help="List available modules") + + create_opt = subparsers.add_parser("create", help="Create a new object") + create_opt.add_argument( + "--position", + dest="position", + help="Set position in tree") + create_opt.add_argument( + "--set", + action="append", dest="set", required=True, + help="Set variable to value, e.g. foo=bar") + create_opt.add_argument( + "--superordinate", + dest="superordinate", + help="Use superordinate module") + create_opt.add_argument( + "--option", + action="append", dest="option", + help="Use only given module options") + create_opt.add_argument( + "--policy-reference", + action="append", dest="policy_reference", + help="Reference to policy given by DN") + create_opt.add_argument( + "--ignore_exists", + action="store_true", dest="ignore_exists", + help="Ignore already existing object") + + modify_opt = subparsers.add_parser("modify", help="Modify an existing object") + modify_opt.add_argument( + "--dn", + dest="dn", required=True, + help="Edit object with DN") + modify_opt.add_argument( + "--set", + action="append", dest="set", + help="Set variable to value, e.g. foo=bar") + modify_opt.add_argument( + "--append", + action="append", dest="append", + help="Append value to variable, e.g. foo=bar") + modify_opt.add_argument( + "--remove", + action="append", dest="remove", + help="Remove value from variable, e.g. foo=bar") + modify_opt.add_argument( + "--option", + action="append", dest="option", + help="Append the module options") + modify_opt.add_argument( + "--policy-reference", + action="append", dest="policy_reference", + help="Reference to policy given by DN") + modify_opt.add_argument( + "--policy-dereference", + action="append", dest="policy_dereference", + help="Remove reference to policy given by DN") + + remove_opt = subparsers.add_parser("remove", help="Remove an existing object") + remove_opt.add_argument( + "--dn", + dest="dn", + help="Remove object with DN") + remove_opt.add_argument( + "--superordinate", + dest="superordinate", + help="Use superordinate module") + remove_opt.add_argument( + "--filter", + dest="filter", + help="Lookup filter e.g. foo=bar") + remove_opt.add_argument( + "--remove_referring", + action="store_true", dest="remove_referring", + help="Remove referring objects") + + list_opt = subparsers.add_parser("list", help="List objects") + list_opt.add_argument( + "--filter", + dest="filter", + help="Lookup filter e.g. foo=bar") + list_opt.add_argument( + "--policies", + dest="policies", type=int, choices=range(2), + help="List policy-based settings: 0:short, 1:long (with policy-DN)") + + move_opt = subparsers.add_parser("move", help="Move object in directory tree") + move_opt.add_argument( + "--dn", + dest="dn", required=True, + help="Move object with DN") + move_opt.add_argument( + "--position", + dest="position", required=True, + help="Move to position in tree") + + args = parser.parse_args() + return args + + class Failed(Exception): def __str__(self): return "OPERATION FAILED" -- 1.8.4.rc3