Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | """ |
| 4 | BitBake 'Fetch' implementation for Amazon AWS S3. |
| 5 | |
| 6 | Class for fetching files from Amazon S3 using the AWS Command Line Interface. |
| 7 | The aws tool must be correctly installed and configured prior to use. |
| 8 | |
| 9 | """ |
| 10 | |
| 11 | # Copyright (C) 2017, Andre McCurdy <armccurdy@gmail.com> |
| 12 | # |
| 13 | # Based in part on bb.fetch2.wget: |
| 14 | # Copyright (C) 2003, 2004 Chris Larson |
| 15 | # |
| 16 | # This program is free software; you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License version 2 as |
| 18 | # published by the Free Software Foundation. |
| 19 | # |
| 20 | # This program is distributed in the hope that it will be useful, |
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | # GNU General Public License for more details. |
| 24 | # |
| 25 | # You should have received a copy of the GNU General Public License along |
| 26 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 28 | # |
| 29 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 30 | |
| 31 | import os |
| 32 | import bb |
| 33 | import urllib.request, urllib.parse, urllib.error |
| 34 | from bb.fetch2 import FetchMethod |
| 35 | from bb.fetch2 import FetchError |
| 36 | from bb.fetch2 import runfetchcmd |
| 37 | |
| 38 | class S3(FetchMethod): |
| 39 | """Class to fetch urls via 'aws s3'""" |
| 40 | |
| 41 | def supports(self, ud, d): |
| 42 | """ |
| 43 | Check to see if a given url can be fetched with s3. |
| 44 | """ |
| 45 | return ud.type in ['s3'] |
| 46 | |
| 47 | def recommends_checksum(self, urldata): |
| 48 | return True |
| 49 | |
| 50 | def urldata_init(self, ud, d): |
| 51 | if 'downloadfilename' in ud.parm: |
| 52 | ud.basename = ud.parm['downloadfilename'] |
| 53 | else: |
| 54 | ud.basename = os.path.basename(ud.path) |
| 55 | |
| 56 | ud.localfile = d.expand(urllib.parse.unquote(ud.basename)) |
| 57 | |
| 58 | ud.basecmd = d.getVar("FETCHCMD_s3") or "/usr/bin/env aws s3" |
| 59 | |
| 60 | def download(self, ud, d): |
| 61 | """ |
| 62 | Fetch urls |
| 63 | Assumes localpath was called first |
| 64 | """ |
| 65 | |
| 66 | cmd = '%s cp s3://%s%s %s' % (ud.basecmd, ud.host, ud.path, ud.localpath) |
| 67 | bb.fetch2.check_network_access(d, cmd, ud.url) |
| 68 | runfetchcmd(cmd, d) |
| 69 | |
| 70 | # Additional sanity checks copied from the wget class (although there |
| 71 | # are no known issues which mean these are required, treat the aws cli |
| 72 | # tool with a little healthy suspicion). |
| 73 | |
| 74 | if not os.path.exists(ud.localpath): |
| 75 | raise FetchError("The aws cp command returned success for s3://%s%s but %s doesn't exist?!" % (ud.host, ud.path, ud.localpath)) |
| 76 | |
| 77 | if os.path.getsize(ud.localpath) == 0: |
| 78 | os.remove(ud.localpath) |
| 79 | raise FetchError("The aws cp command for s3://%s%s resulted in a zero size file?! Deleting and failing since this isn't right." % (ud.host, ud.path)) |
| 80 | |
| 81 | return True |
| 82 | |
| 83 | def checkstatus(self, fetch, ud, d): |
| 84 | """ |
| 85 | Check the status of a URL |
| 86 | """ |
| 87 | |
| 88 | cmd = '%s ls s3://%s%s' % (ud.basecmd, ud.host, ud.path) |
| 89 | bb.fetch2.check_network_access(d, cmd, ud.url) |
| 90 | output = runfetchcmd(cmd, d) |
| 91 | |
| 92 | # "aws s3 ls s3://mybucket/foo" will exit with success even if the file |
| 93 | # is not found, so check output of the command to confirm success. |
| 94 | |
| 95 | if not output: |
| 96 | raise FetchError("The aws ls command for s3://%s%s gave empty output" % (ud.host, ud.path)) |
| 97 | |
| 98 | return True |