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.

94 lines
3.1 KiB

  1. # coding=utf-8
  2. import os
  3. import re
  4. import zipfile
  5. replaceFile = [
  6. "./public/install/install_6.0.sh",
  7. "./public/install/update_panel.sh",
  8. "./public/install/update6.sh",
  9. "./public/win/install/panel_update.py",
  10. "./public/win/panel/data/setup.py",
  11. "./public/win/panel/data/api.py",
  12. ]
  13. replaceZipDir = [
  14. "./public/install/src/",
  15. "./public/install/update/",
  16. "./public/win/panel/",
  17. ]
  18. originalDomain = "www.example.com"
  19. originalDomainSSL = {
  20. "http://www.example.com": "https://",
  21. "http:\/\/www.example.com": "https:\/\/",
  22. }
  23. isSSL = False
  24. def replaceStringInFile(filePath, newString):
  25. with open(filePath, "r+", encoding="utf-8") as f:
  26. fileContent = f.read()
  27. if isSSL:
  28. for key, value in originalDomainSSL.items():
  29. fileContent = fileContent.replace(key, value+newString)
  30. else:
  31. fileContent = fileContent.replace(originalDomain, newString)
  32. f.seek(0)
  33. f.write(fileContent)
  34. f.truncate()
  35. def replaceStringInZip(zipPath, newString):
  36. if zipPath.endswith(".zip"):
  37. oldZipPath = zipPath + ".old"
  38. os.rename(zipPath, oldZipPath)
  39. with zipfile.ZipFile(oldZipPath) as inZip, zipfile.ZipFile(zipPath, "w") as outZip:
  40. for inZipInfo in inZip.infolist():
  41. with inZip.open(inZipInfo) as inFile:
  42. if (inZipInfo.filename.endswith(".py") or inZipInfo.filename.endswith(".sh")) and inZipInfo.filename != "panel/class/sewer/cli.py":
  43. data = inFile.read()
  44. if isSSL:
  45. for key, value in originalDomainSSL.items():
  46. data = data.replace(key.encode("utf-8"), (value + newString).encode("utf-8"))
  47. else:
  48. data = data.replace(originalDomain.encode("utf-8"), newString.encode("utf-8"))
  49. outZip.writestr(inZipInfo, data)
  50. else:
  51. outZip.writestr(inZipInfo, inFile.read())
  52. os.remove(oldZipPath)
  53. if __name__ == '__main__':
  54. newDomain = input("Please enter your domain(www.aaaa.com): ")
  55. if not re.match("^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*$", newDomain):
  56. print("Domain format error!")
  57. exit()
  58. print("Your domain is: " + newDomain)
  59. ssl = input("Is your domain SSL?(y/n): ")
  60. if ssl == "y":
  61. isSSL = True
  62. elif ssl == "n":
  63. isSSL = False
  64. else:
  65. print("Input error!")
  66. exit()
  67. projectPath = os.path.abspath(__file__+"/../..")
  68. print("Your project path is: " + projectPath)
  69. for aFilePath in replaceFile:
  70. aFileFullPath = os.path.abspath(projectPath+aFilePath)
  71. replaceStringInFile(aFileFullPath, newDomain)
  72. print("Single file replace done. wait for zip file replace...")
  73. for aZipDir in replaceZipDir:
  74. if os.path.exists(aZipDir):
  75. for aZipFile in os.listdir(aZipDir):
  76. if aZipFile.endswith(".zip"):
  77. print(os.path.abspath(projectPath+aZipDir+aZipFile))
  78. replaceStringInZip(aZipDir+aZipFile, newDomain)
  79. print("All done.")