Patrick Williams | c124f4f | 2015-09-15 14:41:29 -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 SFTP Fetch implementation |
| 5 | |
| 6 | Class for fetching files via SFTP. It tries to adhere to the (now |
| 7 | expired) IETF Internet Draft for "Uniform Resource Identifier (URI) |
| 8 | Scheme for Secure File Transfer Protocol (SFTP) and Secure Shell |
| 9 | (SSH)" (SECSH URI). |
| 10 | |
| 11 | It uses SFTP (as to adhere to the SECSH URI specification). It only |
| 12 | supports key based authentication, not password. This class, unlike |
| 13 | the SSH fetcher, does not support fetching a directory tree from the |
| 14 | remote. |
| 15 | |
| 16 | http://tools.ietf.org/html/draft-ietf-secsh-scp-sftp-ssh-uri-04 |
| 17 | https://www.iana.org/assignments/uri-schemes/prov/sftp |
| 18 | https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13 |
| 19 | |
| 20 | Please note that '/' is used as host path seperator, and not ":" |
| 21 | as you may be used to from the scp/sftp commands. You can use a |
| 22 | ~ (tilde) to specify a path relative to your home directory. |
| 23 | (The /~user/ syntax, for specyfing a path relative to another |
| 24 | user's home directory is not supported.) Note that the tilde must |
| 25 | still follow the host path seperator ("/"). See exampels below. |
| 26 | |
| 27 | Example SRC_URIs: |
| 28 | |
| 29 | SRC_URI = "sftp://host.example.com/dir/path.file.txt" |
| 30 | |
| 31 | A path relative to your home directory. |
| 32 | |
| 33 | SRC_URI = "sftp://host.example.com/~/dir/path.file.txt" |
| 34 | |
| 35 | You can also specify a username (specyfing password in the |
| 36 | URI is not supported, use SSH keys to authenticate): |
| 37 | |
| 38 | SRC_URI = "sftp://user@host.example.com/dir/path.file.txt" |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | # Copyright (C) 2013, Olof Johansson <olof.johansson@axis.com> |
| 43 | # |
| 44 | # Based in part on bb.fetch2.wget: |
| 45 | # Copyright (C) 2003, 2004 Chris Larson |
| 46 | # |
| 47 | # This program is free software; you can redistribute it and/or modify |
| 48 | # it under the terms of the GNU General Public License version 2 as |
| 49 | # published by the Free Software Foundation. |
| 50 | # |
| 51 | # This program is distributed in the hope that it will be useful, |
| 52 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 53 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 54 | # GNU General Public License for more details. |
| 55 | # |
| 56 | # You should have received a copy of the GNU General Public License along |
| 57 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 58 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 59 | # |
| 60 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 61 | |
| 62 | import os |
| 63 | import bb |
| 64 | import urllib |
| 65 | import commands |
| 66 | from bb import data |
| 67 | from bb.fetch2 import URI |
| 68 | from bb.fetch2 import FetchMethod |
| 69 | from bb.fetch2 import runfetchcmd |
| 70 | |
| 71 | |
| 72 | class SFTP(FetchMethod): |
| 73 | """Class to fetch urls via 'sftp'""" |
| 74 | |
| 75 | def supports(self, ud, d): |
| 76 | """ |
| 77 | Check to see if a given url can be fetched with sftp. |
| 78 | """ |
| 79 | return ud.type in ['sftp'] |
| 80 | |
| 81 | def recommends_checksum(self, urldata): |
| 82 | return True |
| 83 | |
| 84 | def urldata_init(self, ud, d): |
| 85 | if 'protocol' in ud.parm and ud.parm['protocol'] == 'git': |
| 86 | raise bb.fetch2.ParameterError( |
| 87 | "Invalid protocol - if you wish to fetch from a " + |
| 88 | "git repository using ssh, you need to use the " + |
| 89 | "git:// prefix with protocol=ssh", ud.url) |
| 90 | |
| 91 | if 'downloadfilename' in ud.parm: |
| 92 | ud.basename = ud.parm['downloadfilename'] |
| 93 | else: |
| 94 | ud.basename = os.path.basename(ud.path) |
| 95 | |
| 96 | ud.localfile = data.expand(urllib.unquote(ud.basename), d) |
| 97 | |
| 98 | def download(self, ud, d): |
| 99 | """Fetch urls""" |
| 100 | |
| 101 | urlo = URI(ud.url) |
| 102 | basecmd = 'sftp -oBatchMode=yes' |
| 103 | port = '' |
| 104 | if urlo.port: |
| 105 | port = '-P %d' % urlo.port |
| 106 | urlo.port = None |
| 107 | |
| 108 | dldir = data.getVar('DL_DIR', d, True) |
| 109 | lpath = os.path.join(dldir, ud.localfile) |
| 110 | |
| 111 | user = '' |
| 112 | if urlo.userinfo: |
| 113 | user = urlo.userinfo + '@' |
| 114 | |
| 115 | path = urlo.path |
| 116 | |
| 117 | # Supoprt URIs relative to the user's home directory, with |
| 118 | # the tilde syntax. (E.g. <sftp://example.com/~/foo.diff>). |
| 119 | if path[:3] == '/~/': |
| 120 | path = path[3:] |
| 121 | |
| 122 | remote = '%s%s:%s' % (user, urlo.hostname, path) |
| 123 | |
| 124 | cmd = '%s %s %s %s' % (basecmd, port, commands.mkarg(remote), |
| 125 | commands.mkarg(lpath)) |
| 126 | |
| 127 | bb.fetch2.check_network_access(d, cmd, ud.url) |
| 128 | runfetchcmd(cmd, d) |
| 129 | return True |