ssh.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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' implementations
  5. This implementation is for Secure Shell (SSH), and attempts to comply with the
  6. IETF secsh internet draft:
  7. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
  8. Currently does not support the sftp parameters, as this uses scp
  9. Also does not support the 'fingerprint' connection parameter.
  10. Please note that '/' is used as host, path separator not ':' as you may
  11. be used to, also '~' can be used to specify user HOME, but again after '/'
  12. Example SRC_URI:
  13. SRC_URI = "ssh://user@host.example.com/dir/path/file.txt"
  14. SRC_URI = "ssh://user@host.example.com/~/file.txt"
  15. '''
  16. # Copyright (C) 2006 OpenedHand Ltd.
  17. #
  18. #
  19. # Based in part on svk.py:
  20. # Copyright (C) 2006 Holger Hans Peter Freyther
  21. # Based on svn.py:
  22. # Copyright (C) 2003, 2004 Chris Larson
  23. # Based on functions from the base bb module:
  24. # Copyright 2003 Holger Schurig
  25. #
  26. #
  27. # This program is free software; you can redistribute it and/or modify
  28. # it under the terms of the GNU General Public License version 2 as
  29. # published by the Free Software Foundation.
  30. #
  31. # This program is distributed in the hope that it will be useful,
  32. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. # GNU General Public License for more details.
  35. #
  36. # You should have received a copy of the GNU General Public License along
  37. # with this program; if not, write to the Free Software Foundation, Inc.,
  38. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  39. import re, os
  40. from bb import data
  41. from bb.fetch2 import FetchMethod
  42. from bb.fetch2 import FetchError
  43. from bb.fetch2 import logger
  44. from bb.fetch2 import runfetchcmd
  45. __pattern__ = re.compile(r'''
  46. \s* # Skip leading whitespace
  47. ssh:// # scheme
  48. ( # Optional username/password block
  49. (?P<user>\S+) # username
  50. (:(?P<pass>\S+))? # colon followed by the password (optional)
  51. )?
  52. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  53. @
  54. (?P<host>\S+?) # non-greedy match of the host
  55. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  56. /
  57. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  58. # and may include the use of '~' to reference the remote home
  59. # directory
  60. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  61. $
  62. ''', re.VERBOSE)
  63. class SSH(FetchMethod):
  64. '''Class to fetch a module or modules via Secure Shell'''
  65. def supports(self, urldata, d):
  66. return __pattern__.match(urldata.url) != None
  67. def supports_checksum(self, urldata):
  68. return False
  69. def urldata_init(self, urldata, d):
  70. if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git':
  71. raise bb.fetch2.ParameterError(
  72. "Invalid protocol - if you wish to fetch from a git " +
  73. "repository using ssh, you need to use " +
  74. "git:// prefix with protocol=ssh", urldata.url)
  75. m = __pattern__.match(urldata.url)
  76. path = m.group('path')
  77. host = m.group('host')
  78. urldata.localpath = os.path.join(d.getVar('DL_DIR', True),
  79. os.path.basename(os.path.normpath(path)))
  80. def download(self, urldata, d):
  81. dldir = d.getVar('DL_DIR', True)
  82. m = __pattern__.match(urldata.url)
  83. path = m.group('path')
  84. host = m.group('host')
  85. port = m.group('port')
  86. user = m.group('user')
  87. password = m.group('pass')
  88. if port:
  89. portarg = '-P %s' % port
  90. else:
  91. portarg = ''
  92. if user:
  93. fr = user
  94. if password:
  95. fr += ':%s' % password
  96. fr += '@%s' % host
  97. else:
  98. fr = host
  99. fr += ':%s' % path
  100. cmd = 'scp -B -r %s %s %s/' % (
  101. portarg,
  102. fr,
  103. dldir
  104. )
  105. bb.fetch2.check_network_access(d, cmd, urldata.url)
  106. runfetchcmd(cmd, d)