blob: 805eb3f6e32e6ded4d5eb5c69bed5a988d859bf6 [file] [log] [blame]
# Copyright 2022 Google LLC
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# This script is used to find go dependencies
# of a go pacakge. It reads 'go.mod', 'vendor.mod'
# or 'vendor.conf' in the source code.
import os
import re
from chromite.lib import cros_build_lib
# REGEX_GO_MOD_DEP finds
# `require (
# go-pkg1 v1.2.3
# go-pkg2 v4.5.6 ...
# )` in go.mod or other mod file.
REGEX_GO_MOD_DEP = "require \((.*?)\)"
GO_MOD_DEP_FILE = ["go.mod", "vendor.mod", "vendor.conf"]
def get_go_dep(src_path):
res = set()
args = ["find", src_path, "-type", "f"]
result = cros_build_lib.run(args, stdout=True, encoding="utf-8")
files = result.stdout.splitlines()
for name in files:
basename = os.path.basename(name)
if basename not in GO_MOD_DEP_FILE:
continue
f = open(name, "r")
content = f.read()
match = re.findall(REGEX_GO_MOD_DEP, content, re.DOTALL)
for req in match:
deps = req.strip().split("\n")
for dep in deps:
# remove comments.
dep = dep.split("//")[0].strip()
if dep:
res.add(dep)
return list(res)