utils.py 47 KB

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