Commits

zougloub committed d2ef635

fileops: a tool for file operations, based on shutil. In sandbox for now.

I don't quite see the benefit of using multiprocessing:
the file operations won't hurt Python threading,
and it's not available on every Python.

  • Participants
  • Parent commits f0dbccf

Comments (0)

Files changed (2)

File playground/fileops/fileops.py

+#!/usr/bin/env python
+# encoding: utf-8
+# Jérôme Carretero, 2012 (zougloub)
+
+"""
+Wrapper for Python shutil module, useful for copying, etc.
+
+Example::
+
+	def configure(conf):
+		conf.load('fileops')
+	def build(bld):
+		bld.copy(source='a', target='b')
+	
+The 
+
+"""
+
+import os, sys
+from waflib import Utils, Errors
+from waflib.Logs import debug, warn, info, error
+from waflib.TaskGen import before_method, feature
+from waflib.Configure import conf
+from waflib.Utils import subprocess
+from waflib import Task
+import shutil
+
+try:
+	from multiprocessing import Process
+	1/0
+except:
+	class Process(object):
+		def __init__(self):
+			self.code = None
+		def start(self):
+			self.code = self.run()
+		def join(self):
+			return self.code
+
+class copy_process(Process):
+	def __init__(self, src, dst):
+		self.src = src
+		self.dst = dst
+		Process.__init__(self)
+	def run(self):
+		return shutil.copy(self.src, self.dst)
+
+class file_copy(Task.Task):
+	def scan(self):
+		node = self.inputs[0]
+		nodes = [node]
+		names = [node.bldpath()]
+		return (nodes, names)
+
+	def run(self):
+		src = self.inputs[0].abspath()
+		dst = self.outputs[0].abspath()
+		p = copy_process(src, dst)
+		p.start()
+		return p.join()
+
+def to_dstnodes(self, lst, path=None):
+	tmp = []
+	path = path or self.path
+	find = path.find_or_declare
+
+	if isinstance(lst, self.path.__class__):
+		lst = [lst]
+
+	# either  a list or a string, convert to a list of nodes
+	for x in Utils.to_list(lst):
+		if isinstance(x, str):
+			node = find(x)
+		else:
+			node = x
+		if not node:
+			raise Errors.WafError("could not find or declare node %r in %r" % (x, path))
+		tmp.append(node)
+	return tmp
+
+@feature('shutil:copy')
+@before_method('process_source')
+def apply_shutil_copy(self):
+	src = self.to_nodes(getattr(self, 'source'))
+	tgt = to_dstnodes(self, getattr(self, 'target'))
+	task = self.create_task('file_copy', src, tgt)
+	task.env = self.env
+	self.source = []
+
+@conf
+def copy(bld, *k, **kw):
+	"""
+	BuildContext alias, to provide `bld.copy(...)`
+	"""
+	features = kw.get('features', '')
+	features += ' shutil:copy'
+	kw['features'] = features
+	return bld(*k, **kw)
+
+def configure(conf):
+	try:
+		Process.pid
+	except:
+		warn("file operations will be done in a single process (not a problem)")
+	pass
+
+def options(opt):
+	pass
+

File playground/fileops/wscript

+#!/usr/bin/env python
+# Example using fileops
+
+"""
+In this example, we create file in the source directory,
+then copy it to the build directory,
+and finally do something with it.
+"""
+
+import sys
+sys.path.append(".")
+
+def options(opt):
+	opt.load("compiler_c")
+
+def configure(cfg):
+	cfg.load("fileops compiler_c")
+
+def build(bld):
+
+	a = bld.path.make_node(['a.c'])
+	def f(self):
+		with open(self.outputs[0].abspath(), "w") as f:
+			f.write("int main() { return 0; }")
+	bld(rule=f, target=a)
+
+	bld.copy(
+	 source=a,
+	 target="b.c",
+	)
+
+	bld.program(
+	 target="pouet",
+	 source="b.c",
+	)
+