MetaData Sharing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

88 lines
2.0 KiB

  1. #!/usr/bin/env python
  2. import unittest
  3. def _destrvis(s):
  4. r = []
  5. state = 0
  6. for i in s:
  7. if state == 0:
  8. if i == '\\':
  9. state = 1
  10. v = 0
  11. else:
  12. r.append(i)
  13. elif state in (1, 2):
  14. v = v * 8 + int(i)
  15. state += 1
  16. elif state == 3:
  17. v = v * 8 + int(i)
  18. r.append(chr(v))
  19. state = 0
  20. return ''.join(r)
  21. def parse(fname):
  22. with open(fname) as fp:
  23. i = fp.readline().strip()
  24. if not i.startswith('#mtree'):
  25. raise ValueError('not an mtree file')
  26. path = []
  27. res = {}
  28. while True:
  29. i = fp.readline().strip()
  30. # don't believe the bsdtar mtree output
  31. # contains the cases marked below
  32. if not i:
  33. break
  34. elif i.startswith('#'): # pragma: no cover
  35. raise NotImplementedError
  36. elif i.startswith('/set'): # pragma: no cover
  37. raise NotImplementedError
  38. elif i.startswith('/unset'): # pragma: no cover
  39. raise NotImplementedError
  40. parts = i.split()
  41. fname = parts.pop(0)
  42. while parts[-1] == '\\':
  43. del parts[-1]
  44. parts.extend(fp.readline().strip().split())
  45. res[fname] = dict(x.split('=') for x in parts)
  46. return res
  47. class Test(unittest.TestCase):
  48. def test_parsemtree(self):
  49. self.assertRaises(ValueError, parse, 'fixtures/genfixtures.py')
  50. data = parse('fixtures/sample.mtree')
  51. proof = {
  52. '.': {
  53. 'time': '1567323634.0'
  54. },
  55. "./\\134\\040\\015\\012\\134*\\134?\\134['\\012": {
  56. 'size': '0',
  57. 'sha512digest': 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
  58. 'time': '1567323609.0'
  59. },
  60. './test.txt': {
  61. 'size': '15',
  62. 'sha512digest': '7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f',
  63. 'time': '1567323634.0'
  64. },
  65. }
  66. self.assertEqual(data, proof)
  67. def test_debsstr(self):
  68. self.assertEqual(_destrvis(r'\015'), '\r')
  69. self.assertEqual(_destrvis(r'\011'), '\t')
  70. self.assertEqual(_destrvis(
  71. "./\\134\\040\\015\\012\\134*\\134?\\134['\\012"),
  72. "./\134\040\015\012\134*\134?\134['\012")