def update(session): """Session -> None Update to latest svn revision if needed """ revision = session.query(func.max(Revision.id)).first()[0] cwd = os.getcwd() os.chdir(config['app_conf']['media_dir']) xml_string = call_svn_cmd(".", "info", "--incremental --xml") svn = parse_svn(xml_string) while revision < svn.revision: revision += 1 update_rev(session, revision) session.commit() os.chdir(cwd)
def get_create_revision(path, session, rev_id): """string, Session, int -> Revision Fetch revision with id 'rev_id' from database or create a new entry. """ revision = session.query(Revision).get(rev_id) if revision is None: xml_string = call_svn_cmd(path, "log", "--incremental --xml -r %s" % rev_id) if xml_string == "": return None svn = parse_svn(xml_string) revision = Revision(svn.revision, u"r%s" % svn.revision, svn.msg, svn.author, svn.date) session.add(revision) return revision
def update_rev(session, rev_id): """Session, int -> None Update a revision entry """ xml_string = call_svn_cmd(".", "log", "--incremental --xml -v -r %s" % rev_id) svn = parse_svn(xml_string) revision = Revision(svn.revision, u"r%s" % svn.revision, svn.msg, svn.author, svn.date) session.add(revision) # reverse the order of the changed paths, as svn info --xml reverses them as # well. This way we don't add files before adding the parent dir. svn.changed_paths.reverse() for action, path in svn.changed_paths: if action in (u'M', u'A'): create_rev_entry(path, session, revision) elif action in (u'D'): delete_file_entry(path, session) else: continue
def create_rev_entry(rev_path, session, rev=None): """string, Session -> None Generate a database entry. """ if rev_path == u'': rev_path = u'.' if not os.path.exists(rev_path): return if rev is not None: xml_string = call_svn_cmd(rev_path, "info", "--incremental --xml -r %s" % rev.id) else: xml_string = call_svn_cmd(rev_path, "info", "--incremental --xml") svn = parse_svn(xml_string) if rev is None: revision = get_create_revision(rev_path, session, svn.revision) else: revision = rev if revision is None: return if svn.kind == u"file": old_file = session.query(File).get(unicode(rev_path)) if old_file is not None: old_file.size = os.path.getsize(rev_path) old_file.revision = revision parent = old_file.directory while parent is not None: parent.revision = revision session.add(parent) parent = parent.parent session.add(old_file) return new_file = File(rev_path, os.path.basename(rev_path), os.path.getsize(rev_path), svn.root) new_file.revision = revision session.add(new_file) parent_path = os.path.dirname(rev_path) if parent_path == u'': parent_path = u'.' parent_dir = session.query(Dir).get(parent_path) if parent_dir is None: # Parent directory was probably created in the same commit. # Recurse just to make sure the parent dir exits. This is # sub-optimal performance-wise, but keeps the database consistent. # SVN XML sucks. create_rev_entry(parent_path, session, rev) parent_dir = session.query(Dir).get(parent_path) new_file.directory = parent_dir session.add(new_file) tags = [] tags.append(get_tag_from_path(session, new_file.path)) tags.append(get_tag_from_filetype(session, new_file.type)) asset = session.query(Asset).filter(Asset.files.contains(new_file)).first() if asset is None: asset = Asset("auto_%s" % new_file.name, tags) asset.files.append(new_file) session.add(asset) elif svn.kind == u"dir": old_dir = session.query(Dir).get(unicode(rev_path)) if old_dir is not None: old_dir.revision = revision session.add(revision) return if rev_path != u'.': dir_name = os.path.basename(rev_path) else: dir_name = u'/' new_dir = Dir(rev_path, dir_name, svn.root) new_dir.revision = revision parent_path = os.path.dirname(rev_path) if parent_path == u'': parent_path = u'.' parent_dir = session.query(Dir).get(parent_path) if parent_dir is not None and rev_path != u'.': new_dir.parent = parent_dir session.add(new_dir)