Skip to content

Commit

Permalink
Reodering to alphabetical for release, tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemccoll committed Aug 15, 2016
1 parent 4b74643 commit 18e0e4e
Show file tree
Hide file tree
Showing 7 changed files with 573 additions and 584 deletions.
2 changes: 1 addition & 1 deletion gitnet/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class MergeError(gitnetException):

class GraphStatsError(gitnetException):
"""
Exception given when the statistics are not generated as expected.
Exception given when the network graph statistics are not generated as expected.
"""
pass
98 changes: 47 additions & 51 deletions gitnet/get_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,41 @@
# *********************************************************************************************

import bash as sh
import subprocess as sub
import os
import warnings
from gitnet.exceptions import RepositoryError, ParseError, InputError
from gitnet.commit_log import CommitLog
import subprocess as sub


def retrieve_commits(path, mode="stat"):
def get_log(path, mode="stat", commit_source="local git"):
"""
Takes a file path string and a mode string and produces the git log for the
specified directory. The default mode, "stat" retrieves the logs by running "git log --stat".
Modes include: "basic" (git log), "raw" ("git log --raw"), and "stat" ("git log --stat").
A function for gathering data from a local Git repository.
**Parameters** :
> *path* : `string`
>> A string identifying the path to the target git repository.
>> A string containing the path of the Git repository.
> *mode* : `string`
>> A string identifying the git log mode to be retrieved. Default mode is "stat".
>> The retrieval mode. Modes currently implemented: "basic", "raw", "stat".
**Return** :
> *commit_source* : `string`
> Returns a large string containing the raw output from the repository's git log.
>> The source of the Git repository, local is currently the only option.
"""
print("Attempting local git log retrieval...")
# Log command modes, referenced by "mode" input.
log_commands = {"basic": "git log", "raw": "git log --raw", "stat":"git log --stat"}
if mode not in log_commands.keys():
raise InputError("{} is not a valid retrieval mode.".format(mode))
# Save the current directory. Navigate to new directory. Retrieve logs. Return to original directory.
work_dir = os.getcwd()
os.chdir(path)
raw_logs = sh.bash(log_commands[mode]).stdout.decode("utf-8")
os.chdir(work_dir)
# If the retrieval was unsuccessful, raise an error.
if len(raw_logs) == 0:
print("Raising error.")
if "true" in str(sh.bash("git rev-parse --is-inside-work-tree").stdout):
raise RepositoryError("{} is not a Git repository.".format(path))
else:
raise RepositoryError("{} has no commits.".format(path))
# If the retrieval was successful, print a summary."
print("Got {} characters from: {}".format(len(raw_logs), path))
# Record the retrieval mode.
raw_logs = "Mode =\n{}\n".format(mode) + raw_logs
return raw_logs
**Returns** : `Commitlog`
"""
if commit_source == "local git":
detect_key = "hash"
else:
detect_key = "unknown"
return CommitLog(dofd=parse_commits(retrieve_commits(path, mode)),
source=commit_source,
path=path,
key_type=detect_key)

def identify(s):
"""
Expand Down Expand Up @@ -117,7 +101,6 @@ def identify(s):
" so was identified as 'other'.".format(s))
return "none"


def parse_commits(commit_str):
"""
Parses a raw string containing a commit Log for a Git repository. It produces a dictionary
Expand Down Expand Up @@ -214,33 +197,46 @@ def parse_commits(commit_str):
warnings.warn("Parser was unable to identify {}. Identity string <{}> not recognized".format(line,id))
return collection


def get_log(path, mode="stat", commit_source="local git"):
def retrieve_commits(path, mode="stat"):
"""
A function for gathering data from a local Git repository.
Takes a file path string and a mode string and produces the git log for the
specified directory. The default mode, "stat" retrieves the logs by running "git log --stat".
Modes include: "basic" (git log), "raw" ("git log --raw"), and "stat" ("git log --stat").
**Parameters** :
> *path* : `string`
>> A string containing the path of the Git repository.
>> A string identifying the path to the target git repository.
> *mode* : `string`
>> The retrieval mode. Modes currently implemented: "basic", "raw", "stat".
> *commit_source* : `string`
>> A string identifying the git log mode to be retrieved. Default mode is "stat".
>> The source of the Git repository, local is currently the only option.
**Return** :
**Returns** : `Commitlog`
> Returns a large string containing the raw output from the repository's git log.
"""
if commit_source == "local git":
detect_key = "hash"
else:
detect_key = "unknown"
return CommitLog(dofd=parse_commits(retrieve_commits(path, mode)),
source=commit_source,
path=path,
key_type=detect_key)
print("Attempting local git log retrieval...")
# Log command modes, referenced by "mode" input.
log_commands = {"basic": "git log", "raw": "git log --raw", "stat":"git log --stat"}
if mode not in log_commands.keys():
raise InputError("{} is not a valid retrieval mode.".format(mode))
# Save the current directory. Navigate to new directory. Retrieve logs. Return to original directory.
work_dir = os.getcwd()
os.chdir(path)
raw_logs = sh.bash(log_commands[mode]).stdout.decode("utf-8")
os.chdir(work_dir)
# If the retrieval was unsuccessful, raise an error.
if len(raw_logs) == 0:
print("Raising error.")
if "true" in str(sh.bash("git rev-parse --is-inside-work-tree").stdout):
raise RepositoryError("{} is not a Git repository.".format(path))
else:
raise RepositoryError("{} has no commits.".format(path))
# If the retrieval was successful, print a summary."
print("Got {} characters from: {}".format(len(raw_logs), path))
# Record the retrieval mode.
raw_logs = "Mode =\n{}\n".format(mode) + raw_logs
return raw_logs
12 changes: 6 additions & 6 deletions gitnet/gitnet_tests/coverage_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Name Stmts Miss Cover Missin
C:\Users\User\Desktop\gitnet\gitnet\__init__.py 6 0 100%
C:\Users\User\Desktop\gitnet\gitnet\commit_log.py 113 3 97% 107-108, 163
C:\Users\User\Desktop\gitnet\gitnet\exceptions.py 14 0 100%
C:\Users\User\Desktop\gitnet\gitnet\get_log.py 103 14 86% 62, 111-118, 158, 160, 208-214, 242
C:\Users\User\Desktop\gitnet\gitnet\get_log.py 103 14 86% 48, 95-102, 141, 143, 191-197, 237
test_commit_log.py 259 0 100%
test_get.py 91 1 99% 48
test_helpers.py 201 0 100%
test_log.py 678 0 100%
test_netgen.py 126 0 100%
test_network.py 524 1 99% 659
C:\Users\User\Desktop\gitnet\gitnet\helpers.py 132 2 98% 55, 385
C:\Users\User\Desktop\gitnet\gitnet\log.py 383 13 97% 200, 248-250, 252, 445, 485, 692, 709, 820, 822, 826, 828
C:\Users\User\Desktop\gitnet\gitnet\multigraph.py 201 4 98% 148-151, 254
test_network.py 524 1 99% 657
C:\Users\User\Desktop\gitnet\gitnet\helpers.py 132 2 98% 54, 378
C:\Users\User\Desktop\gitnet\gitnet\log.py 383 13 97% 150, 236-238, 240, 499, 539, 722, 724, 728, 730, 801, 818
C:\Users\User\Desktop\gitnet\gitnet\multigraph.py 203 2 99% 315, 478
---------------------------------------------------------------------------------
TOTAL 2831 38 99%
TOTAL 2833 36 99%

## 9 lines will not be run with our tests:
- log.py line 188,189,190,192
Expand Down
4 changes: 2 additions & 2 deletions gitnet/gitnet_tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def test_basic_fn(self):
# Check that a file exists where expected
self.assertTrue(os.path.exists(self.path))
# Check a summary string is produced
self.assertEqual("Data written to temp.tsv", self.tsv_str)
self.assertIn("Data written to temp.tsv", self.tsv_str)

def test_basic_nofn(self):
"""Is no file produced but a string given? """
Expand All @@ -506,7 +506,7 @@ def test_empty_cols(self):
# Checking file is created
self.assertTrue(os.path.exists(self.path))
# Check a summary string is produced
self.assertEqual("Data written to temp.tsv", tsv_str)
self.assertIn("Data written to temp.tsv", tsv_str)

def test_warnings(self):
# Warning occurs non string values are forced to strings
Expand Down
54 changes: 23 additions & 31 deletions gitnet/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import re
from gitnet.exceptions import InputError


# Working with Git Log date strings
def datetime_git(s):
"""
Expand Down Expand Up @@ -56,7 +55,6 @@ def datetime_reference(s):
"(e.g. 'Mon Apr 18 00:59:02 2016 -0400') or a datetime object.")
return ref_date


# Filtering functions.
def filter_since(s, match):
"""
Expand Down Expand Up @@ -207,8 +205,30 @@ def filter_has(x,match):
except TypeError:
return False

def list_to_scd(lst):
"""
Produces a string which joins the items of the list by semicolons. Non-string items are converted to strings
prior to joining.
**Parameters**
>*lst* : `list`
>> A list of items which are either strings or objects which can be converted to strings using `str()`
**Return** `str`
> A String which includes each item within `lst`, separated by semicolons.
"""
new_lst = []
for i in lst:
if not isinstance(i, str):
new_lst.append(str(i))
else:
new_lst.append(i)

string = ';'.join(new_lst)

return string

# Working with lists.
def most_common(lst, n=1):
"""
Produces a list containing the n most common entries (occurring more than once) in a list. If the nth most common
Expand Down Expand Up @@ -257,7 +277,6 @@ def most_common(lst, n=1):

return sorted(ret_list, reverse=True)


def most_occurrences(lst):
"""
Produces the number of times the most common value appears.
Expand Down Expand Up @@ -285,32 +304,6 @@ def most_occurrences(lst):
m_common.append(i)
return max


def list_to_scd(lst):
"""
Produces a string which joins the items of the list by semicolons. Non-string items are converted to strings
prior to joining.
**Parameters**
>*lst* : `list`
>> A list of items which are either strings or objects which can be converted to strings using `str()`
**Return** `str`
> A String which includes each item within `lst`, separated by semicolons.
"""
new_lst = []
for i in lst:
if not isinstance(i, str):
new_lst.append(str(i))
else:
new_lst.append(i)

string = ';'.join(new_lst)

return string


# Network Edge Generator Functions
def net_edges_simple(v1, v2, record, keep):
"""
Expand Down Expand Up @@ -386,7 +379,6 @@ def net_edges_changes(v1, v2, record, keep):
properties["weight"] = int(weight)
return (v1, v2, properties)


# Network Attribute Helper Functions
def node_colours(d):
"""
Expand Down
Loading

0 comments on commit 18e0e4e

Please sign in to comment.