Skip to content

Commit 40de2ed

Browse files
committed
simplify our update import procedure
1 parent 82e2200 commit 40de2ed

File tree

2 files changed

+129
-57
lines changed

2 files changed

+129
-57
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ overlay: "32242e8837cea562c621c3d1772f26dfb6ebc56d" }
1+
{ "overlay": "3f462a6889cb1b8d0e5db3435e864b70887b0ba5" }

mx.graalpython/mx_graalpython.py

Lines changed: 128 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -738,77 +738,149 @@ def delete_self_if_testdownstream(args):
738738
shutil.rmtree(SUITE.dir, ignore_errors=True)
739739

740740

741-
def update_import(name, rev="origin/master", callback=None):
742-
primary = mx.primary_suite()
743-
dep_dir = mx.suite(name).vc_dir
741+
def update_import(name, suite_py, rev="origin/master"):
742+
mx_name = "mx." + name
743+
parent = os.path.join(SUITE.dir, "..")
744+
for dirpath,dirnames,filenames in os.walk(parent):
745+
if os.path.sep in os.path.relpath(dirpath, parent):
746+
dirnames.clear() # we're looking for siblings or sibling-subdirs
747+
elif name in dirnames:
748+
dep_dir = os.path.join(os.path.join(dirpath))
749+
break
750+
if not dep_dir:
751+
mx.warn("could not find suite %s to update" % name)
752+
return
744753
vc = mx.VC.get_vc(dep_dir)
745-
vc.pull(dep_dir, update=False)
754+
if rev != "HEAD":
755+
vc.pull(dep_dir, update=False)
746756
vc.update(dep_dir, rev=rev)
747757
tip = str(vc.tip(dep_dir)).strip()
748758
contents = None
749-
suitefile = os.path.join(primary.dir, "mx." + primary.name, "suite.py")
750-
with open(suitefile, 'r') as f:
759+
with open(suite_py, 'r') as f:
751760
contents = f.read()
752761
dep_re = re.compile(r"['\"]name['\"]:\s+['\"]%s['\"],\s+['\"]version['\"]:\s+['\"]([a-z0-9]+)['\"]" % name, re.MULTILINE)
753762
dep_match = dep_re.search(contents)
754763
if dep_match:
755764
start = dep_match.start(1)
756765
end = dep_match.end(1)
757766
assert end - start == len(tip)
758-
mx.update_file(suitefile, "".join([contents[:start], tip, contents[end:]]), showDiff=True)
759-
if callback:
760-
callback()
761-
else:
762-
mx.abort("%s not found in %s" % (name, suitefile))
767+
mx.update_file(suite_py, "".join([contents[:start], tip, contents[end:]]), showDiff=True)
763768

764769

765770
def update_import_cmd(args):
766-
"""Update our mx or overlay imports"""
767-
try:
768-
args.remove("--no-pull")
769-
except ValueError:
770-
rev = "origin/master"
771-
else:
772-
rev = "HEAD"
773-
if not args:
774-
args = ["truffle"]
775-
if "overlay" in args:
776-
mx.log("Updating overlays")
777-
dirs = os.listdir(os.path.join(SUITE.dir, ".."))
778-
for d in dirs:
779-
if d.startswith("graalpython"):
780-
d = os.path.join(SUITE.dir, "..", d)
781-
jsonnetfile = os.path.join(d, "ci.jsonnet")
782-
if not os.path.exists(jsonnetfile):
783-
continue
784-
overlaydir = os.path.join(d, "..", "ci-overlays")
785-
if not os.path.exists(overlaydir):
786-
mx.abort("Overlays must be next to repo")
787-
vc = mx.VC.get_vc(overlaydir)
788-
tip = str(vc.tip(overlaydir)).strip()
789-
with open(jsonnetfile, "w") as f:
790-
f.write('{ overlay: "%s" }\n' % tip)
791-
args.remove("overlay")
792-
if "sulong" in args:
793-
args.append("regex")
794-
if "regex" in args:
795-
args.append("sulong")
796-
if "truffle" in args:
797-
args.remove("truffle")
798-
args += ["sulong", "regex"]
799-
if "sulong" in args:
800-
join = os.path.join
801-
callback = lambda: shutil.copy(
802-
join(mx.dependency("SULONG_LEGACY").output, "include", "truffle.h"),
803-
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "truffle.h")
804-
) and shutil.copy(
805-
join(mx.dependency("SULONG_HOME").output, "include", "polyglot.h"),
806-
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "polyglot.h")
807-
)
771+
"""Update our imports"""
772+
join = os.path.join
773+
vc = SUITE.vc
774+
775+
current_branch = vc.active_branch(SUITE.dir)
776+
if current_branch == "master":
777+
mx.abort("updating imports should be done on a branch")
778+
if vc.isDirty(SUITE.dir):
779+
mx.abort("updating imports should be done on a clean branch")
780+
781+
suite_py_files = []
782+
local_names = []
783+
repos = []
784+
785+
# find all relevant other repos that may need updating
786+
for sibling in os.listdir(os.path.join(SUITE.dir, "..")):
787+
if sibling.startswith("graalpython"):
788+
dd = os.path.join(SUITE.dir, "..", sibling)
789+
jsonnetfile = os.path.join(dd, "ci.jsonnet")
790+
if os.path.exists(jsonnetfile):
791+
local_names.append(sibling)
792+
repos.append(dd)
793+
for dirpath,dirnames,filenames in os.walk(dd):
794+
mx_dirs = list(filter(lambda x: x.startswith("mx."), dirnames))
795+
if mx_dirs:
796+
dirnames[:] = mx_dirs # don't go deeper once we found some mx dirs
797+
dirnames[:] = list(filter(lambda x: not (x.startswith(".") or x.startswith("__")), dirnames))
798+
if "suite.py" in filenames:
799+
suite_py_files.append(join(dirpath, "suite.py"))
800+
801+
# make sure all other repos are clean and on the same branch
802+
for d in repos:
803+
if vc.isDirty(d):
804+
mx.abort("repo %s is not clean" % d)
805+
d_branch = vc.active_branch(d)
806+
if d_branch == current_branch:
807+
pass
808+
elif d_branch == "master":
809+
vc.set_branch(d, current_branch, with_remote=False)
810+
vc.git_command(d, ["checkout", current_branch], abortOnError=True)
811+
else:
812+
mx.abort("repo %s is not on master or on %s" % (d, current_branch))
813+
814+
# make sure we can update the overlays
815+
overlaydir = join(SUITE.dir, "..", "ci-overlays")
816+
if not os.path.exists(overlaydir):
817+
mx.abort("Overlays repo must be next to graalpython repo")
818+
vc = mx.VC.get_vc(overlaydir)
819+
if vc.isDirty(overlaydir):
820+
mx.abort("overlays repo must be clean")
821+
overlaybranch = vc.active_branch(overlaydir)
822+
if overlaybranch == "master":
823+
vc.pull(overlaydir)
824+
vc.set_branch(overlaydir, current_branch, with_remote=False)
825+
vc.git_command(overlaydir, ["checkout", current_branch], abortOnError=True)
826+
elif overlaybranch == current_branch:
827+
pass
808828
else:
809-
callback = None
810-
for name in set(args):
811-
update_import(name, rev=rev, callback=callback)
829+
mx.abort("overlays repo must be on master or branch %s" % current_branch)
830+
831+
# find all imports we might update
832+
imports_to_update = set()
833+
for suite_py in suite_py_files:
834+
dict = {}
835+
with open(suite_py) as f:
836+
exec(f.read(), dict, dict)
837+
for suite in dict["suite"].get("imports", {}).get("suites", []):
838+
import_name = suite["name"]
839+
if suite.get("version") and import_name not in local_names:
840+
imports_to_update.add(import_name)
841+
842+
# now update all imports
843+
for name in imports_to_update:
844+
for idx, suite_py in enumerate(suite_py_files):
845+
update_import(name, suite_py, rev=("HEAD" if idx else "origin/master"))
846+
847+
# copy files we inline from our imports
848+
shutil.copy(
849+
join(mx.dependency("SULONG_LEGACY").output, "include", "truffle.h"),
850+
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "truffle.h"))
851+
shutil.copy(
852+
join(mx.dependency("SULONG_HOME").output, "include", "polyglot.h"),
853+
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "polyglot.h"))
854+
shutil.copy(
855+
join(mx.suite("truffle").dir, "..", "common.json"),
856+
join(overlaydir, "python", "graal-common.json"))
857+
858+
repos_updated = []
859+
860+
# commit ci if dirty
861+
overlaytip = str(vc.tip(overlaydir)).strip()
862+
if vc.isDirty(overlaydir):
863+
vc.commit(overlaydir, "Update Python imports")
864+
overlaytip = str(vc.tip(overlaydir)).strip()
865+
repos_updated.append(overlaydir)
866+
867+
# update ci import in all our repos, commit the full update, and push verbosely
868+
prev_verbosity = mx._opts.very_verbose
869+
for repo in repos:
870+
jsonnetfile = os.path.join(repo, "ci.jsonnet")
871+
with open(jsonnetfile, "w") as f:
872+
f.write('{ "overlay": "%s" }\n' % overlaytip)
873+
if vc.isDirty(repo):
874+
vc.commit(repo, "Update imports")
875+
try:
876+
mx._opts.very_verbose = True
877+
vc.git_command(repo, ["push", "-u", "origin", "HEAD:%s" % current_branch], abortOnError=True)
878+
finally:
879+
mx._opts.very_verbose = prev_verbosity
880+
repos_updated.append(repo)
881+
882+
if repos_updated:
883+
mx.log("These repos were updated: " + ", ".join(repos_updated))
812884

813885

814886
def python_style_checks(args):

0 commit comments

Comments
 (0)