PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/autoaptitude/create_packagelist.py

https://bitbucket.org/vinitkme/automatta
Python | 157 lines | 83 code | 32 blank | 42 comment | 14 complexity | ba4c9e394149283f36422b5db67306c6 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. autoaptitude
  5. ~~~~~~~~~~~~
  6. Creates a list for autoaptitude.sh
  7. idea from:
  8. http://ubuntuforums.org/showthread.php?t=442974
  9. Last commit info:
  10. ~~~~~~~~~~~~~~~~~
  11. $LastChangedDate: $
  12. $Rev: $
  13. $Author: $
  14. :copyleft: 2008 by Jens Diemer
  15. :license: GNU GPL v3, see LICENSE.txt for more details.
  16. """
  17. import os, sys, time, subprocess, datetime
  18. from pprint import pprint
  19. APTITUDE = "/usr/bin/aptitude"
  20. ENVIRON = {"LC_LANG":"C"} # Alles auf englisch ausgeben
  21. #~ subprocess.Popen(
  22. #~ [APTITUDE, "--help"],
  23. #~ env=ENVIRON,
  24. #~ )
  25. #~ sys.exit()
  26. TEMP_FILENAME = "packagelist.tmp"
  27. PACKAGE_FILE = "packagelist.txt"
  28. SAVE_APTITUDE = "show ~i >%s" % TEMP_FILENAME
  29. START_packet_NAME = "Package: "
  30. START_SECTION = "Section: "
  31. START_AUTOMARK = "Automatically installed: "
  32. AUTOMARK_MAP = {"yes": True, "no": False}
  33. def create_temp_file():
  34. # TODO: Can we use the piped output directly?
  35. cmd = " ".join([APTITUDE, SAVE_APTITUDE])
  36. print "run:", cmd
  37. p = subprocess.Popen(cmd, shell=True, env=ENVIRON)
  38. while p.poll() == None:
  39. time.sleep(1)
  40. sys.stdout.write(".")
  41. sys.stdout.flush()
  42. print "\nOK"
  43. def clean_section_name(section_name):
  44. if "/" in section_name:
  45. #~ print section_name
  46. return section_name.rsplit("/", 1)[1]
  47. else:
  48. return section_name
  49. def get_automark_data(f):
  50. automark_data = {}
  51. packet_name = None
  52. section = None
  53. automark = None
  54. count = 0
  55. for line in f:
  56. line = line.strip()
  57. #~ print ">>>", line
  58. if line.startswith(START_packet_NAME):
  59. packet_name = line[len(START_packet_NAME):]
  60. #~ print "1", packet_name
  61. elif line.startswith(START_SECTION):
  62. section = line[len(START_SECTION):]
  63. #~ print "2", section
  64. elif line.startswith(START_AUTOMARK):
  65. automark = line[len(START_AUTOMARK):]
  66. automark = AUTOMARK_MAP[automark]
  67. #~ print "3", automark
  68. if packet_name and section and automark:
  69. #~ print "4"
  70. #~ count += 1
  71. #~ if count>2: break
  72. section = clean_section_name(section)
  73. #~ print "packet:", packet_name
  74. #~ print "Section:", section
  75. #~ print " -"*40
  76. if not section in automark_data:
  77. automark_data[section] = []
  78. automark_data[section].append(packet_name)
  79. packet_name = None
  80. section = None
  81. automark = None
  82. elif line == "":
  83. #~ print "-" * 79
  84. packet_name = None
  85. section = None
  86. automark = None
  87. return automark_data
  88. def write_package_list(automark_data, filename):
  89. f = file(filename, "w")
  90. f.writelines([
  91. "#" * 79, "\n",
  92. "# automatic generated with %s" % __file__, "\n",
  93. "# (%s)" % datetime.datetime.now(), "\n",
  94. "#" * 79, "\n",
  95. ])
  96. for section, packages in sorted(automark_data.iteritems()):
  97. f.writelines([
  98. "#" * 20, "\n",
  99. "# %s" % section, "\n",
  100. "#" * 20, "\n",
  101. ])
  102. packages.sort()
  103. f.write("\n".join(packages))
  104. f.write("\n\n")
  105. f.close()
  106. if __name__ == "__main__":
  107. #~ os.remove(TEMP_FILENAME)
  108. # if os.path.isfile(TEMP_FILENAME):
  109. # print "Skip aptitude show, use file %s" % TEMP_FILENAME
  110. # else:
  111. create_temp_file()
  112. #~ sys.exit()
  113. print "-"*79
  114. f = file(TEMP_FILENAME, "r")
  115. automark_data = get_automark_data(f)
  116. f.close()
  117. pprint(automark_data)
  118. write_package_list(automark_data, PACKAGE_FILE)