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.

112 lines
3.5 KiB

5 years ago
5 years ago
  1. #!/usr/bin/env python3
  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 call(args):
  17. return subprocess.Popen(args,stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
  18. def git_tags():
  19. args = ["git", "tag", '-l']
  20. tags = call(args)
  21. tags = [[int(X) for X in tag.split(".")] for tag in tags.split()]
  22. tags.sort()
  23. tags.reverse()
  24. tags = [".".join([str(X) for X in tag]) for tag in tags]
  25. return tags
  26. def git_log(fromtag,totag):
  27. args = ['git','log','--no-merges','--oneline',fromtag+'...'+totag]
  28. return call(args).strip().split('\n')
  29. def git_author_and_time(tag):
  30. args = ['git','log','-1','--format=-- %an <%ae> %cD',tag]
  31. return call(args).strip()
  32. def git_version():
  33. args = ['git','describe','--always','--tags','--dirty']
  34. return call(args).strip()
  35. def guess_distro():
  36. try:
  37. args = ['lsb_release','-i','-s']
  38. return call(args).strip().lower()
  39. except:
  40. return 'unknown'
  41. def guess_codename():
  42. try:
  43. args = ['lsb_release','-c','-s']
  44. return call(args).strip().lower()
  45. except:
  46. return 'unknown'
  47. def main():
  48. parser = argparse.ArgumentParser(description='Generated debian/changelog from git log')
  49. parser.add_argument('--name',type=str,help='Name of package',required=True)
  50. parser.add_argument('--version',type=str,help='Place in git history to include upto',default='::guess::')
  51. parser.add_argument('--distro',type=str,help='Distribution name',default='::guess::')
  52. parser.add_argument('--codename',type=str,help='Distribution codename',default='::guess::')
  53. parser.add_argument('--urgency',type=str,help='Urgency',default='medium')
  54. args = parser.parse_args()
  55. if args.distro == '::guess::':
  56. args.distro = guess_distro()
  57. if args.codename == '::guess::':
  58. args.codename = guess_codename()
  59. versuffix = "~"+args.distro+"-"+args.codename
  60. if args.version == '::guess::':
  61. args.version = git_version()
  62. tags = git_tags()
  63. if args.version in tags:
  64. idx = tags.index(args.version)
  65. tags = tags[idx:]
  66. tags = list(zip(tags,tags))
  67. else:
  68. tags = list(zip(tags,tags))
  69. tags.insert(0,(args.version,'HEAD'))
  70. for i in range(0,len(tags)):
  71. tags[i] = (tags[i][0] + versuffix,tags[i][1])
  72. tag = tags[0]
  73. for prev in tags[1:]:
  74. lines = git_log(tag[1],prev[1])
  75. if lines == ['']:
  76. tag = prev
  77. continue
  78. print('{0} ({1}) {2}; urgency={3}\n'.format(args.name,tag[0],args.codename,args.urgency))
  79. for line in lines:
  80. print(" * " + line)
  81. authorandtime = git_author_and_time(tag[1])
  82. print(' {0}\n'.format(authorandtime))
  83. tag = prev
  84. if __name__ == "__main__":
  85. main()