def putfile(fname, rpath, home='.'): _util.apr_initialize() pool = _util.svn_pool_create(None) db_path = os.path.join(home, 'db') if not os.path.exists(db_path): db_path = home fsob = fs.new(pool) fs.open_berkeley(fsob, db_path) # open a transaction against HEAD rev = fs.youngest_rev(fsob, pool) txn = fs.begin_txn(fsob, rev, pool) print ` txn ` root = fs.txn_root(txn, pool) fs.make_file(root, rpath, pool) handler, baton = fs.apply_textdelta(root, rpath, pool) ### it would be nice to get an svn_stream_t. for now, just load in the ### whole file and shove it into the FS. _delta.svn_txdelta_send_string( open(fname, 'rb').read(), handler, baton, pool) conflicts, new_rev = fs.commit_txn(txn) if conflicts: print 'conflicts:', conflicts print 'New revision:', new_rev _util.svn_pool_destroy(pool) _util.apr_terminate()
def _walk_tree(self, e_factory, base_rev=None, pass_root=0, callback=None): if base_rev is None: # a specific base rev was not provided. use the transaction base, # or the previous revision if self.txn_ptr: base_rev = fs.txn_base_revision(self.txn_ptr) else: base_rev = self.rev - 1 # get the current root if self.txn_ptr: root = fs.txn_root(self.txn_ptr) else: root = fs.revision_root(self.fs_ptr, self.rev) # the base of the comparison base_root = fs.revision_root(self.fs_ptr, base_rev) if callback == None: callback = lambda msg: None if pass_root: editor = e_factory(root, base_root, callback) else: editor = e_factory(callback=callback) # construct the editor for printing these things out e_ptr, e_baton = delta.make_editor(editor) # compute the delta, printing as we go def authz_cb(root, path, pool): return 1 repos.dir_delta(base_root, '', '', root, '', e_ptr, e_baton, authz_cb, 0, 1, 0, 0)
def putfile(fname, rpath, uname="", commitmsg=""): rpath = core.svn_path_canonicalize(rpath) repos_ptr = repos.open(rpath) fsob = repos.fs(repos_ptr) # open a transaction against HEAD rev = fs.youngest_rev(fsob) txn = repos.fs_begin_txn_for_commit(repos_ptr, rev, uname, commitmsg) root = fs.txn_root(txn) rev_root = fs.revision_root(fsob, rev) kind = fs.check_path(root, fname) if kind == core.svn_node_none: print("file '%s' does not exist, creating..." % fname) fs.make_file(root, fname) elif kind == core.svn_node_dir: print("File '%s' is a dir." % fname) return else: print("Updating file '%s'" % fname) handler, baton = fs.apply_textdelta(root, fname, None, None) ### it would be nice to get an svn_stream_t. for now, just load in the ### whole file and shove it into the FS. delta.svn_txdelta_send_string(open(fname, 'rb').read(), handler, baton) newrev = repos.fs_commit_txn(repos_ptr, txn) print("revision: %s" % newrev)
def main(pool, repos_dir, txn): # Construct a ChangeCollector to fetch our changes. fs_ptr = repos.svn_repos_fs(repos.svn_repos_open(repos_dir, pool)) root = fs.txn_root(fs.open_txn(fs_ptr, txn, pool), pool) cc = repos.ChangeCollector(fs_ptr, root, pool) # Call the transaction property validator. Might as well get the # cheap checks outta the way first. retval = test_props(cc.get_root_props()) if retval: return retval # Generate the path-based changes list. e_ptr, e_baton = delta.make_editor(cc, pool) repos.svn_repos_replay(root, e_ptr, e_baton, pool) # Call the path change validator. changes = cc.get_changes() paths = changes.keys() paths.sort(lambda a, b: core.svn_path_compare_paths(a, b)) for path in paths: change = changes[path] retval = test_path_change(path, change) if retval: return retval return 0
def _print_tree(self, e_factory, base_rev=None, pass_root=0): if base_rev is None: # a specific base rev was not provided. use the transaction base, # or the previous revision if self.txn_ptr: base_rev = fs.txn_base_revision(self.txn_ptr) else: base_rev = self.rev - 1 # get the current root if self.txn_ptr: root = fs.txn_root(self.txn_ptr) else: root = fs.revision_root(self.fs_ptr, self.rev) # the base of the comparison base_root = fs.revision_root(self.fs_ptr, base_rev) if pass_root: editor = e_factory(root, base_root) else: editor = e_factory() # construct the editor for printing these things out e_ptr, e_baton = delta.make_editor(editor) # compute the delta, printing as we go def authz_cb(root, path, pool): return 1 repos.dir_delta(base_root, '', '', root, '', e_ptr, e_baton, authz_cb, 0, 1, 0, 0)
def create_file(self, file_name, content, author, comment): """ Creates a file in the SVN repository with the given name and content (text). Returns the committed revision """ assert self.svn_repos is not None, "SVN repository not set..." # Get an SVN file system pointer fs_ptr = repos.fs(self.svn_repos) rev = fs.youngest_rev(fs_ptr) # Create and SVN transaction txn = fs.begin_txn(fs_ptr, rev) txn_root = fs.txn_root(txn) # Create a file in the root transaction fs.make_file(txn_root, file_name) stream = fs.apply_text(txn_root, file_name, None) core.svn_stream_write(stream, "%s\n" % content) core.svn_stream_close(stream) # Now set the properties svn:log and svn:author to # the newly created node (file) fs.change_txn_prop(txn, 'svn:author', author) fs.change_txn_prop(txn, 'svn:log', comment) # Commit the transaction fs.commit_txn(txn) # Add teh file to the list of created files self.files.append(file_name) # Returns therevision number return rev + 1
def write(self, data, uname='', commitmsg='',istext=False): txn = repos.fs_begin_txn_for_commit(self._repo.repos_ptr, self.revno, uname, commitmsg) r = None try: txn_root = fs.txn_root(txn) kind = fs.check_path(txn_root, self.path) if kind == core.svn_node_none: if not _create_file(txn_root, self.path): raise 'attempt to create file, but file creation error: %s'%path pass elif kind == core.svn_node_dir: raise 'attempt to create file, but directory already exists: %s'%self.path if istext: fs.change_node_prop(txn_root, self.path, 'svn:eol-style', 'native') pass handler, baton = fs.apply_textdelta(txn_root, self.path, None, None) delta.svn_txdelta_send_string(data, handler, baton) r = repos.fs_commit_txn(self._repo.repos_ptr, txn) except Exception, a: fs.abort_txn(txn) raise
def _print_tree(self, e_factory, rootpath='', base_rev=None, pass_root=0): # It no longer prints, it returns the editor made by e_factory which # contains the tree in a list. if base_rev is None: # a specific base rev was not provided. use the transaction base, # or the previous revision if self.txn_ptr: base_rev = fs.txn_base_revision(self.txn_ptr) else: base_rev = self.rev - 1 # get the current root if self.txn_ptr: root = fs.txn_root(self.txn_ptr, self.pool) else: root = fs.revision_root(self.fs_ptr, self.rev, self.pool) # the base of the comparison base_root = fs.revision_root(self.fs_ptr, base_rev, self.pool) if pass_root: editor = e_factory(root, base_root) else: editor = e_factory() # construct the editor for printing these things out e_ptr, e_baton = delta.make_editor(editor, self.pool) # compute the delta, printing as we go def authz_cb(root, path, pool): return 1 repos.dir_delta(base_root, '', '', root, rootpath.encode('utf-8'), e_ptr, e_baton, authz_cb, 0, 1, 0, 0, self.pool) return editor
def _revisionSetup(self, subpool, author, message): """ All of the setup for performing a revision. """ revision = fs.youngest_rev(self.fsob, subpool) transaction = repos.svn_repos_fs_begin_txn_for_commit(self.repos_ptr, revision, author, message, subpool) root = fs.txn_root(transaction, subpool) return (revision, transaction, root)
def _revisionSetup(self, subpool, author, message): """ All of the setup for performing a revision. """ revision = fs.youngest_rev(self.fsob, subpool) message = re.sub('\r\n', '<CRLF>\n', message) # Convert CRLF to LF message = re.sub('\r', '<CR>', message) # Convert CR to LF transaction = repos.svn_repos_fs_begin_txn_for_commit(self.repos_ptr, revision, author, message, subpool) root = fs.txn_root(transaction, subpool) return (revision, transaction, root)
def check_po(pool, repos_path, txn): def authz_cb(root, path, pool): return 1 fs_ptr = repos.fs(repos.open(repos_path, pool)) txn_ptr = fs.open_txn(fs_ptr, txn, pool) txn_root = fs.txn_root(txn_ptr, pool) base_root = fs.revision_root(fs_ptr, fs.txn_base_revision(txn_ptr), pool) editor = ChangeReceiver(txn_root, base_root, pool) e_ptr, e_baton = delta.make_editor(editor, pool) repos.dir_delta(base_root, "", "", txn_root, "", e_ptr, e_baton, authz_cb, 0, 1, 0, 0, pool)
def check_po(pool, repos_path, txn): def authz_cb(root, path, pool): return 1 fs_ptr = repos.fs(repos.open(repos_path, pool)) txn_ptr = fs.open_txn(fs_ptr, txn, pool) txn_root = fs.txn_root(txn_ptr, pool) base_root = fs.revision_root(fs_ptr, fs.txn_base_revision(txn_ptr), pool) editor = ChangeReceiver(txn_root, base_root, pool) e_ptr, e_baton = delta.make_editor(editor, pool) repos.dir_delta(base_root, '', '', txn_root, '', e_ptr, e_baton, authz_cb, 0, 1, 0, 0, pool)
def delete_file(self, file_name): """Deletes the given file from the repository""" assert self.svn_repos is not None, "SVN repository not set..." # Get an SVN file system pointer fs_ptr = repos.fs(self.svn_repos) rev = fs.youngest_rev(fs_ptr) # Create and SVN transaction txn = fs.begin_txn(fs_ptr, rev) txn_root = fs.txn_root(txn) # Create a file in the root transaction fs.delete(txn_root, file_name) # Commit the transaction fs.commit_txn(txn)
def do_settxn(self, arg): """set the current transaction to view""" try: txnobj = fs.open_txn(self.fs_ptr, arg) newroot = fs.txn_root(txnobj) except: print("Error setting the transaction to '" + arg + "'.") return fs.close_root(self.root) self.root = newroot self.txn = arg self.is_rev = 0 self._do_path_landing()
def delete_files(self): """Deletes all the files created by the helper""" if self.svn_repos is not None: # Get an SVN file system pointer fs_ptr = repos.fs(self.svn_repos) rev = fs.youngest_rev(fs_ptr) # Create and SVN transaction txn = fs.begin_txn(fs_ptr, rev) txn_root = fs.txn_root(txn) # Create a file in the root transaction for svn_file in self.files: fs.delete(txn_root, svn_file) # Commit the transaction fs.commit_txn(txn)
def __init__(self, repository, rev, username, message, pool, logger=None): if logger: self.logger = logger else: self.logger = sys.stdout self.pool = pool self.rev = rev self.fsptr = repos.svn_repos_fs(repository) self.rev_root = fs.revision_root(self.fsptr, self.rev, self.pool) self.txnp = repos.svn_repos_fs_begin_txn_for_commit( repository, self.rev, username, message, self.pool) self.txn_root = fs.txn_root(self.txnp, self.pool) self.log('Base revision %d\n' % rev)
def validate_added_extensions(repos_path, txn_name, extensions, action): # Open the repository and transaction. fs_ptr = repos.fs(repos.open(repos_path)) txn_t = fs.open_txn(fs_ptr, txn_name) txn_root = fs.txn_root(txn_t) # Fetch the changes made in this transaction. changes = fs.svn_fs_paths_changed(txn_root) paths = changes.keys() # Check the changes. for path in paths: change = changes[path] # Always allow deletions. if change.change_kind == fs.path_change_delete: continue # Always allow non-files. kind = fs.check_path(txn_root, path) if kind != core.svn_node_file: continue # If this was a newly added (without history) file ... if ((change.change_kind == fs.path_change_replace) \ or (change.change_kind == fs.path_change_add)): copyfrom_rev, copyfrom_path = fs.copied_from(txn_root, path) if copyfrom_rev == core.SVN_INVALID_REVNUM: # ... then check it for a valid extension. base, ext = os.path.splitext(path) if ext: ext = ext[1:].lower() if ((ext in extensions) and (action == 'deny')) \ or ((ext not in extensions) and (action == 'allow')): sys.stderr.write( "Path '%s' has an extension disallowed by server " "configuration.\n" % (path)) sys.exit(1)
def __init__(self, path, rev, txn): self.repo_root_path = core.svn_path_canonicalize(path) root_ptr = repos.open(self.repo_root_path) self.fs_ptr = repos.fs(root_ptr) # Set the revision/transaction if txn: self.txn_ptr = fs.open_txn(self.fs_ptr, txn) else: self.txn_ptr = None if rev is None: rev = fs.youngest_rev(self.fs_ptr) self.rev = rev # Set the root if self.txn_ptr: self.root = fs.txn_root(self.txn_ptr) else: self.root = fs.revision_root(self.fs_ptr, self.rev) # Set the base revision/transaction if self.txn_ptr: self.base_rev = fs.txn_base_revision(self.txn_ptr) else: self.base_rev = self.rev - 1 # Set the base root of the comparison self.base_root = fs.revision_root(self.fs_ptr, self.base_rev) # Get all the changes and sort by path editor = repos.ChangeCollector(self.fs_ptr, self.root) e_ptr, e_baton = delta.make_editor(editor) repos.replay(self.root, e_ptr, e_baton) self.changelist = editor.get_changes().items() self.changelist.sort()
def validate_added_extensions(repos_path, txn_name, extensions, action): # Open the repository and transaction. fs_ptr = repos.fs(repos.open(repos_path)) txn_t = fs.open_txn(fs_ptr, txn_name) txn_root = fs.txn_root(txn_t) # Fetch the changes made in this transaction. changes = fs.svn_fs_paths_changed(txn_root) paths = changes.keys() # Check the changes. for path in paths: change = changes[path] # Always allow deletions. if change.change_kind == fs.path_change_delete: continue # Always allow non-files. kind = fs.check_path(txn_root, path) if kind != core.svn_node_file: continue # If this was a newly added (without history) file ... if ((change.change_kind == fs.path_change_replace) \ or (change.change_kind == fs.path_change_add)): copyfrom_rev, copyfrom_path = fs.copied_from(txn_root, path) if copyfrom_rev == core.SVN_INVALID_REVNUM: # ... then check it for a valid extension. base, ext = os.path.splitext(path) if ext: ext = ext[1:].lower() if ((ext in extensions) and (action == 'deny')) \ or ((ext not in extensions) and (action == 'allow')): sys.stderr.write("Path '%s' has an extension disallowed by server " "configuration.\n" % (path)) sys.exit(1)
def commit(self, t_fs, ctx): # commit this transaction print 'committing: %s, over %d seconds' % (time.ctime( self.t_min), self.t_max - self.t_min) # create a pool for the entire commit c_pool = util.svn_pool_create(ctx.pool) rev = fs.youngest_rev(t_fs, c_pool) txn = fs.begin_txn(t_fs, rev, c_pool) root = fs.txn_root(txn, c_pool) lastcommit = (None, None) # create a pool for each file; it will be cleared on each iteration f_pool = util.svn_pool_create(c_pool) for f, r in self.changes: # compute a repository path. ensure we have a leading "/" and drop # the ,v from the file name repos_path = '/' + relative_name(ctx.cvsroot, f[:-2]) #print 'DEBUG:', repos_path print ' changing %s : %s' % (r, repos_path) ### hmm. need to clarify OS path separators vs FS path separators dirname = os.path.dirname(repos_path) if dirname != '/': # get the components of the path (skipping the leading '/') parts = string.split(dirname[1:], os.sep) for i in range(1, len(parts) + 1): # reassemble the pieces, adding a leading slash parent_dir = '/' + string.join(parts[:i], '/') if fs.check_path(root, parent_dir, f_pool) == svn_node_none: print ' making dir:', parent_dir fs.make_dir(root, parent_dir, f_pool) if fs.check_path(root, repos_path, f_pool) == svn_node_none: created_file = 1 fs.make_file(root, repos_path, f_pool) else: created_file = 0 handler, baton = fs.apply_textdelta(root, repos_path, f_pool) # figure out the real file path for "co" try: statcache.stat(f) except os.error: dirname, fname = os.path.split(f) f = os.path.join(dirname, 'Attic', fname) statcache.stat(f) pipe = os.popen('co -q -p%s %s' % (r, f), 'r', 102400) # if we just made the file, we can send it in one big hunk, rather # than streaming it in. ### we should watch out for file sizes here; we don't want to yank ### in HUGE files... if created_file: _delta.svn_txdelta_send_string(pipe.read(), handler, baton, f_pool) else: # open an SVN stream onto the pipe stream2 = util.svn_stream_from_stdio(pipe, f_pool) # Get the current file contents from the repo, or, if we have # multiple CVS revisions to the same file being done in this # single commit, then get the contents of the previous # revision from co, or else the delta won't be correct because # the contents in the repo won't have changed yet. if repos_path == lastcommit[0]: infile2 = os.popen("co -q -p%s %s" % (lastcommit[1], f), "r", 102400) stream1 = util.svn_stream_from_stdio(infile2, f_pool) else: stream1 = fs.file_contents(root, repos_path, f_pool) txstream = _delta.svn_txdelta(stream1, stream2, f_pool) _delta.svn_txdelta_send_txstream(txstream, handler, baton, f_pool) # shut down the previous-rev pipe, if we opened it infile2 = None # shut down the current-rev pipe pipe.close() # wipe the pool. this will get rid of the pipe streams and the delta # stream, and anything the FS may have done. util.svn_pool_clear(f_pool) # remember what we just did, for the next iteration lastcommit = (repos_path, r) for f, r in self.deletes: # compute a repository path. ensure we have a leading "/" and drop # the ,v from the file name repos_path = '/' + relative_name(ctx.cvsroot, f[:-2]) print ' deleting %s : %s' % (r, repos_path) # If the file was initially added on a branch, the first mainline # revision will be marked dead, and thus, attempts to delete it will # fail, since it doesn't really exist. if r != '1.1': ### need to discriminate between OS paths and FS paths fs.delete(root, repos_path, f_pool) # wipe the pool, in case the delete loads it up util.svn_pool_clear(f_pool) # get the metadata for this commit author, log, date = self.get_metadata(c_pool) fs.change_txn_prop(txn, 'svn:author', author, c_pool) fs.change_txn_prop(txn, 'svn:log', log, c_pool) conflicts, new_rev = fs.commit_txn(txn) # set the time to the proper (past) time fs.change_rev_prop(t_fs, new_rev, 'svn:date', date, c_pool) ### how come conflicts is a newline? if conflicts != '\n': print ' CONFLICTS:', ` conflicts ` print ' new revision:', new_rev # done with the commit and file pools util.svn_pool_destroy(c_pool)
def ensure_names(path, names, txn_root): if (not names.has_key(path)): names[path] = [] for name, dirent in fs.dir_entries(txn_root, path).iteritems(): names[path].append([canonicalize(name), name]) names = {} # map of: key - path, value - list of two element lists of names clashes = {} # map of: key - path, value - map of: key - path, value - dummy native = locale.getlocale()[1] if not native: native = 'ascii' repos_handle = repos.open(sys.argv[1].decode(native).encode('utf-8')) fs_handle = repos.fs(repos_handle) txn_handle = fs.open_txn(fs_handle, sys.argv[2].decode(native).encode('utf-8')) txn_root = fs.txn_root(txn_handle) new_paths = get_new_paths(txn_root) for path in new_paths: dir, name = split_path(path) canonical = canonicalize(name) ensure_names(dir, names, txn_root) for name_pair in names[dir]: if (name_pair[0] == canonical and name_pair[1] != name): canonical_path = join_path(dir, canonical) if (not clashes.has_key(canonical_path)): clashes[canonical_path] = {} clashes[canonical_path][join_path(dir, name)] = True clashes[canonical_path][join_path(dir, name_pair[1])] = True if (clashes):