showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | """ |
| 4 | Utility to upload or remove the packages from the packages repository. |
| 5 | """ |
| 6 | |
showard | f1175bb | 2009-06-17 19:34:36 +0000 | [diff] [blame^] | 7 | import logging, os, sys, optparse, socket, tempfile, shutil |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 8 | import common |
| 9 | from autotest_lib.client.common_lib import utils as client_utils |
showard | f1175bb | 2009-06-17 19:34:36 +0000 | [diff] [blame^] | 10 | from autotest_lib.client.common_lib import packages, global_config |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 11 | from autotest_lib.server import utils as server_utils |
| 12 | |
| 13 | c = global_config.global_config |
showard | f1175bb | 2009-06-17 19:34:36 +0000 | [diff] [blame^] | 14 | logging.basicConfig(logging.DEBUG) |
mbligh | dbfc4e3 | 2008-08-22 18:08:07 +0000 | [diff] [blame] | 15 | |
| 16 | def get_exclude_string(client_dir): |
| 17 | ''' |
| 18 | Get the exclude string for the tar command to exclude specific |
| 19 | subdirectories inside client_dir. |
| 20 | For profilers we need to exclude everything except the __ini__.py |
| 21 | file so that the profilers can be imported. |
| 22 | ''' |
| 23 | exclude_string = ('--exclude=deps/* --exclude=tests/* ' |
| 24 | '--exclude=site_tests/*') |
| 25 | |
| 26 | # Get the profilers directory |
| 27 | prof_dir = os.path.join(client_dir, 'profilers') |
| 28 | |
| 29 | # Include the __init__.py file for the profilers and exclude all its |
| 30 | # subdirectories |
| 31 | for f in os.listdir(prof_dir): |
| 32 | if os.path.isdir(os.path.join(prof_dir, f)): |
| 33 | exclude_string += ' --exclude=profilers/%s' % f |
| 34 | |
| 35 | # The '.' here is needed to zip the files in the current |
| 36 | # directory. We use '-C' for tar to change to the required |
| 37 | # directory i.e. src_dir and then zip up the files in that |
| 38 | # directory(which is '.') excluding the ones in the exclude_dirs |
| 39 | exclude_string += " ." |
| 40 | |
| 41 | return exclude_string |
| 42 | |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 43 | |
| 44 | def parse_args(): |
| 45 | parser = optparse.OptionParser() |
| 46 | parser.add_option("-d", "--dependency", help="package the dependency" |
| 47 | " from client/deps directory and upload to the repo", |
| 48 | dest="dep") |
| 49 | parser.add_option("-p", "--profiler", help="package the profiler " |
| 50 | "from client/profilers directory and upload to the repo", |
| 51 | dest="prof") |
| 52 | parser.add_option("-t", "--test", help="package the test from client/tests" |
| 53 | " or client/site_tests and upload to the repo.", |
| 54 | dest="test") |
| 55 | parser.add_option("-c", "--client", help="package the client " |
| 56 | "directory alone without the tests, deps and profilers", |
| 57 | dest="client", action="store_true", default=False) |
| 58 | parser.add_option("-f", "--file", help="simply uploads the specified" |
| 59 | "file on to the repo", dest="file") |
| 60 | parser.add_option("-r", "--repository", help="the URL of the packages" |
| 61 | "repository location to upload the packages to.", |
| 62 | dest="repo", default=None) |
| 63 | parser.add_option("--all", help="Upload all the files locally " |
| 64 | "to all the repos specified in global_config.ini. " |
| 65 | "(includes the client, tests, deps and profilers)", |
| 66 | dest="all", action="store_true", default=False) |
| 67 | |
| 68 | options, args = parser.parse_args() |
| 69 | return options, args |
| 70 | |
| 71 | |
| 72 | # Method to upload or remove package depending on the flag passed to it. |
| 73 | def process_packages(pkgmgr, pkg_type, pkg_names, src_dir, repo_url, |
| 74 | remove=False): |
mbligh | dbfc4e3 | 2008-08-22 18:08:07 +0000 | [diff] [blame] | 75 | exclude_string = ' .' |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 76 | names = [p.strip() for p in pkg_names.split(',')] |
| 77 | for name in names: |
| 78 | print "Processing %s ... " % name |
| 79 | if pkg_type=='client': |
| 80 | pkg_dir = src_dir |
mbligh | dbfc4e3 | 2008-08-22 18:08:07 +0000 | [diff] [blame] | 81 | exclude_string = get_exclude_string(pkg_dir) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 82 | elif pkg_type=='test': |
| 83 | # if the package is a test then look whether it is in client/tests |
| 84 | # or client/site_tests |
| 85 | pkg_dir = os.path.join(get_test_dir(name, src_dir), name) |
| 86 | else: |
| 87 | # for the profilers and deps |
| 88 | pkg_dir = os.path.join(src_dir, name) |
| 89 | |
| 90 | pkg_name = pkgmgr.get_tarball_name(name, pkg_type) |
| 91 | if not remove: |
| 92 | # Tar the source and upload |
| 93 | try: |
| 94 | temp_dir = tempfile.mkdtemp() |
mbligh | e183681 | 2009-04-17 18:25:14 +0000 | [diff] [blame] | 95 | try: |
| 96 | packages.check_diskspace(temp_dir) |
| 97 | except packages.RepoDiskFull: |
| 98 | msg = ("Temporary directory for packages does not have " |
| 99 | "enough space available") |
| 100 | raise packages.RepoDiskFull(msg) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 101 | tarball_path = pkgmgr.tar_package(pkg_name, pkg_dir, |
mbligh | dbfc4e3 | 2008-08-22 18:08:07 +0000 | [diff] [blame] | 102 | temp_dir, exclude_string) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 103 | pkgmgr.upload_pkg(tarball_path, repo_url, update_checksum=True) |
| 104 | finally: |
| 105 | # remove the temporary directory |
| 106 | shutil.rmtree(temp_dir) |
| 107 | else: |
| 108 | pkgmgr.remove_pkg(pkg_name, repo_url, remove_checksum=True) |
| 109 | print "Done." |
| 110 | |
| 111 | |
mbligh | 9fc7797 | 2008-10-02 20:32:09 +0000 | [diff] [blame] | 112 | def tar_packages(pkgmgr, pkg_type, pkg_names, src_dir, temp_dir): |
| 113 | """Tar all packages up and return a list of each tar created""" |
| 114 | tarballs = [] |
| 115 | exclude_string = ' .' |
| 116 | names = [p.strip() for p in pkg_names.split(',')] |
| 117 | for name in names: |
| 118 | print "Processing %s ... " % name |
| 119 | if pkg_type=='client': |
| 120 | pkg_dir = src_dir |
| 121 | exclude_string = get_exclude_string(pkg_dir) |
| 122 | elif pkg_type=='test': |
| 123 | # if the package is a test then look whether it is in client/tests |
| 124 | # or client/site_tests |
| 125 | pkg_dir = os.path.join(get_test_dir(name, src_dir), name) |
| 126 | else: |
| 127 | # for the profilers and deps |
| 128 | pkg_dir = os.path.join(src_dir, name) |
| 129 | |
| 130 | pkg_name = pkgmgr.get_tarball_name(name, pkg_type) |
| 131 | tarball_path = pkgmgr.tar_package(pkg_name, pkg_dir, |
| 132 | temp_dir, exclude_string) |
| 133 | |
| 134 | tarballs.append(tarball_path) |
| 135 | |
| 136 | return tarballs |
| 137 | |
| 138 | |
| 139 | |
| 140 | def process_all_packages(pkgmgr, client_dir, upload_paths, remove=False): |
| 141 | """Process a full upload of packages as a directory upload.""" |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 142 | test_dir = os.path.join(client_dir, "tests") |
| 143 | site_test_dir = os.path.join(client_dir, "site_tests") |
| 144 | dep_dir = os.path.join(client_dir, "deps") |
| 145 | prof_dir = os.path.join(client_dir, "profilers") |
mbligh | 9fc7797 | 2008-10-02 20:32:09 +0000 | [diff] [blame] | 146 | # Directory where all are kept |
| 147 | temp_dir = tempfile.mkdtemp() |
mbligh | e183681 | 2009-04-17 18:25:14 +0000 | [diff] [blame] | 148 | try: |
| 149 | packages.check_diskspace(temp_dir) |
| 150 | except packages.RepoDiskFull: |
| 151 | print ("Temp destination for packages is full %s, aborting upload" |
| 152 | % temp_dir) |
| 153 | os.rmdir(temp_dir) |
| 154 | sys.exit(1) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 155 | |
| 156 | # process tests |
| 157 | tests_list = get_subdir_list('tests', client_dir) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 158 | tests = ','.join(tests_list) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 159 | |
| 160 | # process site_tests |
| 161 | site_tests_list = get_subdir_list('site_tests', client_dir) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 162 | site_tests = ','.join(site_tests_list) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 163 | |
| 164 | # process deps |
| 165 | deps_list = get_subdir_list('deps', client_dir) |
| 166 | deps = ','.join(deps_list) |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 167 | |
| 168 | # process profilers |
| 169 | profilers_list = get_subdir_list('profilers', client_dir) |
| 170 | profilers = ','.join(profilers_list) |
mbligh | 9fc7797 | 2008-10-02 20:32:09 +0000 | [diff] [blame] | 171 | |
| 172 | # Update md5sum |
| 173 | if not remove: |
| 174 | tar_packages(pkgmgr, 'profiler', profilers, prof_dir, temp_dir) |
| 175 | tar_packages(pkgmgr, 'dep', deps, dep_dir, temp_dir) |
| 176 | tar_packages(pkgmgr, 'test', site_tests, client_dir, temp_dir) |
| 177 | tar_packages(pkgmgr, 'test', tests, client_dir, temp_dir) |
| 178 | tar_packages(pkgmgr, 'client', 'autotest', client_dir, temp_dir) |
| 179 | cwd = os.getcwd() |
| 180 | os.chdir(temp_dir) |
| 181 | client_utils.system('md5sum * > packages.checksum') |
| 182 | os.chdir(cwd) |
| 183 | for path in upload_paths: |
| 184 | print "Uploading to: " + path |
mbligh | e183681 | 2009-04-17 18:25:14 +0000 | [diff] [blame] | 185 | pkgmgr.upload_pkg(temp_dir, path) |
mbligh | 9fc7797 | 2008-10-02 20:32:09 +0000 | [diff] [blame] | 186 | client_utils.run('rm -rf ' + temp_dir) |
| 187 | else: |
| 188 | for repo_url in upload_paths: |
| 189 | process_packages(pkgmgr, 'test', tests, client_dir, repo_url, |
| 190 | remove=remove) |
| 191 | process_packages(pkgmgr, 'test', site_tests, client_dir, repo_url, |
| 192 | remove=remove) |
| 193 | process_packages(pkgmgr, 'client', 'autotest', client_dir, repo_url, |
| 194 | remove=remove) |
| 195 | process_packages(pkgmgr, 'dep', deps, dep_dir, repo_url, |
| 196 | remove=remove) |
| 197 | process_packages(pkgmgr, 'profiler', profilers, prof_dir, repo_url, |
| 198 | remove=remove) |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 203 | |
| 204 | |
| 205 | # Get the list of sub directories present in a directory |
| 206 | def get_subdir_list(name, client_dir): |
| 207 | dir_name = os.path.join(client_dir, name) |
| 208 | return [f for f in |
| 209 | os.listdir(dir_name) |
| 210 | if os.path.isdir(os.path.join(dir_name, f)) ] |
| 211 | |
| 212 | |
| 213 | # Look whether the test is present in client/tests and client/site_tests dirs |
| 214 | def get_test_dir(name, client_dir): |
| 215 | names_test = os.listdir(os.path.join(client_dir, 'tests')) |
| 216 | names_site_test = os.listdir(os.path.join(client_dir, 'site_tests')) |
| 217 | if name in names_test: |
| 218 | src_dir = os.path.join(client_dir, 'tests') |
| 219 | elif name in names_site_test: |
| 220 | src_dir = os.path.join(client_dir, 'site_tests') |
| 221 | else: |
| 222 | print "Test %s not found" % name |
| 223 | sys.exit(0) |
| 224 | return src_dir |
| 225 | |
| 226 | |
| 227 | # Given the name of the test the following function |
| 228 | # returns the directory in which the test files are present |
| 229 | # in (tests or site_tests) |
mbligh | de613a9 | 2009-02-03 21:55:34 +0000 | [diff] [blame] | 230 | |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 231 | def main(): |
mbligh | de613a9 | 2009-02-03 21:55:34 +0000 | [diff] [blame] | 232 | # get options and args |
| 233 | options, args = parse_args() |
| 234 | |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 235 | server_dir = server_utils.get_server_dir() |
| 236 | autotest_dir = os.path.abspath(os.path.join(server_dir, '..')) |
| 237 | |
| 238 | # extract the pkg locations from global config |
| 239 | repo_urls = c.get_config_value('PACKAGES', 'fetch_location', |
| 240 | type=list, default=[]) |
| 241 | upload_paths = c.get_config_value('PACKAGES', 'upload_location', |
| 242 | type=list, default=[]) |
| 243 | # Having no upload paths basically means you're not using packaging. |
| 244 | if len(upload_paths) == 0: |
| 245 | return |
| 246 | |
mbligh | ddcd90f | 2008-10-10 21:07:40 +0000 | [diff] [blame] | 247 | pkgmgr = packages.PackageManager(autotest_dir, repo_urls=repo_urls, |
| 248 | upload_paths=upload_paths, |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 249 | run_function_dargs={'timeout':600}) |
| 250 | |
| 251 | client_dir = os.path.join(autotest_dir, "client") |
| 252 | |
| 253 | # Bail out if the client directory does not exist |
| 254 | if not os.path.exists(client_dir): |
| 255 | sys.exit(0) |
| 256 | |
| 257 | dep_dir = os.path.join(client_dir, "deps") |
| 258 | prof_dir = os.path.join(client_dir, "profilers") |
| 259 | |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 260 | if len(args)==0 or args[0] not in ['upload','remove']: |
| 261 | print("Either 'upload' or 'remove' needs to be specified " |
| 262 | "for the package") |
| 263 | sys.exit(0) |
| 264 | |
| 265 | if args[0]=='upload': |
| 266 | remove_flag=False |
| 267 | elif args[0]=='remove': |
| 268 | remove_flag=True |
| 269 | else: |
| 270 | # we should not be getting here |
| 271 | assert(False) |
| 272 | |
| 273 | if options.all: |
mbligh | 9fc7797 | 2008-10-02 20:32:09 +0000 | [diff] [blame] | 274 | if options.repo: |
| 275 | upload_path_list = [options.repo] |
| 276 | else: |
| 277 | upload_path_list = upload_paths |
| 278 | process_all_packages(pkgmgr, client_dir, upload_path_list, |
showard | 9d02fb5 | 2008-08-08 18:20:37 +0000 | [diff] [blame] | 279 | remove=remove_flag) |
| 280 | |
| 281 | if options.client: |
| 282 | process_packages(pkgmgr, 'client', 'autotest', client_dir, |
| 283 | options.repo, remove=remove_flag) |
| 284 | |
| 285 | if options.dep: |
| 286 | process_packages(pkgmgr, 'dep', options.dep, dep_dir, |
| 287 | options.repo, remove=remove_flag) |
| 288 | |
| 289 | if options.test: |
| 290 | process_packages(pkgmgr, 'test', options.test, client_dir, |
| 291 | options.repo, remove=remove_flag) |
| 292 | |
| 293 | if options.prof: |
| 294 | process_packages(pkgmgr, 'profiler', options.prof, prof_dir, |
| 295 | options.repo, remove=remove_flag) |
| 296 | |
| 297 | if options.file: |
| 298 | if remove_flag: |
| 299 | pkgmgr.remove_pkg(options.file, options.repo, remove_checksum=True) |
| 300 | else: |
| 301 | pkgmgr.upload_pkg(options.file, options.repo, update_checksum=True) |
| 302 | |
| 303 | |
| 304 | if __name__ == "__main__": |
| 305 | main() |