diff options
author | Andrew Tridgell <tridge@samba.org> | 2010-11-29 14:15:57 +1100 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2010-11-29 18:04:42 +1100 |
commit | 37bfc4ec384df71a4cb1c19ceb136fecd3b9afc6 (patch) | |
tree | e44efedee115beac377d1fc9ab3a22c5e6cc2cee | |
parent | 62506903101e8e8e1cfc6c70fee245f97c646844 (diff) | |
download | samba-37bfc4ec384df71a4cb1c19ceb136fecd3b9afc6.tar.gz samba-37bfc4ec384df71a4cb1c19ceb136fecd3b9afc6.tar.bz2 samba-37bfc4ec384df71a4cb1c19ceb136fecd3b9afc6.zip |
s4-samba-tool: fixed exception handling in subcommands
this switches to the new pattern of:
except Exception, e:
raise CommandError("some error message", e)
-rw-r--r-- | source4/scripting/python/samba/netcmd/drs.py | 20 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/gpo.py | 21 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/group.py | 20 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/newuser.py | 5 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/ntacl.py | 5 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/pwsettings.py | 4 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/rodc.py | 8 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/setpassword.py | 5 | ||||
-rw-r--r-- | source4/scripting/python/samba/ntacls.py | 4 | ||||
-rw-r--r-- | source4/scripting/python/samba/provision/__init__.py | 22 | ||||
-rw-r--r-- | source4/scripting/python/samba/samdb.py | 10 | ||||
-rw-r--r-- | source4/scripting/python/samba/schema.py | 2 | ||||
-rwxr-xr-x | source4/scripting/python/samba/upgradehelpers.py | 16 |
13 files changed, 68 insertions, 74 deletions
diff --git a/source4/scripting/python/samba/netcmd/drs.py b/source4/scripting/python/samba/netcmd/drs.py index 387524b943..6f5b5b8ed3 100644 --- a/source4/scripting/python/samba/netcmd/drs.py +++ b/source4/scripting/python/samba/netcmd/drs.py @@ -44,8 +44,8 @@ def drsuapi_connect(ctx): try: ctx.drsuapi = drsuapi.drsuapi(binding_string, ctx.lp, ctx.creds) (ctx.drsuapi_handle, ctx.bind_supported_extensions) = drs_utils.drs_DsBind(ctx.drsuapi) - except Exception, estr: - raise CommandError("DRS connection to %s failed - %s" % (ctx.server, estr)) + except Exception, e: + raise CommandError("DRS connection to %s failed" % ctx.server, e) def samdb_connect(ctx): @@ -54,8 +54,8 @@ def samdb_connect(ctx): ctx.samdb = SamDB(url="ldap://%s" % ctx.server, session_info=system_session(), credentials=ctx.creds, lp=ctx.lp) - except Exception, estr: - raise CommandError("LDAP connection to %s failed - %s" % (ctx.server, estr)) + except Exception, e: + raise CommandError("LDAP connection to %s failed" % ctx.server, e) def drs_errmsg(werr): @@ -119,8 +119,8 @@ class cmd_drs_showrepl(Command): req1.info_type = info_type try: (info_type, info) = ctx.drsuapi.DsReplicaGetInfo(ctx.drsuapi_handle, 1, req1) - except Exception, estr: - raise CommandError("DsReplicaGetInfo failed : %s" % estr) + except Exception, e: + raise CommandError("DsReplicaGetInfo of type %u failed" % info_type, e) return (info_type, info) @@ -221,8 +221,8 @@ class cmd_drs_kcc(Command): req1 = drsuapi.DsExecuteKCC1() try: self.drsuapi.DsExecuteKCC(self.drsuapi_handle, 1, req1) - except Exception, (ecode, estr): - raise CommandError("DsExecuteKCC failed - %s" % estr) + except Exception, e: + raise CommandError("DsExecuteKCC failed", e) print("Consistency check on %s successful." % DC) @@ -289,8 +289,8 @@ class cmd_drs_replicate(Command): try: self.drsuapi.DsReplicaSync(self.drsuapi_handle, 1, req1) - except Exception, (ecode, estr): - raise CommandError("DsReplicaSync failed - %s" % estr) + except Exception, e: + raise CommandError("DsReplicaSync failed", estr) print("Replicate from %s to %s was successful." % (SOURCE_DC, DEST_DC)) diff --git a/source4/scripting/python/samba/netcmd/gpo.py b/source4/scripting/python/samba/netcmd/gpo.py index 90b7a5efaa..5e8748adc5 100644 --- a/source4/scripting/python/samba/netcmd/gpo.py +++ b/source4/scripting/python/samba/netcmd/gpo.py @@ -41,8 +41,8 @@ def samdb_connect(ctx): ctx.samdb = SamDB(url=ctx.url, session_info=system_session(), credentials=ctx.creds, lp=ctx.lp) - except Exception, estr: - raise CommandError("LDAP connection to %s failed - %s" % (ctx.url, estr)) + except Exception, e: + raise CommandError("LDAP connection to %s failed " % ctx.url, e) def attr_default(msg, attrname, default): @@ -114,9 +114,12 @@ class cmd_listall(Command): ("GPO_FLAG_USER_DISABLE", dsdb.GPO_FLAG_USER_DISABLE ), ( "GPO_FLAG_MACHINE_DISABLE", dsdb.GPO_FLAG_MACHINE_DISABLE ) ] - msg = self.samdb.search(base=policies_dn, scope=ldb.SCOPE_ONELEVEL, - expression="(objectClass=groupPolicyContainer)", - attrs=['nTSecurityDescriptor', 'versionNumber', 'flags', 'name', 'displayName', 'gPCFileSysPath']) + try: + msg = self.samdb.search(base=policies_dn, scope=ldb.SCOPE_ONELEVEL, + expression="(objectClass=groupPolicyContainer)", + attrs=['nTSecurityDescriptor', 'versionNumber', 'flags', 'name', 'displayName', 'gPCFileSysPath']) + except Exception, e: + raise CommandError("Failed to list policies in %s" % policies_dn, e) for m in msg: print("GPO : %s" % m['name'][0]) print("display name : %s" % m['displayName'][0]) @@ -158,15 +161,15 @@ class cmd_list(Command): try: user_dn = self.samdb.search(expression='(&(samAccountName=%s)(objectclass=User))' % username)[0].dn - except Exception, estr: - raise CommandError("Failed to find user %s - %s" % (username, estr)) + except Exception, e: + raise CommandError("Failed to find user %s" % username, e) # check if its a computer account try: msg = self.samdb.search(base=user_dn, scope=ldb.SCOPE_BASE, attrs=['objectClass'])[0] is_computer = 'computer' in msg['objectClass'] - except Exception, estr: - raise CommandError("Failed to find objectClass for user %s - %s" % (username, estr)) + except Exception, e: + raise CommandError("Failed to find objectClass for user %s" % username, e) print("TODO: get user token") # token = self.samdb.get_user_token(username) diff --git a/source4/scripting/python/samba/netcmd/group.py b/source4/scripting/python/samba/netcmd/group.py index 3a467fb28c..57f31cd511 100644 --- a/source4/scripting/python/samba/netcmd/group.py +++ b/source4/scripting/python/samba/netcmd/group.py @@ -83,9 +83,8 @@ class cmd_group_add(Command): credentials=creds, lp=lp) samdb.newgroup(groupname, groupou=groupou, grouptype = gtype, description=description, mailaddress=mail_address, notes=notes) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to create group "%s" : %s' % ( - groupname, msg)) + except Exception, e: + raise CommandError('Failed to create group "%s"' % groupname, e) class cmd_group_delete(Command): @@ -114,9 +113,8 @@ class cmd_group_delete(Command): samdb = SamDB(url=H, session_info=system_session(), credentials=creds, lp=lp) samdb.deletegroup(groupname) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to remove group "%s": %s' % ( - groupname , msg)) + except Exception, e: + raise CommandError('Failed to remove group "%s"' % groupname, e) class cmd_group_add_members(Command): @@ -146,9 +144,8 @@ class cmd_group_add_members(Command): samdb = SamDB(url=H, session_info=system_session(), credentials=creds, lp=lp) samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to add members "%s" to group "%s": %s' % ( - listofmembers, groupname , msg)) + except Exception, e: + raise CommandError('Failed to add members "%s" to group "%s"' % (listofmembers, groupname), e) class cmd_group_remove_members(Command): @@ -178,9 +175,8 @@ class cmd_group_remove_members(Command): samdb = SamDB(url=H, session_info=system_session(), credentials=creds, lp=lp) samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to remove members "%s" from group "%s": %s' % ( - listofmembers, groupname , msg)) + except Exception, e: + raise CommandError('Failed to remove members "%s" from group "%s"' % (listofmembers, groupname), e) class cmd_group(SuperCommand): diff --git a/source4/scripting/python/samba/netcmd/newuser.py b/source4/scripting/python/samba/netcmd/newuser.py index 693568dee2..05c7bfc1bb 100644 --- a/source4/scripting/python/samba/netcmd/newuser.py +++ b/source4/scripting/python/samba/netcmd/newuser.py @@ -91,7 +91,6 @@ class cmd_newuser(Command): jobtitle=job_title, department=department, company=company, description=description, mailaddress=mail_address, internetaddress=internet_address, telephonenumber=telephone_number, physicaldeliveryoffice=physical_delivery_office) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to create user "%s" : %s' % ( - username, msg)) + except Exception, e: + raise CommandError('Failed to create user "%s"' % username, e) diff --git a/source4/scripting/python/samba/netcmd/ntacl.py b/source4/scripting/python/samba/netcmd/ntacl.py index 8365658761..49f8fbc77f 100644 --- a/source4/scripting/python/samba/netcmd/ntacl.py +++ b/source4/scripting/python/samba/netcmd/ntacl.py @@ -65,9 +65,8 @@ class cmd_acl_set(Command): try: ldb = Ldb(path, session_info=system_session(), credentials=creds, lp=lp) - except: - # XXX: Should catch a particular exception type - raise CommandError("Unable to read domain SID from configuration files") + except Exception, e: + raise CommandError("Unable to read domain SID from configuration files", e) attrs = ["objectSid"] print lp.get("realm") res = ldb.search(expression="(objectClass=*)", diff --git a/source4/scripting/python/samba/netcmd/pwsettings.py b/source4/scripting/python/samba/netcmd/pwsettings.py index 4a1645dd91..3f1a8e189d 100644 --- a/source4/scripting/python/samba/netcmd/pwsettings.py +++ b/source4/scripting/python/samba/netcmd/pwsettings.py @@ -86,8 +86,8 @@ class cmd_pwsettings(Command): # ticks -> days cur_min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24)) cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24)) - except KeyError: - raise CommandError("Could not retrieve password properties!") + except Exception, e: + raise CommandError("Could not retrieve password properties!", e) if subcommand == "show": self.message("Password informations for domain '%s'" % domain_dn) diff --git a/source4/scripting/python/samba/netcmd/rodc.py b/source4/scripting/python/samba/netcmd/rodc.py index 5f97c32b28..b1f9d66d1f 100644 --- a/source4/scripting/python/samba/netcmd/rodc.py +++ b/source4/scripting/python/samba/netcmd/rodc.py @@ -106,12 +106,8 @@ class cmd_rodc_preload(Command): try: repl.replicate(dn, source_dsa_invocation_id, destination_dsa_guid, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET, rodc=True) - except RuntimeError, (ecode, estring): - if estring == 'WERR_DS_DRA_ACCESS_DENIED': - local_samdb.transaction_cancel() - raise CommandError("Access denied replicating DN %s" % dn) - else: - raise + except Exception, e: + raise CommandError("Error replicating DN %s" % dn, e) local_samdb.transaction_commit() diff --git a/source4/scripting/python/samba/netcmd/setpassword.py b/source4/scripting/python/samba/netcmd/setpassword.py index 047a95afae..b32b651822 100644 --- a/source4/scripting/python/samba/netcmd/setpassword.py +++ b/source4/scripting/python/samba/netcmd/setpassword.py @@ -75,7 +75,6 @@ class cmd_setpassword(Command): samdb.setpassword(filter, password, force_change_at_next_login=must_change_at_next_login, username=username) - except ldb.LdbError, (num, msg): - raise CommandError('Failed to set password for user "%s" - %s' % - (username, msg)) + except Exception, e: + raise CommandError('Failed to set password for user "%s"' % username, e) print "Changed password OK" diff --git a/source4/scripting/python/samba/ntacls.py b/source4/scripting/python/samba/ntacls.py index 0667a5199c..78365e98b8 100644 --- a/source4/scripting/python/samba/ntacls.py +++ b/source4/scripting/python/samba/ntacls.py @@ -51,7 +51,7 @@ def getntacl(lp, file, backend=None, eadbfile=None): try: attribute = samba.xattr_tdb.wrap_getxattr(eadbname, file, xattr.XATTR_NTACL_NAME) - except: + except Exception: # FIXME: Don't catch all exceptions, just those related to opening # xattrdb print "Fail to open %s" % eadbname @@ -75,7 +75,7 @@ def setntacl(lp, file, sddl, domsid, backend=None, eadbfile=None): try: samba.xattr_tdb.wrap_setxattr(eadbname, file, xattr.XATTR_NTACL_NAME, ndr_pack(ntacl)) - except: + except Exception: # FIXME: Don't catch all exceptions, just those related to opening # xattrdb print "Fail to open %s" % eadbname diff --git a/source4/scripting/python/samba/provision/__init__.py b/source4/scripting/python/samba/provision/__init__.py index 37d99a519d..e184ad8340 100644 --- a/source4/scripting/python/samba/provision/__init__.py +++ b/source4/scripting/python/samba/provision/__init__.py @@ -404,7 +404,7 @@ def setup_ldb(ldb, ldif_path, subst_vars): ldb.transaction_start() try: setup_add_ldif(ldb, ldif_path, subst_vars) - except: + except Exception: ldb.transaction_cancel() raise else: @@ -728,7 +728,7 @@ def setup_samdb_partitions(samdb_path, setup_path, logger, lp, session_info, logger.info("Setting up sam.ldb rootDSE") setup_samdb_rootdse(samdb, setup_path, names) - except: + except Exception: samdb.transaction_cancel() raise else: @@ -899,7 +899,7 @@ def setup_secretsdb(paths, setup_path, session_info, backend_credentials, lp): }) return secrets_ldb - except: + except Exception: secrets_ldb.transaction_cancel() raise @@ -1200,7 +1200,7 @@ def setup_samdb(path, setup_path, session_info, provision_backend, lp, names, {"SCHEMADN": names.schemadn}) logger.info("Reopening sam.ldb with new schema") - except: + except Exception: samdb.transaction_cancel() raise else: @@ -1302,7 +1302,7 @@ def setup_samdb(path, setup_path, session_info, provision_backend, lp, names, names.ntdsguid = samdb.searchone(basedn=ntds_dn, attribute="objectGUID", expression="", scope=ldb.SCOPE_BASE) assert isinstance(names.ntdsguid, str) - except: + except Exception: samdb.transaction_cancel() raise else: @@ -1369,7 +1369,7 @@ def setsysvolacl(samdb, netlogon, sysvol, gid, domainsid, dnsdomain, domaindn, try: os.chown(sysvol, -1, gid) - except: + except OSError: canchown = False else: canchown = True @@ -1641,9 +1641,11 @@ def provision(setup_dir, logger, session_info, credentials, smbconf=None, elements=kerberos_enctypes, flags=ldb.FLAG_MOD_REPLACE, name="msDS-SupportedEncryptionTypes") samdb.modify(msg) - except ldb.LdbError, (ldb.ERR_NO_SUCH_ATTRIBUTE, _): - # It might be that this attribute does not exist in this schema - pass + except ldb.LdbError, (enum, estr): + if enum == ldb.ERR_NO_SUCH_ATTRIBUTE: + # It might be that this attribute does not exist in this schema + pass + raise if serverrole == "domain controller": secretsdb_setup_dns(secrets_ldb, setup_path, names, @@ -1694,7 +1696,7 @@ def provision(setup_dir, logger, session_info, credentials, smbconf=None, create_phpldapadmin_config(paths.phpldapadminconfig, setup_path, ldapi_url) - except: + except Exception: secrets_ldb.transaction_cancel() raise diff --git a/source4/scripting/python/samba/samdb.py b/source4/scripting/python/samba/samdb.py index 43b1938a0e..67e77e303b 100644 --- a/source4/scripting/python/samba/samdb.py +++ b/source4/scripting/python/samba/samdb.py @@ -169,7 +169,7 @@ pwdLastSet: 0 raise Exception('Unable to find group "%s"' % groupname) assert(len(targetgroup) == 1) self.delete(targetgroup[0].dn) - except: + except Exception: self.transaction_cancel() raise else: @@ -225,7 +225,7 @@ member: %s if modified is True: self.modify_ldif(addtargettogroup) - except: + except Exception: self.transaction_cancel() raise else: @@ -351,7 +351,7 @@ member: %s if setpassword: self.setpassword("(samAccountName=%s)" % username, password, force_password_change_at_next_login_req) - except: + except Exception: self.transaction_cancel() raise else: @@ -389,7 +389,7 @@ unicodePwd:: %s # modify the userAccountControl to remove the disabled bit self.enable_account(search_filter) - except: + except Exception: self.transaction_cancel() raise else: @@ -430,7 +430,7 @@ accountExpires: %u """ % (user_dn, userAccountControl, accountExpires) self.modify_ldif(setexp) - except: + except Exception: self.transaction_cancel() raise else: diff --git a/source4/scripting/python/samba/schema.py b/source4/scripting/python/samba/schema.py index 0bc60fa321..c82e70daf6 100644 --- a/source4/scripting/python/samba/schema.py +++ b/source4/scripting/python/samba/schema.py @@ -136,7 +136,7 @@ dn: @INDEXLIST self.ldb.add_ldif(self.schema_dn_add) self.ldb.modify_ldif(self.schema_dn_modify) self.ldb.add_ldif(self.schema_data) - except: + except Exception: self.ldb.transaction_cancel() raise else: diff --git a/source4/scripting/python/samba/upgradehelpers.py b/source4/scripting/python/samba/upgradehelpers.py index b6e7de8c8f..ded3ff1307 100755 --- a/source4/scripting/python/samba/upgradehelpers.py +++ b/source4/scripting/python/samba/upgradehelpers.py @@ -91,22 +91,22 @@ class ProvisionLDB(object): ok = True try: self.sam.transaction_cancel() - except: + except Exception: ok = False try: self.secrets.transaction_cancel() - except: + except Exception: ok = False try: self.idmap.transaction_cancel() - except: + except Exception: ok = False try: self.privilege.transaction_cancel() - except: + except Exception: ok = False return ok @@ -122,7 +122,7 @@ class ProvisionLDB(object): self.secrets.transaction_prepare_commit() self.idmap.transaction_prepare_commit() self.privilege.transaction_prepare_commit() - except: + except Exception: return self.groupedRollback() # TO BE DONE # self.hkcr.transaction_prepare_commit() @@ -134,7 +134,7 @@ class ProvisionLDB(object): self.secrets.transaction_commit() self.idmap.transaction_commit() self.privilege.transaction_commit() - except: + except Exception: return self.groupedRollback() # TO BE DONE @@ -688,13 +688,13 @@ def update_gpo(paths, samdb, names, lp, message, force=0): try: attribute = samba.xattr_tdb.wrap_getxattr(eadbname, paths.sysvol, xattr.XATTR_NTACL_NAME) - except: + except Exception: attribute = samba.xattr_native.wrap_getxattr(paths.sysvol, xattr.XATTR_NTACL_NAME) else: attribute = samba.xattr_native.wrap_getxattr(paths.sysvol, xattr.XATTR_NTACL_NAME) - except: + except Exception: resetacls = True if force: |