|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# |
| 4 | +# Collects CI artifacts from S3 storage, downloading them |
| 5 | +# to a local directory. |
| 6 | +# |
| 7 | +# The artifacts' folder in the S3 bucket must have the following token |
| 8 | +# format: |
| 9 | +# <token>-[<value>]__ (repeat) |
| 10 | +# |
| 11 | +# Recognized tokens (unrecognized tokens are ignored): |
| 12 | +# p - project (e.g., "confluent-kafka-python") |
| 13 | +# bld - builder (e.g., "travis") |
| 14 | +# plat - platform ("osx", "linux", ..) |
| 15 | +# tag - git tag |
| 16 | +# sha - git sha |
| 17 | +# bid - builder's build-id |
| 18 | +# |
| 19 | +# Example: |
| 20 | +# p-confluent-kafka-python__bld-travis__plat-linux__tag-__sha-112130ce297656ea1c39e7c94c99286f95133a24__bid-271588764__/confluent_kafka-0.11.0-cp35-cp35m-manylinux1_x86_64.whl |
| 21 | + |
| 22 | + |
| 23 | +import sys |
| 24 | +import re |
| 25 | +import os |
| 26 | +import argparse |
| 27 | + |
| 28 | +import boto3 |
| 29 | + |
| 30 | +s3_bucket = 'librdkafka-ci-packages' |
| 31 | +dry_run = False |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +class Artifact (object): |
| 36 | + def __init__ (self, arts, path, info=None): |
| 37 | + self.path = path |
| 38 | + # Remove unexpanded AppVeyor $(..) tokens from filename |
| 39 | + self.fname = re.sub(r'\$\([^\)]+\)', '', os.path.basename(path)) |
| 40 | + self.lpath = os.path.join(arts.dlpath, self.fname) |
| 41 | + self.info = info |
| 42 | + self.arts = arts |
| 43 | + arts.artifacts.append(self) |
| 44 | + |
| 45 | + def __repr__ (self): |
| 46 | + return self.path |
| 47 | + |
| 48 | + def download (self, dirpath): |
| 49 | + """ Download artifact from S3 and store in dirpath directory. |
| 50 | + If the artifact is already downloaded nothing is done. """ |
| 51 | + dest = self.lpath |
| 52 | + if os.path.isfile(self.lpath) and os.path.getsize(self.lpath) > 0: |
| 53 | + return |
| 54 | + print('Downloading %s -> %s' % (self.path, self.lpath)) |
| 55 | + if dry_run: |
| 56 | + return |
| 57 | + self.arts.s3_bucket.download_file(self.path, self.lpath) |
| 58 | + |
| 59 | + |
| 60 | +class Artifacts (object): |
| 61 | + def __init__ (self, gitref, dlpath): |
| 62 | + super(Artifacts, self).__init__() |
| 63 | + self.gitref = gitref |
| 64 | + self.artifacts = list() |
| 65 | + # Download directory |
| 66 | + self.dlpath = dlpath |
| 67 | + if not os.path.isdir(self.dlpath): |
| 68 | + if not dry_run: |
| 69 | + os.makedirs(self.dlpath, 0755) |
| 70 | + |
| 71 | + def collect_single (self, path, folder): |
| 72 | + """ Collect single entry |
| 73 | + :param: path string: S3 or local path to file |
| 74 | + :param: folder string: S3 folder name |
| 75 | + """ |
| 76 | + folder = os.path.dirname(key.key) |
| 77 | + |
| 78 | + rinfo = re.findall(r'(?P<tag>[^-]+)-(?P<val>.*?)__', folder) |
| 79 | + if rinfo is None or len(rinfo) == 0: |
| 80 | + # print('Incorrect folder/file name format for %s' % folder) |
| 81 | + return None |
| 82 | + |
| 83 | + info = dict(rinfo) |
| 84 | + |
| 85 | + # Ignore AppVeyor Debug builds |
| 86 | + if info.get('bldtype', '').lower() == 'debug': |
| 87 | + print('Ignoring debug artifact %s' % folder) |
| 88 | + return None |
| 89 | + |
| 90 | + tag = info.get('tag', None) |
| 91 | + if tag is not None and (len(tag) == 0 or tag.startswith('$(')): |
| 92 | + # AppVeyor doesn't substite $(APPVEYOR_REPO_TAG_NAME) |
| 93 | + # with an empty value when not set, it leaves that token |
| 94 | + # in the string - so translate that to no tag. |
| 95 | + tag = None |
| 96 | + |
| 97 | + sha = info.get('sha', None) |
| 98 | + if (tag is not None and tag == self.gitref) or (sha is not None and sha.startswith(self.gitref)): |
| 99 | + return Artifact(self, path, info, lpath=lpath) |
| 100 | + |
| 101 | + return None |
| 102 | + |
| 103 | + def collect_single_s3 (self, path): |
| 104 | + """ Collect single S3 artifact |
| 105 | + :param: path string: S3 path |
| 106 | + """ |
| 107 | + |
| 108 | + # The S3 folder contains the tokens needed to perform |
| 109 | + # matching of project, gitref, etc. |
| 110 | + folder = os.path.dirname(path) |
| 111 | + |
| 112 | + rinfo = re.findall(r'(?P<tag>[^-]+)-(?P<val>.*?)__', folder) |
| 113 | + if rinfo is None or len(rinfo) == 0: |
| 114 | + # print('Incorrect folder/file name format for %s' % folder) |
| 115 | + return None |
| 116 | + |
| 117 | + info = dict(rinfo) |
| 118 | + |
| 119 | + # Ignore AppVeyor Debug builds |
| 120 | + if info.get('bldtype', '').lower() == 'debug': |
| 121 | + print('Ignoring debug artifact %s' % folder) |
| 122 | + return None |
| 123 | + |
| 124 | + tag = info.get('tag', None) |
| 125 | + if tag is not None and (len(tag) == 0 or tag.startswith('$(')): |
| 126 | + # AppVeyor doesn't substite $(APPVEYOR_REPO_TAG_NAME) |
| 127 | + # with an empty value when not set, it leaves that token |
| 128 | + # in the string - so translate that to no tag. |
| 129 | + tag = None |
| 130 | + |
| 131 | + sha = info.get('sha', None) |
| 132 | + |
| 133 | + # Match tag or sha to gitref |
| 134 | + if (tag is not None and tag == self.gitref) or (sha is not None and sha.startswith(self.gitref)): |
| 135 | + return Artifact(self, path, info) |
| 136 | + |
| 137 | + return None |
| 138 | + |
| 139 | + |
| 140 | + def collect_s3 (self): |
| 141 | + """ Collect and download build-artifacts from S3 based on git reference """ |
| 142 | + print('Collecting artifacts matching tag/sha %s from S3 bucket %s' % (self.gitref, s3_bucket)) |
| 143 | + self.s3 = boto3.resource('s3') |
| 144 | + self.s3_bucket = self.s3.Bucket(s3_bucket) |
| 145 | + self.s3.meta.client.head_bucket(Bucket=s3_bucket) |
| 146 | + for key in self.s3_bucket.objects.all(): |
| 147 | + self.collect_single_s3(key.key) |
| 148 | + |
| 149 | + for a in self.artifacts: |
| 150 | + a.download(self.dlpath) |
| 151 | + |
| 152 | + def collect_local (self, path): |
| 153 | + """ Collect artifacts from a local directory possibly previously |
| 154 | + collected from s3 """ |
| 155 | + for f in os.listdir(path): |
| 156 | + lpath = os.path.join(path, f) |
| 157 | + if not os.path.isfile(lpath): |
| 158 | + continue |
| 159 | + Artifact(self, lpath) |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == '__main__': |
| 164 | + |
| 165 | + parser = argparse.ArgumentParser() |
| 166 | + parser.add_argument("--no-s3", help="Don't collect from S3", action="store_true") |
| 167 | + parser.add_argument("--dry-run", help="Locate artifacts but don't actually download or do anything", action="store_true") |
| 168 | + parser.add_argument("--directory", help="Download directory (default: dl-<gitref>)", default=None) |
| 169 | + parser.add_argument("tag", help="Tag or git SHA to collect") |
| 170 | + |
| 171 | + args = parser.parse_args() |
| 172 | + dry_run = args.dry_run |
| 173 | + gitref = args.tag |
| 174 | + if not args.directory: |
| 175 | + args.directory = 'dl-%s' % gitref |
| 176 | + |
| 177 | + arts = Artifacts(gitref, args.directory) |
| 178 | + |
| 179 | + if not args.no_s3: |
| 180 | + arts.collect_s3() |
| 181 | + else: |
| 182 | + arts.collect_local(arts.dlpath) |
| 183 | + |
| 184 | + if len(arts.artifacts) == 0: |
| 185 | + raise ValueError('No artifacts found for %s' % arts.gitref) |
| 186 | + |
| 187 | + print('Collected artifacts:') |
| 188 | + for a in arts.artifacts: |
| 189 | + print(' %s -> %s' % (a.path, a.lpath)) |
0 commit comments