PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/audio_to_code.py

https://github.com/gnr/facebook-ios-sdk
Python | 132 lines | 124 code | 7 blank | 1 comment | 0 complexity | ce49e6776f57df799e4c94d33a6103a9 MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/python
  2. import sys
  3. import getopt
  4. import os
  5. headerTemplate = """/*
  6. * Copyright 2010-present Facebook.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * This is is a generated file during the SDK build process.
  21. * Please do not hand edit this file.
  22. * It can be rebuilt by running
  23. *
  24. * ./audio_to_code.py %(args)s
  25. *
  26. */
  27. """
  28. def bytes_from_file(filename, chunksize=8192):
  29. with open(filename, "rb") as f:
  30. while True:
  31. chunk = f.read(chunksize)
  32. if chunk:
  33. for b in chunk:
  34. yield b
  35. else:
  36. break
  37. def write_header_file(header, className, outputFile):
  38. with open(outputFile, "w") as f:
  39. f.write(header)
  40. f.write("""
  41. #import <Foundation/Foundation.h>
  42. #import "FBAudioResourceLoader.h"
  43. """)
  44. f.write("@interface " + className + " : FBAudioResourceLoader\n")
  45. f.write("@end")
  46. def write_implementation_file(inputFile, header, className, outputFile):
  47. formattedBytes = ["0x{0:02x}".format(ord(x)) for x in bytes_from_file(inputFile)]
  48. with open(outputFile, "w") as f:
  49. f.write(header)
  50. f.write("\n")
  51. f.write("#import \"" + className + ".h\"\n\n")
  52. f.write("@implementation " + className + "\n\n")
  53. f.write("+ (NSString *)name\n");
  54. f.write("{\n");
  55. f.write(" return @\"" + os.path.basename(inputFile) + "\";\n")
  56. f.write("}\n\n");
  57. f.write("+ (NSData *)data\n")
  58. f.write("{\n")
  59. f.write(" const Byte bytes[] = {\n")
  60. f.write(", ".join(formattedBytes))
  61. f.write(" };\n")
  62. f.write(" NSUInteger length = sizeof(bytes) / sizeof(Byte);\n")
  63. f.write(" return [NSData dataWithBytesNoCopy:(void *)bytes length:length freeWhenDone:NO];\n")
  64. f.write("}\n\n")
  65. f.write("@end\n")
  66. def usage(exitCode):
  67. print 'audio_to_code.py -i <inputFile> -c <class> -o <outputDir>'
  68. sys.exit(exitCode)
  69. def main(argv):
  70. inputFile = ''
  71. outputClass = ''
  72. outputDir = ''
  73. try:
  74. opts, args = getopt.getopt(argv,"hi:c:o:")
  75. except getopt.GetoptError:
  76. usage(2)
  77. for opt, arg in opts:
  78. if opt == '-h':
  79. usage(0)
  80. elif opt == '-i':
  81. inputFile = arg
  82. elif opt == '-c':
  83. outputClass = arg
  84. elif opt in '-o':
  85. outputDir = arg
  86. if not inputFile:
  87. print 'inputFile is required.'
  88. usage(2)
  89. if not outputClass:
  90. print 'outputFile is required.'
  91. usage(2)
  92. if not outputDir:
  93. print 'outputDir is required.'
  94. usage(2)
  95. # Build file headers
  96. header = headerTemplate % {"args" : " ".join(argv)}
  97. # outputClass needs to add WAV as part of it
  98. outputClass = outputClass + "WAV"
  99. # Build the output base filename
  100. outputFileBase = outputDir + "/" + outputClass
  101. # Build .h file
  102. outputFile = outputFileBase + ".h"
  103. write_header_file(header, outputClass, outputFile)
  104. # Build .m file
  105. outputFile = outputFileBase + ".m"
  106. write_implementation_file(inputFile, header, outputClass, outputFile)
  107. if __name__ == "__main__":
  108. main(sys.argv[1:])