Commit 9cd33ca4 authored by Julien Muchembled's avatar Julien Muchembled

Simplify code by using some new Python syntax

- PEP 308: Conditional Expressions
- PEP 341: Unified try/except/finally
- PEP 343: The ‘with’ statement
parent 48212534
......@@ -79,16 +79,15 @@ class ActivityBuffer(TM):
def _finish(self):
# LOG('ActivityBuffer', 0, '_finish %r' % (self,))
try:
try:
# Try to push / delete all messages
for activity, message in self.flushed_activity:
activity.finishDeleteMessage(self._activity_tool_path, message)
for activity, message in self.queued_activity:
activity.finishQueueMessage(self._activity_tool_path, message)
except:
LOG('ActivityBuffer', ERROR, "exception during _finish",
error=sys.exc_info())
raise
# Try to push / delete all messages
for activity, message in self.flushed_activity:
activity.finishDeleteMessage(self._activity_tool_path, message)
for activity, message in self.queued_activity:
activity.finishQueueMessage(self._activity_tool_path, message)
except:
LOG('ActivityBuffer', ERROR, "exception during _finish",
error=sys.exc_info())
raise
finally:
self._clear()
......
......@@ -877,16 +877,14 @@ class ActivityTool (Folder, UniqueObject):
LOG('CMFActivity', INFO, "Shutdown: Activities finished.")
def process_timer(self, tick, interval, prev="", next=""):
"""
Call distribute() if we are the Distributing Node and call tic()
with our node number.
This method is called by TimerService in the interval given
in zope.conf. The Default is every 5 seconds.
"""
# Prevent TimerService from starting multiple threads in parallel
acquired = timerservice_lock.acquire(0)
if not acquired:
return
"""
Call distribute() if we are the Distributing Node and call tic()
with our node number.
This method is called by TimerService in the interval given
in zope.conf. The Default is every 5 seconds.
"""
# Prevent TimerService from starting multiple threads in parallel
if timerservice_lock.acquire(0):
try:
# make sure our skin is set-up. On CMF 1.5 it's setup by acquisition,
# but on 2.2 it's by traversal, and our site probably wasn't traversed
......@@ -896,37 +894,37 @@ class ActivityTool (Folder, UniqueObject):
self.setupCurrentSkin(self.REQUEST)
old_sm = getSecurityManager()
try:
# get owner of portal_catalog, so normally we should be able to
# have the permission to invoke all activities
user = self.portal_catalog.getWrappedOwner()
newSecurityManager(self.REQUEST, user)
currentNode = self.getCurrentNode()
self.registerNode(currentNode)
processing_node_list = self.getNodeList(role=ROLE_PROCESSING)
# only distribute when we are the distributingNode
if self.getDistributingNode() == currentNode:
self.distribute(len(processing_node_list))
# SkinsTool uses a REQUEST cache to store skin objects, as
# with TimerService we have the same REQUEST over multiple
# portals, we clear this cache to make sure the cache doesn't
# contains skins from another portal.
try:
# get owner of portal_catalog, so normally we should be able to
# have the permission to invoke all activities
user = self.portal_catalog.getWrappedOwner()
newSecurityManager(self.REQUEST, user)
currentNode = self.getCurrentNode()
self.registerNode(currentNode)
processing_node_list = self.getNodeList(role=ROLE_PROCESSING)
# only distribute when we are the distributingNode
if (self.getDistributingNode() == currentNode):
self.distribute(len(processing_node_list))
# SkinsTool uses a REQUEST cache to store skin objects, as
# with TimerService we have the same REQUEST over multiple
# portals, we clear this cache to make sure the cache doesn't
# contains skins from another portal.
try:
self.getPortalObject().portal_skins.changeSkin(None)
except AttributeError:
pass
# call tic for the current processing_node
# the processing_node numbers are the indices of the elements in the node tuple +1
# because processing_node starts form 1
if currentNode in processing_node_list:
self.tic(processing_node_list.index(currentNode)+1)
except:
# Catch ALL exception to avoid killing timerserver.
LOG('ActivityTool', ERROR, 'process_timer received an exception', error=sys.exc_info())
self.getPortalObject().portal_skins.changeSkin(None)
except AttributeError:
pass
# call tic for the current processing_node
# the processing_node numbers are the indices of the elements
# in the node tuple +1 because processing_node starts form 1
if currentNode in processing_node_list:
self.tic(processing_node_list.index(currentNode) + 1)
except:
# Catch ALL exception to avoid killing timerserver.
LOG('ActivityTool', ERROR, 'process_timer received an exception',
error=sys.exc_info())
finally:
setSecurityManager(old_sm)
finally:
......@@ -984,8 +982,7 @@ class ActivityTool (Folder, UniqueObject):
while has_awake_activity:
has_awake_activity = 0
for activity in activity_list:
acquired = is_running_lock.acquire(0)
if acquired:
if is_running_lock.acquire(0):
try:
activity.tic(inner_self, processing_node) # Transaction processing is the responsability of the activity
has_awake_activity = has_awake_activity or activity.isAwake(inner_self, processing_node)
......
......@@ -192,7 +192,7 @@ class TradeModelPath(Path):
# * remove categories which base name is not category
# * respect base parameter
prefix = category + '/'
start_index = not base and len(prefix) or 0
start_index = 0 if base else len(prefix)
return [category[start_index:]
for category in category_list
if category.startswith(prefix)]
......
......@@ -179,11 +179,9 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
msg += 'This does not modify anything by default. If you really want to fix skin names, specify %s/ERP5Site_fixSkinNames?file=NAME&dry_run=0 \n\n' % self.absolute_url()
return msg
file = os.path.join(data_dir, file)
file = open(file, 'r')
class NamingInformation: pass
info_list = []
try:
with open(os.path.join(data_dir, file)) as file:
class NamingInformation: pass
info_list = []
reader = csv.reader(file)
for row in reader:
folder, name, new_name, meta_type = row[:4]
......@@ -205,8 +203,6 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
info.regexp = re.compile('\\b' + re.escape(name) + '\\b') # This is used to search the name
info.removed = removed
info_list.append(info)
finally:
file.close()
# Now we have information enough. Check the skins.
msg = ''
......@@ -232,11 +228,8 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
msg += '%s\n' % line
if not dry_run:
if skin.meta_type in fs_skin_spec:
f = open(expandpath(skin.getObjectFSPath()), 'w')
try:
with open(expandpath(skin.getObjectFSPath()), 'w') as f:
f.write(text)
finally:
f.close()
else:
REQUEST['BODY'] = text
skin.manage_FTPput(REQUEST, REQUEST.RESPONSE)
......
......@@ -206,8 +206,7 @@ class AlarmTool(BaseTool):
This method is called by TimerService in the interval given
in zope.conf. The Default is every 5 seconds.
"""
acquired = last_tic_lock.acquire(0)
if not acquired:
if not last_tic_lock.acquire(0):
return
try:
# make sure our skin is set-up. On CMF 1.5 it's setup by acquisition,
......
......@@ -141,8 +141,10 @@ class IntrospectionTool(LogMixin, BaseTool):
if compressed:
tmp_file_path = tempfile.mktemp(dir=tmp_file_path)
tmp_file = tarfile.open(tmp_file_path,"w:gz")
tmp_file.add(file_path)
tmp_file.close()
try:
tmp_file.add(file_path)
finally:
tmp_file.close()
RESPONSE.setHeader('Content-type', 'application/x-tar')
RESPONSE.setHeader('Content-Disposition', \
'attachment;filename="%s.tar.gz"' % file_path.split('/')[-1])
......@@ -154,13 +156,10 @@ class IntrospectionTool(LogMixin, BaseTool):
tmp_file_path = file_path
f = open(tmp_file_path)
try:
with open(tmp_file_path) as f:
RESPONSE.setHeader('Content-Length', os.stat(tmp_file_path).st_size)
for data in f:
RESPONSE.write(data)
finally:
f.close()
if compressed:
os.remove(tmp_file_path)
......@@ -190,30 +189,21 @@ class IntrospectionTool(LogMixin, BaseTool):
char_per_line = 75
tailed_file = open(log_file,'r')
while 1:
try:
tailed_file.seek(-1 * char_per_line * line_number, 2)
except IOError:
tailed_file.seek(0)
if tailed_file.tell() == 0:
at_start = 1
else:
at_start = 0
lines = tailed_file.read().split("\n")
if (len(lines) > (line_number + 1)) or at_start:
break
# The lines are bigger than we thought
char_per_line = char_per_line * 1.3 # Inc for retry
tailed_file.close()
if len(lines) > line_number:
start = len(lines) - line_number - 1
else:
start = 0
with open(log_file,'r') as tailed_file:
while 1:
try:
tailed_file.seek(-1 * char_per_line * line_number, 2)
except IOError:
tailed_file.seek(0)
pos = tailed_file.tell()
lines = tailed_file.read().split("\n")
if len(lines) > (line_number + 1) or not pos:
break
# The lines are bigger than we thought
char_per_line *= 1.3 # Inc for retry
start = max(len(lines) - line_number - 1, 0)
return "\n".join(lines[start:len(lines)])
security.declareProtected(Permissions.ManagePortal, 'tailEventLog')
......@@ -330,20 +320,17 @@ class IntrospectionTool(LogMixin, BaseTool):
"""
def cached_getSystemVersionDict():
import pkg_resources
def tuple_to_format_str(t):
return '.'.join([str(i) for i in t])
version_dict = {}
for dist in pkg_resources.working_set:
version_dict[dist.key] = dist.version
from Products import ERP5 as erp5_product
erp5_product_path = erp5_product.__file__.split("/")[:-1]
erp5_product_path = os.path.dirname(erp5_product.__file__)
try:
erp5_v = open("/".join((erp5_product_path) + ["VERSION.txt"])).read().strip()
erp5_version = erp5_v.replace("ERP5 ", "")
except:
erp5_version = None
with open(os.path.join(erp5_product_path, "VERSION.txt")) as f:
erp5_version = f.read().strip().replace("ERP5 ", "")
except Exception:
erp5_version = None
version_dict["ProductS.ERP5"] = erp5_version
return version_dict
......
......@@ -298,13 +298,10 @@ class TemplateTool (BaseTool):
"""
Import template from a temp file (as uploaded by the user)
"""
file = open(path, 'rb')
try:
with open(path, 'rb') as file:
# read magic key to determine wich kind of bt we use
file.seek(0)
magic = file.read(5)
finally:
file.close()
if magic == '<?xml': # old version
self._importObjectFromFile(path, id=id)
......@@ -342,11 +339,8 @@ class TemplateTool (BaseTool):
prop_dict.pop('id', '')
bt.edit(**prop_dict)
# import all other files from bt
fobj = open(path, 'rb')
try:
with open(path, 'rb') as fobj:
bt.importFile(file=fobj)
finally:
fobj.close()
finally:
tar.close()
return bt
......@@ -398,7 +392,8 @@ class TemplateTool (BaseTool):
if not os.path.exists(prop_path):
value = None
else:
value = open(prop_path, 'rb').read()
with open(prop_path, 'rb') as f:
value = f.read()
if value is 'None':
# At export time, we used to export non-existent properties:
# str(obj.getProperty('non-existing')) == 'None'
......@@ -523,11 +518,8 @@ class TemplateTool (BaseTool):
tempid, temppath = mkstemp()
try:
os.close(tempid) # Close the opened fd as soon as possible
tempfile = open(temppath, 'wb')
try:
with open(temppath, 'wb') as tempfile:
tempfile.write(import_file.read())
finally:
tempfile.close()
bt = self._importBT(temppath, id)
finally:
os.remove(temppath)
......
......@@ -427,13 +427,8 @@ class TestERP5Catalog(ERP5TypeTestCase, LogInterceptor):
#if uid_buffer_key in uid_buffer_dict:
# del uid_buffer_dict[uid_buffer_key]
def getUIDBuffer(*args, **kw):
uid_lock = catalog.__class__._reserved_uid_lock
uid_lock.acquire()
try:
result = catalog.getUIDBuffer(*args, **kw)
finally:
uid_lock.release()
return result
with catalog.__class__._reserved_uid_lock:
return catalog.getUIDBuffer(*args, **kw)
getUIDBuffer(force_new_buffer=True)
......
......@@ -2343,8 +2343,8 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine):
url_column_dict = dict(renderer.getUrlColumnList())
selection = renderer.getSelection()
selection_name = renderer.getSelectionName()
ignore_layout = int(request.get('ignore_layout', \
not request.get('is_web_mode', False) and 1 or 0))
ignore_layout = int(request.get('ignore_layout',
0 if request.get('is_web_mode') else 1))
editable_mode = int(request.get('editable_mode', 0))
ui_domain = 'erp5_ui'
# We need a way to pass the current line object (ie. brain) to the
......
......@@ -976,33 +976,25 @@ class %s(Constraint):
# Create an empty __init__.py.
init = os.path.join(path, '__init__.py')
if not os.path.exists(init):
f = open(init, 'w')
f.close()
open(init, 'w').close()
# For convenience, make .cvsignore.
if generate_cvsignore:
cvsignore = os.path.join(path, '.cvsignore')
if not os.path.exists(cvsignore):
f = open(cvsignore, 'w')
try:
with open(cvsignore, 'w') as f:
f.write('*.pyc' + os.linesep)
finally:
f.close()
# Create a Permissions module for this Product.
permissions = os.path.join(base_path, 'Permissions.py')
if not os.path.exists(permissions):
f = open(permissions, 'w')
f.close()
open(permissions, 'w').close()
# Make .cvsignore for convenience.
if generate_cvsignore:
cvsignore = os.path.join(base_path, '.cvsignore')
if not os.path.exists(cvsignore):
f = open(cvsignore, 'w')
try:
with open(cvsignore, 'w') as f:
f.write('*.pyc' + os.linesep)
finally:
f.close()
# Create an init file for this Product.
init = os.path.join(base_path, '__init__.py')
......@@ -1055,11 +1047,8 @@ def initialize( context ):
content_constructors = (),
content_classes = ())
''' % COPYRIGHT
f = open(init, 'w')
try:
with open(init, 'w') as f:
f.write(text)
finally:
f.close()
# Create a skeleton README.txt.
readme = os.path.join(base_path, 'README.txt')
......@@ -1069,11 +1058,8 @@ def initialize( context ):
%s was automatically generated by ERP5 Class Tool.
''' % (product_id, product_id)
f = open(readme, 'w')
try:
with open(readme, 'w') as f:
f.write(text)
finally:
f.close()
# Now, copy selected code.
for d, m, id_list in (('Document', readLocalDocument, document_id_list),
......@@ -1084,11 +1070,8 @@ def initialize( context ):
for class_id in id_list:
path = os.path.join(base_path, d, class_id) + '.py'
text = m(class_id)
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
if REQUEST is not None:
REQUEST.RESPONSE.redirect('%s/manage_viewProductGeneration?manage_tabs_message=New+Product+Saved+In+%s' % (self.absolute_url(), base_path))
......
......@@ -536,12 +536,8 @@ def readLocalPropertySheet(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "PropertySheet")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalPropertySheet(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -554,11 +550,8 @@ def writeLocalPropertySheet(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
# load the file, so that an error is raised if file is invalid
module = imp.load_source(class_id, path)
getattr(module, class_id)
......@@ -570,8 +563,7 @@ def importLocalPropertySheet(class_id, path = None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "PropertySheet")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
klass = None
try:
......@@ -582,8 +574,6 @@ def importLocalPropertySheet(class_id, path = None):
setattr(PropertySheet, class_id, klass)
# Register base categories
registerBaseCategories(klass)
finally:
f.close()
base_category_dict = {}
def registerBaseCategories(property_sheet):
......@@ -608,11 +598,8 @@ def importLocalInterface(module_id, path = None, is_erp5_type=False):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "interfaces")
path = os.path.join(path, "%s.py" % module_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
finally:
f.close()
from zope.interface import Interface
from Products.ERP5Type import interfaces
InterfaceClass = type(Interface)
......@@ -627,12 +614,9 @@ def importLocalConstraint(class_id, path = None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Constraint")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
setattr(Products.ERP5Type.Constraint, class_id, getattr(module, class_id))
finally:
f.close()
def importLocalInteractor(class_id, path=None):
import Products.ERP5Type.Interactor
......@@ -640,13 +624,10 @@ def importLocalInteractor(class_id, path=None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Interactor")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
setattr(Products.ERP5Type.Interactor, class_id, getattr(module, class_id))
registerInteractorClass(class_id, getattr(Products.ERP5Type.Interactor, class_id))
finally:
f.close()
def getLocalExtensionList():
if not getConfiguration:
......@@ -699,12 +680,8 @@ def readLocalExtension(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Extensions")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def removeLocalTest(class_id):
instance_home = getConfiguration().instancehome
......@@ -718,23 +695,15 @@ def readLocalTest(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "tests")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def readLocalConstraint(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Constraint")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalExtension(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -747,11 +716,8 @@ def writeLocalExtension(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def writeLocalTest(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -764,11 +730,8 @@ def writeLocalTest(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def writeLocalConstraint(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -781,11 +744,8 @@ def writeLocalConstraint(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
# load the file, so that an error is raised if file is invalid
module = imp.load_source(class_id, path)
getattr(module, class_id)
......@@ -829,12 +789,8 @@ def readLocalDocument(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Document")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalDocument(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -849,11 +805,8 @@ def writeLocalDocument(class_id, text, create=1, instance_home=None):
raise IOError, 'the file %s is already present' % path
# check there is no syntax error (that's the most we can do at this time)
compile(text, path, 'exec')
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def setDefaultClassProperties(property_holder):
"""Initialize default properties for ERP5Type Documents.
......
......@@ -287,47 +287,46 @@ class PortalTypeMetaClass(GhostBaseMetaClass, PropertyHolder):
ERP5Base.aq_method_lock.acquire()
try:
try:
try:
class_definition = generatePortalTypeClass(site, portal_type)
except AttributeError:
LOG("ERP5Type.Dynamic", WARNING,
"Could not access Portal Type Object for type %r"
% portal_type, error=sys.exc_info())
base_tuple = (ERP5BaseBroken, )
portal_type_category_list = []
attribute_dict = dict(_categories=[], constraints=[])
interface_list = []
else:
base_tuple, portal_type_category_list, \
interface_list, attribute_dict = class_definition
klass.__isghost__ = False
klass.__bases__ = base_tuple
klass.resetAcquisition()
for key, value in attribute_dict.iteritems():
setattr(klass, key, value)
if getattr(klass.__setstate__, 'im_func', None) is \
persistent_migration.__setstate__:
# optimization to reduce overhead of compatibility code
klass.__setstate__ = persistent_migration.Base__setstate__
for interface in interface_list:
classImplements(klass, interface)
# skip this during the early Base Type / Types Tool generation
# because they dont have accessors, and will mess up
# workflow methods. We KNOW that we will re-load this type anyway
if len(base_tuple) > 1:
klass.generatePortalTypeAccessors(site, portal_type_category_list)
# need to set %s__roles__ for generated methods
cls.setupSecurity()
except Exception:
import traceback; traceback.print_exc()
raise
class_definition = generatePortalTypeClass(site, portal_type)
except AttributeError:
LOG("ERP5Type.Dynamic", WARNING,
"Could not access Portal Type Object for type %r"
% portal_type, error=sys.exc_info())
base_tuple = (ERP5BaseBroken, )
portal_type_category_list = []
attribute_dict = dict(_categories=[], constraints=[])
interface_list = []
else:
base_tuple, portal_type_category_list, \
interface_list, attribute_dict = class_definition
klass.__isghost__ = False
klass.__bases__ = base_tuple
klass.resetAcquisition()
for key, value in attribute_dict.iteritems():
setattr(klass, key, value)
if getattr(klass.__setstate__, 'im_func', None) is \
persistent_migration.__setstate__:
# optimization to reduce overhead of compatibility code
klass.__setstate__ = persistent_migration.Base__setstate__
for interface in interface_list:
classImplements(klass, interface)
# skip this during the early Base Type / Types Tool generation
# because they dont have accessors, and will mess up
# workflow methods. We KNOW that we will re-load this type anyway
if len(base_tuple) > 1:
klass.generatePortalTypeAccessors(site, portal_type_category_list)
# need to set %s__roles__ for generated methods
cls.setupSecurity()
except Exception:
import traceback; traceback.print_exc()
raise
finally:
ERP5Base.aq_method_lock.release()
......
......@@ -27,8 +27,7 @@ if '__getstate__' not in Uninstalled.BrokenClass.__dict__:
cache = Uninstalled.broken_klasses
def Broken(self, oid, pair):
lock.acquire()
try:
with lock:
cached = pair in cache
result = Uninstalled_Broken(self, oid, pair)
if not cached:
......@@ -36,8 +35,6 @@ if '__getstate__' not in Uninstalled.BrokenClass.__dict__:
assert not issubclass(klass, PersistentBroken), \
"This monkey-patch is not useful anymore"
cache[pair] = persistentBroken(klass)
finally:
lock.release()
return result
Uninstalled.Broken = Broken
......@@ -15,14 +15,11 @@ class DummyTaskDistributionTool(object):
return None, revision
def startUnitTest(self, test_result_path, exclude_list=()):
self.lock.acquire()
try:
with self.lock:
for i, test in enumerate(self.test_name_list):
if test not in exclude_list:
del self.test_name_list[i]
return None, test
finally:
self.lock.release()
def stopUnitTest(self, test_path, status_dict):
pass
\ No newline at end of file
......@@ -143,12 +143,8 @@ class Browser:
def _createFile(self, filename, content):
file_path = os.path.join(self.profile_dir, filename)
f = open(file_path, 'w')
try:
with open(file_path, 'w') as f:
f.write(content)
finally:
f.close()
return file_path
def _setDisplay(self, display):
......
......@@ -289,11 +289,9 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
raise WriteError
portal.person_module.__class__._setLastId = _setLastId
try:
try:
o = portal.person_module.newContent(portal_type="Person",
temp_object=1)
except WriteError:
self.fail("Container last ID modified")
o = portal.person_module.newContent(portal_type="Person", temp_object=1)
except WriteError:
self.fail("Container last ID modified")
finally:
del portal.person_module.__class__._setLastId
......
......@@ -323,7 +323,7 @@ class Git(WorkingCopy):
if push:
# if we can't push because we are not up-to-date, we'll either 'merge' or
# 'rebase' depending on we already have local commits or not
merge = self.getAheadCount() and 'merge' or 'rebase'
merge = 'merge' if self.getAheadCount() else 'rebase'
selected_set = set(added)
selected_set.update(modified)
......
......@@ -48,16 +48,13 @@ _chdir_lock = threading.RLock()
@simple_decorator
def chdir_working_copy(func):
def decorator(self, *args, **kw):
_chdir_lock.acquire()
try:
with _chdir_lock:
cwd = os.getcwd()
try:
os.chdir(self.working_copy)
return func(self, *args, **kw)
finally:
os.chdir(cwd)
finally:
_chdir_lock.release()
return decorator
class Subversion(WorkingCopy):
......
......@@ -209,11 +209,8 @@ class WorkingCopy(Implicit):
revision = int(self.showOld(path)) + 1
except NotVersionedError:
return 1
file = open(os.path.join(self.working_copy, path), 'w')
try:
with open(os.path.join(self.working_copy, path), 'w') as file:
file.write(str(revision))
finally:
file.close()
return revision
def hasDiff(self, path):
......@@ -279,11 +276,8 @@ class WorkingCopy(Implicit):
head = '<span style="font-weight: bold; color: black;">%s</span>' \
% real_path
try:
f = open(os.path.join(self.working_copy, path), 'rU')
try:
with open(os.path.join(self.working_copy, path), 'rU') as f:
text = f.read()
finally:
f.close()
except IOError, e:
if e.errno == errno.EISDIR:
return '%s<hr/>%r is a folder!' % (head, path)
......
......@@ -214,43 +214,41 @@ class EGOVUserManager(ERP5UserManager):
sm = getSecurityManager()
if sm.getUser().getId() != SUPER_USER:
newSecurityManager(self, self.getUser(SUPER_USER))
try:
try:
result = portal.portal_catalog.unrestrictedSearchResults(
result = portal.portal_catalog.unrestrictedSearchResults(
select_expression='reference',
portal_type=self.portal_type_list, reference=login)
if len(result) != 1: # we won't proceed with groups
if len(result) > 1: # configuration is screwed
raise ConsistencyError, 'There is more than one Person whose \
login is %s : %s' % (user_name,
repr([r.getObject() for r in catalog_result]))
else: # no person is linked to this user login
# this permit to get the module of the application
# the goal is to work with anonymous applications, even if
# they are not reindexed
module_id = self.REQUEST.get('anonymous_module', None)
if module_id:
module = getattr(portal, module_id, None)
if module is not None:
result = module._getOb(login[0], None)
if result is not None:
return [result.getPath(),]
else:
return []
else:
return []
except ConflictError:
raise
except:
LOG('ERP5Security', PROBLEM, 'getUserByLogin failed', error=sys.exc_info())
# Here we must raise an exception to prevent callers from caching
# a result of a degraded situation.
# The kind of exception does not matter as long as it's catched by
# PAS and causes a lookup using another plugin or user folder.
# As PAS does not define explicitely such exception, we must use
# the _SWALLOWABLE_PLUGIN_EXCEPTIONS list.
raise _SWALLOWABLE_PLUGIN_EXCEPTIONS[0]
if len(result) != 1: # we won't proceed with groups
if len(result) > 1: # configuration is screwed
raise ConsistencyError('There is more than one Person whose'
' login is %s : %s' % (user_name,
repr([r.getObject() for r in catalog_result]))
else: # no person is linked to this user login
# this permit to get the module of the application
# the goal is to work with anonymous applications, even if
# they are not reindexed
module_id = self.REQUEST.get('anonymous_module', None)
if module_id:
module = getattr(portal, module_id, None)
if module is not None:
result = module._getOb(login[0], None)
if result is not None:
return [result.getPath(),]
else:
return []
else:
return []
except ConflictError:
raise
except:
LOG('ERP5Security', PROBLEM, 'getUserByLogin failed', error=sys.exc_info())
# Here we must raise an exception to prevent callers from caching
# a result of a degraded situation.
# The kind of exception does not matter as long as it's catched by
# PAS and causes a lookup using another plugin or user folder.
# As PAS does not define explicitely such exception, we must use
# the _SWALLOWABLE_PLUGIN_EXCEPTIONS list.
raise _SWALLOWABLE_PLUGIN_EXCEPTIONS[0]
finally:
setSecurityManager(sm)
# XXX: Here, we filter catalog result list ALTHOUGH we did pass
......
......@@ -697,8 +697,7 @@ class Catalog(Folder,
"""
Import properties from an XML file.
"""
f = open(file)
try:
with open(file) as f:
doc = parse(f)
root = doc.documentElement
try:
......@@ -747,8 +746,6 @@ class Catalog(Folder,
self.filter_dict[id]['expression_instance'] = None
finally:
doc.unlink()
finally:
f.close()
def manage_historyCompare(self, rev1, rev2, REQUEST,
historyComparisonResults=''):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment