Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(818)

Side by Side Diff: client/cipd.py

Issue 2060983006: luci-py/isolateserver.py: Add archive support when downloading. (Closed) Base URL: https://github.com/luci/luci-py.git@master
Patch Set: Rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2016 The LUCI Authors. All rights reserved. 1 # Copyright 2016 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 """Fetches CIPD client and installs packages.""" 5 """Fetches CIPD client and installs packages."""
6 6
7 __version__ = '0.2' 7 __version__ = '0.2'
8 8
9 import collections 9 import collections
10 import contextlib 10 import contextlib
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 # It does not take a lot of disk space. 349 # It does not take a lot of disk space.
350 version_cache = isolateserver.DiskCache( 350 version_cache = isolateserver.DiskCache(
351 unicode(os.path.join(cache_dir, 'versions')), 351 unicode(os.path.join(cache_dir, 'versions')),
352 isolateserver.CachePolicies(0, 0, 300), 352 isolateserver.CachePolicies(0, 0, 300),
353 hashlib.sha1) 353 hashlib.sha1)
354 with version_cache: 354 with version_cache:
355 # Convert |version| to a string that may be used as a filename in disk 355 # Convert |version| to a string that may be used as a filename in disk
356 # cache by hashing it. 356 # cache by hashing it.
357 version_digest = hashlib.sha1(version).hexdigest() 357 version_digest = hashlib.sha1(version).hexdigest()
358 try: 358 try:
359 instance_id = version_cache.read(version_digest) 359 with version_cache.getfileobj(version_digest) as f:
360 instance_id = f.read()
360 except isolateserver.CacheMiss: 361 except isolateserver.CacheMiss:
361 instance_id = resolve_version( 362 instance_id = resolve_version(
362 service_url, package_name, version, timeout=timeoutfn()) 363 service_url, package_name, version, timeout=timeoutfn())
363 version_cache.write(version_digest, instance_id) 364 version_cache.write(version_digest, instance_id)
364 365
365 # instance_cache is {instance_id -> client binary} mapping. 366 # instance_cache is {instance_id -> client binary} mapping.
366 # It is bounded by 5 client versions. 367 # It is bounded by 5 client versions.
367 instance_cache = isolateserver.DiskCache( 368 instance_cache = isolateserver.DiskCache(
368 unicode(os.path.join(cache_dir, 'clients')), 369 unicode(os.path.join(cache_dir, 'clients')),
369 isolateserver.CachePolicies(0, 0, 5), 370 isolateserver.CachePolicies(0, 0, 5),
370 hashlib.sha1) 371 hashlib.sha1)
371 with instance_cache: 372 with instance_cache:
372 if instance_id not in instance_cache: 373 if instance_id not in instance_cache:
373 logging.info('Fetching CIPD client %s:%s', package_name, instance_id) 374 logging.info('Fetching CIPD client %s:%s', package_name, instance_id)
374 fetch_url = get_client_fetch_url( 375 fetch_url = get_client_fetch_url(
375 service_url, package_name, instance_id, timeout=timeoutfn()) 376 service_url, package_name, instance_id, timeout=timeoutfn())
376 _fetch_cipd_client(instance_cache, instance_id, fetch_url, timeoutfn) 377 _fetch_cipd_client(instance_cache, instance_id, fetch_url, timeoutfn)
377 378
378 # A single host can run multiple swarming bots, but ATM they do not share 379 # A single host can run multiple swarming bots, but ATM they do not share
379 # same root bot directory. Thus, it is safe to use the same name for the 380 # same root bot directory. Thus, it is safe to use the same name for the
380 # binary. 381 # binary.
381 binary_path = unicode(os.path.join(cache_dir, 'cipd' + EXECUTABLE_SUFFIX)) 382 binary_path = unicode(os.path.join(cache_dir, 'cipd' + EXECUTABLE_SUFFIX))
382 if fs.isfile(binary_path): 383 if fs.isfile(binary_path):
383 file_path.remove(binary_path) 384 file_path.remove(binary_path)
384 instance_cache.hardlink(instance_id, binary_path, 0511) # -r-x--x--x 385 with instance_cache.getfileobj(instance_id) as f:
385 386 isolateserver.putfile(f, binary_path, 0511) # -r-x--x--x
386 yield CipdClient(binary_path) 387 yield CipdClient(binary_path)
387 388
388 389
389 def parse_package_list_file(path): 390 def parse_package_list_file(path):
390 """Returns a map {site_root_path: [(package, version)]} read from file. 391 """Returns a map {site_root_path: [(package, version)]} read from file.
391 392
392 Slashes in site_root_path are replaced with os.path.sep. 393 Slashes in site_root_path are replaced with os.path.sep.
393 """ 394 """
394 with open(path) as f: 395 with open(path) as f:
395 try: 396 try:
396 parsed = json.load(f) 397 parsed = json.load(f)
397 except ValueError as ex: 398 except ValueError as ex:
398 raise Error('Invalid package list file: %s' % ex) 399 raise Error('Invalid package list file: %s' % ex)
399 400
400 packages = collections.defaultdict(list) 401 packages = collections.defaultdict(list)
401 for package in parsed.get('packages') or []: 402 for package in parsed.get('packages') or []:
402 path = package.get('path') or '.' 403 path = package.get('path') or '.'
403 path = path.replace('/', os.path.sep) 404 path = path.replace('/', os.path.sep)
404 405
405 name = package.get('package_name') 406 name = package.get('package_name')
406 if not name: 407 if not name:
407 raise Error('Invalid package list file: package name is not specified') 408 raise Error('Invalid package list file: package name is not specified')
408 version = package.get('version') 409 version = package.get('version')
409 if not version: 410 if not version:
410 raise Error('Invalid package list file: package version is not specified') 411 raise Error('Invalid package list file: package version is not specified')
411 packages[path].append((name, version)) 412 packages[path].append((name, version))
412 return packages 413 return packages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698