mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 3 | # Copyright 2008 Google Inc. Released under the GPL v2 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 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 | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 15 | import atexit, os, re, shutil, textwrap, sys, tempfile, types, urllib |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame^] | 16 | |
| 17 | from autotest_lib.client.common_lib.utils import * |
mbligh | ea397bb | 2008-02-02 19:17:51 +0000 | [diff] [blame] | 18 | |
| 19 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 20 | # A dictionary of pid and a list of tmpdirs for that pid |
| 21 | __tmp_dirs = {} |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def sh_escape(command): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 25 | """ |
| 26 | Escape special characters from a command so that it can be passed |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 27 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 28 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 29 | Args: |
| 30 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 31 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 32 | Returns: |
| 33 | The escaped command string. The required englobing double |
| 34 | quotes are NOT added and so should be added at some point by |
| 35 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 36 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 37 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 38 | """ |
| 39 | command= command.replace("\\", "\\\\") |
| 40 | command= command.replace("$", r'\$') |
| 41 | command= command.replace('"', r'\"') |
| 42 | command= command.replace('`', r'\`') |
| 43 | return command |
| 44 | |
| 45 | |
| 46 | def scp_remote_escape(filename): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 47 | """ |
| 48 | Escape special characters from a filename so that it can be passed |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 49 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 50 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 51 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 52 | as in quoting x 2 |
| 53 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 54 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 55 | Args: |
| 56 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 57 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 58 | Returns: |
| 59 | The escaped filename string. The required englobing double |
| 60 | quotes are NOT added and so should be added at some point by |
| 61 | the caller. |
| 62 | """ |
| 63 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 64 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 65 | new_name= [] |
| 66 | for char in filename: |
| 67 | if char in escape_chars: |
| 68 | new_name.append("\\%s" % (char,)) |
| 69 | else: |
| 70 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 71 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 72 | return sh_escape("".join(new_name)) |
| 73 | |
| 74 | |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 75 | def get(location, local_copy = False): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 76 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 77 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 78 | Args: |
| 79 | location: the source of the material to get. This source may |
| 80 | be one of: |
| 81 | * a local file or directory |
| 82 | * a URL (http or ftp) |
| 83 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 84 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 85 | Returns: |
| 86 | The location of the file or directory where the requested |
| 87 | content was saved. This will be contained in a temporary |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 88 | directory on the local host. If the material to get was a |
| 89 | directory, the location will contain a trailing '/' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 90 | """ |
| 91 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 92 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 93 | # location is a file-like object |
| 94 | if hasattr(location, "read"): |
| 95 | tmpfile = os.path.join(tmpdir, "file") |
| 96 | tmpfileobj = file(tmpfile, 'w') |
| 97 | shutil.copyfileobj(location, tmpfileobj) |
| 98 | tmpfileobj.close() |
| 99 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 100 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 101 | if isinstance(location, types.StringTypes): |
| 102 | # location is a URL |
| 103 | if location.startswith('http') or location.startswith('ftp'): |
| 104 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 105 | urllib.urlretrieve(location, tmpfile) |
| 106 | return tmpfile |
| 107 | # location is a local path |
| 108 | elif os.path.exists(os.path.abspath(location)): |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 109 | if not local_copy: |
mbligh | 59f70aa | 2007-10-25 14:44:38 +0000 | [diff] [blame] | 110 | if os.path.isdir(location): |
| 111 | return location.rstrip('/') + '/' |
| 112 | else: |
| 113 | return location |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 114 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 115 | if os.path.isdir(location): |
| 116 | tmpfile += '/' |
| 117 | shutil.copytree(location, tmpfile, symlinks=True) |
| 118 | return tmpfile |
| 119 | shutil.copyfile(location, tmpfile) |
| 120 | return tmpfile |
| 121 | # location is just a string, dump it to a file |
| 122 | else: |
| 123 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 124 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 125 | tmpfileobj.write(location) |
| 126 | tmpfileobj.close() |
| 127 | return tmpfile |
| 128 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 129 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 130 | def get_tmp_dir(): |
| 131 | """Return the pathname of a directory on the host suitable |
| 132 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 133 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 134 | The directory and its content will be deleted automatically |
| 135 | at the end of the program execution if they are still present. |
| 136 | """ |
| 137 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 138 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 139 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 140 | pid = os.getpid() |
| 141 | if not pid in __tmp_dirs: |
| 142 | __tmp_dirs[pid] = [] |
| 143 | __tmp_dirs[pid].append(dir_name) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 144 | return dir_name |
| 145 | |
| 146 | |
| 147 | @atexit.register |
| 148 | def __clean_tmp_dirs(): |
| 149 | """Erase temporary directories that were created by the get_tmp_dir() |
| 150 | function and that are still present. |
| 151 | """ |
| 152 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 153 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 154 | pid = os.getpid() |
| 155 | if pid not in __tmp_dirs: |
| 156 | return |
| 157 | for dir in __tmp_dirs[pid]: |
| 158 | try: |
| 159 | shutil.rmtree(dir) |
| 160 | except OSError, e: |
| 161 | if e.errno == 2: |
| 162 | pass |
| 163 | __tmp_dirs[pid] = [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def unarchive(host, source_material): |
| 167 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 168 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 169 | If the "source_material" is compresses (according to the file |
| 170 | extension) it will be uncompressed. Supported compression formats |
| 171 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 172 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 173 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 174 | Args: |
| 175 | host: the host object on which the archive is located |
| 176 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 177 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 178 | Returns: |
| 179 | The file or directory name of the unarchived source material. |
| 180 | If the material is a tar archive, it will be extracted in the |
| 181 | directory where it is and the path returned will be the first |
| 182 | entry in the archive, assuming it is the topmost directory. |
| 183 | If the material is not an archive, nothing will be done so this |
| 184 | function is "harmless" when it is "useless". |
| 185 | """ |
| 186 | # uncompress |
| 187 | if (source_material.endswith(".gz") or |
| 188 | source_material.endswith(".gzip")): |
| 189 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 190 | source_material= ".".join(source_material.split(".")[:-1]) |
| 191 | elif source_material.endswith("bz2"): |
| 192 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 193 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 194 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 195 | # untar |
| 196 | if source_material.endswith(".tar"): |
| 197 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 198 | sh_escape(os.path.dirname(source_material)), |
| 199 | sh_escape(source_material),)) |
| 200 | source_material= os.path.join(os.path.dirname(source_material), |
| 201 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 202 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 203 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 204 | |
| 205 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 206 | def get_server_dir(): |
| 207 | path = os.path.dirname(sys.modules['utils'].__file__) |
| 208 | return os.path.abspath(path) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 209 | |
| 210 | |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 211 | def find_pid(command): |
| 212 | for line in system_output('ps -eo pid,cmd').rstrip().split('\n'): |
| 213 | (pid, cmd) = line.split(None, 1) |
| 214 | if re.search(command, cmd): |
| 215 | return int(pid) |
| 216 | return None |
| 217 | |
| 218 | |
| 219 | def nohup(command, stdout='/dev/null', stderr='/dev/null', background=True, |
| 220 | env = {}): |
| 221 | cmd = ' '.join(key+'='+val for key, val in env.iteritems()) |
| 222 | cmd += ' nohup ' + command |
| 223 | cmd += ' > %s' % stdout |
| 224 | if stdout == stderr: |
| 225 | cmd += ' 2>&1' |
| 226 | else: |
| 227 | cmd += ' 2> %s' % stderr |
| 228 | if background: |
| 229 | cmd += ' &' |
| 230 | system(cmd) |
| 231 | |
| 232 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 233 | class AutoservOptionParser: |
| 234 | """Custom command-line options parser for autoserv. |
| 235 | |
| 236 | We can't use the general getopt methods here, as there will be unknown |
| 237 | extra arguments that we pass down into the control file instead. |
| 238 | Thus we process the arguments by hand, for which we are duly repentant. |
| 239 | Making a single function here just makes it harder to read. Suck it up. |
| 240 | """ |
| 241 | |
| 242 | def __init__(self, args): |
| 243 | self.args = args |
| 244 | |
| 245 | |
| 246 | def parse_opts(self, flag): |
| 247 | if self.args.count(flag): |
| 248 | idx = self.args.index(flag) |
| 249 | self.args[idx : idx+1] = [] |
| 250 | return True |
| 251 | else: |
| 252 | return False |
| 253 | |
| 254 | |
| 255 | def parse_opts_param(self, flag, default = None, split = False): |
| 256 | if self.args.count(flag): |
| 257 | idx = self.args.index(flag) |
| 258 | ret = self.args[idx+1] |
| 259 | self.args[idx : idx+2] = [] |
| 260 | if split: |
| 261 | return ret.split(split) |
| 262 | else: |
| 263 | return ret |
| 264 | else: |
| 265 | return default |