func.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. #
  3. #Copyright (C) 2017 avideo authors (see AUTHORS)
  4. #
  5. # This file is part of AVideo.
  6. #
  7. # AVideo is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # AVideo is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with AVideo. If not, see <http://www.gnu.org/licenses/>.
  19. import re, os, sys
  20. from Patches import badcode
  21. GPL_HEADER = '''
  22. #
  23. # This file is part of Foobar.
  24. #
  25. # Foobar is free software: you can redistribute it and/or modify
  26. # it under the terms of the GNU General Public License as published by
  27. # the Free Software Foundation, either version 3 of the License, or
  28. # (at your option) any later version.
  29. #
  30. # Foobar is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU General Public License for more details.
  34. #
  35. # You should have received a copy of the GNU General Public License
  36. # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  37. '''
  38. def fixer(name, savere):
  39. gpl_header = GPL_HEADER.replace("Foobar", name)
  40. with open("./temp.txt", mode="r") as f:
  41. files = f.read().split("\n")
  42. for file in files:
  43. if file == "":
  44. continue
  45. newfile = file.replace("youtube-dl", name).replace("youtube_dl", name)
  46. os.renames(file, newfile)
  47. #Get file text if this is a text file- if not, we're done with it
  48. try:
  49. with open(newfile, mode="r") as f:
  50. ans = f.read()
  51. except UnicodeDecodeError:
  52. continue
  53. #Don't waste time or add snowflake exceptions...
  54. if ans == "":
  55. continue
  56. #"Protect" github.com/rg3/youtube-dl URLs which point to things that can't be copied (e.g. issue reports),
  57. #Then convert the URLs as listed at the top of this file, then "unprotect" (i.e. youtu`be --> youtube)
  58. while True:
  59. resp = savere.search(ans)
  60. if resp == None:
  61. break
  62. ans = ans.replace(resp.group(0), resp.group(0).replace("youtube-dl", "youtu`be-dl"))
  63. ans = ans.replace("://github.com/rg3/youtube-dl", "://notabug.org/GPast/%s" % name)
  64. ans = ans.replace("://rg3.github.io/youtube-dl", "://notabug.org/GPast/%s/wiki" % name)
  65. ans = ans.replace("://yt-dl.org", "://notabug.org/GPast/%s/wiki" % name)
  66. ans = ans.replace("youtube-dl", name)
  67. ans = ans.replace("YOUTUBE-DL", name.upper())
  68. ans = ans.replace("youtube\-dl", name)
  69. ans = ans.replace("YOUTUBE\-DL", name.upper())
  70. ans = ans.replace("youtube_dl", name)
  71. ans = ans.replace("Youtube-dl", name.capitalize())
  72. ans = ans.replace("Youtube\-dl", name.capitalize())
  73. ans = ans.replace("Youtube-DL", name.capitalize())
  74. ans = ans.replace("Youtube\-DL", name.capitalize())
  75. ans = ans.replace("youtu`be", "youtube")
  76. f = open(newfile, mode="w")
  77. #Also, add GPL spiel at top (below key pseudo-comments!) if this is code
  78. if newfile.endswith(".py") or newfile.endswith(".sh"):
  79. while True:
  80. if ans[0] != "#":
  81. f.write("\n# Copyright (C) 2017 avideo authors (see AUTHORS)\n")
  82. f.write(gpl_header)
  83. f.write(ans)
  84. break
  85. seg, ans = ans.split("\n", maxsplit=1)
  86. f.write(seg+"\n")
  87. else:
  88. f.write(ans)
  89. f.close()
  90. def wipe_code(src):
  91. ztdout = open("tmp-output", mode="w")
  92. for bcel in badcode.PATCHES:
  93. ztdout.write("\n\n./%s:\n" % bcel[0].upper())
  94. file, dic, n = src+bcel[0], bcel[1:], 0
  95. with open(file, mode="r") as f:
  96. edcode = f.read()
  97. for bc in dic:
  98. edsegs = edcode.split(bc[0])
  99. ztdout.write("%d: %d\n" % (n, len(edsegs)-1))
  100. edcode = bc[1].join(edsegs)
  101. n += 1
  102. with open(file, mode="w") as f:
  103. f.write(edcode)
  104. ztdout.close()
  105. if __name__ == "__main__":
  106. wipe_code(sys.argv[1])
  107. fixer(sys.argv[2], re.compile("://github\\.com/rg3/youtube-dl/[a-z]+/[0-9]+"))