Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Jarkko Pöyry | 3c77ed4 | 2015-01-06 12:54:34 -0800 | [diff] [blame] | 3 | #------------------------------------------------------------------------- |
| 4 | # drawElements Quality Program utilities |
| 5 | # -------------------------------------- |
| 6 | # |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 7 | # Copyright 2015-2017 The Android Open Source Project |
Jarkko Pöyry | 3c77ed4 | 2015-01-06 12:54:34 -0800 | [diff] [blame] | 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # |
| 21 | #------------------------------------------------------------------------- |
| 22 | |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 23 | import os |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 24 | import sys |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 25 | import urllib2 |
| 26 | import hashlib |
| 27 | |
| 28 | import registry |
| 29 | |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 30 | sys.path.append(os.path.join(os.path.dirname(__file__), "..")) |
| 31 | |
| 32 | from build.common import * |
| 33 | |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 34 | BASE_URL = "" |
| 35 | |
| 36 | class RegistrySource: |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 37 | def __init__(self, repository, filename, revision, checksum): |
| 38 | self.repository = repository |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 39 | self.filename = filename |
| 40 | self.revision = revision |
| 41 | self.checksum = checksum |
| 42 | |
| 43 | def __hash__(self): |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 44 | return hash((self.repository, self.filename, self.revision, self.checksum)) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 45 | |
| 46 | def __eq__(self, other): |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 47 | return (self.repository, self.filename, self.revision, self.checksum) == (other.repository, other.filename, other.revision, other.checksum) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 48 | |
| 49 | def getFilename (self): |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 50 | return os.path.basename(self.filename) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 51 | |
| 52 | def getCacheFilename (self): |
Alexander Galazin | ae3759c | 2017-02-24 21:47:42 +0100 | [diff] [blame] | 53 | return "r%s-%s" % (self.revision, self.getFilename()) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 54 | |
| 55 | def getChecksum (self): |
| 56 | return self.checksum |
| 57 | |
| 58 | def getRevision (self): |
| 59 | return self.revision |
| 60 | |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 61 | def getRepo (self): |
| 62 | return self.repository |
| 63 | |
| 64 | def getRevision (self): |
| 65 | return self.revision |
| 66 | |
| 67 | def getFilename (self): |
| 68 | return self.filename |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 69 | |
| 70 | def computeChecksum (data): |
| 71 | return hashlib.sha256(data).hexdigest() |
| 72 | |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 73 | def makeSourceUrl (repository, revision, filename): |
| 74 | return "%s/%s/%s" % (repository, revision, filename) |
| 75 | |
| 76 | def checkoutGit (repository, revision, fullDstPath): |
| 77 | if not os.path.exists(fullDstPath): |
| 78 | execute(["git", "clone", "--no-checkout", repository, fullDstPath]) |
| 79 | |
| 80 | pushWorkingDir(fullDstPath) |
| 81 | try: |
| 82 | execute(["git", "fetch", repository, "+refs/heads/*:refs/remotes/origin/*"]) |
| 83 | execute(["git", "checkout", revision]) |
| 84 | finally: |
| 85 | popWorkingDir() |
| 86 | |
| 87 | def checkoutFile (repository, revision, filename, cacheDir): |
| 88 | try: |
| 89 | req = urllib2.urlopen(makeSourceUrl(repository, revision, filename)) |
| 90 | data = req.read() |
| 91 | except IOError: |
| 92 | fullDstPath = os.path.join(cacheDir, "git") |
| 93 | |
| 94 | checkoutGit(repository, revision, fullDstPath) |
| 95 | f = open(os.path.join(fullDstPath, filename), "r") |
| 96 | data = f.read() |
| 97 | f.close() |
| 98 | except: |
| 99 | print "Unexpected error:", sys.exc_info()[0] |
| 100 | |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 101 | return data |
| 102 | |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 103 | def fetchFile (dstPath, repository, revision, filename, checksum, cacheDir): |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 104 | def writeFile (filename, data): |
| 105 | f = open(filename, 'wb') |
| 106 | f.write(data) |
| 107 | f.close() |
| 108 | |
| 109 | if not os.path.exists(os.path.dirname(dstPath)): |
| 110 | os.makedirs(os.path.dirname(dstPath)) |
| 111 | |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 112 | print "Fetching %s/%s@%s" % (repository, filename, revision) |
| 113 | data = checkoutFile(repository, revision, filename, cacheDir) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 114 | gotChecksum = computeChecksum(data) |
| 115 | |
| 116 | if checksum != gotChecksum: |
Courtney Goeltzenleuchter | 6a26fb1 | 2017-06-19 10:35:01 -0600 | [diff] [blame] | 117 | raise Exception("Checksum mismatch, expected %s, got %s" % (checksum, gotChecksum)) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 118 | |
| 119 | writeFile(dstPath, data) |
| 120 | |
| 121 | def checkFile (filename, checksum): |
| 122 | def readFile (filename): |
| 123 | f = open(filename, 'rb') |
| 124 | data = f.read() |
| 125 | f.close() |
| 126 | return data |
| 127 | |
| 128 | if os.path.exists(filename): |
| 129 | return computeChecksum(readFile(filename)) == checksum |
| 130 | else: |
| 131 | return False |
| 132 | |
| 133 | g_registryCache = {} |
| 134 | |
| 135 | def getRegistry (source): |
| 136 | global g_registryCache |
| 137 | |
| 138 | if source in g_registryCache: |
| 139 | return g_registryCache[source] |
| 140 | |
| 141 | cacheDir = os.path.join(os.path.dirname(__file__), "cache") |
| 142 | cachePath = os.path.join(cacheDir, source.getCacheFilename()) |
| 143 | |
| 144 | if not checkFile(cachePath, source.checksum): |
Alexander Galazin | 4de1696 | 2017-07-03 11:44:44 +0200 | [diff] [blame] | 145 | fetchFile(cachePath, source.getRepo(), source.getRevision(), source.getFilename(), source.getChecksum(), cacheDir) |
Pyry Haulos | a017863 | 2014-11-21 14:56:21 -0800 | [diff] [blame] | 146 | |
| 147 | parsedReg = registry.parse(cachePath) |
| 148 | |
| 149 | g_registryCache[source] = parsedReg |
| 150 | |
| 151 | return parsedReg |