control_overrides.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/usr/bin/env python
  2. """ Bulk manipulation of the overrides """
  3. # Copyright (C) 2000, 2001, 2002, 2003, 2006 James Troup <james@nocrew.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. ################################################################################
  16. # On 30 Nov 1998, James Troup wrote:
  17. #
  18. # > James Troup<2> <troup2@debian.org>
  19. # >
  20. # > James is a clone of James; he's going to take over the world.
  21. # > After he gets some sleep.
  22. #
  23. # Could you clone other things too? Sheep? Llamas? Giant mutant turnips?
  24. #
  25. # Your clone will need some help to take over the world, maybe clone up an
  26. # army of penguins and threaten to unleash them on the world, forcing
  27. # governments to sway to the new James' will!
  28. #
  29. # Yes, I can envision a day when James' duplicate decides to take a horrific
  30. # vengance on the James that spawned him and unleashes his fury in the form
  31. # of thousands upon thousands of chickens that look just like Captin Blue
  32. # Eye! Oh the horror.
  33. #
  34. # Now you'll have to were name tags to people can tell you apart, unless of
  35. # course the new clone is truely evil in which case he should be easy to
  36. # identify!
  37. #
  38. # Jason
  39. # Chicken. Black. Helicopters.
  40. # Be afraid.
  41. # <Pine.LNX.3.96.981130011300.30365Z-100000@wakko>
  42. ################################################################################
  43. from __future__ import print_function
  44. import sys
  45. import time
  46. import apt_pkg
  47. from daklib.dbconn import *
  48. from daklib.config import Config
  49. from daklib import utils
  50. from daklib import daklog
  51. from daklib.regexes import re_comments
  52. ################################################################################
  53. Logger = None
  54. ################################################################################
  55. def usage(exit_code=0):
  56. print("""Usage: dak control-overrides [OPTIONS]
  57. -h, --help print this help and exit
  58. -c, --component=CMPT list/set overrides by component
  59. (contrib,*main,non-free)
  60. -s, --suite=SUITE list/set overrides by suite
  61. (experimental,stable,testing,*unstable)
  62. -t, --type=TYPE list/set overrides by type
  63. (*deb,dsc,udeb)
  64. -a, --add add overrides (changes and deletions are ignored)
  65. -S, --set set overrides
  66. -C, --change change overrides (additions and deletions are ignored)
  67. -l, --list list overrides
  68. -q, --quiet be less verbose
  69. -n, --no-action only list the action that would have been done
  70. starred (*) values are default""")
  71. sys.exit(exit_code)
  72. ################################################################################
  73. def process_file(file, suite, component, otype, mode, action, session):
  74. cnf = Config()
  75. s = get_suite(suite, session=session)
  76. if s is None:
  77. utils.fubar("Suite '%s' not recognised." % (suite))
  78. suite_id = s.suite_id
  79. c = get_component(component, session=session)
  80. if c is None:
  81. utils.fubar("Component '%s' not recognised." % (component))
  82. component_id = c.component_id
  83. o = get_override_type(otype)
  84. if o is None:
  85. utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (otype))
  86. type_id = o.overridetype_id
  87. # --set is done mostly internal for performance reasons; most
  88. # invocations of --set will be updates and making people wait 2-3
  89. # minutes while 6000 select+inserts are run needlessly isn't cool.
  90. original = {}
  91. new = {}
  92. c_skipped = 0
  93. c_added = 0
  94. c_updated = 0
  95. c_removed = 0
  96. c_error = 0
  97. q = session.execute("""SELECT o.package, o.priority, o.section, o.maintainer, p.priority, s.section
  98. FROM override o, priority p, section s
  99. WHERE o.suite = :suiteid AND o.component = :componentid AND o.type = :typeid
  100. and o.priority = p.id and o.section = s.id""",
  101. {'suiteid': suite_id, 'componentid': component_id, 'typeid': type_id})
  102. for i in q.fetchall():
  103. original[i[0]] = i[1:]
  104. start_time = time.time()
  105. section_cache = get_sections(session)
  106. priority_cache = get_priorities(session)
  107. # Our session is already in a transaction
  108. for line in file.readlines():
  109. line = re_comments.sub('', line).strip()
  110. if line == "":
  111. continue
  112. maintainer_override = None
  113. if otype == "dsc":
  114. split_line = line.split(None, 2)
  115. if len(split_line) == 2:
  116. (package, section) = split_line
  117. elif len(split_line) == 3:
  118. (package, section, maintainer_override) = split_line
  119. else:
  120. utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line))
  121. c_error += 1
  122. continue
  123. priority = "extra"
  124. else: # binary or udeb
  125. split_line = line.split(None, 3)
  126. if len(split_line) == 3:
  127. (package, priority, section) = split_line
  128. elif len(split_line) == 4:
  129. (package, priority, section, maintainer_override) = split_line
  130. else:
  131. utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line))
  132. c_error += 1
  133. continue
  134. if section not in section_cache:
  135. utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component))
  136. c_error += 1
  137. continue
  138. section_id = section_cache[section]
  139. if priority not in priority_cache:
  140. utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component))
  141. c_error += 1
  142. continue
  143. priority_id = priority_cache[priority]
  144. if package in new:
  145. utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component))
  146. c_error += 1
  147. continue
  148. new[package] = ""
  149. if package in original:
  150. (old_priority_id, old_section_id, old_maintainer_override, old_priority, old_section) = original[package]
  151. if mode == "add" or old_priority_id == priority_id and \
  152. old_section_id == section_id and \
  153. old_maintainer_override == maintainer_override:
  154. # If it's unchanged or we're in 'add only' mode, ignore it
  155. c_skipped += 1
  156. continue
  157. else:
  158. # If it's changed, delete the old one so we can
  159. # reinsert it with the new information
  160. c_updated += 1
  161. if action:
  162. session.execute("""DELETE FROM override WHERE suite = :suite AND component = :component
  163. AND package = :package AND type = :typeid""",
  164. {'suite': suite_id, 'component': component_id,
  165. 'package': package, 'typeid': type_id})
  166. # Log changes
  167. if old_priority_id != priority_id:
  168. Logger.log(["changed priority", package, old_priority, priority])
  169. if old_section_id != section_id:
  170. Logger.log(["changed section", package, old_section, section])
  171. if old_maintainer_override != maintainer_override:
  172. Logger.log(["changed maintainer override", package, old_maintainer_override, maintainer_override])
  173. update_p = 1
  174. elif mode == "change":
  175. # Ignore additions in 'change only' mode
  176. c_skipped += 1
  177. continue
  178. else:
  179. c_added += 1
  180. update_p = 0
  181. if action:
  182. if not maintainer_override:
  183. m_o = None
  184. else:
  185. m_o = maintainer_override
  186. session.execute("""INSERT INTO override (suite, component, type, package,
  187. priority, section, maintainer)
  188. VALUES (:suiteid, :componentid, :typeid,
  189. :package, :priorityid, :sectionid,
  190. :maintainer)""",
  191. {'suiteid': suite_id, 'componentid': component_id,
  192. 'typeid': type_id, 'package': package,
  193. 'priorityid': priority_id, 'sectionid': section_id,
  194. 'maintainer': m_o})
  195. if not update_p:
  196. Logger.log(["new override", suite, component, otype, package, priority, section, maintainer_override])
  197. if mode == "set":
  198. # Delete any packages which were removed
  199. for package in original.keys():
  200. if package not in new:
  201. if action:
  202. session.execute("""DELETE FROM override
  203. WHERE suite = :suiteid AND component = :componentid
  204. AND package = :package AND type = :typeid""",
  205. {'suiteid': suite_id, 'componentid': component_id,
  206. 'package': package, 'typeid': type_id})
  207. c_removed += 1
  208. Logger.log(["removed override", suite, component, otype, package])
  209. if action:
  210. session.commit()
  211. if not cnf["Control-Overrides::Options::Quiet"]:
  212. print("Done in %d seconds. [Updated = %d, Added = %d, Removed = %d, Skipped = %d, Errors = %d]" % (int(time.time() - start_time), c_updated, c_added, c_removed, c_skipped, c_error))
  213. Logger.log(["set complete", c_updated, c_added, c_removed, c_skipped, c_error])
  214. ################################################################################
  215. def list_overrides(suite, component, otype, session):
  216. dat = {}
  217. s = get_suite(suite, session)
  218. if s is None:
  219. utils.fubar("Suite '%s' not recognised." % (suite))
  220. dat['suiteid'] = s.suite_id
  221. c = get_component(component, session)
  222. if c is None:
  223. utils.fubar("Component '%s' not recognised." % (component))
  224. dat['componentid'] = c.component_id
  225. o = get_override_type(otype)
  226. if o is None:
  227. utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (otype))
  228. dat['typeid'] = o.overridetype_id
  229. if otype == "dsc":
  230. q = session.execute("""SELECT o.package, s.section, o.maintainer FROM override o, section s
  231. WHERE o.suite = :suiteid AND o.component = :componentid
  232. AND o.type = :typeid AND o.section = s.id
  233. ORDER BY s.section, o.package""", dat)
  234. for i in q.fetchall():
  235. print(utils.result_join(i))
  236. else:
  237. q = session.execute("""SELECT o.package, p.priority, s.section, o.maintainer, p.level
  238. FROM override o, priority p, section s
  239. WHERE o.suite = :suiteid AND o.component = :componentid
  240. AND o.type = :typeid AND o.priority = p.id AND o.section = s.id
  241. ORDER BY s.section, p.level, o.package""", dat)
  242. for i in q.fetchall():
  243. print(utils.result_join(i[:-1]))
  244. ################################################################################
  245. def main():
  246. global Logger
  247. cnf = Config()
  248. Arguments = [('a', "add", "Control-Overrides::Options::Add"),
  249. ('c', "component", "Control-Overrides::Options::Component", "HasArg"),
  250. ('h', "help", "Control-Overrides::Options::Help"),
  251. ('l', "list", "Control-Overrides::Options::List"),
  252. ('q', "quiet", "Control-Overrides::Options::Quiet"),
  253. ('s', "suite", "Control-Overrides::Options::Suite", "HasArg"),
  254. ('S', "set", "Control-Overrides::Options::Set"),
  255. ('C', "change", "Control-Overrides::Options::Change"),
  256. ('n', "no-action", "Control-Overrides::Options::No-Action"),
  257. ('t', "type", "Control-Overrides::Options::Type", "HasArg")]
  258. # Default arguments
  259. for i in ["add", "help", "list", "quiet", "set", "change", "no-action"]:
  260. key = "Control-Overrides::Options::%s" % i
  261. if key not in cnf:
  262. cnf[key] = ""
  263. if "Control-Overrides::Options::Component" not in cnf:
  264. cnf["Control-Overrides::Options::Component"] = "main"
  265. if "Control-Overrides::Options::Suite" not in cnf:
  266. cnf["Control-Overrides::Options::Suite"] = "unstable"
  267. if "Control-Overrides::Options::Type" not in cnf:
  268. cnf["Control-Overrides::Options::Type"] = "deb"
  269. file_list = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
  270. if cnf["Control-Overrides::Options::Help"]:
  271. usage()
  272. session = DBConn().session()
  273. mode = None
  274. for i in ["add", "list", "set", "change"]:
  275. if cnf["Control-Overrides::Options::%s" % (i)]:
  276. if mode:
  277. utils.fubar("Can not perform more than one action at once.")
  278. mode = i
  279. # Need an action...
  280. if mode is None:
  281. utils.fubar("No action specified.")
  282. (suite, component, otype) = (cnf["Control-Overrides::Options::Suite"],
  283. cnf["Control-Overrides::Options::Component"],
  284. cnf["Control-Overrides::Options::Type"])
  285. if mode == "list":
  286. list_overrides(suite, component, otype, session)
  287. else:
  288. if get_suite(suite).untouchable:
  289. utils.fubar("%s: suite is untouchable" % suite)
  290. action = True
  291. if cnf["Control-Overrides::Options::No-Action"]:
  292. utils.warn("In No-Action Mode")
  293. action = False
  294. Logger = daklog.Logger("control-overrides", mode)
  295. if file_list:
  296. for f in file_list:
  297. process_file(utils.open_file(f), suite, component, otype, mode, action, session)
  298. else:
  299. process_file(sys.stdin, suite, component, otype, mode, action, session)
  300. Logger.close()
  301. #######################################################################################
  302. if __name__ == '__main__':
  303. main()