1
+ import Utils
2
+ import tarfile
3
+ from TaskGen import feature , before
4
+ import Task
5
+ import os
6
+
7
+ # this is a clever little thing
8
+ # given a list of nodes, build or source
9
+ # construct a tar file containing them
10
+ # rooted in the parameter root =, specified in the task generator
11
+ # and renaming the names of the files according to a rename(x) function passed to the task generator as well
12
+ # if a build node's result of rename() has the same name as a source node, the build node will take precedence
13
+ # for as long as the build node appears later than the source node (this is an implementation detail of waf we are relying on)
14
+ def tar_up (task ):
15
+ tgt = task .outputs [0 ].bldpath (task .env )
16
+ if os .path .exists (tgt ): os .unlink (tgt )
17
+ if tgt .lower ().endswith (".bz2" ): z = tarfile .open (tgt ,"w:bz2" )
18
+ elif tgt .lower ().endswith (".gz" ): z = tarfile .open (tgt ,"w:gz" )
19
+ elif tgt .lower ().endswith (".tgz" ): z = tarfile .open (tgt ,"w:gz" )
20
+ else : z = tarfile .open (tgt ,"w" )
21
+ fileset = {}
22
+ for inp in task .inputs :
23
+ src = inp .srcpath (task .env )
24
+ if src .startswith (".." ):
25
+ srcname = Utils .relpath (src ,os .path .join (".." ,"." )) # file in source dir
26
+ else :
27
+ srcname = Utils .relpath (src ,os .path .join (task .env .variant (),"." )) # file in artifacts dir
28
+ if task .generator .rename : srcname = task .generator .rename (srcname )
29
+ for dummy in task .generator .root .split ("/" ):
30
+ splittedname = srcname .split ("/" )
31
+ srcname = "/" .join (splittedname [1 :])
32
+ fileset [srcname ] = src
33
+ for srcname ,src in fileset .items ():
34
+ ti = tarfile .TarInfo (srcname )
35
+ ti .mode = 0755
36
+ ti .size = os .path .getsize (src )
37
+ f = file (src )
38
+ z .addfile (ti ,fileobj = f )
39
+ f .close ()
40
+ z .close ()
41
+ if task .chmod : os .chmod (tgt ,task .chmod )
42
+ return 0
43
+
44
+ def apply_tar (self ):
45
+ Utils .def_attrs (self ,fun = tar_up )
46
+ self .default_install_path = 0
47
+ lst = self .to_list (self .source )
48
+ self .meths .remove ('apply_core' )
49
+ self .dict = getattr (self ,'dict' ,{})
50
+ out = self .path .find_or_declare (self .target )
51
+ ins = []
52
+ for x in Utils .to_list (self .source ):
53
+ node = self .path .find_resource (x )
54
+ if not node :raise Utils .WafError ('cannot find input file %s for processing' % x )
55
+ ins .append (node )
56
+ if self .dict and not self .env ['DICT_HASH' ]:
57
+ self .env = self .env .copy ()
58
+ keys = list (self .dict .keys ())
59
+ keys .sort ()
60
+ lst = [self .dict [x ]for x in keys ]
61
+ self .env ['DICT_HASH' ]= str (Utils .h_list (lst ))
62
+ tsk = self .create_task ('tar' ,ins ,out )
63
+ tsk .fun = self .fun
64
+ tsk .dict = self .dict
65
+ tsk .dep_vars = ['DICT_HASH' ]
66
+ tsk .install_path = self .install_path
67
+ tsk .chmod = self .chmod
68
+ if not tsk .env :
69
+ tsk .debug ()
70
+ raise Utils .WafError ('task without an environment' )
71
+
72
+ Task .task_type_from_func ('tar' ,func = tar_up )
73
+ feature ('tar' )(apply_tar )
74
+ before ('apply_core' )(apply_tar )
0 commit comments