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