Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame^] | 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 | |
| 10 | import re |
| 11 | import os |
| 12 | import sys |
| 13 | import tempfile |
| 14 | import shutil |
| 15 | import mimetypes |
| 16 | |
| 17 | if len(sys.argv) < 2: |
| 18 | print("Please specify a directory to run the conversion script against.") |
| 19 | sys.exit(1) |
| 20 | |
| 21 | def processfile(fn): |
| 22 | def matchline(line): |
| 23 | if "MIRROR" in line or ".*" in line or "GNOME_GIT" in line: |
| 24 | return False |
| 25 | return True |
| 26 | print("processing file '%s'" % fn) |
| 27 | try: |
| 28 | if "distro_alias.inc" in fn or "linux-yocto-custom.bb" in fn: |
| 29 | return |
| 30 | fh, abs_path = tempfile.mkstemp() |
| 31 | modified = False |
| 32 | with os.fdopen(fh, 'w') as new_file: |
| 33 | with open(fn, "r") as old_file: |
| 34 | for line in old_file: |
| 35 | if ("git://" in line or "gitsm://" in line) and "branch=" not in line and matchline(line): |
| 36 | if line.endswith('"\n'): |
| 37 | line = line.replace('"\n', ';branch=master"\n') |
| 38 | elif re.search('\s*\\\\$', line): |
| 39 | line = re.sub('\s*\\\\$', ';branch=master \\\\', line) |
| 40 | modified = True |
| 41 | if ("git://" in line or "gitsm://" in line) and "github.com" in line and "protocol=https" not in line and matchline(line): |
| 42 | if "protocol=git" in line: |
| 43 | line = line.replace('protocol=git', 'protocol=https') |
| 44 | elif line.endswith('"\n'): |
| 45 | line = line.replace('"\n', ';protocol=https"\n') |
| 46 | elif re.search('\s*\\\\$', line): |
| 47 | line = re.sub('\s*\\\\$', ';protocol=https \\\\', line) |
| 48 | modified = True |
| 49 | new_file.write(line) |
| 50 | if modified: |
| 51 | shutil.copymode(fn, abs_path) |
| 52 | os.remove(fn) |
| 53 | shutil.move(abs_path, fn) |
| 54 | except UnicodeDecodeError: |
| 55 | pass |
| 56 | |
| 57 | ourname = os.path.basename(sys.argv[0]) |
| 58 | ourversion = "0.1" |
| 59 | |
| 60 | if os.path.isfile(sys.argv[1]): |
| 61 | processfile(sys.argv[1]) |
| 62 | sys.exit(0) |
| 63 | |
| 64 | for targetdir in sys.argv[1:]: |
| 65 | print("processing directory '%s'" % targetdir) |
| 66 | for root, dirs, files in os.walk(targetdir): |
| 67 | for name in files: |
| 68 | if name == ourname: |
| 69 | continue |
| 70 | fn = os.path.join(root, name) |
| 71 | if os.path.islink(fn): |
| 72 | continue |
| 73 | if "/.git/" in fn or fn.endswith(".html") or fn.endswith(".patch") or fn.endswith(".m4") or fn.endswith(".diff"): |
| 74 | continue |
| 75 | processfile(fn) |
| 76 | |
| 77 | print("All files processed with version %s" % ourversion) |