1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 """Tools for searching/manipulating the manifests repository.""" 5 6 from __future__ import print_function 7 8 __author__ = 'llozano (at] google.com (Luis Lozano)' 9 10 import os 11 import re 12 import shutil 13 import tempfile 14 import time 15 16 import command_executer 17 import logger 18 19 20 def IsCrosVersion(version): 21 match = re.search(r'(\d+\.\d+\.\d+\.\d+)', version) 22 return match is not None 23 24 25 def IsRFormatCrosVersion(version): 26 match = re.search(r'(R\d+-\d+\.\d+\.\d+)', version) 27 return match is not None 28 29 30 def RFormatCrosVersion(version): 31 assert IsCrosVersion(version) 32 tmp_major, tmp_minor = version.split('.', 1) 33 rformat = 'R' + tmp_major + '-' + tmp_minor 34 assert IsRFormatCrosVersion(rformat) 35 return rformat 36 37 38 class ManifestVersions(object): 39 """This class handles interactions with the manifests repo.""" 40 41 def __init__(self, internal=True): 42 self.internal = internal 43 self.clone_location = tempfile.mkdtemp() 44 self.ce = command_executer.GetCommandExecuter() 45 if internal: 46 versions_git = ('https://chrome-internal.googlesource.com/' 47 'chromeos/manifest-versions.git') 48 else: 49 versions_git = ( 50 'https://chromium.googlesource.com/chromiumos/manifest-versions.git') 51 commands = ['cd {0}'.format(self.clone_location), 52 'git clone {0}'.format(versions_git)] 53 ret = self.ce.RunCommands(commands) 54 if ret: 55 logger.GetLogger().LogFatal('Failed to clone manifest-versions.') 56 57 def __del__(self): 58 if self.clone_location: 59 shutil.rmtree(self.clone_location) 60 61 def TimeToVersion(self, my_time): 62 """Convert timestamp to version number.""" 63 cur_time = time.mktime(time.gmtime()) 64 des_time = float(my_time) 65 if cur_time - des_time > 7000000: 66 logger.GetLogger().LogFatal('The time you specify is too early.') 67 commands = ['cd {0}'.format(self.clone_location), 'cd manifest-versions', 68 'git checkout -f $(git rev-list' + 69 ' --max-count=1 --before={0} origin/master)'.format(my_time)] 70 ret = self.ce.RunCommands(commands) 71 if ret: 72 logger.GetLogger().LogFatal('Failed to checkout manifest at ' 73 'specified time') 74 path = os.path.realpath('{0}/manifest-versions/LKGM/lkgm.xml'.format( 75 self.clone_location)) 76 pp = path.split('/') 77 small = os.path.basename(path).split('.xml')[0] 78 version = pp[-2] + '.' + small 79 commands = ['cd {0}'.format(self.clone_location), 'cd manifest-versions', 80 'git checkout master'] 81 self.ce.RunCommands(commands) 82 return version 83 84 def GetManifest(self, version, to_file): 85 """Get the manifest file from a given chromeos-internal version.""" 86 assert not IsRFormatCrosVersion(version) 87 version = version.split('.', 1)[1] 88 os.chdir(self.clone_location) 89 files = [os.path.join(r, f) 90 for r, _, fs in os.walk('.') for f in fs if version in f] 91 if files: 92 command = 'cp {0} {1}'.format(files[0], to_file) 93 ret = self.ce.RunCommand(command) 94 if ret: 95 raise RuntimeError('Cannot copy manifest to {0}'.format(to_file)) 96 else: 97 raise RuntimeError('Version {0} is not available.'.format(version)) 98