mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | Miscellaneous small functions. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 9 | __author__ = """ |
| 10 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 11 | poirier@google.com (Benjamin Poirier), |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 12 | stutsman@google.com (Ryan Stutsman) |
| 13 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 14 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 15 | import atexit, os, select, shutil, signal, StringIO, subprocess, tempfile |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 16 | import time, types, urllib, re, sys |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 17 | import hosts, errors |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 18 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 19 | # A dictionary of pid and a list of tmpdirs for that pid |
| 20 | __tmp_dirs = {} |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def sh_escape(command): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 24 | """ |
| 25 | Escape special characters from a command so that it can be passed |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 26 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 27 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 28 | Args: |
| 29 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 30 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 31 | Returns: |
| 32 | The escaped command string. The required englobing double |
| 33 | quotes are NOT added and so should be added at some point by |
| 34 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 35 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 36 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 37 | """ |
| 38 | command= command.replace("\\", "\\\\") |
| 39 | command= command.replace("$", r'\$') |
| 40 | command= command.replace('"', r'\"') |
| 41 | command= command.replace('`', r'\`') |
| 42 | return command |
| 43 | |
| 44 | |
| 45 | def scp_remote_escape(filename): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 46 | """ |
| 47 | Escape special characters from a filename so that it can be passed |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 48 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 49 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 50 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 51 | as in quoting x 2 |
| 52 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 53 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 54 | Args: |
| 55 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 56 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 57 | Returns: |
| 58 | The escaped filename string. The required englobing double |
| 59 | quotes are NOT added and so should be added at some point by |
| 60 | the caller. |
| 61 | """ |
| 62 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 63 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 64 | new_name= [] |
| 65 | for char in filename: |
| 66 | if char in escape_chars: |
| 67 | new_name.append("\\%s" % (char,)) |
| 68 | else: |
| 69 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 70 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 71 | return sh_escape("".join(new_name)) |
| 72 | |
| 73 | |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 74 | def get(location, local_copy = False): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 75 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 76 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 77 | Args: |
| 78 | location: the source of the material to get. This source may |
| 79 | be one of: |
| 80 | * a local file or directory |
| 81 | * a URL (http or ftp) |
| 82 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 83 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 84 | Returns: |
| 85 | The location of the file or directory where the requested |
| 86 | content was saved. This will be contained in a temporary |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 87 | directory on the local host. If the material to get was a |
| 88 | directory, the location will contain a trailing '/' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 89 | """ |
| 90 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 91 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 92 | # location is a file-like object |
| 93 | if hasattr(location, "read"): |
| 94 | tmpfile = os.path.join(tmpdir, "file") |
| 95 | tmpfileobj = file(tmpfile, 'w') |
| 96 | shutil.copyfileobj(location, tmpfileobj) |
| 97 | tmpfileobj.close() |
| 98 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 99 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 100 | if isinstance(location, types.StringTypes): |
| 101 | # location is a URL |
| 102 | if location.startswith('http') or location.startswith('ftp'): |
| 103 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 104 | urllib.urlretrieve(location, tmpfile) |
| 105 | return tmpfile |
| 106 | # location is a local path |
| 107 | elif os.path.exists(os.path.abspath(location)): |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 108 | if not local_copy: |
mbligh | 59f70aa | 2007-10-25 14:44:38 +0000 | [diff] [blame] | 109 | if os.path.isdir(location): |
| 110 | return location.rstrip('/') + '/' |
| 111 | else: |
| 112 | return location |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 113 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 114 | if os.path.isdir(location): |
| 115 | tmpfile += '/' |
| 116 | shutil.copytree(location, tmpfile, symlinks=True) |
| 117 | return tmpfile |
| 118 | shutil.copyfile(location, tmpfile) |
| 119 | return tmpfile |
| 120 | # location is just a string, dump it to a file |
| 121 | else: |
| 122 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 123 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 124 | tmpfileobj.write(location) |
| 125 | tmpfileobj.close() |
| 126 | return tmpfile |
| 127 | |
| 128 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 129 | def _process_output(pipe, fbuffer, teefile=None, use_os_read=True): |
| 130 | if use_os_read: |
| 131 | data = os.read(pipe.fileno(), 1024) |
| 132 | else: |
| 133 | data = pipe.read() |
| 134 | fbuffer.write(data) |
| 135 | if teefile: |
| 136 | teefile.write(data) |
| 137 | teefile.flush() |
| 138 | |
| 139 | |
| 140 | def _wait_for_command(subproc, start_time, timeout, stdout_file, stderr_file, |
| 141 | stdout_tee, stderr_tee): |
| 142 | if timeout: |
| 143 | stop_time = start_time + timeout |
| 144 | time_left = stop_time - time.time() |
| 145 | else: |
| 146 | time_left = None # so that select never times out |
| 147 | while not timeout or time_left > 0: |
| 148 | # select will return when stdout is ready (including when it is |
| 149 | # EOF, that is the process has terminated). |
| 150 | ready, _, _ = select.select([subproc.stdout, subproc.stderr], |
| 151 | [], [], time_left) |
| 152 | # os.read() has to be used instead of |
| 153 | # subproc.stdout.read() which will otherwise block |
| 154 | if subproc.stdout in ready: |
| 155 | _process_output(subproc.stdout, stdout_file, |
| 156 | stdout_tee) |
| 157 | if subproc.stderr in ready: |
| 158 | _process_output(subproc.stderr, stderr_file, |
| 159 | stderr_tee) |
| 160 | |
| 161 | pid, exit_status_indication = os.waitpid(subproc.pid, |
| 162 | os.WNOHANG) |
| 163 | if pid: |
| 164 | break |
| 165 | if timeout: |
| 166 | time_left = stop_time - time.time() |
| 167 | |
| 168 | # the process has not terminated within timeout, |
| 169 | # kill it via an escalating series of signals. |
| 170 | if not pid: |
| 171 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 172 | for sig in signal_queue: |
| 173 | try: |
| 174 | os.kill(subproc.pid, sig) |
| 175 | # handle race condition in which |
| 176 | # process died before we could kill it. |
| 177 | except OSError: |
| 178 | pass |
| 179 | |
| 180 | for i in range(5): |
| 181 | pid, exit_status_indication = ( |
| 182 | os.waitpid(subproc.pid, os.WNOHANG)) |
| 183 | if pid: |
| 184 | return exit_status_indication |
| 185 | else: |
| 186 | time.sleep(1) |
| 187 | return exit_status_indication |
| 188 | |
| 189 | |
| 190 | def run(command, timeout=None, ignore_status=False, |
| 191 | stdout_tee=None, stderr_tee=None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 192 | """ |
| 193 | Run a command on the host. |
| 194 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 195 | Args: |
| 196 | command: the command line string |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 197 | timeout: time limit in seconds before attempting to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 198 | kill the running process. The run() function |
| 199 | will take a few seconds longer than 'timeout' |
| 200 | to complete if it has to kill the process. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 201 | ignore_status: do not raise an exception, no matter what |
| 202 | the exit code of the command is. |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 203 | stdout_tee: optional file-like object to which stdout data |
| 204 | will be written as it is generated (data will still |
| 205 | be stored in result.stdout) |
| 206 | stderr_tee: likewise for stderr |
| 207 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 208 | Returns: |
| 209 | a hosts.CmdResult object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 210 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 211 | Raises: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 212 | AutoservRunError: the exit code of the command |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 213 | execution was not 0 |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 214 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 215 | TODO(poirier): Should a timeout raise an exception? Should |
| 216 | exceptions be raised at all? |
| 217 | """ |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 218 | result = hosts.CmdResult() |
| 219 | result.command = command |
| 220 | sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 221 | stderr=subprocess.PIPE, close_fds=True, |
| 222 | shell=True, executable="/bin/bash") |
| 223 | stdout_file = StringIO.StringIO() |
| 224 | stderr_file = StringIO.StringIO() |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 225 | |
| 226 | try: |
| 227 | # We are holding ends to stdin, stdout pipes |
| 228 | # hence we need to be sure to close those fds no mater what |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 229 | start_time = time.time() |
| 230 | exit_status_indication = _wait_for_command( |
| 231 | sp, start_time, timeout, |
| 232 | stdout_file, stderr_file, |
| 233 | stdout_tee, stderr_tee) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 234 | |
| 235 | result.duration = time.time() - start_time |
| 236 | result.aborted = exit_status_indication & 127 |
| 237 | if result.aborted: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 238 | result.exit_status = None |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 239 | else: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 240 | result.exit_status = exit_status_indication / 256 |
| 241 | # don't use os.read now, so we get all the rest of the output |
| 242 | _process_output(sp.stdout, stdout_file, stdout_tee, |
| 243 | use_os_read=False) |
| 244 | _process_output(sp.stderr, stderr_file, stderr_tee, |
| 245 | use_os_read=False) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 246 | finally: |
| 247 | # close our ends of the pipes to the sp no matter what |
| 248 | sp.stdout.close() |
| 249 | sp.stderr.close() |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 250 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 251 | result.stdout = stdout_file.getvalue() |
| 252 | result.stderr = stderr_file.getvalue() |
| 253 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 254 | if not ignore_status and result.exit_status > 0: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 255 | raise errors.AutoservRunError("command execution error", |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 256 | result) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 257 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 258 | return result |
| 259 | |
| 260 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 261 | def system(command, timeout=None, ignore_status=False): |
| 262 | return run(command, timeout, ignore_status).exit_status |
| 263 | |
| 264 | |
| 265 | def system_output(command, timeout=None, ignore_status=False): |
| 266 | return run(command, timeout, ignore_status).stdout |
| 267 | |
| 268 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 269 | def get_tmp_dir(): |
| 270 | """Return the pathname of a directory on the host suitable |
| 271 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 272 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 273 | The directory and its content will be deleted automatically |
| 274 | at the end of the program execution if they are still present. |
| 275 | """ |
| 276 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 277 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 278 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 279 | pid = os.getpid() |
| 280 | if not pid in __tmp_dirs: |
| 281 | __tmp_dirs[pid] = [] |
| 282 | __tmp_dirs[pid].append(dir_name) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 283 | return dir_name |
| 284 | |
| 285 | |
| 286 | @atexit.register |
| 287 | def __clean_tmp_dirs(): |
| 288 | """Erase temporary directories that were created by the get_tmp_dir() |
| 289 | function and that are still present. |
| 290 | """ |
| 291 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 292 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 293 | pid = os.getpid() |
| 294 | if pid not in __tmp_dirs: |
| 295 | return |
| 296 | for dir in __tmp_dirs[pid]: |
| 297 | try: |
| 298 | shutil.rmtree(dir) |
| 299 | except OSError, e: |
| 300 | if e.errno == 2: |
| 301 | pass |
| 302 | __tmp_dirs[pid] = [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 303 | |
| 304 | |
| 305 | def unarchive(host, source_material): |
| 306 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 307 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 308 | If the "source_material" is compresses (according to the file |
| 309 | extension) it will be uncompressed. Supported compression formats |
| 310 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 311 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 312 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 313 | Args: |
| 314 | host: the host object on which the archive is located |
| 315 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 316 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 317 | Returns: |
| 318 | The file or directory name of the unarchived source material. |
| 319 | If the material is a tar archive, it will be extracted in the |
| 320 | directory where it is and the path returned will be the first |
| 321 | entry in the archive, assuming it is the topmost directory. |
| 322 | If the material is not an archive, nothing will be done so this |
| 323 | function is "harmless" when it is "useless". |
| 324 | """ |
| 325 | # uncompress |
| 326 | if (source_material.endswith(".gz") or |
| 327 | source_material.endswith(".gzip")): |
| 328 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 329 | source_material= ".".join(source_material.split(".")[:-1]) |
| 330 | elif source_material.endswith("bz2"): |
| 331 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 332 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 333 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 334 | # untar |
| 335 | if source_material.endswith(".tar"): |
| 336 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 337 | sh_escape(os.path.dirname(source_material)), |
| 338 | sh_escape(source_material),)) |
| 339 | source_material= os.path.join(os.path.dirname(source_material), |
| 340 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 341 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 342 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | def write_keyval(dirname, dictionary): |
| 346 | keyval = open(os.path.join(dirname, 'keyval'), 'w') |
| 347 | for key in dictionary.keys(): |
| 348 | value = '%s' % dictionary[key] # convert numbers to strings |
| 349 | if re.search(r'\W', key): |
| 350 | raise 'Invalid key: ' + key |
| 351 | keyval.write('%s=%s\n' % (key, str(value))) |
| 352 | keyval.close() |
| 353 | |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 354 | |
| 355 | def update_version(srcdir, preserve_srcdir, new_version, install, *args, **dargs): |
| 356 | """ |
| 357 | Make sure srcdir is version new_version |
| 358 | |
| 359 | If not, delete it and install() the new version. |
| 360 | |
| 361 | In the preserve_srcdir case, we just check it's up to date, |
| 362 | and if not, we rerun install, without removing srcdir |
| 363 | """ |
| 364 | versionfile = srcdir + '/.version' |
| 365 | install_needed = True |
| 366 | |
| 367 | if os.path.exists(srcdir): |
| 368 | if os.path.exists(versionfile): |
| 369 | old_version = pickle.load(open(versionfile, 'r')) |
| 370 | if (old_version == new_version): |
| 371 | install_needed = False |
| 372 | |
| 373 | if install_needed: |
| 374 | if not preserve_srcdir: |
| 375 | system('rm -rf ' + srcdir) |
| 376 | install(*args, **dargs) |
| 377 | if os.path.exists(srcdir): |
| 378 | pickle.dump(new_version, open(versionfile, 'w')) |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | def get_server_dir(): |
| 382 | path = os.path.dirname(sys.modules['utils'].__file__) |
| 383 | return os.path.abspath(path) |