thirdparty:waf: New files for waf 1.9.10
[sfrench/samba-autobuild/.git] / third_party / waf / waflib / extras / unc.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2014 (ita)
4
5 """
6 This module enables automatic handling of network paths of the form \\server\share for both input
7 and output files. While a typical script may require the following::
8
9         import os
10         def build(bld):
11
12                 node = bld.root.make_node('\\\\COMPUTER\\share\\test.txt')
13
14                 # mark the server/share levels as folders
15                 k = node.parent
16                 while k:
17                         k.cache_isdir = True
18                         k = k.parent
19
20                 # clear the file if removed
21                 if not os.path.isfile(node.abspath()):
22                         node.sig = None
23
24                 # create the folder structure
25                 if node.parent.height() > 2:
26                         node.parent.mkdir()
27
28                 # then the task generator
29                 def myfun(tsk):
30                         tsk.outputs[0].write("data")
31                 bld(rule=myfun, source='wscript', target=[nd])
32
33 this tool will make the process much easier, for example::
34
35         def configure(conf):
36                 conf.load('unc') # do not import the module directly
37
38         def build(bld):
39                 def myfun(tsk):
40                         tsk.outputs[0].write("data")
41                 bld(rule=myfun, update_outputs=True,
42                         source='wscript',
43                         target='\\\\COMPUTER\\share\\test.txt')
44                 bld(rule=myfun, update_outputs=True,
45                         source='\\\\COMPUTER\\share\\test.txt',
46                         target='\\\\COMPUTER\\share\\test2.txt')
47 """
48
49 import os
50 from waflib import Node, Utils, Context
51
52 def find_resource(self, lst):
53         if isinstance(lst, str):
54                 lst = [x for x in Node.split_path(lst) if x and x != '.']
55
56         if lst[0].startswith('\\\\'):
57                 if len(lst) < 3:
58                         return None
59                 node = self.ctx.root.make_node(lst[0]).make_node(lst[1])
60                 node.cache_isdir = True
61                 node.parent.cache_isdir = True
62
63                 ret = node.search_node(lst[2:])
64                 if not ret:
65                         ret = node.find_node(lst[2:])
66                 if ret and os.path.isdir(ret.abspath()):
67                         return None
68                 return ret
69
70         return self.find_resource_orig(lst)
71
72 def find_or_declare(self, lst):
73         if isinstance(lst, str):
74                 lst = [x for x in Node.split_path(lst) if x and x != '.']
75
76         if lst[0].startswith('\\\\'):
77                 if len(lst) < 3:
78                         return None
79                 node = self.ctx.root.make_node(lst[0]).make_node(lst[1])
80                 node.cache_isdir = True
81                 node.parent.cache_isdir = True
82                 ret = node.find_node(lst[2:])
83                 if not ret:
84                         ret = node.make_node(lst[2:])
85                 if not os.path.isfile(ret.abspath()):
86                         ret.sig = None
87                         ret.parent.mkdir()
88                 return ret
89
90         return self.find_or_declare_orig(lst)
91
92 def abspath(self):
93         """For MAX_PATH limitations"""
94         ret = self.abspath_orig()
95         if not ret.startswith("\\"):
96                 return "\\\\?\\" + ret
97         return ret
98
99 if Utils.is_win32:
100         Node.Node.find_resource_orig = Node.Node.find_resource
101         Node.Node.find_resource = find_resource
102
103         Node.Node.find_or_declare_orig = Node.Node.find_or_declare
104         Node.Node.find_or_declare = find_or_declare
105
106         Node.Node.abspath_orig = Node.Node.abspath
107         Node.Node.abspath = abspath
108
109         for k in list(Context.cache_modules.keys()):
110                 Context.cache_modules["\\\\?\\" + k] = Context.cache_modules[k]