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.

134 lines
4.5 KiB

  1. #!/usr/bin/python
  2. # The MIT License (MIT)
  3. # Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. # THE SOFTWARE.
  19. import sys
  20. import subprocess
  21. import argparse
  22. def git_tags():
  23. args = ["git", "tag", '-l']
  24. tags = subprocess.check_output(args)
  25. tags = tags.split()
  26. tags.reverse()
  27. return tags
  28. def git_log(fromtag,totag):
  29. args = ['git','log','--no-merges','--oneline',fromtag+'...'+totag]
  30. return subprocess.check_output(args).strip().split('\n')
  31. def git_author_and_time(tag):
  32. args = ['git','log','-1','--format=-- %an <%ae> %cD',tag]
  33. return subprocess.check_output(args).strip()
  34. def git_version():
  35. args = ['git','describe','--always','--tags','--dirty']
  36. return subprocess.check_output(args).strip()
  37. def guess_distro():
  38. try:
  39. args = ['lsb_release','-i','-s']
  40. return subprocess.check_output(args).strip().lower()
  41. except:
  42. return 'unknown'
  43. def guess_codename():
  44. try:
  45. args = ['lsb_release','-c','-s']
  46. return subprocess.check_output(args).strip().lower()
  47. except:
  48. return 'unknown'
  49. def patch_subprocess():
  50. if "check_output" not in dir( subprocess ): # duck punch it in!
  51. def check_output(*popenargs, **kwargs):
  52. r"""Run command with arguments and return its output as a byte string.
  53. Backported from Python 2.7 as it's implemented as pure python on stdlib.
  54. >>> check_output(['/usr/bin/python', '--version'])
  55. Python 2.6.2
  56. """
  57. process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
  58. output, unused_err = process.communicate()
  59. retcode = process.poll()
  60. if retcode:
  61. cmd = kwargs.get("args")
  62. if cmd is None:
  63. cmd = popenargs[0]
  64. error = subprocess.CalledProcessError(retcode, cmd)
  65. error.output = output
  66. raise error
  67. return output
  68. subprocess.check_output = check_output
  69. def main():
  70. patch_subprocess()
  71. parser = argparse.ArgumentParser(description='Generated debian/changelog from git log')
  72. parser.add_argument('--name',type=str,help='Name of package',required=True)
  73. parser.add_argument('--version',type=str,help='Place in git history to include upto',default='::guess::')
  74. parser.add_argument('--distro',type=str,help='Distribution name',default='::guess::')
  75. parser.add_argument('--codename',type=str,help='Distribution codename',default='::guess::')
  76. parser.add_argument('--urgency',type=str,help='Urgency',default='medium')
  77. args = parser.parse_args()
  78. if args.distro == '::guess::':
  79. args.distro = guess_distro()
  80. if args.codename == '::guess::':
  81. args.codename = guess_codename()
  82. if args.version == '::guess::':
  83. ver = git_version()
  84. args.version = ver+"~"+args.distro+"-"+args.codename
  85. tags = git_tags()
  86. if args.version in tags:
  87. idx = tags.index(args.version)
  88. tags = tags[idx:]
  89. tags = zip(tags,tags)
  90. else:
  91. tags = zip(tags,tags)
  92. tags.insert(0,(args.version,'HEAD'))
  93. tag = tags[0]
  94. for prev in tags[1:]:
  95. print('%s (%s) %s; urgency=%s\n' % (args.name,tag[0],args.codename,args.urgency))
  96. lines = git_log(tag[1],prev[1])
  97. for line in lines:
  98. print " * " + line
  99. authorandtime = git_author_and_time(tag[1])
  100. print(' %s\n' % authorandtime)
  101. tag = prev
  102. if __name__ == "__main__":
  103. main()