1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import sys
18 import shutil
19
20 from twisted.internet import defer
21 from twisted.python import runtime, log
22
23 from buildslave import runprocess
24 from buildslave.commands import base, utils
25
27 """This is a Command which creates a directory. The args dict contains
28 the following keys:
29
30 - ['dir'] (required): subdirectory which the command will create,
31 relative to the builder dir
32
33 MakeDirectory creates the following status messages:
34 - {'rc': rc} : when the process has terminated
35 """
36
37 header = "mkdir"
38
40 args = self.args
41
42 assert args['dir'] is not None
43 dirname = os.path.join(self.builder.basedir, args['dir'])
44
45 try:
46 if not os.path.isdir(dirname):
47 os.makedirs(dirname)
48 self.sendStatus({'rc': 0})
49 except:
50 self.sendStatus({'rc': 1})
51
53 """This is a Command which removes a directory. The args dict contains
54 the following keys:
55
56 - ['dir'] (required): subdirectory which the command will create,
57 relative to the builder dir
58
59 - ['timeout']: seconds of silence tolerated before we kill off the
60 command
61
62 - ['maxTime']: seconds before we kill off the command
63
64
65 RemoveDirectory creates the following status messages:
66 - {'rc': rc} : when the process has terminated
67 """
68
69 header = "rmdir"
70
72 args = self.args
73
74 assert args['dir'] is not None
75 dirname = args['dir']
76
77 self.timeout = args.get('timeout', 120)
78 self.maxTime = args.get('maxTime', None)
79
80
81 self.dir = os.path.join(self.builder.basedir, dirname)
82 if runtime.platformType != "posix":
83
84
85 utils.rmdirRecursive(self.dir)
86 d = defer.succeed(0)
87 else:
88 d = self._clobber(None)
89
90
91 d.addCallbacks(self._sendRC, self._checkAbandoned)
92 return d
93
94 - def _clobber(self, dummy, chmodDone = False):
113
115 assert isinstance(rc, int)
116 if rc == 0:
117 return defer.succeed(0)
118
119
120 command = ["chmod", "-Rf", "u+rwx", os.path.join(self.builder.basedir, self.dir)]
121 if sys.platform.startswith('freebsd'):
122
123
124
125 command = ["find", os.path.join(self.builder.basedir, self.dir),
126 '-exec', 'chmod', 'u+rwx', '{}', ';' ]
127 c = runprocess.RunProcess(self.builder, command, self.builder.basedir,
128 sendRC=0, timeout=self.timeout, maxTime=self.maxTime,
129 usePTY=False)
130
131 self.command = c
132 d = c.start()
133 d.addCallback(self._abandonOnFailure)
134 d.addCallback(lambda dummy: self._clobber(dummy, True))
135 return d
136
138 """This is a Command which copies a directory. The args dict contains
139 the following keys:
140
141 - ['fromdir'] (required): subdirectory which the command will copy,
142 relative to the builder dir
143 - ['todir'] (required): subdirectory which the command will create,
144 relative to the builder dir
145
146 - ['timeout']: seconds of silence tolerated before we kill off the
147 command
148
149 - ['maxTime']: seconds before we kill off the command
150
151
152 CopyDirectory creates the following status messages:
153 - {'rc': rc} : when the process has terminated
154 """
155
156 header = "rmdir"
157
159 args = self.args
160
161
162 assert args['todir'] is not None
163 assert args['fromdir'] is not None
164
165 fromdir = os.path.join(self.builder.basedir, args['fromdir'])
166 todir = os.path.join(self.builder.basedir, args['todir'])
167
168 self.timeout = args.get('timeout', 120)
169 self.maxTime = args.get('maxTime', None)
170
171 if runtime.platformType != "posix":
172 self.sendStatus({'header': "Since we're on a non-POSIX platform, "
173 "we're not going to try to execute cp in a subprocess, but instead "
174 "use shutil.copytree(), which will block until it is complete. "
175 "fromdir: %s, todir: %s\n" % (fromdir, todir)})
176 shutil.copytree(fromdir, todir)
177 d = defer.succeed(0)
178 else:
179 if not os.path.exists(os.path.dirname(todir)):
180 os.makedirs(os.path.dirname(todir))
181 if os.path.exists(todir):
182
183 log.msg("cp target '%s' already exists -- cp will not do what you think!" % todir)
184
185 command = ['cp', '-R', '-P', '-p', fromdir, todir]
186 c = runprocess.RunProcess(self.builder, command, self.builder.basedir,
187 sendRC=False, timeout=self.timeout, maxTime=self.maxTime,
188 usePTY=False)
189 self.command = c
190 d = c.start()
191 d.addCallback(self._abandonOnFailure)
192
193
194 d.addCallbacks(self._sendRC, self._checkAbandoned)
195 return d
196
198 """This is a command which stats a file on the slave. The args dict contains the following keys:
199
200 - ['file'] (required): file to stat
201
202 StatFile creates the following status messages:
203 - {'rc': rc} : 0 if the file is found, 1 otherwise
204 - {'stat': stat} : if the files is found, stat contains the result of os.stat
205 """
206
207 header = "stat"
208
221