os.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. '''
  2. os module test for the CC3200 based boards
  3. '''
  4. from machine import SD
  5. import os
  6. mch = os.uname().machine
  7. if 'LaunchPad' in mch:
  8. sd_pins = ('GP16', 'GP17', 'GP15')
  9. elif 'WiPy' in mch:
  10. sd_pins = ('GP10', 'GP11', 'GP15')
  11. else:
  12. raise Exception('Board not supported!')
  13. sd = SD(pins=sd_pins)
  14. os.mount(sd, '/sd')
  15. os.mkfs('/sd')
  16. os.chdir('/flash')
  17. print(os.listdir())
  18. os.chdir('/sd')
  19. print(os.listdir())
  20. # create a test directory in flash
  21. os.mkdir('/flash/test')
  22. os.chdir('/flash/test')
  23. print(os.getcwd())
  24. os.chdir('..')
  25. print(os.getcwd())
  26. os.chdir('test')
  27. print(os.getcwd())
  28. # create a new file
  29. f = open('test.txt', 'w')
  30. test_bytes = os.urandom(1024)
  31. n_w = f.write(test_bytes)
  32. print(n_w == len(test_bytes))
  33. f.close()
  34. f = open('test.txt', 'r')
  35. r = bytes(f.read(), 'ascii')
  36. # check that we can write and read it correctly
  37. print(r == test_bytes)
  38. f.close()
  39. os.rename('test.txt', 'newtest.txt')
  40. print(os.listdir())
  41. os.rename('/flash/test', '/flash/newtest')
  42. print(os.listdir('/flash'))
  43. os.remove('newtest.txt')
  44. os.chdir('..')
  45. os.rmdir('newtest')
  46. # create a test directory in the sd card
  47. os.mkdir('/sd/test')
  48. os.chdir('/sd/test')
  49. print(os.getcwd())
  50. os.chdir('..')
  51. print(os.getcwd())
  52. os.chdir('test')
  53. print(os.getcwd())
  54. # create a new file
  55. f = open('test.txt', 'w')
  56. test_bytes = os.urandom(1024)
  57. n_w = f.write(test_bytes)
  58. print(n_w == len(test_bytes))
  59. f.close()
  60. f = open('test.txt', 'r')
  61. r = bytes(f.read(), 'ascii')
  62. # check that we can write and read it correctly
  63. print(r == test_bytes)
  64. f.close()
  65. print('CC3200' in os.uname().machine)
  66. print('WiPy' == os.uname().sysname)
  67. os.sync()
  68. os.stat('/flash')
  69. os.stat('/flash/sys')
  70. os.stat('/flash/boot.py')
  71. os.stat('/sd')
  72. os.stat('/')
  73. os.chdir('/sd/test')
  74. os.remove('test.txt')
  75. os.chdir('/sd')
  76. os.rmdir('test')
  77. os.listdir('/sd')
  78. print(os.listdir('/'))
  79. os.unmount('/sd')
  80. print(os.listdir('/'))
  81. os.mkfs(sd)
  82. os.mount(sd, '/sd')
  83. print(os.listdir('/'))
  84. os.chdir('/flash')
  85. # next ones must raise
  86. sd.deinit()
  87. try:
  88. os.listdir('/sd')
  89. except:
  90. print('Exception')
  91. #re-initialization must work
  92. sd.init()
  93. print(os.listdir('/sd'))
  94. try:
  95. os.mount(sd, '/sd')
  96. except:
  97. print('Exception')
  98. try:
  99. os.mount(sd, '/sd2')
  100. except:
  101. print('Exception')
  102. os.unmount('/sd')
  103. try:
  104. os.listdir('/sd')
  105. except:
  106. print('Exception')
  107. try:
  108. os.unmount('/flash')
  109. except:
  110. print('Exception')
  111. try:
  112. os.unmount('/something')
  113. except:
  114. print('Exception')
  115. try:
  116. os.unmount('something')
  117. except:
  118. print('Exception')
  119. try:
  120. os.mkfs('flash') # incorrect path format
  121. except:
  122. print('Exception')
  123. try:
  124. os.remove('/flash/nofile.txt')
  125. except:
  126. print('Exception')
  127. try:
  128. os.rename('/flash/nofile.txt', '/flash/nofile2.txt')
  129. except:
  130. print('Exception')
  131. try:
  132. os.chdir('/flash/nodir')
  133. except:
  134. print('Exception')
  135. try:
  136. os.listdir('/flash/nodir')
  137. except:
  138. print('Exception')
  139. os.mount(sd, '/sd')
  140. print(os.listdir('/'))
  141. os.unmount('/sd')