Skip to content

Commit

Permalink
renamed module to pyprojen; fixed issue where pyprojen was deleting a…
Browse files Browse the repository at this point in the history
…ll files
  • Loading branch information
phitoduck committed Sep 27, 2024
1 parent 818f725 commit 52cfd84
Show file tree
Hide file tree
Showing 26 changed files with 49 additions and 20 deletions.
17 changes: 0 additions & 17 deletions src/pygen/cleanup.py

This file was deleted.

File renamed without changes.
45 changes: 45 additions & 0 deletions src/pyprojen/cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import json
from typing import List
import logging

FILE_MANIFEST = ".pyprojen/files.json"

def cleanup(dir: str, new_files: List[str], exclude: List[str]):
try:
manifest_files = get_files_from_manifest(dir)
if manifest_files:
# Use `FILE_MANIFEST` to remove files that are no longer managed by pyprojen
remove_files(find_orphaned_files(dir, manifest_files, new_files))
else:
# Remove all files managed by pyprojen with legacy logic
remove_files(find_generated_files(dir, exclude))
except Exception as e:
logging.warn(f"warning: failed to clean up generated files: {str(e)}")

def remove_files(files: List[str]):
for file in files:
try:
os.remove(file)
except Exception as e:
logging.warn(f"Failed to remove file {file}: {str(e)}")

def find_orphaned_files(dir: str, old_files: List[str], new_files: List[str]) -> List[str]:
return [os.path.join(dir, f) for f in old_files if f not in new_files]

def find_generated_files(dir: str, exclude: List[str]) -> List[str]:
# Implement this function to find generated files based on a marker
# This is a placeholder and should be implemented based on your specific needs
return []

def get_files_from_manifest(dir: str) -> List[str]:
try:
manifest_path = os.path.join(dir, FILE_MANIFEST)
if os.path.exists(manifest_path):
with open(manifest_path, 'r') as f:
manifest = json.load(f)
if 'files' in manifest:
return manifest['files']
except Exception as e:
logging.warn(f"warning: unable to get files to clean from file manifest: {str(e)}")
return []
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions src/pygen/project.py → src/pyprojen/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pyprojen.ignore_file import IgnoreFile
from pyprojen.common import FILE_MANIFEST
from pyprojen.json_file import JsonFile
from pyprojen.cleanup import cleanup
from pyprojen.cleanup import cleanup, FILE_MANIFEST

# from pyprojen.gitattributes import GitAttributesFile
# from pyprojen.tasks import Tasks
Expand Down Expand Up @@ -175,10 +175,11 @@ def synth(self):
Synthesize all project files into `outdir`.
"""
# Generate file manifest
JsonFile(self, FILE_MANIFEST, lambda: {"files": sorted(list(self._manifest_files))}, omit_empty=True)
manifest_files = sorted(list(self._manifest_files))
JsonFile(self, FILE_MANIFEST, lambda: {"files": manifest_files}, omit_empty=True)

# Cleanup orphaned files
cleanup(self.outdir, list(self._manifest_files), self._exclude_from_cleanup)
cleanup(self.outdir, manifest_files, self._exclude_from_cleanup)

# self.logger.debug("Synthesizing project...")
self.pre_synthesize()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 52cfd84

Please sign in to comment.