Skip to content

Commit

Permalink
#109 Update and refactor code
Browse files Browse the repository at this point in the history
Update tests to use `with` context manager
Update `Command::execute()` to use `with` context manager
Update analysis.py code logic
  • Loading branch information
ropapermaker committed Jun 4, 2023
1 parent 29642a2 commit 72c8f59
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
23 changes: 17 additions & 6 deletions pygount/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,11 @@ def __init__(

def close(self):
# Remove temp dir if exists.
if self._is_git_link and os.path.isdir(self._source_patterns):
# TODO#109 Here self.is_git_link always returns False
# so the temporary folder never gets deleted
if self.is_git_link and os.path.isdir(self._source_patterns[0]):
try:
rmtree(self._source_patterns)
rmtree(self._source_patterns[0])
except OSError as e:
print(f"Error: {e.filename} - {e.strerror}.")

Expand All @@ -536,6 +538,14 @@ def source_patterns(self):
def suffixes(self):
return self._suffixes

@property
def is_git_link(self):
return self._is_git_link

@is_git_link.setter
def is_git_link(self, value):
self._is_git_link = value

@property
def folder_regexps_to_skip(self):
return self._folder_regexps_to_skip
Expand All @@ -558,7 +568,7 @@ def name_regexps_to_skip(self, regexps_or_pattern_text):
def is_valid_git_repo(git_repo: str) -> bool:
# Regex to check valid GIT Repository
# from https://stackoverflow.com/questions/2514859/regular-expression-for-git-repository
git_regex = re.compile(r"((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?")
git_regex = re.compile(r"((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:\/\-~]+)(\.git)(\/)?")

if git_regex.match(git_repo):
return True
Expand All @@ -567,14 +577,15 @@ def is_valid_git_repo(git_repo: str) -> bool:

def _set_temp_path_from_git_repo(self, source_patterns):
if source_patterns != [] and self.is_valid_git_repo(source_patterns[0]):
self.is_git_link = True
temp_folder = mkdtemp()

# TODO#109 implement a faster version than 'git clone'
# TODO#109 option to clone from a single hash
git.Repo.clone_from("https://github.com/roskakori/pygount", temp_folder)
self._is_git_link = True
git.Repo.clone_from(source_patterns[0], temp_folder)
return [temp_folder]

# TODO#109 is this setting to False even needed
# self.is_git_link = False
return source_patterns

def _is_path_to_skip(self, name, is_folder):
Expand Down
50 changes: 26 additions & 24 deletions pygount/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,32 +321,34 @@ def apply_arguments(self, arguments=None):

def execute(self):
_log.setLevel(logging.INFO if self.is_verbose else logging.WARNING)
source_scanner = pygount.analysis.SourceScanner(
with pygount.analysis.SourceScanner(
self.source_patterns, self.suffixes, self.folders_to_skip, self.names_to_skip
)
source_paths_and_groups_to_analyze = list(source_scanner.source_paths())
duplicate_pool = pygount.analysis.DuplicatePool() if not self.has_duplicates else None
writer_class = _OUTPUT_FORMAT_TO_WRITER_CLASS_MAP[self.output_format]
is_stdout = self.output == "STDOUT"
target_context_manager = (
contextlib.nullcontext(sys.stdout) if is_stdout else open(self.output, "w", encoding="utf-8", newline="")
)
with target_context_manager as target_file, writer_class(target_file) as writer:
with Progress(disable=not writer.has_to_track_progress, transient=True) as progress:
try:
for source_path, group in progress.track(source_paths_and_groups_to_analyze):
writer.add(
pygount.analysis.SourceAnalysis.from_file(
source_path,
group,
self.default_encoding,
self.fallback_encoding,
generated_regexes=self._generated_regexs,
duplicate_pool=duplicate_pool,
) as source_scanner:
source_paths_and_groups_to_analyze = list(source_scanner.source_paths())
duplicate_pool = pygount.analysis.DuplicatePool() if not self.has_duplicates else None
writer_class = _OUTPUT_FORMAT_TO_WRITER_CLASS_MAP[self.output_format]
is_stdout = self.output == "STDOUT"
target_context_manager = (
contextlib.nullcontext(sys.stdout)
if is_stdout
else open(self.output, "w", encoding="utf-8", newline="")
)
with target_context_manager as target_file, writer_class(target_file) as writer:
with Progress(disable=not writer.has_to_track_progress, transient=True) as progress:
try:
for source_path, group in progress.track(source_paths_and_groups_to_analyze):
writer.add(
pygount.analysis.SourceAnalysis.from_file(
source_path,
group,
self.default_encoding,
self.fallback_encoding,
generated_regexes=self._generated_regexs,
duplicate_pool=duplicate_pool,
)
)
)
finally:
progress.stop()
finally:
progress.stop()


def pygount_command(arguments=None):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def test_can_find_python_files_in_dot(self):

def test_can_find_files_from_cloned_git_repo(self):
git_link = "https://github.com/roskakori/pygount.git"
scanner = analysis.SourceScanner([git_link])
actual_paths = list(scanner.source_paths())
assert actual_paths != []
with analysis.SourceScanner([git_link]) as scanner:
actual_paths = list(scanner.source_paths())
assert actual_paths != []


class AnalysisTest(unittest.TestCase):
Expand Down

0 comments on commit 72c8f59

Please sign in to comment.