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.

126 lines
4.1 KiB

  1. #!/usr/bin/python
  2. # Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. import sys
  14. import subprocess
  15. import argparse
  16. def git_tags():
  17. args = ["git", "tag", '-l']
  18. tags = subprocess.check_output(args)
  19. tags = tags.split()
  20. tags.reverse()
  21. return tags
  22. def git_log(fromtag,totag):
  23. args = ['git','log','--no-merges','--oneline',fromtag+'...'+totag]
  24. return subprocess.check_output(args).strip().split('\n')
  25. def git_author_and_time(tag):
  26. args = ['git','log','-1','--format=-- %an <%ae> %cD',tag]
  27. return subprocess.check_output(args).strip()
  28. def git_version():
  29. args = ['git','describe','--always','--tags','--dirty']
  30. return subprocess.check_output(args).strip()
  31. def guess_distro():
  32. try:
  33. args = ['lsb_release','-i','-s']
  34. return subprocess.check_output(args).strip().lower()
  35. except:
  36. return 'unknown'
  37. def guess_codename():
  38. try:
  39. args = ['lsb_release','-c','-s']
  40. return subprocess.check_output(args).strip().lower()
  41. except:
  42. return 'unknown'
  43. def patch_subprocess():
  44. if "check_output" not in dir( subprocess ): # duck punch it in!
  45. def check_output(*popenargs, **kwargs):
  46. r"""Run command with arguments and return its output as a byte string.
  47. Backported from Python 2.7 as it's implemented as pure python on stdlib.
  48. >>> check_output(['/usr/bin/python', '--version'])
  49. Python 2.6.2
  50. """
  51. process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
  52. output, unused_err = process.communicate()
  53. retcode = process.poll()
  54. if retcode:
  55. cmd = kwargs.get("args")
  56. if cmd is None:
  57. cmd = popenargs[0]
  58. error = subprocess.CalledProcessError(retcode, cmd)
  59. error.output = output
  60. raise error
  61. return output
  62. subprocess.check_output = check_output
  63. def main():
  64. patch_subprocess()
  65. parser = argparse.ArgumentParser(description='Generated debian/changelog from git log')
  66. parser.add_argument('--name',type=str,help='Name of package',required=True)
  67. parser.add_argument('--version',type=str,help='Place in git history to include upto',default='::guess::')
  68. parser.add_argument('--distro',type=str,help='Distribution name',default='::guess::')
  69. parser.add_argument('--codename',type=str,help='Distribution codename',default='::guess::')
  70. parser.add_argument('--urgency',type=str,help='Urgency',default='medium')
  71. args = parser.parse_args()
  72. if args.distro == '::guess::':
  73. args.distro = guess_distro()
  74. if args.codename == '::guess::':
  75. args.codename = guess_codename()
  76. if args.version == '::guess::':
  77. ver = git_version()
  78. args.version = ver+"~"+args.distro+"-"+args.codename
  79. tags = git_tags()
  80. if args.version in tags:
  81. idx = tags.index(args.version)
  82. tags = tags[idx:]
  83. tags = zip(tags,tags)
  84. else:
  85. tags = zip(tags,tags)
  86. tags.insert(0,(args.version,'HEAD'))
  87. tag = tags[0]
  88. for prev in tags[1:]:
  89. print('%s (%s) %s; urgency=%s\n' % (args.name,tag[0],args.codename,args.urgency))
  90. lines = git_log(tag[1],prev[1])
  91. for line in lines:
  92. print " * " + line
  93. authorandtime = git_author_and_time(tag[1])
  94. print(' %s\n' % authorandtime)
  95. tag = prev
  96. if __name__ == "__main__":
  97. main()