python-package-folder 2.0.2__py3-none-any.whl → 2.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,6 +86,8 @@ class BuildManager:
86
86
  self.project_root, self.src_dir, exclude_patterns=exclude_patterns
87
87
  )
88
88
  self.subfolder_config: SubfolderBuildConfig | None = None
89
+ # Cache for package name lookups (expensive operation)
90
+ self._packages_distributions_cache: dict[str, list[str]] | None = None
89
91
 
90
92
  # Check if it's a valid Python package directory
91
93
  if not any(self.src_dir.glob("*.py")) and not (self.src_dir / "__init__.py").exists():
@@ -271,7 +273,9 @@ class BuildManager:
271
273
  # For subfolder builds, extract third-party dependencies and add to pyproject.toml
272
274
  if self._is_subfolder_build() and self.subfolder_config:
273
275
  # Re-analyze all Python files (including copied dependencies) to find third-party imports
276
+ print("Analyzing Python files for third-party dependencies...")
274
277
  all_python_files = analyzer.find_all_python_files(self.src_dir)
278
+ print(f"Found {len(all_python_files)} Python files to analyze")
275
279
  third_party_deps = self._extract_third_party_dependencies(all_python_files, analyzer)
276
280
  if third_party_deps:
277
281
  print(
@@ -394,6 +398,111 @@ class BuildManager:
394
398
  elif src_item.is_dir():
395
399
  self._copytree_excluding(src_item, dst_item)
396
400
 
401
+ def _get_package_name_from_import(self, module_name: str) -> str | None:
402
+ """
403
+ Get the actual PyPI package name from an import module name.
404
+
405
+ This handles cases where the import name differs from the package name
406
+ (e.g., 'import fitz' from 'pymupdf' package).
407
+
408
+ Args:
409
+ module_name: The module name from the import statement
410
+
411
+ Returns:
412
+ The actual package name, or None if not found
413
+ """
414
+ root_module = module_name.split(".")[0]
415
+ try:
416
+ # Try Python 3.10+ first (has packages_distributions)
417
+ import importlib.metadata as importlib_metadata
418
+
419
+ # Use packages_distributions() if available (Python 3.10+)
420
+ # Cache the result since it's expensive to call
421
+ if hasattr(importlib_metadata, "packages_distributions"):
422
+ if self._packages_distributions_cache is None:
423
+ # Cache the packages_distributions() result
424
+ self._packages_distributions_cache = importlib_metadata.packages_distributions()
425
+ packages_map = self._packages_distributions_cache
426
+ # packages_map is a dict mapping module names to list of distribution names
427
+ if root_module in packages_map:
428
+ # Return the first distribution name (usually there's only one)
429
+ dist_names = packages_map[root_module]
430
+ if dist_names:
431
+ return dist_names[0]
432
+
433
+ # Fallback: search all distributions (this can be slow, so limit search)
434
+ # Only check top-level package matches to speed up search
435
+ dist_count = 0
436
+ max_distributions_to_check = 1000 # Limit to prevent excessive searching
437
+ for dist in importlib_metadata.distributions():
438
+ dist_count += 1
439
+ if dist_count > max_distributions_to_check:
440
+ # Too many distributions, give up to avoid hanging
441
+ break
442
+ try:
443
+ # Check distribution name first (fast check)
444
+ dist_name = dist.metadata.get("Name", "")
445
+ # If distribution name matches or contains the module name, check files
446
+ if dist_name.lower().replace(
447
+ "-", "_"
448
+ ) == root_module.lower() or root_module.lower() in dist_name.lower().replace(
449
+ "-", "_"
450
+ ):
451
+ # Check if this distribution provides the module by looking at its files
452
+ files = dist.files or []
453
+ # Limit file checking to first 100 files per distribution
454
+ file_count = 0
455
+ for file in files:
456
+ file_count += 1
457
+ if file_count > 100:
458
+ break
459
+ file_str = str(file)
460
+ # Check if file is the module itself or in a package directory
461
+ if (
462
+ file.suffix == ".py"
463
+ and (file.stem == root_module or file.stem == "__init__")
464
+ ) or (
465
+ "/" in file_str
466
+ and (
467
+ file_str.startswith(f"{root_module}/")
468
+ or file_str.startswith(f"{root_module.replace('_', '-')}/")
469
+ )
470
+ ):
471
+ return dist.metadata["Name"]
472
+ except Exception:
473
+ continue
474
+
475
+ except ImportError:
476
+ try:
477
+ # Fallback for older Python versions
478
+ import importlib_metadata
479
+
480
+ # Search all distributions
481
+ for dist in importlib_metadata.distributions():
482
+ try:
483
+ files = dist.files or []
484
+ for file in files:
485
+ file_str = str(file)
486
+ if (
487
+ file.suffix == ".py"
488
+ and (file.stem == root_module or file.stem == "__init__")
489
+ ) or (
490
+ "/" in file_str
491
+ and (
492
+ file_str.startswith(f"{root_module}/")
493
+ or file_str.startswith(f"{root_module.replace('_', '-')}/")
494
+ )
495
+ ):
496
+ return dist.metadata["Name"]
497
+ except Exception:
498
+ continue
499
+ except ImportError:
500
+ pass
501
+ except Exception:
502
+ pass
503
+
504
+ return None
505
+
397
506
  def _extract_third_party_dependencies(
398
507
  self, python_files: list[Path], analyzer: ImportAnalyzer
399
508
  ) -> list[str]:
@@ -401,18 +510,25 @@ class BuildManager:
401
510
  Extract third-party package dependencies from Python files.
402
511
 
403
512
  Analyzes all Python files to find imports classified as "third_party"
404
- and returns a list of unique package names.
513
+ and returns a list of unique package names. Handles cases where the
514
+ import name differs from the package name (e.g., 'fitz' -> 'pymupdf').
405
515
 
406
516
  Args:
407
517
  python_files: List of Python file paths to analyze
408
518
  analyzer: ImportAnalyzer instance to use for classification
409
519
 
410
520
  Returns:
411
- List of unique third-party package names (e.g., ["pypdf", "requests"])
521
+ List of unique third-party package names (e.g., ["pypdf", "requests", "pymupdf"])
412
522
  """
413
523
  third_party_packages: set[str] = set()
524
+ # Cache package name lookups to avoid repeated expensive searches
525
+ package_name_cache: dict[str, str | None] = {}
526
+
527
+ total_files = len(python_files)
528
+ for idx, file_path in enumerate(python_files):
529
+ if idx > 0 and idx % 50 == 0:
530
+ print(f" Analyzing file {idx}/{total_files}...", end="\r", flush=True)
414
531
 
415
- for file_path in python_files:
416
532
  imports = analyzer.extract_imports(file_path)
417
533
  for imp in imports:
418
534
  analyzer.classify_import(imp, self.src_dir)
@@ -425,17 +541,38 @@ class BuildManager:
425
541
  if root_module in stdlib_modules:
426
542
  continue
427
543
 
428
- # If classified as third_party, add it
544
+ # If classified as third_party, try to get actual package name
429
545
  if imp.classification == "third_party":
430
- third_party_packages.add(root_module)
546
+ # Check cache first
547
+ if root_module not in package_name_cache:
548
+ package_name_cache[root_module] = self._get_package_name_from_import(
549
+ imp.module_name
550
+ )
551
+ actual_package = package_name_cache[root_module]
552
+ if actual_package:
553
+ third_party_packages.add(actual_package)
554
+ else:
555
+ # Fallback to using the import name
556
+ third_party_packages.add(root_module)
431
557
  # If it's ambiguous or unresolved, and not stdlib/local/external,
432
558
  # it's likely a third-party package that needs to be declared
433
559
  elif imp.classification == "ambiguous" or imp.classification is None:
434
560
  # Check if it's not a local or external module
435
561
  if not imp.resolved_path:
436
- # This is likely a third-party package that's not installed
437
- # in the build environment but needs to be declared
438
- third_party_packages.add(root_module)
562
+ # Check cache first
563
+ if root_module not in package_name_cache:
564
+ package_name_cache[root_module] = self._get_package_name_from_import(
565
+ imp.module_name
566
+ )
567
+ actual_package = package_name_cache[root_module]
568
+ if actual_package:
569
+ third_party_packages.add(actual_package)
570
+ else:
571
+ # Fallback: use import name (will be normalized later)
572
+ third_party_packages.add(root_module)
573
+
574
+ if total_files > 50:
575
+ print() # New line after progress indicator
439
576
 
440
577
  return sorted(list(third_party_packages))
441
578
 
@@ -590,17 +590,36 @@ class SubfolderBuildConfig:
590
590
  updated_content = self._add_dependencies_to_pyproject(content, dependencies)
591
591
  self.temp_pyproject.write_text(updated_content, encoding="utf-8")
592
592
 
593
+ def _normalize_package_name(self, package_name: str) -> str:
594
+ """
595
+ Normalize package name for PyPI.
596
+
597
+ Converts underscores to hyphens, as PyPI package names typically use hyphens
598
+ while Python import names use underscores (e.g., 'better_enum' -> 'better-enum').
599
+
600
+ Args:
601
+ package_name: Package name from import statement
602
+
603
+ Returns:
604
+ Normalized package name for PyPI
605
+ """
606
+ # Convert underscores to hyphens for PyPI package names
607
+ # This handles the common case where import names use underscores
608
+ # but PyPI package names use hyphens
609
+ return package_name.replace("_", "-")
610
+
593
611
  def _add_dependencies_to_pyproject(self, content: str, dependencies: list[str]) -> str:
594
612
  """
595
613
  Add dependencies to pyproject.toml content.
596
614
 
597
615
  Adds the specified dependencies to the [project] section's dependencies list.
598
616
  If dependencies already exist, merges them. If no dependencies section exists,
599
- creates one.
617
+ creates one. Package names are normalized (underscores -> hyphens) to match
618
+ PyPI naming conventions.
600
619
 
601
620
  Args:
602
621
  content: Current pyproject.toml content
603
- dependencies: List of dependency names to add
622
+ dependencies: List of dependency names to add (will be normalized)
604
623
 
605
624
  Returns:
606
625
  Updated pyproject.toml content with dependencies added
@@ -608,6 +627,9 @@ class SubfolderBuildConfig:
608
627
  if not dependencies:
609
628
  return content
610
629
 
630
+ # Normalize package names (convert underscores to hyphens for PyPI)
631
+ normalized_deps = [self._normalize_package_name(dep) for dep in dependencies]
632
+
611
633
  lines = content.split("\n")
612
634
  result = []
613
635
  in_project = False
@@ -631,8 +653,8 @@ class SubfolderBuildConfig:
631
653
  if line.strip().endswith("]"):
632
654
  in_dependencies = False
633
655
 
634
- # Merge with new dependencies
635
- all_deps = sorted(existing_deps | set(dependencies))
656
+ # Merge with new dependencies (normalized)
657
+ all_deps = sorted(existing_deps | set(normalized_deps))
636
658
 
637
659
  # Second pass: build result with dependencies
638
660
  in_project = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-package-folder
3
- Version: 2.0.2
3
+ Version: 2.0.5
4
4
  Summary: Python package to automatically package and build a folder, fetching all relevant dependencies.
5
5
  Project-URL: Repository, https://github.com/alelom/python-package-folder
6
6
  Author-email: Alessio Lombardi <work@alelom.com>
@@ -2,16 +2,16 @@ python_package_folder/__init__.py,sha256=DQt-uldOEKfh0MUqCvKdeNKOnpuOvpb7blYvXMy
2
2
  python_package_folder/__main__.py,sha256=a-__-VLhYw-J7S7CsHdhtEvQr3RiAZxiYDvKhKTgMX4,291
3
3
  python_package_folder/analyzer.py,sha256=w7hc2oyOoPK7tvlwcJDXnB3eiJsuGZc4BkOpTfZP7Vo,12257
4
4
  python_package_folder/finder.py,sha256=_LvJ9xBVKv41UK5sbwbNyKmuYjAOqUbzvZhK7NCYQF8,9130
5
- python_package_folder/manager.py,sha256=t3mbTnn42QCLWSOC5P_XRJPi1bL9hyYaCZVdITENHXk,33764
5
+ python_package_folder/manager.py,sha256=NER-sg-KiTshsnfpwLjsFVkFAbqNbH69gRK-4u4Br0s,40495
6
6
  python_package_folder/publisher.py,sha256=TSjdOvxvnWLbJCnduTK_xZBRfvsrq9kpEH-sfebeWkU,13507
7
7
  python_package_folder/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  python_package_folder/python_package_folder.py,sha256=RPsqRcIy_LjzzTHdp4qdtFJ4-4xhtR_0YLIC0RlUxFo,8841
9
- python_package_folder/subfolder_build.py,sha256=IfnXW5Xx8-WMuBc8hTdbCImDIuUAiHvVe1k5V2FbwoY,34339
9
+ python_package_folder/subfolder_build.py,sha256=LMiJ4Ck6PW1x3hmiSK-9fDH4Q6RrHqot4drx4lise3E,35310
10
10
  python_package_folder/types.py,sha256=3yeSRR5p_3PDKEAaehW_RJ7NwJHexOIeA08bGaT1iSY,2368
11
11
  python_package_folder/utils.py,sha256=lIkWsFKeAYAJ9TDUM99T4pUBHJVbUvCdUgkWQN-LUho,3111
12
12
  python_package_folder/version.py,sha256=kIDP6S9trEfs9gj7lBYGxrWm4RPssRla24UtlO9Jkh4,9111
13
- python_package_folder-2.0.2.dist-info/METADATA,sha256=1AFTide6hdpSNzjVFpLai2Mo-UYyfSSlA9hcx66QJBM,33282
14
- python_package_folder-2.0.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
- python_package_folder-2.0.2.dist-info/entry_points.txt,sha256=ttu4wAhoYSHGhWQNercLz9IVTTpXxhVlRA9vSTvaLe0,91
16
- python_package_folder-2.0.2.dist-info/licenses/LICENSE,sha256=vNgRJh8YiecqZoZld7TtwPI5I72HIymKD9g32fiJjCE,1073
17
- python_package_folder-2.0.2.dist-info/RECORD,,
13
+ python_package_folder-2.0.5.dist-info/METADATA,sha256=k44aRgDTztx-nSRrykq2BVtNDsXe2Mbftgm9cxPqY58,33282
14
+ python_package_folder-2.0.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
+ python_package_folder-2.0.5.dist-info/entry_points.txt,sha256=ttu4wAhoYSHGhWQNercLz9IVTTpXxhVlRA9vSTvaLe0,91
16
+ python_package_folder-2.0.5.dist-info/licenses/LICENSE,sha256=vNgRJh8YiecqZoZld7TtwPI5I72HIymKD9g32fiJjCE,1073
17
+ python_package_folder-2.0.5.dist-info/RECORD,,