1 import os, tarfile, datetime, logging, re, ConfigParser, shutil
3 from django.core.cache import cache
4 from django.utils.translation import ugettext as _
5 from forum.models import *
6 from forum.settings import APP_URL
7 from forum.templatetags.extra_tags import diff_date
8 import xml.etree.ElementTree
9 from xml.etree import ElementTree as ET
10 from xml.etree.ElementTree import Comment, _encode, ProcessingInstruction, QName, fixtag, _escape_attrib, _escape_cdata
11 from forum import settings
12 from django.conf import settings as djsettings
13 import settings as selfsettings
15 CACHE_KEY = "%s_exporter_state" % APP_URL
18 TMP_FOLDER = os.path.join(os.path.dirname(__file__), 'tmp')
19 LAST_BACKUP = os.path.join(TMP_FOLDER, 'backup.tar.gz')
21 DATE_AND_AUTHOR_INF_SECTION = 'DateAndAuthor'
22 OPTIONS_INF_SECTION = 'Options'
24 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
25 DATE_FORMAT = "%Y-%m-%d"
27 def Etree_pretty__write(self, file, node, encoding, namespaces,
28 level=0, identator=" "):
31 file.write(level * identator + "<!-- %s -->" % _escape_cdata(node.text, encoding))
32 elif tag is ProcessingInstruction:
33 file.write("<?%s?>" % _escape_cdata(node.text, encoding))
36 xmlns_items = [] # new namespaces in this scope
38 if isinstance(tag, QName) or tag[:1] == "{":
39 tag, xmlns = fixtag(tag, namespaces)
40 if xmlns: xmlns_items.append(xmlns)
42 raise #_raise_serialization_error(tag)
43 file.write("\n" + level * identator + "<" + _encode(tag, encoding))
44 if items or xmlns_items:
45 items.sort() # lexical order
48 if isinstance(k, QName) or k[:1] == "{":
49 k, xmlns = fixtag(k, namespaces)
50 if xmlns: xmlns_items.append(xmlns)
52 raise #_raise_serialization_error(k)
54 if isinstance(v, QName):
55 v, xmlns = fixtag(v, namespaces)
56 if xmlns: xmlns_items.append(xmlns)
58 raise #_raise_serialization_error(v)
59 file.write(" %s=\"%s\"" % (_encode(k, encoding),
60 _escape_attrib(v, encoding)))
61 for k, v in xmlns_items:
62 file.write(" %s=\"%s\"" % (_encode(k, encoding),
63 _escape_attrib(v, encoding)))
64 if node.text or len(node):
67 file.write(_escape_cdata(node.text.replace("\n", (level + 1) * identator + "\n"), encoding))
69 self._write(file, n, encoding, namespaces, level + 1, identator)
70 if node.text and len(node.text) < 125:
71 file.write("</" + _encode(tag, encoding) + ">")
73 file.write("\n" + level * identator + "</" + _encode(tag, encoding) + ">")
76 for k, v in xmlns_items:
79 file.write(_escape_cdata(node.tail.replace("\n", level * identator + "\n"), encoding))
81 def _add_tag(el, name, content = None):
82 tag = ET.SubElement(el, name)
87 def ET_Element_add_tag(el, tag_name, content = None, **attrs):
88 tag = ET.SubElement(el, tag_name)
91 tag.text = unicode(content).encode('utf-8')
93 for k, v in attrs.items():
94 tag.set(k, unicode(v))
98 GOOD_TAG_NAME = re.compile("^\w+$")
100 def make_extra(el, v):
105 if isinstance(v, (int, long, str, unicode, float, bool, dict, list, tuple)):
106 if isinstance(v, tuple):
109 t = v.__class__.__name__
113 value = el.add('value', type=t)
115 if isinstance(v, (list, tuple)):
117 item = value.add('item')
120 elif isinstance(v, dict):
121 for k, i in v.items():
122 item = value.add('item', key=k)
125 value.text = unicode(v)
127 def write_to_file(root, tmp, filename):
128 tree = ET.ElementTree(root)
129 tree.write(os.path.join(tmp, filename), encoding='UTF-8')
131 def create_targz(tmp, files, start_time, options, user, state, set_state):
132 if os.path.exists(LAST_BACKUP):
133 os.remove(LAST_BACKUP)
135 t = tarfile.open(name=LAST_BACKUP, mode = 'w:gz')
137 state['overall']['status'] = _('Compressing xml files')
141 t.add(os.path.join(tmp, f), arcname=f)
143 if options.get('uplodaded_files', False):
144 state['overall']['status'] = _('Importing uploaded files')
148 if options.get('import_skins_folder', False):
149 state['overall']['status'] = _('Importing skins folder')
151 export_skinsfolder(t)
153 state['overall']['status'] = _('Writing inf file.')
156 now = datetime.datetime.now()
157 domain = re.match('[\w-]+\.[\w-]+(\.[\w-]+)*', djsettings.APP_URL)
159 domain = '_'.join(domain.get(0).split('.'))
163 fname = "%s-%s" % (domain, now.strftime('%Y%m%d%H%M'))
165 inf = ConfigParser.SafeConfigParser()
167 inf.add_section(DATE_AND_AUTHOR_INF_SECTION)
169 inf.set(DATE_AND_AUTHOR_INF_SECTION, 'file-name', "%s.tar.gz" % fname)
170 inf.set(DATE_AND_AUTHOR_INF_SECTION, 'author', unicode(user.id))
171 inf.set(DATE_AND_AUTHOR_INF_SECTION, 'site', djsettings.APP_URL)
172 inf.set(DATE_AND_AUTHOR_INF_SECTION, 'started', start_time.strftime(DATETIME_FORMAT))
173 inf.set(DATE_AND_AUTHOR_INF_SECTION, 'finished', now.strftime(DATETIME_FORMAT))
175 inf.add_section(OPTIONS_INF_SECTION)
176 inf.set(OPTIONS_INF_SECTION, 'anon-data', str(options.get('anon_data', False)))
177 inf.set(OPTIONS_INF_SECTION, 'with-upfiles', str(options.get('uplodaded_files', False)))
178 inf.set(OPTIONS_INF_SECTION, 'with-skins', str(options.get('import_skins_folder', False)))
180 with open(os.path.join(tmp, 'backup.inf'), 'wb') as inffile:
183 t.add(os.path.join(tmp, 'backup.inf'), arcname='backup.inf')
184 state['overall']['status'] = _('Saving backup file')
187 shutil.copyfile(LAST_BACKUP, os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, "%s.tar.gz" % fname))
188 shutil.copyfile(os.path.join(tmp, 'backup.inf'), os.path.join(selfsettings.EXPORTER_BACKUP_STORAGE, "%s.backup.inf" % fname))
192 def export_upfiles(tf):
193 folder = str(settings.UPFILES_FOLDER)
195 if os.path.exists(folder):
196 tf.add(folder, arcname='upfiles')
199 def export_skinsfolder(tf):
200 folder = djsettings.TEMPLATE_DIRS[0]
202 if os.path.exists(folder):
203 tf.add(folder, arcname='skins')
206 def export(options, user):
207 original__write = xml.etree.ElementTree.ElementTree._write
208 xml.etree.ElementTree.ElementTree._write = Etree_pretty__write
209 xml.etree.ElementTree._ElementInterface.add = ET_Element_add_tag
211 start_time = datetime.datetime.now()
213 anon_data = options.get('anon_data', False)
215 steps = [s for s in EXPORT_STEPS if not (anon_data and s['fn'].is_user_data())]
217 state = dict([(s['id'], {
218 'status': _('Queued'), 'count': s['fn'].count(start_time), 'parsed': 0
219 }) for s in steps] + [
221 'status': _('Starting'), 'count': sum([s['fn'].count(start_time) for s in steps]), 'parsed': 0
225 full_state = dict(running=True, state=state, time_started="")
228 full_state['time_started'] = diff_date(start_time)
229 cache.set(CACHE_KEY, full_state)
233 def ping_state(name):
234 state[name]['parsed'] += 1
235 state['overall']['parsed'] += 1
242 state['overall']['status'] = _('Exporting %s') % s['name']
243 state[name]['status'] = _('Exporting')
245 root, fname = fn(ping, start_time, anon_data)
247 state[name]['status'] = _('Writing temp file')
248 state['overall']['status'] = _('Writing %s temp file') % s['name']
252 write_to_file(root, tmp, fname)
253 state[name]['status'] = _('Done')
263 dump_files.append(run(s['fn'], s['id']))
265 state['overall']['status'] = _('Compressing files')
268 create_targz(tmp, dump_files, start_time, options, user, state, set_state)
269 full_state['running'] = False
270 full_state['errors'] = False
271 state['overall']['status'] = _('Done')
275 full_state['running'] = False
276 full_state['errors'] = "%s: %s" % (e.__class__.__name__, unicode(e))
280 logging.error("Error executing xml backup: \n %s" % (traceback.format_exc()))
282 xml.etree.ElementTree.ElementTree._write = original__write
283 del xml.etree.ElementTree._ElementInterface.add
286 def exporter_step(queryset, root_tag_name, el_tag_name, name, date_lock=None, user_data=False):
291 return queryset.filter(**{"%s__lte" % date_lock: lock})
294 def decorated(ping, lock, anon_data):
295 root = ET.Element(root_tag_name)
297 for item in qs(lock).order_by('id').select_related():
298 el = root.add(el_tag_name)
299 fn(item, el, anon_data)
302 return root, "%s.xml" % root_tag_name
305 return qs(lock).count()
310 decorated.count = count
311 decorated.is_user_data = is_user_data
313 EXPORT_STEPS.append(dict(id=root_tag_name, name=name, fn=decorated))
319 @exporter_step(Tag.objects.all(), 'tags', 'tag', _('Tags'))
320 def export_tags(t, el, anon_data):
321 el.add('name', t.name)
323 el.add('author', t.created_by.id)
324 el.add('used', t.used_count)
327 @exporter_step(User.objects.all(), 'users', 'user', _('Users'), 'date_joined', True)
328 def export_users(u, el, anon_data):
330 el.add('username', u.username)
331 el.add('password', u.password)
332 el.add('email', u.email, validated=u.email_isvalid and 'true' or 'false')
333 el.add('reputation', u.reputation)
334 el.add('badges', bronze=u.bronze, silver=u.silver, gold=u.gold)
335 el.add('joindate', u.date_joined.strftime(DATETIME_FORMAT))
336 el.add('active', u.is_active and 'true' or 'false')
338 el.add('realname', u.real_name)
339 el.add('bio', u.about)
340 el.add('location', u.location)
341 el.add('website', u.website)
342 el.add('birthdate', u.date_of_birth and u.date_of_birth.strftime(DATE_FORMAT) or "")
344 roles = el.add('roles')
347 roles.add('role', 'superuser')
350 roles.add('role', 'moderator')
352 auth = el.add('authKeys')
353 for a in u.auth_keys.all():
354 key = auth.add('key')
355 key.add('provider', a.provider)
356 key.add('key', a.key)
359 ss = u.subscription_settings
361 notify = el.add('notifications', enabled=ss.enable_notifications and 'true' or 'false')
363 notify.add('notify', **dict([(t, ss.__dict__.get(t, 'n') == 'i' and 'true' or 'false') for t in ['member_joins', 'new_question', 'new_question_watched_tags', 'subscribed_questions']]))
365 notify.add('autoSubscribe', **dict([(t, ss.__dict__.get(t, False) and 'true' or 'false') for t in [
366 'all_questions', 'all_questions_watched_tags', 'questions_asked', 'questions_answered', 'questions_commented', 'questions_viewed']]))
368 notify.add('notifyOnSubscribed', **dict([(t, ss.__dict__.get("notify_%s" % t, False) and 'true' or 'false') for t in [
369 'answers', 'reply_to_comments', 'comments_own_post', 'comments', 'accepted']]))
371 notify.add('digest', ss.send_digest and 'on' or 'off')
373 watched = el.add('watchedTags')
374 rejected = el.add('rejectedTags')
376 for m in u.tag_selections.all():
377 if m.reason == 'good':
378 watched.add('tag', m.tag.name)
380 rejected.add('tag', m.tag.name)
384 @exporter_step(Node.objects.all(), 'nodes', 'node', _('Nodes'), 'added_at')
385 def export_nodes(n, el, anon_data):
387 el.add('type', n.node_type)
390 el.add('author', n.author.id)
391 el.add('date', n.added_at.strftime(DATETIME_FORMAT))
392 el.add('parent', n.parent and n.parent.id or "")
393 el.add('absparent', n.abs_parent and n.abs_parent or "")
395 act = el.add('lastactivity')
396 act.add('by', n.last_activity_by and n.last_activity_by.id or "")
397 act.add('at', n.last_activity_at and n.last_activity_at.strftime(DATETIME_FORMAT) or "")
399 el.add('title', n.title)
400 el.add('body', n.body)
402 tags = el.add('tags')
404 for t in n.tagname_list():
407 revs = el.add('revisions', active=n.active_revision and n.active_revision.revision or n.revisions.order_by('revision')[0].revision)
409 for r in n.revisions.order_by('revision'):
410 rev = _add_tag(revs, 'revision')
411 rev.add('number', r.revision)
412 rev.add('summary', r.summary)
414 rev.add('author', r.author.id)
415 rev.add('date', r.revised_at.strftime(DATETIME_FORMAT))
417 rev.add('title', r.title)
418 rev.add('body', r.body)
419 rev.add('tags', ", ".join(r.tagname_list()))
421 el.add('marked', n.marked and 'true' or 'false')
422 el.add('extraRef', n.extra_ref and n.extra_ref.id or "")
423 make_extra(el.add('extraData'), n.extra)
424 el.add('extraCount', n.extra_count and n.extra_count or "")
427 @exporter_step(Action.objects.all(), 'actions', 'action', _('Actions'), 'action_date')
428 def export_actions(a, el, anon_data):
430 el.add('type', a.action_type)
431 el.add('date', a.action_date)
434 el.add('user', a.user.id)
435 el.add('realUser', a.real_user and a.real_user.id or "")
437 el.add('node', a.node and a.node.id or "")
439 make_extra(el.add('extraData'), a.extra)
441 canceled = el.add('canceled', state=a.canceled and 'true' or 'false')
445 canceled.add('user', a.canceled_by.id)
446 canceled.add('ip', a.canceled_ip)
448 canceled.add('date', a.canceled_at)
451 reputes = el.add('reputes')
453 for r in a.reputes.all():
454 repute = reputes.add('repute', byCanceled=r.by_canceled and 'true' or 'false')
455 repute.add('user', r.user.id)
456 repute.add('value', r.value)
459 #@exporter_step(NodeState.objects.all(), 'states', 'state', _('Node states'), 'action__action_date')
460 #def export_states(s, el, anon_data):
461 # el.add('type', s.state_type)
462 # el.add('node', s.node.id)
463 # el.add('trigger', s.action.id)
466 #@exporter_step(Badge.objects.all(), 'badges', 'badge', _('Badges'), user_data=True)
467 #def export_badges(b, el, anon_data):
468 # el.add('type', ["", 'gold', 'silver', 'bronze'][b.type])
469 # el.add('name', b.cls)
470 # el.add('count', b.awarded_count)
473 @exporter_step(Award.objects.all(), 'awards', 'award', _('Awards'), 'awarded_at', True)
474 def export_awards(a, el, anon_data):
475 el.add('badge', a.badge.cls)
476 el.add('user', a.user.id)
477 el.add('node', a.node and a.node.id or "")
478 el.add('trigger', a.trigger and a.trigger.id or "")
479 el.add('action', a.action.id)
481 @exporter_step(KeyValue.objects.all(), 'settings', 'setting', _('Settings'))
482 def export_settings(s, el, anon_data):
484 make_extra(el.add('value'), s.value)