summaryrefslogtreecommitdiff
path: root/tools/examples
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-08-05 16:22:51 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-08-05 16:22:51 +0000
commitcf46733632c7279a9fd0fe6ce26f9185a4ae82a9 (patch)
treeda27775a2161723ef342e91af41a8b51fedef405 /tools/examples
parentbb0ef45f7c46b0ae221b26265ef98a768c33f820 (diff)
downloadsubversion-tarball-master.tar.gz
Diffstat (limited to 'tools/examples')
-rw-r--r--tools/examples/ExampleAuthn.java105
-rw-r--r--tools/examples/ExampleAuthnOld.java119
-rw-r--r--tools/examples/ExampleAuthnVeryOld.java118
-rwxr-xr-xtools/examples/svnlook.py203
-rwxr-xr-xtools/examples/walk-config-auth.py4
5 files changed, 505 insertions, 44 deletions
diff --git a/tools/examples/ExampleAuthn.java b/tools/examples/ExampleAuthn.java
new file mode 100644
index 0000000..9e0a23e
--- /dev/null
+++ b/tools/examples/ExampleAuthn.java
@@ -0,0 +1,105 @@
+/* Username/password prompt/save using 1.9 org.apache.subversion API.
+
+ Compile against non-installed Subversion JavaHL build:
+
+ javac -cp subversion/bindings/javahl/classes -d subversion/bindings/javahl/classes ExampleAuthn.java
+
+ Run:
+
+ LD_LIBRARY_PATH=subversion/libsvn_auth_gnome_keyring/.libs java -cp subversion/bindings/javahl/classes -Djava.library.path=subversion/bindings/javahl/native/.libs ExampleAuthn <URL> <config-dir>
+
+ */
+import org.apache.subversion.javahl.*;
+import org.apache.subversion.javahl.types.*;
+import org.apache.subversion.javahl.remote.*;
+import org.apache.subversion.javahl.callback.*;
+import java.io.Console;
+
+public class ExampleAuthn {
+
+ protected static class MyAuthn {
+
+ public static AuthnCallback getAuthn() {
+ return new MyAuthnCallback();
+ }
+
+ private static class MyAuthnCallback
+ implements AuthnCallback {
+
+ public UserPasswordResult
+ userPasswordPrompt(String realm, String username, boolean maySave) {
+ System.out.println("userPasswordPrompt");
+ System.out.println("Realm: " + realm);
+ String prompt;
+ if (username == null) {
+ System.out.print("Username: ");
+ username = System.console().readLine();
+ prompt = "Password: ";
+ } else {
+ prompt = "Password for " + username + ": ";
+ }
+ String password = new String(System.console().readPassword(prompt));
+ return new UserPasswordResult(username, password, maySave);
+ }
+
+ public boolean
+ allowStorePlaintextPassword(String realm) {
+ System.out.println("allowStorePlaintextPassword");
+ System.out.println("Realm: " + realm);
+ System.out.print("Store plaintext password on disk? (y/n): ");
+ String s = System.console().readLine();
+ return s.equals("y") ? true : false;
+ }
+
+ public UsernameResult
+ usernamePrompt(String realm, boolean maySave) {
+ System.out.println("usernamePrompt not implemented!");
+ return null;
+ }
+
+ public boolean
+ allowStorePlaintextPassphrase(String realm) {
+ System.out.println("allowStorePlaintextPassphrase not implemented!");
+ return false;
+ }
+
+ public SSLServerTrustResult
+ sslServerTrustPrompt(String realm,
+ SSLServerCertFailures failures,
+ SSLServerCertInfo info,
+ boolean maySave) {
+ System.out.println("sslServerTrustPrompt not implemented!");
+ return SSLServerTrustResult.acceptTemporarily();
+ }
+
+ public SSLClientCertResult
+ sslClientCertPrompt(String realm, boolean maySave) {
+ System.out.println("sslClientCertPrompt not implemented!");
+ return null;
+ }
+
+ public SSLClientCertPassphraseResult
+ sslClientCertPassphrasePrompt(String realm, boolean maySave) {
+ System.out.println("sslClientCertPassphrasePrompt not implemented!");
+ return null;
+ }
+ }
+ }
+
+ public static void main(String argv[]) {
+
+ if (argv.length != 2) {
+ System.err.println("usage: ExampleAuthn <URL> <config-dir>");
+ return;
+ }
+ RemoteFactory factory = new RemoteFactory();
+ factory.setConfigDirectory(argv[1]);
+ factory.setPrompt(MyAuthn.getAuthn());
+ try {
+ ISVNRemote raSession = factory.openRemoteSession(argv[0]);
+ raSession.getReposUUID();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/tools/examples/ExampleAuthnOld.java b/tools/examples/ExampleAuthnOld.java
new file mode 100644
index 0000000..cbcd92d
--- /dev/null
+++ b/tools/examples/ExampleAuthnOld.java
@@ -0,0 +1,119 @@
+/* Username/password prompt/save using old org.apache.subversion API.
+
+ Compile against non-installed Subversion JavaHL build:
+
+ javac -cp subversion/bindings/javahl/classes -d subversion/bindings/javahl/classes ExampleAuthnOld.java
+
+ Run:
+
+ LD_LIBRARY_PATH=subversion/libsvn_auth_gnome_keyring/.libs java -cp subversion/bindings/javahl/classes -Djava.library.path=subversion/bindings/javahl/native/.libs ExampleAuthnOld <URL> <config-dir>
+
+ */
+import org.apache.subversion.javahl.*;
+import org.apache.subversion.javahl.types.*;
+import org.apache.subversion.javahl.callback.*;
+import java.io.Console;
+
+public class ExampleAuthnOld {
+
+ protected static class MyAuthn {
+
+ public static UserPasswordCallback getAuthn() {
+ return new MyUserPasswordCallback();
+ }
+
+ private static class MyUserPasswordCallback
+ implements UserPasswordCallback {
+
+ private String _username = null;
+
+ public String
+ getUsername() {
+ System.out.println("getUsername");
+ return _username;
+ }
+
+ private String _password = null;
+
+ public String
+ getPassword() {
+ System.out.println("getPassword");
+ return _password;
+ }
+
+ public boolean
+ userAllowedSave() {
+ System.out.println("userAllowedSave");
+ return true;
+ }
+
+ public boolean
+ askYesNo(String realm, String question, boolean yesIsDefault) {
+ System.out.println("askYesNo");
+ System.out.print(question + " (y/n): ");
+ String s = System.console().readLine();
+ return s.equals("y") ? true : s.equals("") ? yesIsDefault : false;
+ }
+
+ public boolean
+ prompt(String realm, String username, boolean maySave) {
+ System.out.println("prompt");
+ System.out.println("Realm: " + realm);
+ String prompt;
+ if (username == null) {
+ System.out.print("Username: ");
+ _username = System.console().readLine();
+ prompt = "Password: ";
+ } else {
+ _username = username;
+ prompt = "Password for " + username + ": ";
+ }
+ _password = new String(System.console().readPassword(prompt));
+ return maySave;
+ }
+
+ public boolean
+ prompt(String realm, String username) {
+ System.out.println("prompt not implemented!");
+ return true;
+ }
+
+ public String
+ askQuestion(String realm,
+ String question,
+ boolean showAnswer,
+ boolean maySave) {
+ System.out.println("askQuestion not implemented!");
+ return null;
+ }
+
+ public String
+ askQuestion(String realm, String question, boolean showAnswer) {
+ System.out.println("askQuestion not implemented!");
+ return null;
+ }
+
+ public int
+ askTrustSSLServer(String info, boolean allowPermanently) {
+ System.out.println("askTrustSSLServer not implemented!");
+ return UserPasswordCallback.AcceptTemporary;
+ }
+ }
+ }
+
+ public static void main(String argv[]) {
+
+ if (argv.length != 2) {
+ System.err.println("usage: ExampleAuthnOld <URL> <config-dir>");
+ return;
+ }
+ ISVNClient client = new SVNClient();
+ client.setPrompt(MyAuthn.getAuthn());
+ try {
+ client.setConfigDirectory(argv[1]);
+ client.revProperty(argv[0], "svn:log", Revision.getInstance(0));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/tools/examples/ExampleAuthnVeryOld.java b/tools/examples/ExampleAuthnVeryOld.java
new file mode 100644
index 0000000..b2bd8d0
--- /dev/null
+++ b/tools/examples/ExampleAuthnVeryOld.java
@@ -0,0 +1,118 @@
+/* Username/password prompt/save using very old org.tigris.subversion API.
+
+ Compile against non-installed Subversion JavaHL build:
+
+ javac -cp subversion/bindings/javahl/classes -d subversion/bindings/javahl/classes ExampleAuthnVeryOld.java
+
+ Run:
+
+ LD_LIBRARY_PATH=subversion/libsvn_auth_gnome_keyring/.libs java -cp subversion/bindings/javahl/classes -Djava.library.path=subversion/bindings/javahl/native/.libs ExampleAuthnVeryOld <URL> <config-dir>
+
+ */
+import org.tigris.subversion.javahl.*;
+import java.io.Console;
+
+public class ExampleAuthnVeryOld {
+
+ protected static class MyAuthn {
+
+ public static PromptUserPassword3 getAuthn() {
+ return new MyUserPasswordCallback();
+ }
+
+ private static class MyUserPasswordCallback
+ implements PromptUserPassword3 {
+
+ private String _username = null;
+
+ public String
+ getUsername() {
+ System.out.println("getUsername");
+ return _username;
+ }
+
+ private String _password = null;
+
+ public String
+ getPassword() {
+ System.out.println("getPassword");
+ return _password;
+ }
+
+ public boolean
+ userAllowedSave() {
+ System.out.println("userAllowedSave");
+ return true;
+ }
+
+ public boolean
+ askYesNo(String realm, String question, boolean yesIsDefault) {
+ System.out.println("askYesNo");
+ System.out.print(question + " (y/n): ");
+ String s = System.console().readLine();
+ return s.equals("y") ? true : s.equals("") ? yesIsDefault : false;
+ }
+
+ public boolean
+ prompt(String realm, String username, boolean maySave) {
+ System.out.println("prompt");
+ System.out.println("Realm: " + realm);
+ String prompt;
+ if (username == null) {
+ System.out.print("Username: ");
+ _username = System.console().readLine();
+ prompt = "Password: ";
+ } else {
+ _username = username;
+ prompt = "Password for " + username + ": ";
+ }
+ _password = new String(System.console().readPassword(prompt));
+ return maySave;
+ }
+
+ public boolean
+ prompt(String realm, String username) {
+ System.out.println("prompt not implemented!");
+ return true;
+ }
+
+ public String
+ askQuestion(String realm,
+ String question,
+ boolean showAnswer,
+ boolean maySave) {
+ System.out.println("askQuestion not implemented!");
+ return null;
+ }
+
+ public String
+ askQuestion(String realm, String question, boolean showAnswer) {
+ System.out.println("askQuestion not implemented!");
+ return null;
+ }
+
+ public int
+ askTrustSSLServer(String info, boolean allowPermanently) {
+ System.out.println("askTrustSSLServer not implemented!");
+ return PromptUserPassword3.AcceptTemporary;
+ }
+ }
+ }
+
+ public static void main(String argv[]) {
+
+ if (argv.length != 2) {
+ System.err.println("usage: ExampleAuthnVeryOld <URL> <config-dir>");
+ return;
+ }
+ SVNClientInterface client = new SVNClient();
+ client.setPrompt(MyAuthn.getAuthn());
+ try {
+ client.setConfigDirectory(argv[1]);
+ client.logMessages(argv[0], Revision.getInstance(0),
+ Revision.getInstance(0), false, false, 0);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/tools/examples/svnlook.py b/tools/examples/svnlook.py
index 72043e6..bd36951 100755
--- a/tools/examples/svnlook.py
+++ b/tools/examples/svnlook.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# svnlook.py : a Python-based replacement for svnlook
+# svnlook.py : alternative svnlook in Python with library API
#
######################################################################
# Licensed to the Apache Software Foundation (ASF) under one
@@ -21,6 +21,34 @@
# under the License.
######################################################################
#
+# $HeadURL: https://svn.apache.org/repos/asf/subversion/branches/1.9.x/tools/examples/svnlook.py $
+# $LastChangedDate: 2013-11-14 11:11:07 +0000 (Thu, 14 Nov 2013) $
+# $LastChangedRevision: 1541878 $
+
+"""
+svnlook.py can also be used as a Python module::
+
+ >>> import svnlook
+ >>> svnlook = svnlook.SVNLook("/testrepo")
+ >>> svnlook.get_author()
+ 'randomjoe'
+
+
+Accessible API::
+
+[x] author
+[x] changed
+[x] date
+[ ] diff
+[x] dirs-changed
+[ ] ids
+[x] info
+[x] log
+[ ] tree
+---
+[ ] generator API to avoid passing lists
+"""
+
import sys
import time
@@ -28,86 +56,155 @@ import os
from svn import core, fs, delta, repos
-class SVNLook:
- def __init__(self, path, cmd, rev, txn):
+class SVNLook(object):
+ def __init__(self, path, rev=None, txn=None, cmd=None):
+ """
+ path - path to repository
+ rev - revision number
+ txn - name of transaction (usually the one about to be committed)
+ cmd - if set, specifies cmd_* method to execute
+
+ txn takes precedence over rev; if both are None, inspect the head revision
+ """
path = core.svn_path_canonicalize(path)
repos_ptr = repos.open(path)
self.fs_ptr = repos.fs(repos_ptr)
+ # if set, txn takes precendence
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)
+ else:
+ rev = int(rev)
self.rev = rev
- getattr(self, 'cmd_' + cmd)()
+ if cmd != None:
+ getattr(self, 'cmd_' + cmd)()
def cmd_default(self):
self.cmd_info()
self.cmd_tree()
def cmd_author(self):
- # get the author property, or empty string if the property is not present
- author = self._get_property(core.SVN_PROP_REVISION_AUTHOR) or ''
- print(author)
+ print(self.get_author() or '')
def cmd_changed(self):
- self._print_tree(ChangedEditor, pass_root=1)
+ for status, path in self.get_changed():
+ print("%-3s %s" % (status, path))
def cmd_date(self):
- if self.txn_ptr:
+ # duplicate original svnlook format, which is
+ # 2010-02-08 21:53:15 +0200 (Mon, 08 Feb 2010)
+ secs = self.get_date(unixtime=True)
+ if secs is None:
print("")
else:
- date = self._get_property(core.SVN_PROP_REVISION_DATE)
- if date:
- aprtime = core.svn_time_from_cstring(date)
- # ### convert to a time_t; this requires intimate knowledge of
- # ### the apr_time_t type
- secs = aprtime / 1000000 # aprtime is microseconds; make seconds
-
- # assume secs in local TZ, convert to tuple, and format
- ### we don't really know the TZ, do we?
- print(time.strftime('%Y-%m-%d %H:%M', time.localtime(secs)))
- else:
- print("")
+ # convert to tuple, detect time zone and format
+ stamp = time.localtime(secs)
+ isdst = stamp.tm_isdst
+ utcoffset = -(time.altzone if (time.daylight and isdst) else time.timezone) // 60
+
+ suffix = "%+03d%02d" % (utcoffset // 60, abs(utcoffset) % 60)
+ outstr = time.strftime('%Y-%m-%d %H:%M:%S ', stamp) + suffix
+ outstr += time.strftime(' (%a, %d %b %Y)', stamp)
+ print(outstr)
+
def cmd_diff(self):
self._print_tree(DiffEditor, pass_root=1)
def cmd_dirs_changed(self):
- self._print_tree(DirsChangedEditor)
+ for dir in self.get_changed_dirs():
+ print(dir)
def cmd_ids(self):
self._print_tree(Editor, base_rev=0, pass_root=1)
def cmd_info(self):
+ """print the author, data, log_size, and log message"""
self.cmd_author()
self.cmd_date()
- self.cmd_log(1)
-
- def cmd_log(self, print_size=0):
- # get the log property, or empty string if the property is not present
- log = self._get_property(core.SVN_PROP_REVISION_LOG) or ''
- if print_size:
- print(len(log))
+ log = self.get_log() or ''
+ print(len(log))
print(log)
+ def cmd_log(self):
+ print(self.get_log() or '')
+
def cmd_tree(self):
self._print_tree(Editor, base_rev=0)
+
+ # --- API getters
+ def get_author(self):
+ """return string with the author name or None"""
+ return self._get_property(core.SVN_PROP_REVISION_AUTHOR)
+
+ def get_changed(self):
+ """return list of tuples (status, path)"""
+ ret = []
+ def list_callback(status, path):
+ ret.append( (status, path) )
+ self._walk_tree(ChangedEditor, pass_root=1, callback=list_callback)
+ return ret
+
+ def get_date(self, unixtime=False):
+ """return commit timestamp in RFC 3339 format (2010-02-08T20:37:25.195000Z)
+ if unixtime is True, return unix timestamp
+ return None for a txn, or if date property is not set
+ """
+ if self.txn_ptr:
+ return None
+
+ date = self._get_property(core.SVN_PROP_REVISION_DATE)
+ if not unixtime or date == None:
+ return date
+
+ # convert to unix time
+ aprtime = core.svn_time_from_cstring(date)
+ # ### convert to a time_t; this requires intimate knowledge of
+ # ### the apr_time_t type
+ secs = aprtime / 1000000 # aprtime is microseconds; make seconds
+ return secs
+
+ def get_changed_dirs(self):
+ """return list of changed dirs
+ dir names end with trailing forward slash even on windows
+ """
+ dirlist = []
+ def list_callback(item):
+ dirlist.append(item)
+ self._walk_tree(DirsChangedEditor, callback=list_callback)
+ return dirlist
+
+ def get_log(self):
+ """return log message string or None if not present"""
+ return self._get_property(core.SVN_PROP_REVISION_LOG)
+
+
+ # --- Internal helpers
def _get_property(self, name):
if self.txn_ptr:
return fs.txn_prop(self.txn_ptr, name)
return fs.revision_prop(self.fs_ptr, self.rev, name)
def _print_tree(self, e_factory, base_rev=None, pass_root=0):
+ def print_callback(msg):
+ print(msg)
+ self._walk_tree(e_factory, base_rev, pass_root, callback=print_callback)
+
+ # svn fs, delta, repos calls needs review according to DeltaEditor documentation
+ 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)
+ elif self.rev == 0:
+ base_rev = 0
else:
base_rev = self.rev - 1
@@ -120,10 +217,13 @@ class SVNLook:
# 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)
+ editor = e_factory(root, base_root, callback)
else:
- editor = e_factory()
+ editor = e_factory(callback=callback)
# construct the editor for printing these things out
e_ptr, e_baton = delta.make_editor(editor)
@@ -135,8 +235,17 @@ class SVNLook:
e_ptr, e_baton, authz_cb, 0, 1, 0, 0)
+# ---------------------------------------------------------
+# Delta Editors. For documentation see:
+# http://subversion.apache.org/docs/community-guide/#docs
+
+# this one doesn't process delete_entry, change_dir_prop, apply_text_delta,
+# change_file_prop, close_file, close_edit, abort_edit
+# ?set_target_revision
+# need review
class Editor(delta.Editor):
- def __init__(self, root=None, base_root=None):
+ def __init__(self, root=None, base_root=None, callback=None):
+ """callback argument is unused for this editor"""
self.root = root
# base_root ignored
@@ -172,7 +281,14 @@ class Editor(delta.Editor):
return ' <%s>' % fs.unparse_id(id)
return ''
+# doesn't process close_directory, apply_text_delta,
+# change_file_prop, close_file, close_edit, abort_edit
+# ?set_target_revision
class DirsChangedEditor(delta.Editor):
+ """print names of changed dirs, callback(dir) is a printer function"""
+ def __init__(self, callback):
+ self.callback = callback
+
def open_root(self, base_revision, dir_pool):
return [ 1, '' ]
@@ -201,13 +317,15 @@ class DirsChangedEditor(delta.Editor):
def _dir_changed(self, baton):
if baton[0]:
# the directory hasn't been printed yet. do it.
- print(baton[1] + '/')
+ self.callback(baton[1] + '/')
baton[0] = 0
class ChangedEditor(delta.Editor):
- def __init__(self, root, base_root):
+ def __init__(self, root, base_root, callback):
+ """callback(status, path) is a printer function"""
self.root = root
self.base_root = base_root
+ self.callback = callback
def open_root(self, base_revision, dir_pool):
return [ 1, '' ]
@@ -215,13 +333,13 @@ class ChangedEditor(delta.Editor):
def delete_entry(self, path, revision, parent_baton, pool):
### need more logic to detect 'replace'
if fs.is_dir(self.base_root, '/' + path):
- print('D ' + path + '/')
+ self.callback('D', path + '/')
else:
- print('D ' + path)
+ self.callback('D', path)
def add_directory(self, path, parent_baton,
copyfrom_path, copyfrom_revision, dir_pool):
- print('A ' + path + '/')
+ self.callback('A', path + '/')
return [ 0, path ]
def open_directory(self, path, parent_baton, base_revision, dir_pool):
@@ -230,12 +348,12 @@ class ChangedEditor(delta.Editor):
def change_dir_prop(self, dir_baton, name, value, pool):
if dir_baton[0]:
# the directory hasn't been printed yet. do it.
- print('_U ' + dir_baton[1] + '/')
+ self.callback('_U', dir_baton[1] + '/')
dir_baton[0] = 0
def add_file(self, path, parent_baton,
copyfrom_path, copyfrom_revision, file_pool):
- print('A ' + path)
+ self.callback('A', path)
return [ '_', ' ', None ]
def open_file(self, path, parent_baton, base_revision, file_pool):
@@ -257,11 +375,12 @@ class ChangedEditor(delta.Editor):
status = text_mod + prop_mod
# was there some kind of change?
if status != '_ ':
- print(status + ' ' + path)
+ self.callback(status.rstrip(), path)
class DiffEditor(delta.Editor):
- def __init__(self, root, base_root):
+ def __init__(self, root, base_root, callback=None):
+ """callback argument is unused for this editor"""
self.root = root
self.base_root = base_root
self.target_revision = 0
@@ -435,7 +554,7 @@ def main():
if not hasattr(SVNLook, 'cmd_' + cmd):
usage(1)
- SVNLook(sys.argv[1], cmd, rev, txn)
+ SVNLook(sys.argv[1], rev, txn, cmd)
if __name__ == '__main__':
main()
diff --git a/tools/examples/walk-config-auth.py b/tools/examples/walk-config-auth.py
index b3298f3..5841b6c 100755
--- a/tools/examples/walk-config-auth.py
+++ b/tools/examples/walk-config-auth.py
@@ -37,8 +37,8 @@ def print_help():
def show_creds(hash):
hash_keys = hash.keys()
- maxkeylen = max(map(lambda x: len(x), hash_keys))
- maxvallen = max(map(lambda x: len(x), hash.values()))
+ maxkeylen = max(map(len, hash_keys))
+ maxvallen = max(map(len, hash.values()))
hash_keys.sort()
sys.stdout.write("+")
sys.stdout.write("-" * (maxkeylen + 2))