_release.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 zope.interface import Interface, implementer
  14. from subprocess import check_output, STDOUT, CalledProcessError
  15. from twisted.python.filepath import FilePath
  16. from twisted.python.monkey import MonkeyPatcher
  17. # Types of topfiles.
  18. TOPFILE_TYPES = ["doc", "bugfix", "misc", "feature", "removal"]
  19. intersphinxURLs = [
  20. "https://docs.python.org/2/objects.inv",
  21. "https://docs.python.org/3/objects.inv",
  22. "https://pyopenssl.readthedocs.io/en/stable/objects.inv",
  23. "https://hyperlink.readthedocs.io/en/stable/objects.inv",
  24. "https://twisted.github.io/constantly/docs/objects.inv",
  25. "https://twisted.github.io/incremental/docs/objects.inv",
  26. "https://python-hyper.org/h2/en/stable/objects.inv",
  27. "https://python-hyper.org/priority/en/stable/objects.inv",
  28. "https://docs.zope.org/zope.interface/objects.inv",
  29. ]
  30. def runCommand(args, **kwargs):
  31. """Execute a vector of arguments.
  32. This is a wrapper around L{subprocess.check_output}, so it takes
  33. the same arguments as L{subprocess.Popen} with one difference: all
  34. arguments after the vector must be keyword arguments.
  35. """
  36. kwargs['stderr'] = STDOUT
  37. return check_output(args, **kwargs)
  38. class IVCSCommand(Interface):
  39. """
  40. An interface for VCS commands.
  41. """
  42. def ensureIsWorkingDirectory(path):
  43. """
  44. Ensure that C{path} is a working directory of this VCS.
  45. @type path: L{twisted.python.filepath.FilePath}
  46. @param path: The path to check.
  47. """
  48. def isStatusClean(path):
  49. """
  50. Return the Git status of the files in the specified path.
  51. @type path: L{twisted.python.filepath.FilePath}
  52. @param path: The path to get the status from (can be a directory or a
  53. file.)
  54. """
  55. def remove(path):
  56. """
  57. Remove the specified path from a the VCS.
  58. @type path: L{twisted.python.filepath.FilePath}
  59. @param path: The path to remove from the repository.
  60. """
  61. def exportTo(fromDir, exportDir):
  62. """
  63. Export the content of the VCSrepository to the specified directory.
  64. @type fromDir: L{twisted.python.filepath.FilePath}
  65. @param fromDir: The path to the VCS repository to export.
  66. @type exportDir: L{twisted.python.filepath.FilePath}
  67. @param exportDir: The directory to export the content of the
  68. repository to. This directory doesn't have to exist prior to
  69. exporting the repository.
  70. """
  71. @implementer(IVCSCommand)
  72. class GitCommand(object):
  73. """
  74. Subset of Git commands to release Twisted from a Git repository.
  75. """
  76. @staticmethod
  77. def ensureIsWorkingDirectory(path):
  78. """
  79. Ensure that C{path} is a Git working directory.
  80. @type path: L{twisted.python.filepath.FilePath}
  81. @param path: The path to check.
  82. """
  83. try:
  84. runCommand(["git", "rev-parse"], cwd=path.path)
  85. except (CalledProcessError, OSError):
  86. raise NotWorkingDirectory(
  87. "%s does not appear to be a Git repository."
  88. % (path.path,))
  89. @staticmethod
  90. def isStatusClean(path):
  91. """
  92. Return the Git status of the files in the specified path.
  93. @type path: L{twisted.python.filepath.FilePath}
  94. @param path: The path to get the status from (can be a directory or a
  95. file.)
  96. """
  97. status = runCommand(
  98. ["git", "-C", path.path, "status", "--short"]).strip()
  99. return status == ''
  100. @staticmethod
  101. def remove(path):
  102. """
  103. Remove the specified path from a Git repository.
  104. @type path: L{twisted.python.filepath.FilePath}
  105. @param path: The path to remove from the repository.
  106. """
  107. runCommand(["git", "-C", path.dirname(), "rm", path.path])
  108. @staticmethod
  109. def exportTo(fromDir, exportDir):
  110. """
  111. Export the content of a Git repository to the specified directory.
  112. @type fromDir: L{twisted.python.filepath.FilePath}
  113. @param fromDir: The path to the Git repository to export.
  114. @type exportDir: L{twisted.python.filepath.FilePath}
  115. @param exportDir: The directory to export the content of the
  116. repository to. This directory doesn't have to exist prior to
  117. exporting the repository.
  118. """
  119. runCommand(["git", "-C", fromDir.path,
  120. "checkout-index", "--all", "--force",
  121. # prefix has to end up with a "/" so that files get copied
  122. # to a directory whose name is the prefix.
  123. "--prefix", exportDir.path + "/"])
  124. def getRepositoryCommand(directory):
  125. """
  126. Detect the VCS used in the specified directory and return a L{GitCommand}
  127. if the directory is a Git repository. If the directory is not git, it
  128. raises a L{NotWorkingDirectory} exception.
  129. @type directory: L{FilePath}
  130. @param directory: The directory to detect the VCS used from.
  131. @rtype: L{GitCommand}
  132. @raise NotWorkingDirectory: if no supported VCS can be found from the
  133. specified directory.
  134. """
  135. try:
  136. GitCommand.ensureIsWorkingDirectory(directory)
  137. return GitCommand
  138. except (NotWorkingDirectory, OSError):
  139. # It's not Git, but that's okay, eat the error
  140. pass
  141. raise NotWorkingDirectory("No supported VCS can be found in %s" %
  142. (directory.path,))
  143. class Project(object):
  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{topfiles} directory that contains a
  149. C{README} file.
  150. """
  151. def __init__(self, directory):
  152. self.directory = directory
  153. def __repr__(self):
  154. return '%s(%r)' % (
  155. self.__class__.__name__, self.directory)
  156. def getVersion(self):
  157. """
  158. @return: A L{incremental.Version} specifying the version number of the
  159. project based on live python modules.
  160. """
  161. namespace = {}
  162. directory = self.directory
  163. while not namespace:
  164. if directory.path == "/":
  165. raise Exception("Not inside a Twisted project.")
  166. elif not directory.basename() == "twisted":
  167. directory = directory.parent()
  168. else:
  169. execfile(directory.child("_version.py").path, namespace)
  170. return namespace["__version__"]
  171. def findTwistedProjects(baseDirectory):
  172. """
  173. Find all Twisted-style projects beneath a base directory.
  174. @param baseDirectory: A L{twisted.python.filepath.FilePath} to look inside.
  175. @return: A list of L{Project}.
  176. """
  177. projects = []
  178. for filePath in baseDirectory.walk():
  179. if filePath.basename() == 'topfiles':
  180. projectDirectory = filePath.parent()
  181. projects.append(Project(projectDirectory))
  182. return projects
  183. def replaceInFile(filename, oldToNew):
  184. """
  185. I replace the text `oldstr' with `newstr' in `filename' using science.
  186. """
  187. os.rename(filename, filename + '.bak')
  188. with open(filename + '.bak') as f:
  189. d = f.read()
  190. for k, v in oldToNew.items():
  191. d = d.replace(k, v)
  192. with open(filename + '.new', 'w') as f:
  193. f.write(d)
  194. os.rename(filename + '.new', filename)
  195. os.unlink(filename + '.bak')
  196. class NoDocumentsFound(Exception):
  197. """
  198. Raised when no input documents are found.
  199. """
  200. class APIBuilder(object):
  201. """
  202. Generate API documentation from source files using
  203. U{pydoctor<https://github.com/twisted/pydoctor>}. This requires
  204. pydoctor to be installed and usable.
  205. """
  206. def build(self, projectName, projectURL, sourceURL, packagePath,
  207. outputPath):
  208. """
  209. Call pydoctor's entry point with options which will generate HTML
  210. documentation for the specified package's API.
  211. @type projectName: C{str}
  212. @param projectName: The name of the package for which to generate
  213. documentation.
  214. @type projectURL: C{str}
  215. @param projectURL: The location (probably an HTTP URL) of the project
  216. on the web.
  217. @type sourceURL: C{str}
  218. @param sourceURL: The location (probably an HTTP URL) of the root of
  219. the source browser for the project.
  220. @type packagePath: L{FilePath}
  221. @param packagePath: The path to the top-level of the package named by
  222. C{projectName}.
  223. @type outputPath: L{FilePath}
  224. @param outputPath: An existing directory to which the generated API
  225. documentation will be written.
  226. """
  227. intersphinxes = []
  228. for intersphinx in intersphinxURLs:
  229. intersphinxes.append("--intersphinx")
  230. intersphinxes.append(intersphinx)
  231. # Super awful monkeypatch that will selectively use our templates.
  232. from pydoctor.templatewriter import util
  233. originalTemplatefile = util.templatefile
  234. def templatefile(filename):
  235. if filename in ["summary.html", "index.html", "common.html"]:
  236. twistedPythonDir = FilePath(__file__).parent()
  237. templatesDir = twistedPythonDir.child("_pydoctortemplates")
  238. return templatesDir.child(filename).path
  239. else:
  240. return originalTemplatefile(filename)
  241. monkeyPatch = MonkeyPatcher((util, "templatefile", templatefile))
  242. monkeyPatch.patch()
  243. from pydoctor.driver import main
  244. main(
  245. ["--project-name", projectName,
  246. "--project-url", projectURL,
  247. "--system-class", "twisted.python._pydoctor.TwistedSystem",
  248. "--project-base-dir", packagePath.parent().path,
  249. "--html-viewsource-base", sourceURL,
  250. "--add-package", packagePath.path,
  251. "--html-output", outputPath.path,
  252. "--html-write-function-pages", "--quiet", "--make-html",
  253. ] + intersphinxes)
  254. monkeyPatch.restore()
  255. class SphinxBuilder(object):
  256. """
  257. Generate HTML documentation using Sphinx.
  258. Generates and runs a shell command that looks something like::
  259. sphinx-build -b html -d [BUILDDIR]/doctrees
  260. [DOCDIR]/source
  261. [BUILDDIR]/html
  262. where DOCDIR is a directory containing another directory called "source"
  263. which contains the Sphinx source files, and BUILDDIR is the directory in
  264. which the Sphinx output will be created.
  265. """
  266. def main(self, args):
  267. """
  268. Build the main documentation.
  269. @type args: list of str
  270. @param args: The command line arguments to process. This must contain
  271. one string argument: the path to the root of a Twisted checkout.
  272. Additional arguments will be ignored for compatibility with legacy
  273. build infrastructure.
  274. """
  275. output = self.build(FilePath(args[0]).child("docs"))
  276. if output:
  277. sys.stdout.write("Unclean build:\n{}\n".format(output))
  278. raise sys.exit(1)
  279. def build(self, docDir, buildDir=None, version=''):
  280. """
  281. Build the documentation in C{docDir} with Sphinx.
  282. @param docDir: The directory of the documentation. This is a directory
  283. which contains another directory called "source" which contains the
  284. Sphinx "conf.py" file and sphinx source documents.
  285. @type docDir: L{twisted.python.filepath.FilePath}
  286. @param buildDir: The directory to build the documentation in. By
  287. default this will be a child directory of {docDir} named "build".
  288. @type buildDir: L{twisted.python.filepath.FilePath}
  289. @param version: The version of Twisted to set in the docs.
  290. @type version: C{str}
  291. @return: the output produced by running the command
  292. @rtype: L{str}
  293. """
  294. if buildDir is None:
  295. buildDir = docDir.parent().child('doc')
  296. doctreeDir = buildDir.child('doctrees')
  297. output = runCommand(['sphinx-build', '-q', '-b', 'html',
  298. '-d', doctreeDir.path, docDir.path,
  299. buildDir.path])
  300. # Delete the doctrees, as we don't want them after the docs are built
  301. doctreeDir.remove()
  302. for path in docDir.walk():
  303. if path.basename() == "man":
  304. segments = path.segmentsFrom(docDir)
  305. dest = buildDir
  306. while segments:
  307. dest = dest.child(segments.pop(0))
  308. if not dest.parent().isdir():
  309. dest.parent().makedirs()
  310. path.copyTo(dest)
  311. return output
  312. def filePathDelta(origin, destination):
  313. """
  314. Return a list of strings that represent C{destination} as a path relative
  315. to C{origin}.
  316. It is assumed that both paths represent directories, not files. That is to
  317. say, the delta of L{twisted.python.filepath.FilePath} /foo/bar to
  318. L{twisted.python.filepath.FilePath} /foo/baz will be C{../baz},
  319. not C{baz}.
  320. @type origin: L{twisted.python.filepath.FilePath}
  321. @param origin: The origin of the relative path.
  322. @type destination: L{twisted.python.filepath.FilePath}
  323. @param destination: The destination of the relative path.
  324. """
  325. commonItems = 0
  326. path1 = origin.path.split(os.sep)
  327. path2 = destination.path.split(os.sep)
  328. for elem1, elem2 in zip(path1, path2):
  329. if elem1 == elem2:
  330. commonItems += 1
  331. else:
  332. break
  333. path = [".."] * (len(path1) - commonItems)
  334. return path + path2[commonItems:]
  335. class NotWorkingDirectory(Exception):
  336. """
  337. Raised when a directory does not appear to be a repository directory of a
  338. supported VCS.
  339. """
  340. class BuildAPIDocsScript(object):
  341. """
  342. A thing for building API documentation. See L{main}.
  343. """
  344. def buildAPIDocs(self, projectRoot, output):
  345. """
  346. Build the API documentation of Twisted, with our project policy.
  347. @param projectRoot: A L{FilePath} representing the root of the Twisted
  348. checkout.
  349. @param output: A L{FilePath} pointing to the desired output directory.
  350. """
  351. version = Project(
  352. projectRoot.child("twisted")).getVersion()
  353. versionString = version.base()
  354. sourceURL = ("https://github.com/twisted/twisted/tree/"
  355. "twisted-%s" % (versionString,) + "/src")
  356. apiBuilder = APIBuilder()
  357. apiBuilder.build(
  358. "Twisted",
  359. "http://twistedmatrix.com/",
  360. sourceURL,
  361. projectRoot.child("twisted"),
  362. output)
  363. def main(self, args):
  364. """
  365. Build API documentation.
  366. @type args: list of str
  367. @param args: The command line arguments to process. This must contain
  368. two strings: the path to the root of the Twisted checkout, and a
  369. path to an output directory.
  370. """
  371. if len(args) != 2:
  372. sys.exit("Must specify two arguments: "
  373. "Twisted checkout and destination path")
  374. self.buildAPIDocs(FilePath(args[0]), FilePath(args[1]))
  375. class CheckTopfileScript(object):
  376. """
  377. A thing for checking whether a checkout has a newsfragment.
  378. """
  379. def __init__(self, _print):
  380. self._print = _print
  381. def main(self, args):
  382. """
  383. Run the script.
  384. @type args: L{list} of L{str}
  385. @param args: The command line arguments to process. This must contain
  386. one string: the path to the root of the Twisted checkout.
  387. """
  388. if len(args) != 1:
  389. sys.exit("Must specify one argument: the Twisted checkout")
  390. location = os.path.abspath(args[0])
  391. branch = runCommand([b"git", b"rev-parse", b"--abbrev-ref", "HEAD"],
  392. cwd=location).strip()
  393. r = runCommand([b"git", b"diff", b"--name-only", b"origin/trunk..."],
  394. cwd=location).strip()
  395. if not r:
  396. self._print(
  397. "On trunk or no diffs from trunk; no need to look at this.")
  398. sys.exit(0)
  399. files = r.strip().split(os.linesep)
  400. self._print("Looking at these files:")
  401. for change in files:
  402. self._print(change)
  403. self._print("----")
  404. if len(files) == 1:
  405. if files[0] == os.sep.join(["docs", "fun", "Twisted.Quotes"]):
  406. self._print("Quotes change only; no newsfragment needed.")
  407. sys.exit(0)
  408. topfiles = []
  409. for change in files:
  410. if os.sep + "newsfragments" + os.sep in change:
  411. if "." in change and change.rsplit(".", 1)[1] in TOPFILE_TYPES:
  412. topfiles.append(change)
  413. if branch.startswith("release-"):
  414. if topfiles:
  415. self._print("No newsfragments should be on the release branch.")
  416. sys.exit(1)
  417. else:
  418. self._print("Release branch with no newsfragments, all good.")
  419. sys.exit(0)
  420. for change in topfiles:
  421. self._print("Found " + change)
  422. sys.exit(0)
  423. self._print("No newsfragment found. Have you committed it?")
  424. sys.exit(1)