ssh.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. '''
  5. BitBake 'Fetch' implementations
  6. This implementation is for Secure Shell (SSH), and attempts to comply with the
  7. IETF secsh internet draft:
  8. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
  9. Currently does not support the sftp parameters, as this uses scp
  10. Also does not support the 'fingerprint' connection parameter.
  11. '''
  12. # Copyright (C) 2006 OpenedHand Ltd.
  13. #
  14. #
  15. # Based in part on svk.py:
  16. # Copyright (C) 2006 Holger Hans Peter Freyther
  17. # Based on svn.py:
  18. # Copyright (C) 2003, 2004 Chris Larson
  19. # Based on functions from the base bb module:
  20. # Copyright 2003 Holger Schurig
  21. #
  22. #
  23. # This program is free software; you can redistribute it and/or modify
  24. # it under the terms of the GNU General Public License version 2 as
  25. # published by the Free Software Foundation.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. #
  32. # You should have received a copy of the GNU General Public License along
  33. # with this program; if not, write to the Free Software Foundation, Inc.,
  34. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  35. import re, os
  36. import bb
  37. from bb import data
  38. from bb.fetch import Fetch
  39. from bb.fetch import FetchError
  40. from bb.fetch import MissingParameterError
  41. __pattern__ = re.compile(r'''
  42. \s* # Skip leading whitespace
  43. ssh:// # scheme
  44. ( # Optional username/password block
  45. (?P<user>\S+) # username
  46. (:(?P<pass>\S+))? # colon followed by the password (optional)
  47. )?
  48. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  49. @
  50. (?P<host>\S+?) # non-greedy match of the host
  51. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  52. /
  53. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  54. # and may include the use of '~' to reference the remote home
  55. # directory
  56. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  57. $
  58. ''', re.VERBOSE)
  59. class SSH(Fetch):
  60. '''Class to fetch a module or modules via Secure Shell'''
  61. def supports(self, url, urldata, d):
  62. return __pattern__.match(url) != None
  63. def localpath(self, url, urldata, d):
  64. m = __pattern__.match(url)
  65. path = m.group('path')
  66. host = m.group('host')
  67. lpath = os.path.join(data.getVar('DL_DIR', d, True), host, os.path.basename(path))
  68. return lpath
  69. def go(self, url, urldata, d):
  70. dldir = data.getVar('DL_DIR', d, 1)
  71. m = __pattern__.match(url)
  72. path = m.group('path')
  73. host = m.group('host')
  74. port = m.group('port')
  75. user = m.group('user')
  76. password = m.group('pass')
  77. ldir = os.path.join(dldir, host)
  78. lpath = os.path.join(ldir, os.path.basename(path))
  79. if not os.path.exists(ldir):
  80. os.makedirs(ldir)
  81. if port:
  82. port = '-P %s' % port
  83. else:
  84. port = ''
  85. if user:
  86. fr = user
  87. if password:
  88. fr += ':%s' % password
  89. fr += '@%s' % host
  90. else:
  91. fr = host
  92. fr += ':%s' % path
  93. import commands
  94. cmd = 'scp -B -r %s %s %s/' % (
  95. port,
  96. commands.mkarg(fr),
  97. commands.mkarg(ldir)
  98. )
  99. (exitstatus, output) = commands.getstatusoutput(cmd)
  100. if exitstatus != 0:
  101. print output
  102. raise FetchError('Unable to fetch %s' % url)