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

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: Adding a test. 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 instance_id = version_cache.getfileobj(version_digest).read()
nodir 2016/06/20 15:54:34 DiskCache.getfileobject returns a file and it is r
mithro 2016/06/21 06:42:33 I've changed this one to a "with xxxx". See my co
360 except isolateserver.CacheMiss: 360 except isolateserver.CacheMiss:
361 instance_id = resolve_version( 361 instance_id = resolve_version(
362 service_url, package_name, version, timeout=timeoutfn()) 362 service_url, package_name, version, timeout=timeoutfn())
363 version_cache.write(version_digest, instance_id) 363 version_cache.write(version_digest, instance_id)
364 364
365 # instance_cache is {instance_id -> client binary} mapping. 365 # instance_cache is {instance_id -> client binary} mapping.
366 # It is bounded by 5 client versions. 366 # It is bounded by 5 client versions.
367 instance_cache = isolateserver.DiskCache( 367 instance_cache = isolateserver.DiskCache(
368 unicode(os.path.join(cache_dir, 'clients')), 368 unicode(os.path.join(cache_dir, 'clients')),
369 isolateserver.CachePolicies(0, 0, 5), 369 isolateserver.CachePolicies(0, 0, 5),
370 hashlib.sha1) 370 hashlib.sha1)
371 with instance_cache: 371 with instance_cache:
372 if instance_id not in instance_cache: 372 if instance_id not in instance_cache:
373 logging.info('Fetching CIPD client %s:%s', package_name, instance_id) 373 logging.info('Fetching CIPD client %s:%s', package_name, instance_id)
374 fetch_url = get_client_fetch_url( 374 fetch_url = get_client_fetch_url(
375 service_url, package_name, instance_id, timeout=timeoutfn()) 375 service_url, package_name, instance_id, timeout=timeoutfn())
376 _fetch_cipd_client(instance_cache, instance_id, fetch_url, timeoutfn) 376 _fetch_cipd_client(instance_cache, instance_id, fetch_url, timeoutfn)
377 377
378 # A single host can run multiple swarming bots, but ATM they do not share 378 # 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 379 # same root bot directory. Thus, it is safe to use the same name for the
380 # binary. 380 # binary.
381 binary_path = unicode(os.path.join(cache_dir, 'cipd' + EXECUTABLE_SUFFIX)) 381 binary_path = unicode(os.path.join(cache_dir, 'cipd' + EXECUTABLE_SUFFIX))
382 if fs.isfile(binary_path): 382 if fs.isfile(binary_path):
383 file_path.remove(binary_path) 383 file_path.remove(binary_path)
384 instance_cache.hardlink(instance_id, binary_path, 0511) # -r-x--x--x 384 isolateserver.putfile(
385 385 instance_cache.getfileobj(instance_id),
386 binary_path,
387 0511) # -r-x--x--x
386 yield CipdClient(binary_path) 388 yield CipdClient(binary_path)
387 389
388 390
389 def parse_package_list_file(path): 391 def parse_package_list_file(path):
390 """Returns a map {site_root_path: [(package, version)]} read from file. 392 """Returns a map {site_root_path: [(package, version)]} read from file.
391 393
392 Slashes in site_root_path are replaced with os.path.sep. 394 Slashes in site_root_path are replaced with os.path.sep.
393 """ 395 """
394 with open(path) as f: 396 with open(path) as f:
395 try: 397 try:
396 parsed = json.load(f) 398 parsed = json.load(f)
397 except ValueError as ex: 399 except ValueError as ex:
398 raise Error('Invalid package list file: %s' % ex) 400 raise Error('Invalid package list file: %s' % ex)
399 401
400 packages = collections.defaultdict(list) 402 packages = collections.defaultdict(list)
401 for package in parsed.get('packages') or []: 403 for package in parsed.get('packages') or []:
402 path = package.get('path') or '.' 404 path = package.get('path') or '.'
403 path = path.replace('/', os.path.sep) 405 path = path.replace('/', os.path.sep)
404 406
405 name = package.get('package_name') 407 name = package.get('package_name')
406 if not name: 408 if not name:
407 raise Error('Invalid package list file: package name is not specified') 409 raise Error('Invalid package list file: package name is not specified')
408 version = package.get('version') 410 version = package.get('version')
409 if not version: 411 if not version:
410 raise Error('Invalid package list file: package version is not specified') 412 raise Error('Invalid package list file: package version is not specified')
411 packages[path].append((name, version)) 413 packages[path].append((name, version))
412 return packages 414 return packages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698