<?xml version="1.0" encoding="ISO-8859-1"?>
<ss:description type="action" id="com.autodesk.XML.MoveFiles"
		xmlns:ss="urn:Autodesk:Server"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="urn:Autodesk:Server Schema.xsd">
	<ss:title>Copy/Delete/Move Files</ss:title>
	<ss:section>Files</ss:section>
	<ss:input>com.autodesk.objects.FilesObject</ss:input>
	<ss:output>com.autodesk.objects.FilesObject</ss:output>
	<ss:options>
		<ss:label/>
		<ss:option name="directory">
			<ss:label>Destination Directory:</ss:label>
			<ss:tooltip>
				Enter a directory name to copy or move the files into.
			</ss:tooltip>
			<ss:dirname/>
		</ss:option>
		<ss:option name="op">
			<ss:label/>
			<ss:tooltip>
				Copy the files if selected, move the files otherwise
			</ss:tooltip>
			<ss:enumeration default="2" buttons="true">
				<ss:label value="copy">Copy</ss:label>
				<ss:label value="delete">Delete</ss:label>
				<ss:label value="move">Move</ss:label>
			</ss:enumeration>
		</ss:option>
		<ss:option name="keepHierarchy">
			<ss:label>Keep Hierarchy</ss:label>
			<ss:tooltip>
				Keep the file hierarchy under the destination directory.
			</ss:tooltip>
			<ss:checkbox default="true"/>
		</ss:option>
		<ss:option name="overwrite">
			<ss:label>Overwrite Existing Files</ss:label>
			<ss:tooltip>
				Overwrite existing files in the target directory.
			</ss:tooltip>
			<ss:checkbox default="true"/>
		</ss:option>
	</ss:options>
	<ss:code>
import os
import sys
from com.autodesk.objects import FilesObject
from java.io import File
from java.io import FileInputStream
from java.io import FileOutputStream

# os.mkdirs() and os.renames() do not exist in the current version of
# jython, so I have to duplicate the functionality.
def mkdirs(dir):
	if dir != None and not os.path.isdir(dir):
		root = os.path.dirname(dir)
		mkdirs(root)
		os.mkdir(dir)

# os.access() isn't implemented in this version of jython so I
# have to work around this absence. This could be beefed up to
# support R_OK et al.
def access(filename):
	file = File(filename)
	return file.canRead()

# With all the other problems with IO operations in jython I
# don't trust shutil.copy(). So here's an alternative.
def copy(frm, to):
	if not access(frm):
		print "copy() cannot find " + frm
		return

	instream = FileInputStream(frm)
	outstream = FileOutputStream(to)
	inchannel = instream.getChannel()
	outchannel = outstream.getChannel()
	inchannel.transferTo(0, inchannel.size(), outchannel)
	inchannel.close()
	outchannel.close()

def operate(srcDir, dstDir, file, op, keepHierarchy, overwrite):
	if op == "delete":
		if os.path.exists(file):
			os.remove(file)
		if os.path.exists(file):
			# If the file was not deleted return it.
			return file
		else:
			return None

	if keepHierarchy and srcDir != None and file.startswith(srcDir):
		dstFile = file[len(srcDir)+1:]
	else:
		(root, tail) = os.path.split(file)
		dstFile = tail

	dst = os.path.join(dstDir, dstFile)
	(root, tail) = os.path.split(dst)
	mkdirs(root)
	if overwrite or not os.path.exists(dst):
		try:
			# On Windows rename() cannot write to an existing file.
			if os.path.exists(dst):
				os.remove(dst)
			if op == "copy":
				copy(file, dst)
			else:
				os.rename(file, dst)
		except OSError:
			sys.stderr.write("MoveFiles failed to move " + file + ". Cannot overwrite " + dst + ".\n")
	else:
		 sys.stderr.write("MoveFiles skipping " + file + " since " + dst + " exists and overwrite is false.")

	return dst

def main(input, op, directory, keepHierarchy, overwrite):
	if input != None and directory != None:
		base = input.getBaseDirectory()
		if base == None:
			srcDir = None
		else:
			srcDir = base.getPath()

		files = input.getFiles()
		if files != None:
			results = []
			for file in files:
				if file != None:
					result = operate(srcDir, directory, file.getPath(), op, keepHierarchy, overwrite)
					if result != None:
						results.append(result)

			return FilesObject(directory, results)
	return None

	</ss:code>
</ss:description>
