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.6 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 find_distrib_codename(f):
  38. for line in f:
  39. (key,value) = line.partition('=')[::2]
  40. if key == 'DISTRIB_CODENAME':
  41. return value.strip()
  42. def guess_distribution():
  43. try:
  44. with open('/etc/upstream-release/lsb-release') as f:
  45. return find_distrib_codename(f)
  46. except:
  47. try:
  48. with open('/etc/lsb-release') as f:
  49. return find_distrib_codename(f)
  50. except:
  51. try:
  52. args = ['lsb_release','-c','-s']
  53. return subprocess.check_output(args).strip()
  54. except:
  55. return 'unknown'
  56. def patch_subprocess():
  57. if "check_output" not in dir( subprocess ): # duck punch it in!
  58. def check_output(*popenargs, **kwargs):
  59. r"""Run command with arguments and return its output as a byte string.
  60. Backported from Python 2.7 as it's implemented as pure python on stdlib.
  61. >>> check_output(['/usr/bin/python', '--version'])
  62. Python 2.6.2
  63. """
  64. process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
  65. output, unused_err = process.communicate()
  66. retcode = process.poll()
  67. if retcode:
  68. cmd = kwargs.get("args")
  69. if cmd is None:
  70. cmd = popenargs[0]
  71. error = subprocess.CalledProcessError(retcode, cmd)
  72. error.output = output
  73. raise error
  74. return output
  75. subprocess.check_output = check_output
  76. def main():
  77. patch_subprocess()
  78. parser = argparse.ArgumentParser(description='Generated debian/changelog from git log')
  79. parser.add_argument('--name',type=str,help='Name of package',required=True)
  80. parser.add_argument('--version',type=str,help='Place in git history to include upto',default='::guess::')
  81. parser.add_argument('--distro',type=str,help='Distribution name',default='::guess::')
  82. parser.add_argument('--urgency',type=str,help='Urgency',default='medium')
  83. args = parser.parse_args()
  84. if args.distro == '::guess::':
  85. args.distro = guess_distribution()
  86. if args.version == '::guess::':
  87. args.version = git_version()
  88. tags = git_tags()
  89. if args.version in tags:
  90. idx = tags.index(args.version)
  91. tags = tags[idx:]
  92. tags = zip(tags,tags)
  93. else:
  94. tags = zip(tags,tags)
  95. tags.insert(0,(args.version,'HEAD'))
  96. tag = tags[0]
  97. for prev in tags[1:]:
  98. print('%s (%s) %s; urgency=%s\n' % (args.name,tag[0],args.distro,args.urgency))
  99. lines = git_log(tag[1],prev[1])
  100. for line in lines:
  101. print " * " + line
  102. authorandtime = git_author_and_time(tag[1])
  103. print(' %s\n' % authorandtime)
  104. tag = prev
  105. if __name__ == "__main__":
  106. main()