1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Yoshinori Okuji <yo@nexedi.com>
# Christophe Dumez <christophe@nexedi.com>
# 2010 Julien Muchembled <jm@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import errno, json, os, re, shutil
from base64 import b64encode, b64decode
from tempfile import gettempdir
import transaction
from AccessControl import Unauthorized
from AccessControl.SecurityInfo import ModuleSecurityInfo
from Acquisition import aq_base, Implicit
from App.config import getConfiguration
from DateTime import DateTime
from ZTUtils import make_query
from Products.ERP5.Document.BusinessTemplate import BusinessTemplateFolder
from Products.ERP5Type.Utils import simple_decorator
@simple_decorator
def selfcached(func):
"""Return a function which stores a computed value in an instance
at the first call.
"""
key = '_cache_' + str(id(func))
def decorated(self, *args):
try:
cache = getattr(aq_base(self), key)
return cache[args]
except AttributeError:
result = func(self, *args)
setattr(self, key, {args: result})
except KeyError:
cache[args] = result = func(self, *args)
return result
return decorated
class NotAWorkingCopyError(Exception): pass
ModuleSecurityInfo(__name__).declarePublic('NotAWorkingCopyError')
class NotVersionedError(Exception): pass
ModuleSecurityInfo(__name__).declarePublic('NotVersionedError')
class BusinessTemplateNotInstalled(Exception): pass
ModuleSecurityInfo(__name__).declarePublic('BusinessTemplateNotInstalled')
class VcsConflictError(Exception): pass
ModuleSecurityInfo(__name__).declarePublic('VcsConflictError')
def issubdir(parent, child):
return parent == child or child.startswith(parent + os.sep)
ImplicitType = type(Implicit)
class WorkingCopy(Implicit):
__allow_access_to_unprotected_subobjects__ = 1
_registry = []
reference = None
class __metaclass__(ImplicitType):
def __init__(cls, name, bases, d):
ImplicitType.__init__(cls, name, bases, d)
if cls.reference:
cls._registry.append((cls.reference, cls))
def __init__(self, path=None):
if path:
self.working_copy = self.checkWorkingPath(path)
def getWorkingCopyList(self):
working_copy_list = []
preferences = self.getPortalObject().portal_preferences
for path in preferences.getPreferredWorkingCopyList():
try:
getVcsTool(vcs=self.reference, path=path)
working_copy_list.append(path)
except NotAWorkingCopyError:
pass
return working_copy_list
def createBusinessTemplateWorkingCopy(self):
"""Create the working copy path corresponding to the given business template
"""
path = os.path.join(self.working_copy, self.aq_parent.getTitle())
os.mkdir(path)
self.working_copy = path
def checkWorkingPath(self, path):
# First remove any '..' to prevent escaping.
# Note that 'normpath' ignore symlinks so it would not do it correctly.
parts = path.split(os.sep)
try:
i = len(parts) - parts[::-1].index(os.pardir)
parts[:i] = os.realpath(parts[:i].join(os.sep)),
path = parts.join(os.sep)
except ValueError:
pass
# Allow symlinks inside instance home.
path = os.path.normpath(path)
real_path = os.path.realpath(path)
for allowed in getConfiguration().instancehome, gettempdir():
if issubdir(allowed, path) or issubdir(allowed, real_path):
return real_path
raise Unauthorized("Unauthorized access to path %r."
" It is NOT in your Zope home instance." % path)
def _getCookie(self, name, default=None):
try:
return json.loads(b64decode(self.REQUEST[name]))
except StandardError:
return default
def _setCookie(self, name, value, days=30):
portal = self.getPortalObject()
request = portal.REQUEST
value = b64encode(json.dumps(value))
request.set(name, value)
if days:
expires = (DateTime() + days).toZone('GMT').rfc822()
request.RESPONSE.setCookie(name, value, path=portal.absolute_url_path(),
expires=expires)
# path is the path in svn working copy
# return edit_path in zodb to edit it
def editPath(self, path, html=False):
"""Return path to edit file
path can be relative or absolute
"""
if os.path.isabs(path):
return '' # should not happen so maybe we should raise
if path != '.' and os.path.exists(os.path.join(self.working_copy, path)):
try:
path = os.path.splitext(path)[0].split(os.sep)
except ValueError:
pass
else:
if path[0] != 'bt':
path[0] = portal = self.getPortalObject().absolute_url_path()
path.append('manage_main')
path = '/'.join(path)
if html:
return ' <a href="%s"><img style="border:0" alt="Edit"' \
' src="%s/ERP5VCS_imgs/edit.png" /></a>' % (path, portal)
return path
return ''
def getHeader(self, path):
real_path = self.working_copy
if path != '.':
real_path = os.path.join(real_path, path)
return '<a style="font-weight: bold"' \
' href="BusinessTemplate_viewVcsShowFile?%s">%s</a>%s' \
% (make_query(file=path), real_path, self.editPath(path, True))
def getRemoteComment(self):
return
def extractBT(self, business_template):
"""Extract business template to local working copy
"""
if business_template.getBuildingState() == 'draft':
business_template.edit()
business_template.build()
# XXX: Big hack to make export work as expected.
transaction.commit()
self._export(business_template)
def _export(self, business_template):
bta = BusinessTemplateWorkingCopy(creation=1, path=self.working_copy)
self.addremove(*bta.export(business_template))
def showOld(self, path):
raise NotAWorkingCopyError
def update(self, keep=False):
raise NotAWorkingCopyError
def newRevision(self):
path = os.path.join('bt', 'revision')
try:
revision = int(self.showOld(path)) + 1
except NotVersionedError:
return 1
with open(os.path.join(self.working_copy, path), 'w') as file:
file.write(str(revision))
return revision
def hasDiff(self, path):
try:
hasDiff = aq_base(self).__hasDiff
except AttributeError:
template_tool = self.getPortalObject().portal_templates
if template_tool.getDiffFilterScriptList():
getFilteredDiff = template_tool.getFilteredDiff
diff = self.diff
hasDiff = lambda path: bool(getFilteredDiff(diff(path)))
else:
hasDiff = lambda path: True
self.__hasDiff = hasDiff
return hasDiff(path)
def treeToXML(self, item) :
""" Convert tree in memory to XML
"""
output = '<?xml version="1.0" encoding="UTF-8"?>\n<tree id="0">\n'
output = self._treeToXML(item, output)
return output + '</tree>\n'
def _treeToXML(self, item, output, relative_path=''):
"""
Private function to convert recursively tree
in memory to XML
"""
# Choosing a color coresponding to the status
status = item.status
if status == 'added':
color = 'green'
elif status in ('modified', 'replaced'):
color = 'orange'
elif status == 'deleted':
color = 'red'
elif status == 'conflicted':
color = 'grey'
elif status == 'outdated':
color = 'purple'
else :
color = 'black'
output += '<item text="%s" id="%s" aCol="%s" ' % (
item.name, relative_path or '.', color)
if isinstance(item, Dir):
if not relative_path:
output += 'open="1" '
output += 'im0="folder.png" im1="folder_open.png" im2="folder.png">\n'
for node in item.sub_dirs + item.sub_files:
output = self._treeToXML(node, output,
os.path.join(relative_path, node.name))
output += '</item>' + os.linesep
else :
output += 'im0="document.png"/>\n'
return output
def fileHTML(self, path):
""" Display a file content in HTML with syntax highlighting
"""
real_path = self.working_copy
if path != '.':
real_path = os.path.join(real_path, path)
head = '<span style="font-weight: bold; color: black;">%s</span>' \
% real_path
try:
with open(os.path.join(self.working_copy, path), 'rU') as f:
text = f.read()
except IOError, e:
if e.errno == errno.EISDIR:
return '%s<hr/>%r is a folder!' % (head, path)
if e.errno != errno.ENOENT:
raise
head += ' (removed)'
text = self.showOld(path)
else:
head += self.editPath(path, True)
return '%s<hr/><span style="font-family: monospace">%s</span>' \
% (head, colorize(text))
def revertZODB(self, added_file_list=(), other_file_list=(), recurse=False):
"""Revert local changes in a file or a directory
in ZODB and on hard drive
"""
# Transform params to list if they are not already lists
if isinstance(added_file_list, basestring):
added_file_list = added_file_list,
if isinstance(other_file_list, basestring):
other_file_list = other_file_list,
to_revert = list(added_file_list) + list(other_file_list)
if not to_revert:
return
business_template = self.aq_parent
template_tool = business_template.getParentValue()
installed_bt = template_tool.getInstalledBusinessTemplate(
business_template.getTitle())
if installed_bt is None:
raise BusinessTemplateNotInstalled("Revert can't work if the business"
" template is not installed. Please install it first.")
# Reinstall removed or modified files
object_to_update = {}
for path in other_file_list:
path_list = os.path.splitext(path)[0].split(os.sep)
if path_list[0] != 'bt' and len(path_list) > 2:
object_to_update['/'.join(path_list[2:])] = 'install'
# remove added files
path_added_list = []
for path in added_file_list:
path_list = os.path.splitext(path)[0].split(os.sep)
if path_list[0] != 'bt' and len(path_list) > 2:
path_added_list.append('/'.join(path_list[2:]))
## hack to remove objects
if path_added_list:
# Create a temporary bt with objects to delete
tmp_bt = template_tool.newContent(portal_type="Business Template",
temp_object=1,
title='tmp_bt_revert',
template_path_list=path_added_list)
tmp_bt.edit()
tmp_bt.build()
# Install then uninstall it to remove objects from ZODB
tmp_bt.install()
tmp_bt.uninstall()
# revert changes
#self.revert(to_revert, recurse)
installed_bt.reinstall(object_to_update=object_to_update, force=0)
def getVcsTool(vcs=None, path=None):
if vcs:
for x in WorkingCopy._registry:
if x[0] == vcs:
return x[1](path)
raise ValueError("Unsupported Version Control System: %s" % vcs)
elif path:
for x in WorkingCopy._registry:
try:
return x[1](path)
except NotAWorkingCopyError:
pass
raise NotAWorkingCopyError(path)
return WorkingCopy()
class BusinessTemplateWorkingCopy(BusinessTemplateFolder):
# only works for VCS that have a single metadata folder per working copy
def _writeString(self, obj, path):
self.file_set.add(path)
self._makeParent(path)
path = os.path.join(self.path, path)
# write file unless unchanged
file = None
try:
try:
file = open(path, 'r+b')
except IOError, e:
if e.errno == errno.EISDIR:
shutil.rmtree(path, ignore_errors=True)
elif e.errno != errno.ENOENT:
raise
file = open(path, 'wb')
else:
old_size = os.fstat(file.fileno()).st_size
if len(obj) == old_size and obj == file.read():
return
file.seek(0)
file.write(obj)
file.truncate()
finally:
if file is not None:
file.close()
def _makeParent(self, path):
path = os.path.dirname(path)
if path and path not in self.dir_set:
self._makeParent(path)
real_path = os.path.join(self.path, path)
if not os.path.exists(real_path):
os.mkdir(real_path)
self.dir_set.add(path)
def export(self, business_template):
self.file_set = set()
self.dir_set = set()
# This will call _writeString for every exported object
business_template.export(bta=self)
# Remove dangling files/dirs
removed_set = set()
prefix_length = len(os.path.join(self.path, ''))
for dirpath, dirnames, filenames in os.walk(self.path):
dirpath = dirpath[prefix_length:]
for i in xrange(len(dirnames) - 1, -1, -1):
d = dirnames[i]
if d[0] != '.':
d = os.path.join(dirpath, d)
if d in self.dir_set:
continue
shutil.rmtree(os.path.join(self.path, d))
removed_set.add(d)
del dirnames[i]
for f in filenames:
f = os.path.join(dirpath, f)
if f not in self.file_set:
os.remove(os.path.join(self.path, f))
removed_set.add(f)
return self.file_set, removed_set
class File(object):
""" Class that represents a file in memory
"""
__slots__ = ('status', 'name')
def __init__(self, name, status):
self.status = status
self.name = name
class Dir(object):
""" Class that reprensents a folder in memory
"""
__slots__ = ('status', 'name', 'sub_dirs', 'sub_files')
def __init__(self, name, status):
self.status = status
self.name = name
self.sub_dirs = [] # list of sub directories
self.sub_files = [] # list of sub files
def __getitem__(self, key):
for child in self.sub_dirs:
if child.name == key:
return child
for child in self.sub_files:
if child.name == key:
return child
raise KeyError(key)
from xml.sax.saxutils import escape
NBSP = ' '
NBSP_TAB = NBSP*8
def colorizeTag(tag):
"Return html colored item"
text = tag.group()
if text.startswith('#') :
color = 'grey'
elif text.startswith('\"') :
color = 'red'
elif 'string' in text:
color = 'green'
elif 'tuple' in text:
color = 'orange'
elif 'dictionary' in text:
color = 'brown'
elif 'item' in text:
color = '#a1559a' #light purple
elif 'value' in text:
color = 'purple'
elif 'key' in text:
color = '#0c4f0c'#dark green
else:
color = 'blue'
return "<span style='font-family: inherit; color: %s'>%s</span>" \
% (color, text)
def colorize(text):
"""Return HTML Code with syntax hightlighting
"""
# Escape xml before adding html tags
html = escape(text)
html = html.replace(' ', NBSP)
html = html.replace('\t', NBSP_TAB)
# Colorize comments
pattern = re.compile(r'#.*')
html = pattern.sub(colorizeTag, html)
# Colorize tags
pattern = re.compile(r'<.*?>')
html = pattern.sub(colorizeTag, html)
# Colorize strings
pattern = re.compile(r'\".*?\"')
html = pattern.sub(colorizeTag, html)
html = html.replace(os.linesep, os.linesep+"<br/>")
return html