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.

136 lines
4.4 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 = [[int(X) for X in tag.split(".")] for tag in tags.split()]
  20. tags.sort()
  21. tags.reverse()
  22. tags = [".".join([str(X) for X in tag]) for tag in tags]
  23. return tags
  24. def git_log(fromtag,totag):
  25. args = ['git','log','--no-merges','--oneline',fromtag+'...'+totag]
  26. return subprocess.check_output(args).strip().split('\n')
  27. def git_author_and_time(tag):
  28. args = ['git','log','-1','--format=-- %an <%ae> %cD',tag]
  29. return subprocess.check_output(args).strip()
  30. def git_version():
  31. args = ['git','describe','--always','--tags','--dirty']
  32. return subprocess.check_output(args).strip()
  33. def guess_distro():
  34. try:
  35. args = ['lsb_release','-i','-s']
  36. return subprocess.check_output(args).strip().lower()
  37. except:
  38. return 'unknown'
  39. def guess_codename():
  40. try:
  41. args = ['lsb_release','-c','-s']
  42. return subprocess.check_output(args).strip().lower()
  43. except:
  44. return 'unknown'
  45. def patch_subprocess():
  46. if "check_output" not in dir( subprocess ): # duck punch it in!
  47. def check_output(*popenargs, **kwargs):
  48. r"""Run command with arguments and return its output as a byte string.
  49. Backported from Python 2.7 as it's implemented as pure python on stdlib.
  50. >>> check_output(['/usr/bin/python', '--version'])
  51. Python 2.6.2
  52. """
  53. process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
  54. output, unused_err = process.communicate()
  55. retcode = process.poll()
  56. if retcode:
  57. cmd = kwargs.get("args")
  58. if cmd is None:
  59. cmd = popenargs[0]
  60. error = subprocess.CalledProcessError(retcode, cmd)
  61. error.output = output
  62. raise error
  63. return output
  64. subprocess.check_output = check_output
  65. def main():
  66. patch_subprocess()
  67. parser = argparse.ArgumentParser(description='Generated debian/changelog from git log')
  68. parser.add_argument('--name',type=str,help='Name of package',required=True)
  69. parser.add_argument('--version',type=str,help='Place in git history to include upto',default='::guess::')
  70. parser.add_argument('--distro',type=str,help='Distribution name',default='::guess::')
  71. parser.add_argument('--codename',type=str,help='Distribution codename',default='::guess::')
  72. parser.add_argument('--urgency',type=str,help='Urgency',default='medium')
  73. args = parser.parse_args()
  74. if args.distro == '::guess::':
  75. args.distro = guess_distro()
  76. if args.codename == '::guess::':
  77. args.codename = guess_codename()
  78. versuffix = "~"+args.distro+"-"+args.codename
  79. if args.version == '::guess::':
  80. args.version = git_version()
  81. tags = git_tags()
  82. if args.version in tags:
  83. idx = tags.index(args.version)
  84. tags = tags[idx:]
  85. tags = zip(tags,tags)
  86. else:
  87. tags = zip(tags,tags)
  88. tags.insert(0,(args.version,'HEAD'))
  89. for i in xrange(0,len(tags)):
  90. tags[i] = (tags[i][0] + versuffix,tags[i][1])
  91. tag = tags[0]
  92. for prev in tags[1:]:
  93. lines = git_log(tag[1],prev[1])
  94. if lines == ['']:
  95. tag = prev
  96. continue
  97. print('%s (%s) %s; urgency=%s\n' % (args.name,tag[0],args.codename,args.urgency))
  98. for line in lines:
  99. print " * " + line
  100. authorandtime = git_author_and_time(tag[1])
  101. print(' %s\n' % authorandtime)
  102. tag = prev
  103. if __name__ == "__main__":
  104. main()