ssh.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. '''
  2. BitBake 'Fetch' implementations
  3. This implementation is for Secure Shell (SSH), and attempts to comply with the
  4. IETF secsh internet draft:
  5. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
  6. Currently does not support the sftp parameters, as this uses scp
  7. Also does not support the 'fingerprint' connection parameter.
  8. Please note that '/' is used as host, path separator not ':' as you may
  9. be used to, also '~' can be used to specify user HOME, but again after '/'
  10. Example SRC_URI:
  11. SRC_URI = "ssh://user@host.example.com/dir/path/file.txt"
  12. SRC_URI = "ssh://user@host.example.com/~/file.txt"
  13. '''
  14. # Copyright (C) 2006 OpenedHand Ltd.
  15. #
  16. #
  17. # Based in part on svk.py:
  18. # Copyright (C) 2006 Holger Hans Peter Freyther
  19. # Based on svn.py:
  20. # Copyright (C) 2003, 2004 Chris Larson
  21. # Based on functions from the base bb module:
  22. # Copyright 2003 Holger Schurig
  23. #
  24. #
  25. # SPDX-License-Identifier: GPL-2.0-only
  26. #
  27. import re, os
  28. from bb.fetch2 import FetchMethod
  29. from bb.fetch2 import runfetchcmd
  30. __pattern__ = re.compile(r'''
  31. \s* # Skip leading whitespace
  32. ssh:// # scheme
  33. ( # Optional username/password block
  34. (?P<user>\S+) # username
  35. (:(?P<pass>\S+))? # colon followed by the password (optional)
  36. )?
  37. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  38. @
  39. (?P<host>\S+?) # non-greedy match of the host
  40. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  41. /
  42. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  43. # and may include the use of '~' to reference the remote home
  44. # directory
  45. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  46. $
  47. ''', re.VERBOSE)
  48. class SSH(FetchMethod):
  49. '''Class to fetch a module or modules via Secure Shell'''
  50. def supports(self, urldata, d):
  51. return __pattern__.match(urldata.url) is not None
  52. def supports_checksum(self, urldata):
  53. return False
  54. def urldata_init(self, urldata, d):
  55. if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git':
  56. raise bb.fetch2.ParameterError(
  57. "Invalid protocol - if you wish to fetch from a git " +
  58. "repository using ssh, you need to use " +
  59. "git:// prefix with protocol=ssh", urldata.url)
  60. m = __pattern__.match(urldata.url)
  61. path = m.group('path')
  62. host = m.group('host')
  63. urldata.localpath = os.path.join(d.getVar('DL_DIR'),
  64. os.path.basename(os.path.normpath(path)))
  65. def download(self, urldata, d):
  66. dldir = d.getVar('DL_DIR')
  67. m = __pattern__.match(urldata.url)
  68. path = m.group('path')
  69. host = m.group('host')
  70. port = m.group('port')
  71. user = m.group('user')
  72. password = m.group('pass')
  73. if port:
  74. portarg = '-P %s' % port
  75. else:
  76. portarg = ''
  77. if user:
  78. fr = user
  79. if password:
  80. fr += ':%s' % password
  81. fr += '@%s' % host
  82. else:
  83. fr = host
  84. fr += ':%s' % path
  85. cmd = 'scp -B -r %s %s %s/' % (
  86. portarg,
  87. fr,
  88. dldir
  89. )
  90. bb.fetch2.check_network_access(d, cmd, urldata.url)
  91. runfetchcmd(cmd, d)