config_stub.py 973 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import re
  2. import sys
  3. import os
  4. APP_CONFIG_FILE = sys.argv[1]
  5. APP_KEY = sys.argv[2]
  6. BASE_URL = sys.argv[3]
  7. # Stub a string with a evironment variable
  8. def stubString(searchPattern, rejectPattern, searchtText, replacement, desc):
  9. searchRet = re.search(searchPattern, searchtText).group()
  10. if re.search("\"{}\"".format(rejectPattern), searchRet) is not None:
  11. print
  12. "A valid {} is submitted!".format(desc)
  13. sys.exit(1)
  14. # Stub valid key and write back
  15. ret = re.sub('\"\S+\"', "\"{}\"".format(replacement), searchRet)
  16. ret = re.sub(searchPattern, ret, searchtText)
  17. return ret
  18. # Read heaer file
  19. io = open(APP_CONFIG_FILE, "r+")
  20. text = io.read()
  21. ret = stubString("std::string\sappKey\s?=\s?\"\S*\";", "^[a-f0-9]{32}$", text, APP_KEY, 'APP_KEY')
  22. ret = stubString("std::string\sbaseURL\s?=\s?\"\S*\";",
  23. "(?:rtmp:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+", ret,
  24. BASE_URL, 'BASE_URL')
  25. io.seek(0)
  26. io.write(ret)
  27. io.truncate()
  28. io.close()