sftp.py 4.2 KB

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