blob: a096dff2fc23ff06d2a878d42101c463f93ca630 [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
mbligha32a7432008-10-16 17:31:24 +000010from autotest_lib.client.common_lib import packages, global_config, debug
showard9d02fb52008-08-08 18:20:37 +000011from autotest_lib.server import utils as server_utils
12
13c = global_config.global_config
mbligha32a7432008-10-16 17:31:24 +000014debug.configure(module='utils')
mblighdbfc4e32008-08-22 18:08:07 +000015
16def 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
showard9d02fb52008-08-08 18:20:37 +000043
44def 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.
73def process_packages(pkgmgr, pkg_type, pkg_names, src_dir, repo_url,
74 remove=False):
mblighdbfc4e32008-08-22 18:08:07 +000075 exclude_string = ' .'
showard9d02fb52008-08-08 18:20:37 +000076 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
mblighdbfc4e32008-08-22 18:08:07 +000081 exclude_string = get_exclude_string(pkg_dir)
showard9d02fb52008-08-08 18:20:37 +000082 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()
95 tarball_path = pkgmgr.tar_package(pkg_name, pkg_dir,
mblighdbfc4e32008-08-22 18:08:07 +000096 temp_dir, exclude_string)
showard9d02fb52008-08-08 18:20:37 +000097 pkgmgr.upload_pkg(tarball_path, repo_url, update_checksum=True)
98 finally:
99 # remove the temporary directory
100 shutil.rmtree(temp_dir)
101 else:
102 pkgmgr.remove_pkg(pkg_name, repo_url, remove_checksum=True)
103 print "Done."
104
105
mbligh9fc77972008-10-02 20:32:09 +0000106def tar_packages(pkgmgr, pkg_type, pkg_names, src_dir, temp_dir):
107 """Tar all packages up and return a list of each tar created"""
108 tarballs = []
109 exclude_string = ' .'
110 names = [p.strip() for p in pkg_names.split(',')]
111 for name in names:
112 print "Processing %s ... " % name
113 if pkg_type=='client':
114 pkg_dir = src_dir
115 exclude_string = get_exclude_string(pkg_dir)
116 elif pkg_type=='test':
117 # if the package is a test then look whether it is in client/tests
118 # or client/site_tests
119 pkg_dir = os.path.join(get_test_dir(name, src_dir), name)
120 else:
121 # for the profilers and deps
122 pkg_dir = os.path.join(src_dir, name)
123
124 pkg_name = pkgmgr.get_tarball_name(name, pkg_type)
125 tarball_path = pkgmgr.tar_package(pkg_name, pkg_dir,
126 temp_dir, exclude_string)
127
128 tarballs.append(tarball_path)
129
130 return tarballs
131
132
133
134def process_all_packages(pkgmgr, client_dir, upload_paths, remove=False):
135 """Process a full upload of packages as a directory upload."""
showard9d02fb52008-08-08 18:20:37 +0000136 test_dir = os.path.join(client_dir, "tests")
137 site_test_dir = os.path.join(client_dir, "site_tests")
138 dep_dir = os.path.join(client_dir, "deps")
139 prof_dir = os.path.join(client_dir, "profilers")
mbligh9fc77972008-10-02 20:32:09 +0000140 # Directory where all are kept
141 temp_dir = tempfile.mkdtemp()
showard9d02fb52008-08-08 18:20:37 +0000142
143 # process tests
144 tests_list = get_subdir_list('tests', client_dir)
showard9d02fb52008-08-08 18:20:37 +0000145 tests = ','.join(tests_list)
showard9d02fb52008-08-08 18:20:37 +0000146
147 # process site_tests
148 site_tests_list = get_subdir_list('site_tests', client_dir)
showard9d02fb52008-08-08 18:20:37 +0000149 site_tests = ','.join(site_tests_list)
showard9d02fb52008-08-08 18:20:37 +0000150
151 # process deps
152 deps_list = get_subdir_list('deps', client_dir)
153 deps = ','.join(deps_list)
showard9d02fb52008-08-08 18:20:37 +0000154
155 # process profilers
156 profilers_list = get_subdir_list('profilers', client_dir)
157 profilers = ','.join(profilers_list)
mbligh9fc77972008-10-02 20:32:09 +0000158
159 # Update md5sum
160 if not remove:
161 tar_packages(pkgmgr, 'profiler', profilers, prof_dir, temp_dir)
162 tar_packages(pkgmgr, 'dep', deps, dep_dir, temp_dir)
163 tar_packages(pkgmgr, 'test', site_tests, client_dir, temp_dir)
164 tar_packages(pkgmgr, 'test', tests, client_dir, temp_dir)
165 tar_packages(pkgmgr, 'client', 'autotest', client_dir, temp_dir)
166 cwd = os.getcwd()
167 os.chdir(temp_dir)
168 client_utils.system('md5sum * > packages.checksum')
169 os.chdir(cwd)
170 for path in upload_paths:
171 print "Uploading to: " + path
172 pkgmgr.upload_pkg_dir(temp_dir, path)
173 client_utils.run('rm -rf ' + temp_dir)
174 else:
175 for repo_url in upload_paths:
176 process_packages(pkgmgr, 'test', tests, client_dir, repo_url,
177 remove=remove)
178 process_packages(pkgmgr, 'test', site_tests, client_dir, repo_url,
179 remove=remove)
180 process_packages(pkgmgr, 'client', 'autotest', client_dir, repo_url,
181 remove=remove)
182 process_packages(pkgmgr, 'dep', deps, dep_dir, repo_url,
183 remove=remove)
184 process_packages(pkgmgr, 'profiler', profilers, prof_dir, repo_url,
185 remove=remove)
186
187
188
189
showard9d02fb52008-08-08 18:20:37 +0000190
191
192# Get the list of sub directories present in a directory
193def get_subdir_list(name, client_dir):
194 dir_name = os.path.join(client_dir, name)
195 return [f for f in
196 os.listdir(dir_name)
197 if os.path.isdir(os.path.join(dir_name, f)) ]
198
199
200# Look whether the test is present in client/tests and client/site_tests dirs
201def get_test_dir(name, client_dir):
202 names_test = os.listdir(os.path.join(client_dir, 'tests'))
203 names_site_test = os.listdir(os.path.join(client_dir, 'site_tests'))
204 if name in names_test:
205 src_dir = os.path.join(client_dir, 'tests')
206 elif name in names_site_test:
207 src_dir = os.path.join(client_dir, 'site_tests')
208 else:
209 print "Test %s not found" % name
210 sys.exit(0)
211 return src_dir
212
213
214# Given the name of the test the following function
215# returns the directory in which the test files are present
216# in (tests or site_tests)
217def main():
218 server_dir = server_utils.get_server_dir()
219 autotest_dir = os.path.abspath(os.path.join(server_dir, '..'))
220
221 # extract the pkg locations from global config
222 repo_urls = c.get_config_value('PACKAGES', 'fetch_location',
223 type=list, default=[])
224 upload_paths = c.get_config_value('PACKAGES', 'upload_location',
225 type=list, default=[])
226 # Having no upload paths basically means you're not using packaging.
227 if len(upload_paths) == 0:
228 return
229
mblighddcd90f2008-10-10 21:07:40 +0000230 pkgmgr = packages.PackageManager(autotest_dir, repo_urls=repo_urls,
231 upload_paths=upload_paths,
showard9d02fb52008-08-08 18:20:37 +0000232 run_function_dargs={'timeout':600})
233
234 client_dir = os.path.join(autotest_dir, "client")
235
236 # Bail out if the client directory does not exist
237 if not os.path.exists(client_dir):
238 sys.exit(0)
239
240 dep_dir = os.path.join(client_dir, "deps")
241 prof_dir = os.path.join(client_dir, "profilers")
242
243 options, args = parse_args()
244
245 if len(args)==0 or args[0] not in ['upload','remove']:
246 print("Either 'upload' or 'remove' needs to be specified "
247 "for the package")
248 sys.exit(0)
249
250 if args[0]=='upload':
251 remove_flag=False
252 elif args[0]=='remove':
253 remove_flag=True
254 else:
255 # we should not be getting here
256 assert(False)
257
258 if options.all:
mbligh9fc77972008-10-02 20:32:09 +0000259 if options.repo:
260 upload_path_list = [options.repo]
261 else:
262 upload_path_list = upload_paths
263 process_all_packages(pkgmgr, client_dir, upload_path_list,
showard9d02fb52008-08-08 18:20:37 +0000264 remove=remove_flag)
265
266 if options.client:
267 process_packages(pkgmgr, 'client', 'autotest', client_dir,
268 options.repo, remove=remove_flag)
269
270 if options.dep:
271 process_packages(pkgmgr, 'dep', options.dep, dep_dir,
272 options.repo, remove=remove_flag)
273
274 if options.test:
275 process_packages(pkgmgr, 'test', options.test, client_dir,
276 options.repo, remove=remove_flag)
277
278 if options.prof:
279 process_packages(pkgmgr, 'profiler', options.prof, prof_dir,
280 options.repo, remove=remove_flag)
281
282 if options.file:
283 if remove_flag:
284 pkgmgr.remove_pkg(options.file, options.repo, remove_checksum=True)
285 else:
286 pkgmgr.upload_pkg(options.file, options.repo, update_checksum=True)
287
288
289if __name__ == "__main__":
290 main()