convert-srcuri.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. #
  3. # Conversion script to update SRC_URI to add branch to git urls
  4. #
  5. # Copyright (C) 2021 Richard Purdie
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. import re
  10. import os
  11. import sys
  12. import tempfile
  13. import shutil
  14. import mimetypes
  15. if len(sys.argv) < 2:
  16. print("Please specify a directory to run the conversion script against.")
  17. sys.exit(1)
  18. def processfile(fn):
  19. def matchline(line):
  20. if "MIRROR" in line or ".*" in line or "GNOME_GIT" in line:
  21. return False
  22. return True
  23. print("processing file '%s'" % fn)
  24. try:
  25. if "distro_alias.inc" in fn or "linux-yocto-custom.bb" in fn:
  26. return
  27. fh, abs_path = tempfile.mkstemp()
  28. modified = False
  29. with os.fdopen(fh, 'w') as new_file:
  30. with open(fn, "r") as old_file:
  31. for line in old_file:
  32. if ("git://" in line or "gitsm://" in line) and "branch=" not in line and matchline(line):
  33. if line.endswith('"\n'):
  34. line = line.replace('"\n', ';branch=master"\n')
  35. elif re.search('\s*\\\\$', line):
  36. line = re.sub('\s*\\\\$', ';branch=master \\\\', line)
  37. modified = True
  38. if ("git://" in line or "gitsm://" in line) and "github.com" in line and "protocol=https" not in line and matchline(line):
  39. if "protocol=git" in line:
  40. line = line.replace('protocol=git', 'protocol=https')
  41. elif line.endswith('"\n'):
  42. line = line.replace('"\n', ';protocol=https"\n')
  43. elif re.search('\s*\\\\$', line):
  44. line = re.sub('\s*\\\\$', ';protocol=https \\\\', line)
  45. modified = True
  46. new_file.write(line)
  47. if modified:
  48. shutil.copymode(fn, abs_path)
  49. os.remove(fn)
  50. shutil.move(abs_path, fn)
  51. except UnicodeDecodeError:
  52. pass
  53. ourname = os.path.basename(sys.argv[0])
  54. ourversion = "0.1"
  55. if os.path.isfile(sys.argv[1]):
  56. processfile(sys.argv[1])
  57. sys.exit(0)
  58. for targetdir in sys.argv[1:]:
  59. print("processing directory '%s'" % targetdir)
  60. for root, dirs, files in os.walk(targetdir):
  61. for name in files:
  62. if name == ourname:
  63. continue
  64. fn = os.path.join(root, name)
  65. if os.path.islink(fn):
  66. continue
  67. if "/.git/" in fn or fn.endswith(".html") or fn.endswith(".patch") or fn.endswith(".m4") or fn.endswith(".diff"):
  68. continue
  69. processfile(fn)
  70. print("All files processed with version %s" % ourversion)