utils.py 48 KB

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