utils.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. # vim:set et ts=4 sw=4:
  2. """Utility functions
  3. @contact: Debian FTP Master <ftpmaster@debian.org>
  4. @copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006 James Troup <james@nocrew.org>
  5. @license: GNU General Public License version 2 or later
  6. """
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. import datetime
  19. import os
  20. import pwd
  21. import grp
  22. import shutil
  23. import sqlalchemy.sql as sql
  24. import sys
  25. import tempfile
  26. import apt_inst
  27. import apt_pkg
  28. import re
  29. import email as modemail
  30. import subprocess
  31. import errno
  32. import functools
  33. import daklib.config as config
  34. from .dbconn import DBConn, get_architecture, get_component, get_suite, \
  35. get_active_keyring_paths, \
  36. get_suite_architectures, get_or_set_metadatakey, \
  37. Component, Override, OverrideType
  38. from .dak_exceptions import *
  39. from .gpg import SignedFile
  40. from .textutils import fix_maintainer
  41. from .regexes import re_single_line_field, \
  42. re_multi_line_field, re_srchasver, \
  43. re_re_mark, re_whitespace_comment, re_issource, \
  44. re_build_dep_arch, re_parse_maintainer
  45. from .formats import parse_format, validate_changes_format
  46. from .srcformats import get_format_from_string
  47. from collections import defaultdict
  48. ################################################################################
  49. key_uid_email_cache = {} #: Cache for email addresses from gpg key uids
  50. ################################################################################
  51. def input_or_exit(prompt=None):
  52. try:
  53. return input(prompt)
  54. except EOFError:
  55. sys.exit("\nUser interrupt (^D).")
  56. ################################################################################
  57. def extract_component_from_section(section):
  58. component = ""
  59. if section.find('/') != -1:
  60. component = section.split('/')[0]
  61. # Expand default component
  62. if component == "":
  63. component = "main"
  64. return (section, component)
  65. ################################################################################
  66. def parse_deb822(armored_contents: bytes, signing_rules=0, keyrings=None):
  67. require_signature = True
  68. if keyrings is None:
  69. keyrings = []
  70. require_signature = False
  71. signed_file = SignedFile(armored_contents, keyrings=keyrings, require_signature=require_signature)
  72. contents = signed_file.contents.decode('utf-8')
  73. error = ""
  74. changes = {}
  75. # Split the lines in the input, keeping the linebreaks.
  76. lines = contents.splitlines(True)
  77. if len(lines) == 0:
  78. raise ParseChangesError("[Empty changes file]")
  79. # Reindex by line number so we can easily verify the format of
  80. # .dsc files...
  81. index = 0
  82. indexed_lines = {}
  83. for line in lines:
  84. index += 1
  85. indexed_lines[index] = line[:-1]
  86. num_of_lines = len(indexed_lines)
  87. index = 0
  88. first = -1
  89. while index < num_of_lines:
  90. index += 1
  91. line = indexed_lines[index]
  92. if line == "" and signing_rules == 1:
  93. if index != num_of_lines:
  94. raise InvalidDscError(index)
  95. break
  96. slf = re_single_line_field.match(line)
  97. if slf:
  98. field = slf.groups()[0].lower()
  99. changes[field] = slf.groups()[1]
  100. first = 1
  101. continue
  102. if line == " .":
  103. changes[field] += '\n'
  104. continue
  105. mlf = re_multi_line_field.match(line)
  106. if mlf:
  107. if first == -1:
  108. raise ParseChangesError("'%s'\n [Multi-line field continuing on from nothing?]" % (line))
  109. if first == 1 and changes[field] != "":
  110. changes[field] += '\n'
  111. first = 0
  112. changes[field] += mlf.groups()[0] + '\n'
  113. continue
  114. error += line
  115. changes["filecontents"] = armored_contents.decode()
  116. if "source" in changes:
  117. # Strip the source version in brackets from the source field,
  118. # put it in the "source-version" field instead.
  119. srcver = re_srchasver.search(changes["source"])
  120. if srcver:
  121. changes["source"] = srcver.group(1)
  122. changes["source-version"] = srcver.group(2)
  123. if error:
  124. raise ParseChangesError(error)
  125. return changes
  126. ################################################################################
  127. def parse_changes(filename, signing_rules=0, dsc_file=0, keyrings=None):
  128. """
  129. Parses a changes file and returns a dictionary where each field is a
  130. key. The mandatory first argument is the filename of the .changes
  131. file.
  132. signing_rules is an optional argument:
  133. - If signing_rules == -1, no signature is required.
  134. - If signing_rules == 0 (the default), a signature is required.
  135. - If signing_rules == 1, it turns on the same strict format checking
  136. as dpkg-source.
  137. The rules for (signing_rules == 1)-mode are:
  138. - The PGP header consists of "-----BEGIN PGP SIGNED MESSAGE-----"
  139. followed by any PGP header data and must end with a blank line.
  140. - The data section must end with a blank line and must be followed by
  141. "-----BEGIN PGP SIGNATURE-----".
  142. """
  143. with open(filename, 'rb') as changes_in:
  144. content = changes_in.read()
  145. changes = parse_deb822(content, signing_rules, keyrings=keyrings)
  146. if not dsc_file:
  147. # Finally ensure that everything needed for .changes is there
  148. must_keywords = ('Format', 'Date', 'Source', 'Architecture', 'Version',
  149. 'Distribution', 'Maintainer', 'Changes', 'Files')
  150. missingfields = []
  151. for keyword in must_keywords:
  152. if keyword.lower() not in changes:
  153. missingfields.append(keyword)
  154. if len(missingfields):
  155. raise ParseChangesError("Missing mandatory field(s) in changes file (policy 5.5): %s" % (missingfields))
  156. return changes
  157. ################################################################################
  158. def check_dsc_files(dsc_filename, dsc, dsc_files):
  159. """
  160. Verify that the files listed in the Files field of the .dsc are
  161. those expected given the announced Format.
  162. @type dsc_filename: string
  163. @param dsc_filename: path of .dsc file
  164. @type dsc: dict
  165. @param dsc: the content of the .dsc parsed by C{parse_changes()}
  166. @type dsc_files: dict
  167. @param dsc_files: the file list returned by C{build_file_list()}
  168. @rtype: list
  169. @return: all errors detected
  170. """
  171. rejmsg = []
  172. # Ensure .dsc lists proper set of source files according to the format
  173. # announced
  174. has = defaultdict(lambda: 0)
  175. ftype_lookup = (
  176. (r'orig\.tar\.(gz|bz2|xz)\.asc', ('orig_tar_sig',)),
  177. (r'orig\.tar\.gz', ('orig_tar_gz', 'orig_tar')),
  178. (r'diff\.gz', ('debian_diff',)),
  179. (r'tar\.gz', ('native_tar_gz', 'native_tar')),
  180. (r'debian\.tar\.(gz|bz2|xz)', ('debian_tar',)),
  181. (r'orig\.tar\.(gz|bz2|xz)', ('orig_tar',)),
  182. (r'tar\.(gz|bz2|xz)', ('native_tar',)),
  183. (r'orig-.+\.tar\.(gz|bz2|xz)\.asc', ('more_orig_tar_sig',)),
  184. (r'orig-.+\.tar\.(gz|bz2|xz)', ('more_orig_tar',)),
  185. )
  186. for f in dsc_files:
  187. m = re_issource.match(f)
  188. if not m:
  189. rejmsg.append("%s: %s in Files field not recognised as source."
  190. % (dsc_filename, f))
  191. continue
  192. # Populate 'has' dictionary by resolving keys in lookup table
  193. matched = False
  194. for regex, keys in ftype_lookup:
  195. if re.match(regex, m.group(3)):
  196. matched = True
  197. for key in keys:
  198. has[key] += 1
  199. break
  200. # File does not match anything in lookup table; reject
  201. if not matched:
  202. rejmsg.append("%s: unexpected source file '%s'" % (dsc_filename, f))
  203. break
  204. # Check for multiple files
  205. for file_type in ('orig_tar', 'orig_tar_sig', 'native_tar', 'debian_tar', 'debian_diff'):
  206. if has[file_type] > 1:
  207. rejmsg.append("%s: lists multiple %s" % (dsc_filename, file_type))
  208. # Source format specific tests
  209. try:
  210. format = get_format_from_string(dsc['format'])
  211. rejmsg.extend([
  212. '%s: %s' % (dsc_filename, x) for x in format.reject_msgs(has)
  213. ])
  214. except UnknownFormatError:
  215. # Not an error here for now
  216. pass
  217. return rejmsg
  218. ################################################################################
  219. # Dropped support for 1.4 and ``buggy dchanges 3.4'' (?!) compared to di.pl
  220. def build_file_list(changes, is_a_dsc=0, field="files", hashname="md5sum"):
  221. files = {}
  222. # Make sure we have a Files: field to parse...
  223. if field not in changes:
  224. raise NoFilesFieldError
  225. # Validate .changes Format: field
  226. if not is_a_dsc:
  227. validate_changes_format(parse_format(changes['format']), field)
  228. includes_section = (not is_a_dsc) and field == "files"
  229. # Parse each entry/line:
  230. for i in changes[field].split('\n'):
  231. if not i:
  232. break
  233. s = i.split()
  234. section = priority = ""
  235. try:
  236. if includes_section:
  237. (md5, size, section, priority, name) = s
  238. else:
  239. (md5, size, name) = s
  240. except ValueError:
  241. raise ParseChangesError(i)
  242. if section == "":
  243. section = "-"
  244. if priority == "":
  245. priority = "-"
  246. (section, component) = extract_component_from_section(section)
  247. files[name] = dict(size=size, section=section,
  248. priority=priority, component=component)
  249. files[name][hashname] = md5
  250. return files
  251. ################################################################################
  252. def send_mail(message, filename="", whitelists=None):
  253. """sendmail wrapper, takes _either_ a message string or a file as arguments
  254. @type whitelists: list of (str or None)
  255. @param whitelists: path to whitelists. C{None} or an empty list whitelists
  256. everything, otherwise an address is whitelisted if it is
  257. included in any of the lists.
  258. In addition a global whitelist can be specified in
  259. Dinstall::MailWhiteList.
  260. """
  261. maildir = Cnf.get('Dir::Mail')
  262. if maildir:
  263. path = os.path.join(maildir, datetime.datetime.now().isoformat())
  264. path = find_next_free(path)
  265. with open(path, 'w') as fh:
  266. print(message, end=' ', file=fh)
  267. # Check whether we're supposed to be sending mail
  268. if "Dinstall::Options::No-Mail" in Cnf and Cnf["Dinstall::Options::No-Mail"]:
  269. return
  270. # If we've been passed a string dump it into a temporary file
  271. if message:
  272. (fd, filename) = tempfile.mkstemp()
  273. with os.fdopen(fd, 'wt') as f:
  274. f.write(message)
  275. if whitelists is None or None in whitelists:
  276. whitelists = []
  277. if Cnf.get('Dinstall::MailWhiteList', ''):
  278. whitelists.append(Cnf['Dinstall::MailWhiteList'])
  279. if len(whitelists) != 0:
  280. with open(filename) as message_in:
  281. message_raw = modemail.message_from_file(message_in)
  282. whitelist = []
  283. for path in whitelists:
  284. with open(path, 'r') as whitelist_in:
  285. for line in whitelist_in:
  286. if not re_whitespace_comment.match(line):
  287. if re_re_mark.match(line):
  288. whitelist.append(re.compile(re_re_mark.sub("", line.strip(), 1)))
  289. else:
  290. whitelist.append(re.compile(re.escape(line.strip())))
  291. # Fields to check.
  292. fields = ["To", "Bcc", "Cc"]
  293. for field in fields:
  294. # Check each field
  295. value = message_raw.get(field, None)
  296. if value is not None:
  297. match = []
  298. for item in value.split(","):
  299. (rfc822_maint, rfc2047_maint, name, email) = fix_maintainer(item.strip())
  300. mail_whitelisted = 0
  301. for wr in whitelist:
  302. if wr.match(email):
  303. mail_whitelisted = 1
  304. break
  305. if not mail_whitelisted:
  306. print("Skipping {0} since it's not whitelisted".format(item))
  307. continue
  308. match.append(item)
  309. # Doesn't have any mail in whitelist so remove the header
  310. if len(match) == 0:
  311. del message_raw[field]
  312. else:
  313. message_raw.replace_header(field, ', '.join(match))
  314. # Change message fields in order if we don't have a To header
  315. if "To" not in message_raw:
  316. fields.reverse()
  317. for field in fields:
  318. if field in message_raw:
  319. message_raw[fields[-1]] = message_raw[field]
  320. del message_raw[field]
  321. break
  322. else:
  323. # Clean up any temporary files
  324. # and return, as we removed all recipients.
  325. if message:
  326. os.unlink(filename)
  327. return
  328. fd = os.open(filename, os.O_RDWR | os.O_EXCL, 0o700)
  329. with os.fdopen(fd, 'wt') as f:
  330. f.write(message_raw.as_string(True))
  331. # Invoke sendmail
  332. try:
  333. with open(filename, 'r') as fh:
  334. subprocess.check_output(Cnf["Dinstall::SendmailCommand"].split(), stdin=fh, stderr=subprocess.STDOUT)
  335. except subprocess.CalledProcessError as e:
  336. raise SendmailFailedError(e.output.rstrip())
  337. # Clean up any temporary files
  338. if message:
  339. os.unlink(filename)
  340. ################################################################################
  341. def poolify(source):
  342. if source[:3] == "lib":
  343. return source[:4] + '/' + source + '/'
  344. else:
  345. return source[:1] + '/' + source + '/'
  346. ################################################################################
  347. def move(src, dest, overwrite=0, perms=0o664):
  348. if os.path.exists(dest) and os.path.isdir(dest):
  349. dest_dir = dest
  350. else:
  351. dest_dir = os.path.dirname(dest)
  352. if not os.path.lexists(dest_dir):
  353. umask = os.umask(00000)
  354. os.makedirs(dest_dir, 0o2775)
  355. os.umask(umask)
  356. # print "Moving %s to %s..." % (src, dest)
  357. if os.path.exists(dest) and os.path.isdir(dest):
  358. dest += '/' + os.path.basename(src)
  359. # Don't overwrite unless forced to
  360. if os.path.lexists(dest):
  361. if not overwrite:
  362. fubar("Can't move %s to %s - file already exists." % (src, dest))
  363. else:
  364. if not os.access(dest, os.W_OK):
  365. fubar("Can't move %s to %s - can't write to existing file." % (src, dest))
  366. shutil.copy2(src, dest)
  367. os.chmod(dest, perms)
  368. os.unlink(src)
  369. ################################################################################
  370. def TemplateSubst(subst_map, filename):
  371. """ Perform a substition of template """
  372. with open(filename) as templatefile:
  373. template = templatefile.read()
  374. for k, v in subst_map.items():
  375. template = template.replace(k, str(v))
  376. return template
  377. ################################################################################
  378. def fubar(msg, exit_code=1):
  379. print("E:", msg, file=sys.stderr)
  380. sys.exit(exit_code)
  381. def warn(msg):
  382. print("W:", msg, file=sys.stderr)
  383. ################################################################################
  384. # Returns the user name with a laughable attempt at rfc822 conformancy
  385. # (read: removing stray periods).
  386. def whoami():
  387. return pwd.getpwuid(os.getuid())[4].split(',')[0].replace('.', '')
  388. def getusername():
  389. return pwd.getpwuid(os.getuid())[0]
  390. ################################################################################
  391. def size_type(c):
  392. t = " B"
  393. if c > 10240:
  394. c = c / 1024
  395. t = " KB"
  396. if c > 10240:
  397. c = c / 1024
  398. t = " MB"
  399. return ("%d%s" % (c, t))
  400. ################################################################################
  401. def find_next_free(dest, too_many=100):
  402. extra = 0
  403. orig_dest = dest
  404. while os.path.lexists(dest) and extra < too_many:
  405. dest = orig_dest + '.' + repr(extra)
  406. extra += 1
  407. if extra >= too_many:
  408. raise NoFreeFilenameError
  409. return dest
  410. ################################################################################
  411. def result_join(original, sep='\t'):
  412. return sep.join(
  413. x if x is not None else ""
  414. for x in original
  415. )
  416. ################################################################################
  417. def prefix_multi_line_string(str, prefix, include_blank_lines=0):
  418. out = ""
  419. for line in str.split('\n'):
  420. line = line.strip()
  421. if line or include_blank_lines:
  422. out += "%s%s\n" % (prefix, line)
  423. # Strip trailing new line
  424. if out:
  425. out = out[:-1]
  426. return out
  427. ################################################################################
  428. def join_with_commas_and(list):
  429. if len(list) == 0:
  430. return "nothing"
  431. if len(list) == 1:
  432. return list[0]
  433. return ", ".join(list[:-1]) + " and " + list[-1]
  434. ################################################################################
  435. def pp_deps(deps):
  436. pp_deps = []
  437. for atom in deps:
  438. (pkg, version, constraint) = atom
  439. if constraint:
  440. pp_dep = "%s (%s %s)" % (pkg, constraint, version)
  441. else:
  442. pp_dep = pkg
  443. pp_deps.append(pp_dep)
  444. return " |".join(pp_deps)
  445. ################################################################################
  446. def get_conf():
  447. return Cnf
  448. ################################################################################
  449. def parse_args(Options):
  450. """ Handle -a, -c and -s arguments; returns them as SQL constraints """
  451. # XXX: This should go away and everything which calls it be converted
  452. # to use SQLA properly. For now, we'll just fix it not to use
  453. # the old Pg interface though
  454. session = DBConn().session()
  455. # Process suite
  456. if Options["Suite"]:
  457. suite_ids_list = []
  458. for suitename in split_args(Options["Suite"]):
  459. suite = get_suite(suitename, session=session)
  460. if not suite or suite.suite_id is None:
  461. warn("suite '%s' not recognised." % (suite and suite.suite_name or suitename))
  462. else:
  463. suite_ids_list.append(suite.suite_id)
  464. if suite_ids_list:
  465. con_suites = "AND su.id IN (%s)" % ", ".join([str(i) for i in suite_ids_list])
  466. else:
  467. fubar("No valid suite given.")
  468. else:
  469. con_suites = ""
  470. # Process component
  471. if Options["Component"]:
  472. component_ids_list = []
  473. for componentname in split_args(Options["Component"]):
  474. component = get_component(componentname, session=session)
  475. if component is None:
  476. warn("component '%s' not recognised." % (componentname))
  477. else:
  478. component_ids_list.append(component.component_id)
  479. if component_ids_list:
  480. con_components = "AND c.id IN (%s)" % ", ".join([str(i) for i in component_ids_list])
  481. else:
  482. fubar("No valid component given.")
  483. else:
  484. con_components = ""
  485. # Process architecture
  486. con_architectures = ""
  487. check_source = 0
  488. if Options["Architecture"]:
  489. arch_ids_list = []
  490. for archname in split_args(Options["Architecture"]):
  491. if archname == "source":
  492. check_source = 1
  493. else:
  494. arch = get_architecture(archname, session=session)
  495. if arch is None:
  496. warn("architecture '%s' not recognised." % (archname))
  497. else:
  498. arch_ids_list.append(arch.arch_id)
  499. if arch_ids_list:
  500. con_architectures = "AND a.id IN (%s)" % ", ".join([str(i) for i in arch_ids_list])
  501. else:
  502. if not check_source:
  503. fubar("No valid architecture given.")
  504. else:
  505. check_source = 1
  506. return (con_suites, con_architectures, con_components, check_source)
  507. ################################################################################
  508. @functools.total_ordering
  509. class ArchKey:
  510. """
  511. Key object for use in sorting lists of architectures.
  512. Sorts normally except that 'source' dominates all others.
  513. """
  514. __slots__ = ['arch', 'issource']
  515. def __init__(self, arch, *args):
  516. self.arch = arch
  517. self.issource = arch == 'source'
  518. def __lt__(self, other):
  519. if self.issource:
  520. return not other.issource
  521. if other.issource:
  522. return False
  523. return self.arch < other.arch
  524. def __eq__(self, other):
  525. return self.arch == other.arch
  526. ################################################################################
  527. def split_args(s, dwim=True):
  528. """
  529. Split command line arguments which can be separated by either commas
  530. or whitespace. If dwim is set, it will complain about string ending
  531. in comma since this usually means someone did 'dak ls -a i386, m68k
  532. foo' or something and the inevitable confusion resulting from 'm68k'
  533. being treated as an argument is undesirable.
  534. """
  535. if s.find(",") == -1:
  536. return s.split()
  537. else:
  538. if s[-1:] == "," and dwim:
  539. fubar("split_args: found trailing comma, spurious space maybe?")
  540. return s.split(",")
  541. ################################################################################
  542. def gpg_keyring_args(keyrings=None):
  543. if not keyrings:
  544. keyrings = get_active_keyring_paths()
  545. return ["--keyring={}".format(path) for path in keyrings]
  546. ################################################################################
  547. def _gpg_get_addresses_from_listing(output: bytes):
  548. addresses = []
  549. for line in output.split(b'\n'):
  550. parts = line.split(b':')
  551. if parts[0] not in (b"uid", b"pub"):
  552. continue
  553. if parts[1] in (b"i", b"d", b"r"):
  554. # Skip uid that is invalid, disabled or revoked
  555. continue
  556. try:
  557. uid = parts[9]
  558. except IndexError:
  559. continue
  560. try:
  561. uid = uid.decode(encoding='utf-8')
  562. except UnicodeDecodeError:
  563. # If the uid is not valid UTF-8, we assume it is an old uid
  564. # still encoding in Latin-1.
  565. uid = uid.decode(encoding='latin1')
  566. m = re_parse_maintainer.match(uid)
  567. if not m:
  568. continue
  569. address = m.group(2)
  570. if address.endswith('@debian.org'):
  571. # prefer @debian.org addresses
  572. # TODO: maybe not hardcode the domain
  573. addresses.insert(0, address)
  574. else:
  575. addresses.append(address)
  576. return addresses
  577. def gpg_get_key_addresses(fingerprint):
  578. """retreive email addresses from gpg key uids for a given fingerprint"""
  579. addresses = key_uid_email_cache.get(fingerprint)
  580. if addresses is not None:
  581. return addresses
  582. try:
  583. cmd = ["gpg", "--no-default-keyring"]
  584. cmd.extend(gpg_keyring_args())
  585. cmd.extend(["--with-colons", "--list-keys", "--", fingerprint])
  586. output = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
  587. except subprocess.CalledProcessError:
  588. addresses = []
  589. else:
  590. addresses = _gpg_get_addresses_from_listing(output)
  591. key_uid_email_cache[fingerprint] = addresses
  592. return addresses
  593. ################################################################################
  594. def open_ldap_connection():
  595. """open connection to the configured LDAP server"""
  596. import ldap
  597. LDAPDn = Cnf["Import-LDAP-Fingerprints::LDAPDn"]
  598. LDAPServer = Cnf["Import-LDAP-Fingerprints::LDAPServer"]
  599. ca_cert_file = Cnf.get('Import-LDAP-Fingerprints::CACertFile')
  600. l = ldap.initialize(LDAPServer)
  601. if ca_cert_file:
  602. l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_HARD)
  603. l.set_option(ldap.OPT_X_TLS_CACERTFILE, ca_cert_file)
  604. l.set_option(ldap.OPT_X_TLS_NEWCTX, True)
  605. l.start_tls_s()
  606. l.simple_bind_s("", "")
  607. return l
  608. ################################################################################
  609. def get_logins_from_ldap(fingerprint='*'):
  610. """retrieve login from LDAP linked to a given fingerprint"""
  611. import ldap
  612. l = open_ldap_connection()
  613. LDAPDn = Cnf["Import-LDAP-Fingerprints::LDAPDn"]
  614. Attrs = l.search_s(LDAPDn, ldap.SCOPE_ONELEVEL,
  615. '(keyfingerprint=%s)' % fingerprint,
  616. ['uid', 'keyfingerprint'])
  617. login = {}
  618. for elem in Attrs:
  619. fpr = elem[1]['keyFingerPrint'][0].decode()
  620. uid = elem[1]['uid'][0].decode()
  621. login[fpr] = uid
  622. return login
  623. ################################################################################
  624. def get_users_from_ldap():
  625. """retrieve login and user names from LDAP"""
  626. import ldap
  627. l = open_ldap_connection()
  628. LDAPDn = Cnf["Import-LDAP-Fingerprints::LDAPDn"]
  629. Attrs = l.search_s(LDAPDn, ldap.SCOPE_ONELEVEL,
  630. '(uid=*)', ['uid', 'cn', 'mn', 'sn'])
  631. users = {}
  632. for elem in Attrs:
  633. elem = elem[1]
  634. name = []
  635. for k in ('cn', 'mn', 'sn'):
  636. try:
  637. value = elem[k][0].decode()
  638. if value and value[0] != '-':
  639. name.append(value)
  640. except KeyError:
  641. pass
  642. users[' '.join(name)] = elem['uid'][0]
  643. return users
  644. ################################################################################
  645. def clean_symlink(src, dest, root):
  646. """
  647. Relativize an absolute symlink from 'src' -> 'dest' relative to 'root'.
  648. Returns fixed 'src'
  649. """
  650. src = src.replace(root, '', 1)
  651. dest = dest.replace(root, '', 1)
  652. dest = os.path.dirname(dest)
  653. new_src = '../' * len(dest.split('/'))
  654. return new_src + src
  655. ################################################################################
  656. def temp_dirname(parent=None, prefix="dak", suffix="", mode=None, group=None):
  657. """
  658. Return a secure and unique directory by pre-creating it.
  659. @type parent: str
  660. @param parent: If non-null it will be the directory the directory is pre-created in.
  661. @type prefix: str
  662. @param prefix: The filename will be prefixed with this string
  663. @type suffix: str
  664. @param suffix: The filename will end with this string
  665. @type mode: str
  666. @param mode: If set the file will get chmodded to those permissions
  667. @type group: str
  668. @param group: If set the file will get chgrped to the specified group.
  669. @rtype: list
  670. @return: Returns a pair (fd, name)
  671. """
  672. tfname = tempfile.mkdtemp(suffix, prefix, parent)
  673. if mode:
  674. os.chmod(tfname, mode)
  675. if group:
  676. gid = grp.getgrnam(group).gr_gid
  677. os.chown(tfname, -1, gid)
  678. return tfname
  679. ################################################################################
  680. def get_changes_files(from_dir):
  681. """
  682. Takes a directory and lists all .changes files in it (as well as chdir'ing
  683. to the directory; this is due to broken behaviour on the part of p-u/p-a
  684. when you're not in the right place)
  685. Returns a list of filenames
  686. """
  687. try:
  688. # Much of the rest of p-u/p-a depends on being in the right place
  689. os.chdir(from_dir)
  690. changes_files = [x for x in os.listdir(from_dir) if x.endswith('.changes')]
  691. except OSError as e:
  692. fubar("Failed to read list from directory %s (%s)" % (from_dir, e))
  693. return changes_files
  694. ################################################################################
  695. Cnf = config.Config().Cnf
  696. ################################################################################
  697. def parse_wnpp_bug_file(file="/srv/ftp-master.debian.org/scripts/masterfiles/wnpp_rm"):
  698. """
  699. Parses the wnpp bug list available at https://qa.debian.org/data/bts/wnpp_rm
  700. Well, actually it parsed a local copy, but let's document the source
  701. somewhere ;)
  702. returns a dict associating source package name with a list of open wnpp
  703. bugs (Yes, there might be more than one)
  704. """
  705. try:
  706. with open(file) as f:
  707. lines = f.readlines()
  708. except OSError:
  709. print("Warning: Couldn't open %s; don't know about WNPP bugs, so won't close any." % file)
  710. lines = []
  711. wnpp = {}
  712. for line in lines:
  713. splited_line = line.split(": ", 1)
  714. if len(splited_line) > 1:
  715. wnpp[splited_line[0]] = splited_line[1].split("|")
  716. for source in wnpp:
  717. bugs = []
  718. for wnpp_bug in wnpp[source]:
  719. bug_no = re.search(r"(\d)+", wnpp_bug).group()
  720. if bug_no:
  721. bugs.append(bug_no)
  722. wnpp[source] = bugs
  723. return wnpp
  724. ################################################################################
  725. def deb_extract_control(path):
  726. """extract DEBIAN/control from a binary package"""
  727. return apt_inst.DebFile(path).control.extractdata("control")
  728. ################################################################################
  729. def mail_addresses_for_upload(maintainer, changed_by, fingerprint):
  730. """mail addresses to contact for an upload
  731. @type maintainer: str
  732. @param maintainer: Maintainer field of the .changes file
  733. @type changed_by: str
  734. @param changed_by: Changed-By field of the .changes file
  735. @type fingerprint: str
  736. @param fingerprint: fingerprint of the key used to sign the upload
  737. @rtype: list of str
  738. @return: list of RFC 2047-encoded mail addresses to contact regarding
  739. this upload
  740. """
  741. recipients = Cnf.value_list('Dinstall::UploadMailRecipients')
  742. if not recipients:
  743. recipients = [
  744. 'maintainer',
  745. 'changed_by',
  746. 'signer',
  747. ]
  748. # Ensure signer is last if present
  749. try:
  750. recipients.remove('signer')
  751. recipients.append('signer')
  752. except ValueError:
  753. pass
  754. # Compute the set of addresses of the recipients
  755. addresses = set() # Name + email
  756. emails = set() # Email only, used to avoid duplicates
  757. for recipient in recipients:
  758. if recipient.startswith('mail:'): # Email hardcoded in config
  759. address = recipient[5:]
  760. elif recipient == 'maintainer':
  761. address = maintainer
  762. elif recipient == 'changed_by':
  763. address = changed_by
  764. elif recipient == 'signer':
  765. fpr_addresses = gpg_get_key_addresses(fingerprint)
  766. address = fpr_addresses[0] if fpr_addresses else None
  767. if any(x in emails for x in fpr_addresses):
  768. # The signer already gets a copy via another email
  769. address = None
  770. else:
  771. raise Exception('Unsupported entry in {0}: {1}'.format(
  772. 'Dinstall::UploadMailRecipients', recipient))
  773. if address is not None:
  774. email = fix_maintainer(address)[3]
  775. if email not in emails:
  776. addresses.add(address)
  777. emails.add(email)
  778. encoded_addresses = [fix_maintainer(e)[1] for e in addresses]
  779. return encoded_addresses
  780. ################################################################################
  781. def call_editor_for_file(path):
  782. editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'sensible-editor'))
  783. subprocess.check_call([editor, path])
  784. ################################################################################
  785. def call_editor(text="", suffix=".txt"):
  786. """run editor and return the result as a string
  787. @type text: str
  788. @param text: initial text
  789. @type suffix: str
  790. @param suffix: extension for temporary file
  791. @rtype: str
  792. @return: string with the edited text
  793. """
  794. with tempfile.NamedTemporaryFile(mode='w+t', suffix=suffix) as fh:
  795. print(text, end='', file=fh)
  796. fh.flush()
  797. call_editor_for_file(fh.name)
  798. fh.seek(0)
  799. return fh.read()
  800. ################################################################################
  801. def check_reverse_depends(removals, suite, arches=None, session=None, cruft=False, quiet=False, include_arch_all=True):
  802. dbsuite = get_suite(suite, session)
  803. overridesuite = dbsuite
  804. if dbsuite.overridesuite is not None:
  805. overridesuite = get_suite(dbsuite.overridesuite, session)
  806. dep_problem = 0
  807. p2c = {}
  808. all_broken = defaultdict(lambda: defaultdict(set))
  809. if arches:
  810. all_arches = set(arches)
  811. else:
  812. all_arches = set(x.arch_string for x in get_suite_architectures(suite))
  813. all_arches -= set(["source", "all"])
  814. removal_set = set(removals)
  815. metakey_d = get_or_set_metadatakey("Depends", session)
  816. metakey_p = get_or_set_metadatakey("Provides", session)
  817. params = {
  818. 'suite_id': dbsuite.suite_id,
  819. 'metakey_d_id': metakey_d.key_id,
  820. 'metakey_p_id': metakey_p.key_id,
  821. }
  822. if include_arch_all:
  823. rdep_architectures = all_arches | set(['all'])
  824. else:
  825. rdep_architectures = all_arches
  826. for architecture in rdep_architectures:
  827. deps = {}
  828. sources = {}
  829. virtual_packages = {}
  830. try:
  831. params['arch_id'] = get_architecture(architecture, session).arch_id
  832. except AttributeError:
  833. continue
  834. statement = sql.text('''
  835. SELECT b.package, s.source, c.name as component,
  836. (SELECT bmd.value FROM binaries_metadata bmd WHERE bmd.bin_id = b.id AND bmd.key_id = :metakey_d_id) AS depends,
  837. (SELECT bmp.value FROM binaries_metadata bmp WHERE bmp.bin_id = b.id AND bmp.key_id = :metakey_p_id) AS provides
  838. FROM binaries b
  839. JOIN bin_associations ba ON b.id = ba.bin AND ba.suite = :suite_id
  840. JOIN source s ON b.source = s.id
  841. JOIN files_archive_map af ON b.file = af.file_id
  842. JOIN component c ON af.component_id = c.id
  843. WHERE b.architecture = :arch_id''')
  844. query = session.query(sql.column('package'), sql.column('source'),
  845. sql.column('component'), sql.column('depends'),
  846. sql.column('provides')). \
  847. from_statement(statement).params(params)
  848. for package, source, component, depends, provides in query:
  849. sources[package] = source
  850. p2c[package] = component
  851. if depends is not None:
  852. deps[package] = depends
  853. # Maintain a counter for each virtual package. If a
  854. # Provides: exists, set the counter to 0 and count all
  855. # provides by a package not in the list for removal.
  856. # If the counter stays 0 at the end, we know that only
  857. # the to-be-removed packages provided this virtual
  858. # package.
  859. if provides is not None:
  860. for virtual_pkg in provides.split(","):
  861. virtual_pkg = virtual_pkg.strip()
  862. if virtual_pkg == package:
  863. continue
  864. if virtual_pkg not in virtual_packages:
  865. virtual_packages[virtual_pkg] = 0
  866. if package not in removals:
  867. virtual_packages[virtual_pkg] += 1
  868. # If a virtual package is only provided by the to-be-removed
  869. # packages, treat the virtual package as to-be-removed too.
  870. removal_set.update(virtual_pkg for virtual_pkg in virtual_packages if not virtual_packages[virtual_pkg])
  871. # Check binary dependencies (Depends)
  872. for package in deps:
  873. if package in removals:
  874. continue
  875. try:
  876. parsed_dep = apt_pkg.parse_depends(deps[package])
  877. except ValueError as e:
  878. print("Error for package %s: %s" % (package, e))
  879. parsed_dep = []
  880. for dep in parsed_dep:
  881. # Check for partial breakage. If a package has a ORed
  882. # dependency, there is only a dependency problem if all
  883. # packages in the ORed depends will be removed.
  884. unsat = 0
  885. for dep_package, _, _ in dep:
  886. if dep_package in removals:
  887. unsat += 1
  888. if unsat == len(dep):
  889. component = p2c[package]
  890. source = sources[package]
  891. if component != "main":
  892. source = "%s/%s" % (source, component)
  893. all_broken[source][package].add(architecture)
  894. dep_problem = 1
  895. if all_broken and not quiet:
  896. if cruft:
  897. print(" - broken Depends:")
  898. else:
  899. print("# Broken Depends:")
  900. for source, bindict in sorted(all_broken.items()):
  901. lines = []
  902. for binary, arches in sorted(bindict.items()):
  903. if arches == all_arches or 'all' in arches:
  904. lines.append(binary)
  905. else:
  906. lines.append('%s [%s]' % (binary, ' '.join(sorted(arches))))
  907. if cruft:
  908. print(' %s: %s' % (source, lines[0]))
  909. else:
  910. print('%s: %s' % (source, lines[0]))
  911. for line in lines[1:]:
  912. if cruft:
  913. print(' ' + ' ' * (len(source) + 2) + line)
  914. else:
  915. print(' ' * (len(source) + 2) + line)
  916. if not cruft:
  917. print()
  918. # Check source dependencies (Build-Depends and Build-Depends-Indep)
  919. all_broken = defaultdict(set)
  920. metakey_bd = get_or_set_metadatakey("Build-Depends", session)
  921. metakey_bdi = get_or_set_metadatakey("Build-Depends-Indep", session)
  922. if include_arch_all:
  923. metakey_ids = (metakey_bd.key_id, metakey_bdi.key_id)
  924. else:
  925. metakey_ids = (metakey_bd.key_id,)
  926. params = {
  927. 'suite_id': dbsuite.suite_id,
  928. 'metakey_ids': metakey_ids,
  929. }
  930. statement = sql.text('''
  931. SELECT s.source, string_agg(sm.value, ', ') as build_dep
  932. FROM source s
  933. JOIN source_metadata sm ON s.id = sm.src_id
  934. WHERE s.id in
  935. (SELECT src FROM newest_src_association
  936. WHERE suite = :suite_id)
  937. AND sm.key_id in :metakey_ids
  938. GROUP BY s.id, s.source''')
  939. query = session.query(sql.column('source'), sql.column('build_dep')) \
  940. .from_statement(statement).params(params)
  941. for source, build_dep in query:
  942. if source in removals:
  943. continue
  944. parsed_dep = []
  945. if build_dep is not None:
  946. # Remove [arch] information since we want to see breakage on all arches
  947. build_dep = re_build_dep_arch.sub("", build_dep)
  948. try:
  949. parsed_dep = apt_pkg.parse_src_depends(build_dep)
  950. except ValueError as e:
  951. print("Error for source %s: %s" % (source, e))
  952. for dep in parsed_dep:
  953. unsat = 0
  954. for dep_package, _, _ in dep:
  955. if dep_package in removals:
  956. unsat += 1
  957. if unsat == len(dep):
  958. component, = session.query(Component.component_name) \
  959. .join(Component.overrides) \
  960. .filter(Override.suite == overridesuite) \
  961. .filter(Override.package == re.sub('/(contrib|non-free-firmware|non-free)$', '', source)) \
  962. .join(Override.overridetype).filter(OverrideType.overridetype == 'dsc') \
  963. .first()
  964. key = source
  965. if component != "main":
  966. key = "%s/%s" % (source, component)
  967. all_broken[key].add(pp_deps(dep))
  968. dep_problem = 1
  969. if all_broken and not quiet:
  970. if cruft:
  971. print(" - broken Build-Depends:")
  972. else:
  973. print("# Broken Build-Depends:")
  974. for source, bdeps in sorted(all_broken.items()):
  975. bdeps = sorted(bdeps)
  976. if cruft:
  977. print(' %s: %s' % (source, bdeps[0]))
  978. else:
  979. print('%s: %s' % (source, bdeps[0]))
  980. for bdep in bdeps[1:]:
  981. if cruft:
  982. print(' ' + ' ' * (len(source) + 2) + bdep)
  983. else:
  984. print(' ' * (len(source) + 2) + bdep)
  985. if not cruft:
  986. print()
  987. return dep_problem
  988. ################################################################################
  989. def parse_built_using(control):
  990. """source packages referenced via Built-Using
  991. @type control: dict-like
  992. @param control: control file to take Built-Using field from
  993. @rtype: list of (str, str)
  994. @return: list of (source_name, source_version) pairs
  995. """
  996. built_using = control.get('Built-Using', None)
  997. if built_using is None:
  998. return []
  999. bu = []
  1000. for dep in apt_pkg.parse_depends(built_using):
  1001. assert len(dep) == 1, 'Alternatives are not allowed in Built-Using field'
  1002. source_name, source_version, comp = dep[0]
  1003. assert comp == '=', 'Built-Using must contain strict dependencies'
  1004. bu.append((source_name, source_version))
  1005. return bu
  1006. ################################################################################
  1007. def is_in_debug_section(control):
  1008. """binary package is a debug package
  1009. @type control: dict-like
  1010. @param control: control file of binary package
  1011. @rtype Boolean
  1012. @return: True if the binary package is a debug package
  1013. """
  1014. section = control['Section'].split('/', 1)[-1]
  1015. auto_built_package = control.get("Auto-Built-Package")
  1016. return section == "debug" and auto_built_package == "debug-symbols"
  1017. ################################################################################
  1018. def find_possibly_compressed_file(filename):
  1019. """
  1020. @type filename: string
  1021. @param filename: path to a control file (Sources, Packages, etc) to
  1022. look for
  1023. @rtype string
  1024. @return: path to the (possibly compressed) control file, or null if the
  1025. file doesn't exist
  1026. """
  1027. _compressions = ('', '.xz', '.gz', '.bz2')
  1028. for ext in _compressions:
  1029. _file = filename + ext
  1030. if os.path.exists(_file):
  1031. return _file
  1032. raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
  1033. ################################################################################
  1034. def parse_boolean_from_user(value):
  1035. value = value.lower()
  1036. if value in {'yes', 'true', 'enable', 'enabled'}:
  1037. return True
  1038. if value in {'no', 'false', 'disable', 'disabled'}:
  1039. return False
  1040. raise ValueError("Not sure whether %s should be a True or a False" % value)
  1041. def suite_suffix(suite_name):
  1042. """Return suite_suffix for the given suite"""
  1043. suffix = Cnf.find('Dinstall::SuiteSuffix', '')
  1044. if suffix == '':
  1045. return ''
  1046. elif 'Dinstall::SuiteSuffixSuites' not in Cnf:
  1047. # TODO: warn (once per run) that SuiteSuffix will be deprecated in the future
  1048. return suffix
  1049. elif suite_name in Cnf.value_list('Dinstall::SuiteSuffixSuites'):
  1050. return suffix
  1051. return ''
  1052. ################################################################################
  1053. def process_buildinfos(directory, buildinfo_files, fs_transaction, logger):
  1054. """Copy buildinfo files into Dir::BuildinfoArchive
  1055. @type directory: string
  1056. @param directory: directory where .changes is stored
  1057. @type buildinfo_files: list of str
  1058. @param buildinfo_files: names of buildinfo files
  1059. @type fs_transaction: L{daklib.fstransactions.FilesystemTransaction}
  1060. @param fs_transaction: FilesystemTransaction instance
  1061. @type logger: L{daklib.daklog.Logger}
  1062. @param logger: logger instance
  1063. """
  1064. if 'Dir::BuildinfoArchive' not in Cnf:
  1065. return
  1066. target_dir = os.path.join(
  1067. Cnf['Dir::BuildinfoArchive'],
  1068. datetime.datetime.now().strftime('%Y/%m/%d'),
  1069. )
  1070. for f in buildinfo_files:
  1071. src = os.path.join(directory, f.filename)
  1072. dst = find_next_free(os.path.join(target_dir, f.filename))
  1073. logger.log(["Archiving", f.filename])
  1074. fs_transaction.copy(src, dst, mode=0o644)
  1075. ################################################################################
  1076. def move_to_morgue(morguesubdir, filenames, fs_transaction, logger):
  1077. """Move a file to the correct dir in morgue
  1078. @type morguesubdir: string
  1079. @param morguesubdir: subdirectory of morgue where this file needs to go
  1080. @type filenames: list of str
  1081. @param filenames: names of files
  1082. @type fs_transaction: L{daklib.fstransactions.FilesystemTransaction}
  1083. @param fs_transaction: FilesystemTransaction instance
  1084. @type logger: L{daklib.daklog.Logger}
  1085. @param logger: logger instance
  1086. """
  1087. morguedir = Cnf.get("Dir::Morgue", os.path.join(
  1088. Cnf.get("Dir::Base"), 'morgue'))
  1089. # Build directory as morguedir/morguesubdir/year/month/day
  1090. now = datetime.datetime.now()
  1091. dest = os.path.join(morguedir,
  1092. morguesubdir,
  1093. str(now.year),
  1094. '%.2d' % now.month,
  1095. '%.2d' % now.day)
  1096. for filename in filenames:
  1097. dest_filename = dest + '/' + os.path.basename(filename)
  1098. # If the destination file exists; try to find another filename to use
  1099. if os.path.lexists(dest_filename):
  1100. dest_filename = find_next_free(dest_filename)
  1101. logger.log(["move to morgue", filename, dest_filename])
  1102. fs_transaction.move(filename, dest_filename)