Skip to content

Commit

Permalink
Fix compare script (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Jun 26, 2022
1 parent 0141a84 commit cdf72b3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 51 deletions.
36 changes: 26 additions & 10 deletions hatch_jupyter_builder/migration/_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
builder_version = f"{builder_version.major}.{builder_version.minor - 1}.0"


print("Starting pyproject.toml migration")
print("\n\nStarting pyproject.toml migration")

warnings = []

Expand All @@ -39,7 +39,12 @@
data = tomli.loads(text)
requires = data["build-system"]["requires"]
requires = [
r for r in requires if not r.startswith("jupyter-packaging") and not r.startswith("setuptools")
r
for r in requires
if not r.startswith("jupyter-packaging")
and not r.startswith("setuptools")
and not r.startswith("jupyter_packaging")
and not r.startswith("wheel")
]

# Automatic migration from hatch.
Expand Down Expand Up @@ -145,19 +150,24 @@
build_kwargs = builder_table.setdefault("build-kwargs", {})
editable_build_command = None
if "build_cmd" in text:
match = re.search('build_cmd="(.*?)"', text, re.MULTILINE)
assert match is not None
editable_build_command = match.groups()[0]
match = re.search("build_cmd=['\"](.*?)['\"]", text, re.MULTILINE)
if match:
editable_build_command = match.groups()[0]

if "source_dir" in text or "build_dir" in text:
if "source_dir" in text or "build_dir" in text and editable_build_command:
builder_table["editable-build-kwargs"] = {}
editable_build_kwargs: dict = builder_table["editable-build-kwargs"]
if not editable_build_command:
editable_build_command = "!!! needs manual input !!!"
warnings.append(
"Fill in [tool.hatch.build.hooks.jupyter-builder.editable-build-kwargs][build_cmd]' in 'pyproject.toml', which was the 'build_cmd' argument to 'npm_builder' in 'setup.py'"
)
editable_build_kwargs["build_cmd"] = editable_build_command

for name in ["source_dir", "build_dir"]:
if name not in text:
continue
match = re.search(f'{name}="(.*?)"', text, re.MULTILINE)
match = re.search(f"{name}=['\"](.*?)['\"]", text, re.MULTILINE)
if match is not None:
editable_build_kwargs[name] = match.groups()[0]
else:
Expand All @@ -166,7 +176,12 @@
)
editable_build_kwargs[name] = "!!! needs manual input !!!"

elif editable_build_command:
elif editable_build_command or "build_cmd" in text:
if not editable_build_command:
editable_build_command = "!!! needs manual input !!!"
warnings.append(
"Fill in [tool.hatch.build.hooks.jupyter-builder.editable_build_cmd]' in 'pyproject.toml', which was the 'build_cmd' argument to 'npm_builder' in 'setup.py'"
)
build_kwargs["editable_build_cmd"] = editable_build_command

# Handle versioning with tbump - allows for static versioning and makes
Expand Down Expand Up @@ -217,12 +232,13 @@
os.remove(fname)

# Write out the new config.
print("Writing pyproject.toml")
print("\n\nWriting pyproject.toml")
pyproject.write_text(tomli_w.dumps(data), "utf-8")

if warnings:
print("\n\nWarning!! Not everything could be migrated automatically.")
print("Please address the following concerns:")
for warning in warnings:
print(f" - {warning}")
print("\n\n")

print("\n\nMigration complete!")
63 changes: 33 additions & 30 deletions hatch_jupyter_builder/migration/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
parser.add_argument(dest="target_dir", help="Target Directory")
parser.add_argument(dest="dist_name", help="Dist name")

args = parser.parse_args()

subprocess.check_call([sys.executable, "-m", "pip", "install", "build"])


def build_file(dirname):
def build_file(dirname, dist_name):
orig_dir = os.getcwd()
os.chdir(dirname)
if os.path.exists("dist"):
shutil.rmtree("dist")
subprocess.check_call([sys.executable, "-m", "build", f"--{args.dist_name}"])
subprocess.check_call([sys.executable, "-m", "build", f"--{dist_name}"])
os.chdir(orig_dir)


Expand All @@ -39,40 +35,47 @@ def get_zip_names(dirname):
return set(f.namelist())


def filter_file(path, root):
def filter_file(path):
if "egg-info" in path:
return True
full_path = os.path.join(path, root)
if os.path.isdir(full_path):
_, ext = os.path.splitext(path)
if not ext:
return True
if os.path.basename(path) in [path, "setup.py", "setup.cfg", "MANIFEST.in"]:
return True
return False


build_file(args.source_dir)
build_file(args.target_dir)
def main(source_dir, target_dir, dist_name):
subprocess.check_call([sys.executable, "-m", "pip", "install", "build"])

build_file(source_dir, dist_name)
build_file(target_dir, dist_name)

if dist_name == "sdist":
source_names = get_tar_names(source_dir)
target_names = get_tar_names(target_dir)
else:
source_names = get_zip_names(source_dir)
target_names = get_zip_names(target_dir)

removed = source_names - target_names
removed = [r for r in removed if not filter_file(r)]
if removed:
print("\nRemoved_files:")
[print(f) for f in removed]

if args.dist_name == "sdist":
source_names = get_tar_names(args.source_dir)
target_names = get_tar_names(args.target_dir)
else:
source_names = get_zip_names(args.source_dir)
target_names = get_zip_names(args.target_dir)
added = target_names - source_names
added = [a for a in added if not filter_file(a)]
if added:
print("\nAdded files:")
[print(f) for f in added]

removed = source_names - target_names
removed = [r for r in removed if not filter_file(r, args.source_dir)]
if removed:
print("\nRemoved_files:")
[print(f) for f in removed]
print()

added = target_names - source_names
added = [a for a in added if not filter_file(a, args.target_dir)]
if added:
print("\nAdded files:")
[print(f) for f in added]
return dict(added=added, removed=removed)

print()

if added or removed:
sys.exit(1)
if __name__ == "__main__":
args = parser.parse_args()
main(args.source_dir, args.target_dir, args.dist_name)
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ filterwarnings = [
"error"
]


[tool.coverage.run]
omit = [
"hatch_jupyter_builder/migration/*"
]

[tool.jupyter-releaser]
skip = ["check-links"]

Expand Down
2 changes: 1 addition & 1 deletion tests/data/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ artifacts = [

[tool.hatch.build.targets.sdist]
exclude = [
".github",
".github"
]

[tool.hatch.build.hooks.jupyter-builder]
Expand Down
30 changes: 20 additions & 10 deletions tests/test_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pytest
import tomli

from hatch_jupyter_builder.migration.compare import main

HERE = Path(__file__).parent.absolute()


Expand Down Expand Up @@ -40,16 +42,24 @@ def test_migration():
# Compare the produced wheel and sdist for the migrated and unmigrated
# extensions.
for asset in ["sdist", "wheel"]:
subprocess.check_call(
[
python,
"-m",
"hatch_jupyter_builder.migration.compare",
str(target1),
str(target2),
asset,
]
)
results = main(target1, target2, asset)

if asset == "sdist":
assert len(results["added"]) == 1
assert "static/remoteEntry." in results["added"][0]

assert len(results["removed"]) == 2
for item in results["removed"]:
assert ".eslintrc.js" in item or "static/remoteEntry." in item

else:
assert len(results["added"]) == 3
for item in results["added"]:
assert "static/remoteEntry." in item or "top_level.txt" in item

assert len(results["removed"]) == 3
for item in results["removed"]:
assert "entry_points.txt" in item or "static/remoteEntry." in item

# Check the produced dist file in strict mode.
dist_files = glob.glob(str(target1 / "dist/*.*"))
Expand Down

0 comments on commit cdf72b3

Please sign in to comment.