[Pkg-owncloud-commits] [python-owncloud] 04/96: UGH GIT! you confuzzle me so!!! the rest of the files… :)

Sandro Knauß hefee-guest at moszumanska.debian.org
Fri Nov 22 01:28:21 UTC 2013


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository python-owncloud.

commit 7b76f540bd0d04a7a9224c219fc2f1a12be732dc
Author: Craig Sawyer <csawyer at yumaed.org>
Date:   Sat Mar 9 18:24:31 2013 -0700

    UGH GIT! you confuzzle me so!!! the rest of the files… :)
---
 README.md   |   57 +++++++++++++-
 csync.py    |  244 ++++++++++++++++++++++++++++++++++++++++++++---------------
 csynclib.py |   14 +++-
 3 files changed, 253 insertions(+), 62 deletions(-)

diff --git a/README.md b/README.md
index 87118c8..9ed3d6d 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,59 @@
 pyOwnCloud
 ==========
 
-ownCloud CLI client written in python, more info on owncloud: www.owncloud.org
\ No newline at end of file
+ownCloud CLI client written in python, more info about owncloud: www.owncloud.org
+
+Requirements:
+	* the ocsync C library from ownCloud. If you install Mirall, you get it for free on linux.
+	if you don't want to install mirall, you can install the 'ocsync' binary package from the owncloud repo's.
+	* Python > 2.6
+	* an ownCloud server to sync with. (presumably you already have one of these)
+
+
+usage: just run csync.py -h, and it will give you help.
+
+usage: csync.py [-h] [-v] [-c [CONFIG]] [-u [USER]] [-p [PASS]] [-d [DST]]
+                [src] [url]
+
+Synchronize files across machines using ownCloud DAV server
+
+positional arguments:
+  src                   local Directory to sync with
+  url                   url to sync to.
+
+optional arguments:
+  -h, --help            show this help message and exit
+  -v, --version         show program's version number and exit
+  -c [CONFIG], --config [CONFIG]
+                        username on server.
+  -u [USER], --user [USER]
+                        username on server.
+  -p [PASS], --pass [PASS]
+                        password on server. you can also store this in
+                        environment variable OCPASS
+  -d [DST], --dst [DST]
+                        fodler on server.
+
+I only support the 'ownCloud' section of the ownCloud config file.
+I support the following keys in the cfg  file:
+	user: The username on the ownCloud server
+	url: the url of the ownCloud Server
+	pass: the password on the ownCloud server
+	sslFingerprint: a valid SSL fingerprint for the server.
+	src: local directory to sync against.
+	dst: folder on the server to sync against.
+complete example:
+[ownCloud]
+user=awesomeSauce
+pass=PasswordThisIsSuperSuperSecretReallyISwearLOL
+url=url=https://www.example.org/owncloud/
+sslFingerprint=
+src=/home/awesomeSauce/ownCloud
+dst=clientsync
+
+Password options:
+  *) You can specify on the cmd line: -p (not very safe)
+  *) in the envifonment variable: OCPASS
+  *) in the owncloud.cfg file as pass = <password>
+  the choice is yours, if you put it in the cfg file, be careful to 
+  make sure nobody but you can read the file. (0400/0600 file perms)
diff --git a/csync.py b/csync.py
index 9431857..767d5fa 100755
--- a/csync.py
+++ b/csync.py
@@ -1,39 +1,25 @@
 #!/usr/bin/env python
 
+import os
 import sys
-
-import csynclib
+import argparse
+import ConfigParser
 import ctypes
 
-def getUser():
-	"""return the username for authentication to the ownCloud server"""
-	return 'USER'
-
-def getPass():
-	"""return the password for authentication to the ownCloud server"""
-	return 'password'
-
-def log(ctx, verbosity, function, buffer, userdata):
-	"""Log stuff from the ocsync library, but it does not work..."""
-	print 'LOG:', verbosity, function, buffer, userdata
-	return 0
+import csynclib
+import version
+VERSION = version.version
 
-def error(ctx, cmd, returnCode):
-	"""handle library errors"""
-	errNum = csynclib.csync_get_error(ctx)
-	errMsg = csynclib.csync_get_error_string(ctx)
-	print 'ERROR: %s exited %s, error %s: %s' % (
-		cmd,
-		returnCode,
-		errNum,
-		errMsg,
-		)
-	sys.exit(1)
+#Use global variables for user/pass & fingerprint because we have to handle this C callback stuff.
+USERNAME = ''
+PASSWORD = ''
+#TODO, right now, we just blindly accept SSL servers.
+SSLFINGERPRINT = ''
 
 def authCallback(prompt, buffer, bufferLength, echo, verify, userData):
 	"""
 	(const char *prompt, char *buf, size_t len,
-	    int echo, int verify, void *userdata)
+		int echo, int verify, void *userdata)
 	called like this:
 		("Enter your username: ", buf, NE_ABUFSIZ-1, 1, 0, dav_session.userdata )
 		type is 1 for username, 0 for password.
@@ -42,9 +28,9 @@ def authCallback(prompt, buffer, bufferLength, echo, verify, userData):
 	#print 'string:', ctypes.string_at(buffer, bufferLength-1)
 	ret = ''
 	if 'username' in prompt:
-		ret = getUser()
+		ret = USERNAME
 	elif 'password' in prompt:
-		ret = getPass()
+		ret = PASSWORD
 	elif 'SSL' in prompt:
 		ret = 'yes'
 	else:
@@ -53,39 +39,179 @@ def authCallback(prompt, buffer, bufferLength, echo, verify, userData):
 	bufferLength = len(ret)
 	for i in range(len(ret)):
 		ctypes.memset(buffer+i, ord(ret[i]), 1)
-	#print 'returning:', ctypes.string_at(buffer, bufferLength)
+	print 'returning:', ctypes.string_at(buffer, bufferLength)
 	return 0
 
-def sync(local, remote):
-	c = csynclib.CSYNC()
-	ctx = ctypes.pointer(c)
-	r = csynclib.csync_create(ctx, local, remote)
-	if r != 0:
-		error(ctx,'csync_create', r)
-	csynclib.csync_set_log_callback(ctx, csynclib.csync_log_callback(log))
-	acb = csynclib.csync_auth_callback(authCallback)
-	csynclib.csync_set_auth_callback(ctx, acb)
-	r = csynclib.csync_init(ctx)
-	if r != 0:
-		error(ctx, 'csync_init', r)
-	#csynclib.csync_set_log_verbosity(ctx, 11)
-	r = csynclib.csync_update(ctx)
-	if r != 0:
-		error(ctx, 'csync_update', r)
-	r = csynclib.csync_reconcile(ctx)
-	if r != 0:
-		error(ctx, 'csync_reconcile', r)
-	print 'reconcile done'
-	r = csynclib.csync_propagate(ctx)
-	if r != 0:
-		error(ctx, 'csync_propogate', r)
-	r = csynclib.csync_destroy(ctx)
-	if r != 0:
-		error(ctx, 'csync_destroy', r)
-	return 
+class ownCloudSync():
+	"""This handles the actual syncying with ownCloud
+	cfg is a {}.  should have these things:
+		user: 
+		pass:
+		url:
+		src:
+	None of them are optional. :)
+	optional items:
+		SSLfingerPrint:
+	"""
+	def __init__(self, cfg = None):
+		"""initialize"""
+		self.cfg = cfg
+		global USERNAME, PASSWORD, SSLFINGERPRINT
+		USERNAME = cfg['user']
+		PASSWORD = cfg['pass']
+		SSLFINGERPRINT = cfg['sslFingerprint']
+		c = csynclib.CSYNC()
+		self.ctx = ctypes.pointer(c)
+		self.connect()
+		self.sync()
+	
+	def connect(self):
+		r = csynclib.csync_create(self.ctx, self.cfg['localDir'], self.cfg['url'])
+		if r != 0:
+			error(self.ctx,'csync_create', r)
+		csynclib.csync_set_log_callback(self.ctx, csynclib.csync_log_callback(log))
+		acb = csynclib.csync_auth_callback(authCallback)
+		csynclib.csync_set_auth_callback(self.ctx, acb)
+		return 0
+	
+	def sync(self):
+		r = csynclib.csync_init(self.ctx)
+		if r != 0:
+			error(self.ctx, 'csync_init', r)
+		#csynclib.csync_set_log_verbosity(ctx, 11)
+		r = csynclib.csync_update(self.ctx)
+		if r != 0:
+			error(self.ctx, 'csync_update', r)
+		r = csynclib.csync_reconcile(self.ctx)
+		if r != 0:
+			error(self.ctx, 'csync_reconcile', r)
+		print 'reconcile done'
+		r = csynclib.csync_propagate(self.ctx)
+		if r != 0:
+			error(self.ctx, 'csync_propogate', r)
+		r = csynclib.csync_destroy(self.ctx)
+		if r != 0:
+			error(self.ctx, 'csync_destroy', r)
+		return 
+
+def log(ctx, verbosity, function, buffer, userdata):
+	"""Log stuff from the ocsync library, but it does not work..."""
+	print 'LOG:', verbosity, function, buffer, userdata
+	return 0
+
+def error(ctx, cmd, returnCode):
+	"""handle library errors"""
+	errNum = csynclib.csync_get_error(ctx)
+	errMsg = csynclib.csync_get_error_string(ctx)
+	print 'ERROR: %s exited %s, error %s: %s' % (
+		cmd,
+		returnCode,
+		errNum,
+		errMsg,
+		)
+	sys.exit(1)
+
+
+def getConfigPath():
+	"""get the local configuration file path
+	"""
+	if sys.platform.startswith('linux'):
+		cfgPath = os.path.join('~','.local','share','data','ownCloud')
+		cfgPath = os.path.expanduser(cfgPath) 
+	elif sys.platform == 'darwin':
+		cfgPath = os.path.join('~','Library','Application Support','ownCloud')
+		cfgPath = os.path.expanduser(cfgPath) 
+	elif 'win' in sys.platform:
+		cfgPath = os.path.join('%LOCALAPPDATA%','ownCloud')
+		cfgPath = os.path.expandvars(cfgPath)
+	else:
+		print 'unkown/not supported platform:', sys.platform
+		sys.exit(1)
+	return cfgPath
+
+def getConfig(args):
+	cfg = {}
+	cfgFile = None
+	if args['config']:
+		cfgFile = args['config']
+	else:
+		cfgPath = getConfigPath()
+		if os.path.exists(os.path.join(cfgPath,'owncloud.cfg')):
+			cfgFile = os.path.join(cfgPath, 'owncloud.cfg')
+	if cfgFile:	
+		with open(cfgFile) as fd:
+			"""We use the INI file format that Mirall does. we allow more 
+			things in the cfg file...
+				pass: the password
+			"""
+			c = ConfigParser.SafeConfigParser()
+			c.readfp(fd)
+			cfg = dict(c.items('ownCloud'))
+	cfg.setdefault('pass', '')
+	if os.environ.has_key('OCPASS'):
+		cfg['pass'] = os.environ['OCPASS']
+	#make sure we take it out if it's None, for environ option.
+	if not args['pass']:
+		del args['pass']
+	#cmd line arguments win out over config files.
+	cfg.update(args)
+	return cfg
+
+def main(args):
+	cfg = getConfig(args)
+	print cfg
+	sync = ownCloudSync(cfg)
 
-def main(src, dst):
-	sync(src,dst)
 
 if __name__ == '__main__':
-	main(sys.argv[1], sys.argv[2])
+	parser = argparse.ArgumentParser(
+		formatter_class=argparse.RawDescriptionHelpFormatter,
+		description = 'Synchronize files across machines using ownCloud DAV server',
+		epilog = """
+I support the ownCloud config file, which is located here:
+  {cfg}
+I only support the 'ownCloud' section of the config.
+I support the following keys in the cfg  file:
+	user: The username on the ownCloud server
+	url: the url of the ownCloud Server
+	pass: the password on the ownCloud server
+	sslFingerprint: a valid SSL fingerprint for the server.
+	src: local directory to sync against.
+	dst: folder on the server to sync against.
+complete example:
+[ownCloud]
+user=awesomeSauce
+pass=PasswordThisIsSuperSuperSecretReallyISwearLOL
+url=url=https://www.example.org/owncloud/
+sslFingerprint=
+src=/home/awesomeSauce/ownCloud
+dst=clientsync
+
+Password options:
+  *) You can specify on the cmd line: -p (not very safe)
+  *) in the envifonment variable: OCPASS
+  *) in the owncloud.cfg file as pass = <password>
+  the choice is yours, if you put it in the cfg file, be careful to 
+  make sure nobody but you can read the file. (0400/0600 file perms)
+		""".format(cfg = os.path.join(getConfigPath(),'owncloud.cfg')),
+	)
+	v = "%s - repo: %s" % (VERSION.asString, VERSION.asHead)
+	parser.add_argument('-v', '--version', 
+		action='version', 
+		version = '%(prog)s ' + v)
+	parser.add_argument('-c', '--config', nargs='?', default = None,
+		help = "username on server.")
+	parser.add_argument('-u', '--user', nargs='?', default = None,
+		help = "username on server.")
+	parser.add_argument('-p', '--pass', nargs='?',
+		help = "password on server. you can also store this in environment variable OCPASS")
+	parser.add_argument('src', nargs='?',
+		default =  os.path.expanduser(os.path.join('~','ownCloud')),
+		 help = "local Directory to sync with")
+	parser.add_argument('-d', '--dst', nargs='?', default = 'clientsync',
+		help = "fodler on server.")
+	parser.add_argument('url', nargs='?', default = None,
+		 help = "url to sync to.")
+	args = vars(parser.parse_args())
+	main(args)
+
diff --git a/csynclib.py b/csynclib.py
index e5dbc03..46a89dc 100644
--- a/csynclib.py
+++ b/csynclib.py
@@ -1,9 +1,19 @@
 from ctypes import *
+import ctypes.util
+import os
+import sys
 
 STRING = c_char_p
 _libraries = {}
-_libraries['/usr/lib/libocsync.so.0'] = CDLL('/usr/lib/libocsync.so.0')
-
+if os.path.exists('/usr/lib/libocsync.so.0'):
+	_libraries['/usr/lib/libocsync.so.0'] = CDLL('/usr/lib/libocsync.so.0')
+else:
+	path = ctypes.util.find_library('libocsync')
+	if path:
+		_libraries['/usr/lib/libocsync.so.0'] = CDLL(path)
+	else:
+		print 'ERROR, can not find shared library libocsync'
+		sys.exit(1)
 
 class LP_LP_csync_s(Structure):
 	pass

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/python-owncloud.git



More information about the Pkg-owncloud-commits mailing list