OpenMPCD
ProgramVersionDatabase.py
1 import os.path
2 import pygit2
3 import yaml
4 
5 class ProgramVersionDatabase:
6  """
7  Provides information on the versions of the OpenMPCD program.
8  """
9 
10  def __init__(self):
11  """
12  The constructor.
13 
14  This will require a file `.OpenMPCD/config/ProgramVersionDatabase.yaml`
15  to be readable, and contain in `repositoryPath` the path to the git
16  repository of the `OpenMPCD` project. The path may contain an initial '~'
17  character, which will be expanded to the user's home directory.
18  """
19 
20  configPath = \
21  os.path.expanduser("~/.OpenMPCD/config/ProgramVersionDatabase.yaml")
22 
23  config = yaml.safe_load(open(configPath, "r"))
24 
25  repoPath = os.path.expanduser(config["repositoryPath"])
26 
27  self.repository = pygit2.Repository(repoPath)
28 
29  self.knownBugs = {
30  1: "ee7ec551c6f6d038d2a1195f35e4da8264f7f8e5",
31  8: "62e287cb9d60906bf4b514291f42fc882d2d4f90",
32  15: "9daa26741fb622759b4b32bef542441b693c7e22",
33  18: "28b668cdf97e4a478c31bd17702bfacfddd2ed07",
34  19: "53af091fd877cb52dcbe150aeaa5a8e03ebb67ca",
35  }
36 
37 
39  """
40  Returns the current commit SHA1 id, with the string "+MODIFICATIONS"
41  appended if any of the currently tracked files have been modified, and
42  with the string "+UNTRACKED" appended if there are files which are not
43  tracked.
44  """
45 
46  modificationsString = "+MODIFICATIONS"
47  untrackedString = "+UNTRACKED"
48 
49  description = \
50  self.repository.describe(
51  describe_strategy = pygit2.GIT_DESCRIBE_ALL,
52  abbreviated_size = 100,
53  dirty_suffix = modificationsString)
54 
55  modified = False
56 
57 
58  if description.endswith(modificationsString):
59  modified = True
60  description = description[:-len(modificationsString)]
61 
62  commit = str(self.repository.revparse_single(description).id)
63 
64  ret = commit
65  if modified:
66  ret += modificationsString
67 
68  for path, flags in self.repository.status().items():
69  if flags & pygit2.GIT_STATUS_WT_NEW:
70  if not self.repository.path_is_ignored(path):
71  ret += untrackedString
72  break
73 
74  return ret
75 
76 
77  def commitContainsNoKnownBugs(self, commit):
78  """
79  Returns whether the specified `commit` contains no known bugs.
80  """
81 
82  for bugID, fixingCommit in self.knownBugs.items():
83  if fixingCommit is None:
84  return False
85 
86  if not self._gitCommitAContainsCommitB(commit, fixingCommit):
87  return False
88 
89  return True
90 
91 
93  """
94  Returns whether the current commit contains no known bugs.
95 
96  Untracked and unstaged changes are ignored for this function.
97  """
98 
100 
101 
102  def _getCurrentGitCommit(self):
103  """
104  Returns the commit ID of the current commit.
105 
106  Untracked and unstaged changes have no influence on the returned value.
107  """
108 
109  description = \
110  self.repository.describe(
111  describe_strategy = pygit2.GIT_DESCRIBE_ALL,
112  abbreviated_size = 100)
113 
114  return self.repository.revparse_single(description).id
115 
116 
117  def _gitCommitAContainsCommitB(self, commitA, commitB):
118  """
119  Returns whether the commit specified in `commitA` contains in its
120  history the commit specified in `commitB`.
121  """
122 
123  if not isinstance(commitB, pygit2.Oid):
124  commitB = pygit2.Oid(hex = commitB)
125 
126  for commit in self.repository.walk(commitA):
127  if commit.id == commitB:
128  return True
129 
130  return False
131 
132 
133  def _currentCommitContainsCommitB(self, commitB):
134  """
135  Returns whether the current commit conatins in its history `commitB`.
136  """
137 
138  return \
140  self._getCurrentGitCommit(), commitB)
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.returnCurrentRepositoryDescription
def returnCurrentRepositoryDescription(self)
Definition: ProgramVersionDatabase.py:47
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase._gitCommitAContainsCommitB
def _gitCommitAContainsCommitB(self, commitA, commitB)
Definition: ProgramVersionDatabase.py:128
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.__init__
def __init__(self)
Definition: ProgramVersionDatabase.py:20
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.repository
repository
Definition: ProgramVersionDatabase.py:29
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.commitContainsNoKnownBugs
def commitContainsNoKnownBugs(self, commit)
Definition: ProgramVersionDatabase.py:84
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase._getCurrentGitCommit
def _getCurrentGitCommit(self)
Definition: ProgramVersionDatabase.py:113
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.knownBugs
knownBugs
Definition: ProgramVersionDatabase.py:31
MPCDAnalysis.ProgramVersionDatabase.ProgramVersionDatabase.currentCommitContainsNoKnownBugs
def currentCommitContainsNoKnownBugs(self)
Definition: ProgramVersionDatabase.py:102