Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_release.py 12KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # -*- test-case-name: twisted.python.test.test_release -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Twisted's automated release system.
  6. This module is only for use within Twisted's release system. If you are anyone
  7. else, do not use it. The interface and behaviour will change without notice.
  8. Only Linux is supported by this code. It should not be used by any tools
  9. which must run on multiple platforms (eg the setup.py script).
  10. """
  11. import os
  12. import sys
  13. from subprocess import STDOUT, CalledProcessError, check_output
  14. from typing import Dict
  15. from zope.interface import Interface, implementer
  16. from twisted.python.compat import execfile
  17. # Types of newsfragments.
  18. NEWSFRAGMENT_TYPES = ["doc", "bugfix", "misc", "feature", "removal"]
  19. def runCommand(args, **kwargs):
  20. """Execute a vector of arguments.
  21. This is a wrapper around L{subprocess.check_output}, so it takes
  22. the same arguments as L{subprocess.Popen} with one difference: all
  23. arguments after the vector must be keyword arguments.
  24. @param args: arguments passed to L{subprocess.check_output}
  25. @param kwargs: keyword arguments passed to L{subprocess.check_output}
  26. @return: command output
  27. @rtype: L{bytes}
  28. """
  29. kwargs["stderr"] = STDOUT
  30. return check_output(args, **kwargs)
  31. class IVCSCommand(Interface):
  32. """
  33. An interface for VCS commands.
  34. """
  35. def ensureIsWorkingDirectory(path):
  36. """
  37. Ensure that C{path} is a working directory of this VCS.
  38. @type path: L{twisted.python.filepath.FilePath}
  39. @param path: The path to check.
  40. """
  41. def isStatusClean(path):
  42. """
  43. Return the Git status of the files in the specified path.
  44. @type path: L{twisted.python.filepath.FilePath}
  45. @param path: The path to get the status from (can be a directory or a
  46. file.)
  47. """
  48. def remove(path):
  49. """
  50. Remove the specified path from a the VCS.
  51. @type path: L{twisted.python.filepath.FilePath}
  52. @param path: The path to remove from the repository.
  53. """
  54. def exportTo(fromDir, exportDir):
  55. """
  56. Export the content of the VCSrepository to the specified directory.
  57. @type fromDir: L{twisted.python.filepath.FilePath}
  58. @param fromDir: The path to the VCS repository to export.
  59. @type exportDir: L{twisted.python.filepath.FilePath}
  60. @param exportDir: The directory to export the content of the
  61. repository to. This directory doesn't have to exist prior to
  62. exporting the repository.
  63. """
  64. @implementer(IVCSCommand)
  65. class GitCommand:
  66. """
  67. Subset of Git commands to release Twisted from a Git repository.
  68. """
  69. @staticmethod
  70. def ensureIsWorkingDirectory(path):
  71. """
  72. Ensure that C{path} is a Git working directory.
  73. @type path: L{twisted.python.filepath.FilePath}
  74. @param path: The path to check.
  75. """
  76. try:
  77. runCommand(["git", "rev-parse"], cwd=path.path)
  78. except (CalledProcessError, OSError):
  79. raise NotWorkingDirectory(
  80. f"{path.path} does not appear to be a Git repository."
  81. )
  82. @staticmethod
  83. def isStatusClean(path):
  84. """
  85. Return the Git status of the files in the specified path.
  86. @type path: L{twisted.python.filepath.FilePath}
  87. @param path: The path to get the status from (can be a directory or a
  88. file.)
  89. """
  90. status = runCommand(["git", "-C", path.path, "status", "--short"]).strip()
  91. return status == b""
  92. @staticmethod
  93. def remove(path):
  94. """
  95. Remove the specified path from a Git repository.
  96. @type path: L{twisted.python.filepath.FilePath}
  97. @param path: The path to remove from the repository.
  98. """
  99. runCommand(["git", "-C", path.dirname(), "rm", path.path])
  100. @staticmethod
  101. def exportTo(fromDir, exportDir):
  102. """
  103. Export the content of a Git repository to the specified directory.
  104. @type fromDir: L{twisted.python.filepath.FilePath}
  105. @param fromDir: The path to the Git repository to export.
  106. @type exportDir: L{twisted.python.filepath.FilePath}
  107. @param exportDir: The directory to export the content of the
  108. repository to. This directory doesn't have to exist prior to
  109. exporting the repository.
  110. """
  111. runCommand(
  112. [
  113. "git",
  114. "-C",
  115. fromDir.path,
  116. "checkout-index",
  117. "--all",
  118. "--force",
  119. # prefix has to end up with a "/" so that files get copied
  120. # to a directory whose name is the prefix.
  121. "--prefix",
  122. exportDir.path + "/",
  123. ]
  124. )
  125. def getRepositoryCommand(directory):
  126. """
  127. Detect the VCS used in the specified directory and return a L{GitCommand}
  128. if the directory is a Git repository. If the directory is not git, it
  129. raises a L{NotWorkingDirectory} exception.
  130. @type directory: L{FilePath}
  131. @param directory: The directory to detect the VCS used from.
  132. @rtype: L{GitCommand}
  133. @raise NotWorkingDirectory: if no supported VCS can be found from the
  134. specified directory.
  135. """
  136. try:
  137. GitCommand.ensureIsWorkingDirectory(directory)
  138. return GitCommand
  139. except (NotWorkingDirectory, OSError):
  140. # It's not Git, but that's okay, eat the error
  141. pass
  142. raise NotWorkingDirectory(f"No supported VCS can be found in {directory.path}")
  143. class Project:
  144. """
  145. A representation of a project that has a version.
  146. @ivar directory: A L{twisted.python.filepath.FilePath} pointing to the base
  147. directory of a Twisted-style Python package. The package should contain
  148. a C{_version.py} file and a C{newsfragments} directory that contains a
  149. C{README} file.
  150. """
  151. def __init__(self, directory):
  152. self.directory = directory
  153. def __repr__(self) -> str:
  154. return f"{self.__class__.__name__}({self.directory!r})"
  155. def getVersion(self):
  156. """
  157. @return: A L{incremental.Version} specifying the version number of the
  158. project based on live python modules.
  159. """
  160. namespace: Dict[str, object] = {}
  161. directory = self.directory
  162. while not namespace:
  163. if directory.path == "/":
  164. raise Exception("Not inside a Twisted project.")
  165. elif not directory.basename() == "twisted":
  166. directory = directory.parent()
  167. else:
  168. execfile(directory.child("_version.py").path, namespace)
  169. return namespace["__version__"]
  170. def findTwistedProjects(baseDirectory):
  171. """
  172. Find all Twisted-style projects beneath a base directory.
  173. @param baseDirectory: A L{twisted.python.filepath.FilePath} to look inside.
  174. @return: A list of L{Project}.
  175. """
  176. projects = []
  177. for filePath in baseDirectory.walk():
  178. if filePath.basename() == "newsfragments":
  179. projectDirectory = filePath.parent()
  180. projects.append(Project(projectDirectory))
  181. return projects
  182. def replaceInFile(filename, oldToNew):
  183. """
  184. I replace the text `oldstr' with `newstr' in `filename' using science.
  185. """
  186. os.rename(filename, filename + ".bak")
  187. with open(filename + ".bak") as f:
  188. d = f.read()
  189. for k, v in oldToNew.items():
  190. d = d.replace(k, v)
  191. with open(filename + ".new", "w") as f:
  192. f.write(d)
  193. os.rename(filename + ".new", filename)
  194. os.unlink(filename + ".bak")
  195. class NoDocumentsFound(Exception):
  196. """
  197. Raised when no input documents are found.
  198. """
  199. def filePathDelta(origin, destination):
  200. """
  201. Return a list of strings that represent C{destination} as a path relative
  202. to C{origin}.
  203. It is assumed that both paths represent directories, not files. That is to
  204. say, the delta of L{twisted.python.filepath.FilePath} /foo/bar to
  205. L{twisted.python.filepath.FilePath} /foo/baz will be C{../baz},
  206. not C{baz}.
  207. @type origin: L{twisted.python.filepath.FilePath}
  208. @param origin: The origin of the relative path.
  209. @type destination: L{twisted.python.filepath.FilePath}
  210. @param destination: The destination of the relative path.
  211. """
  212. commonItems = 0
  213. path1 = origin.path.split(os.sep)
  214. path2 = destination.path.split(os.sep)
  215. for elem1, elem2 in zip(path1, path2):
  216. if elem1 == elem2:
  217. commonItems += 1
  218. else:
  219. break
  220. path = [".."] * (len(path1) - commonItems)
  221. return path + path2[commonItems:]
  222. class NotWorkingDirectory(Exception):
  223. """
  224. Raised when a directory does not appear to be a repository directory of a
  225. supported VCS.
  226. """
  227. class CheckNewsfragmentScript:
  228. """
  229. A thing for checking whether a checkout has a newsfragment.
  230. """
  231. def __init__(self, _print):
  232. self._print = _print
  233. def main(self, args):
  234. """
  235. Run the script.
  236. @type args: L{list} of L{str}
  237. @param args: The command line arguments to process. This must contain
  238. one string: the path to the root of the Twisted checkout.
  239. """
  240. if len(args) != 1:
  241. sys.exit("Must specify one argument: the Twisted checkout")
  242. encoding = sys.stdout.encoding or "ascii"
  243. location = os.path.abspath(args[0])
  244. branch = (
  245. runCommand([b"git", b"rev-parse", b"--abbrev-ref", "HEAD"], cwd=location)
  246. .decode(encoding)
  247. .strip()
  248. )
  249. # diff-filter=d to exclude deleted newsfiles (which will happen on the
  250. # release branch)
  251. r = (
  252. runCommand(
  253. [
  254. b"git",
  255. b"diff",
  256. b"--name-only",
  257. b"origin/trunk...",
  258. b"--diff-filter=d",
  259. ],
  260. cwd=location,
  261. )
  262. .decode(encoding)
  263. .strip()
  264. )
  265. if not r:
  266. self._print("On trunk or no diffs from trunk; no need to look at this.")
  267. sys.exit(0)
  268. files = r.strip().split(os.linesep)
  269. self._print("Looking at these files:")
  270. for change in files:
  271. self._print(change)
  272. self._print("----")
  273. if len(files) == 1:
  274. if files[0] == os.sep.join(["docs", "fun", "Twisted.Quotes"]):
  275. self._print("Quotes change only; no newsfragment needed.")
  276. sys.exit(0)
  277. newsfragments = []
  278. for change in files:
  279. if os.sep + "newsfragments" + os.sep in change:
  280. if "." in change and change.rsplit(".", 1)[1] in NEWSFRAGMENT_TYPES:
  281. newsfragments.append(change)
  282. if branch.startswith("release-"):
  283. if newsfragments:
  284. self._print("No newsfragments should be on the release branch.")
  285. sys.exit(1)
  286. else:
  287. self._print("Release branch with no newsfragments, all good.")
  288. sys.exit(0)
  289. if os.environ.get("GITHUB_HEAD_REF", "") == "pre-commit-ci-update-config":
  290. # The run was triggered by pre-commit.ci.
  291. if newsfragments:
  292. self._print(
  293. "No newsfragments should be present on an autoupdated branch."
  294. )
  295. sys.exit(1)
  296. else:
  297. self._print("Autoupdated branch with no newsfragments, all good.")
  298. sys.exit(0)
  299. for change in newsfragments:
  300. self._print("Found " + change)
  301. sys.exit(0)
  302. self._print("No newsfragment found. Have you committed it?")
  303. sys.exit(1)