From: James Troup Date: Thu, 1 Apr 2004 17:13:10 +0000 (+0000) Subject: 2004-04-01 James Troup * utils.py (temp_filename): new helper... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd377d74e836353932847343062f114edd1140a4;p=dak 2004-04-01 James Troup * utils.py (temp_filename): new helper function that wraps around tempfile.mktemp(). * lisa (prod_maintainer): likewise. (edit_note): likewise. (edit_new): likewise. * lauren (reject): likewise. * melanie (main): likewise. * neve (do_sources): likewise. * rene (main): likewise. * tea (validate_sources): likewise.2004-03-31 James Troup * tea (validate_sources): remove unused 's' temporary variable. --- diff --git a/lauren b/lauren index 7498f91d..72bd8800 100755 --- a/lauren +++ b/lauren @@ -1,8 +1,8 @@ #!/usr/bin/env python # Manually reject packages for proprosed-updates -# Copyright (C) 2001, 2002, 2003 James Troup -# $Id: lauren,v 1.3 2003-11-20 02:37:25 troup Exp $ +# Copyright (C) 2001, 2002, 2003, 2004 James Troup +# $Id: lauren,v 1.4 2004-04-01 17:13:11 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,14 +20,14 @@ ################################################################################ -import os, pg, sys, tempfile; +import os, pg, sys; import db_access, katie, logging, utils; import apt_pkg; ################################################################################ # Globals -lauren_version = "$Revision: 1.3 $"; +lauren_version = "$Revision: 1.4 $"; Cnf = None; Options = None; @@ -123,9 +123,7 @@ def reject (reject_message = ""): # If we weren't given a manual rejection message, spawn an editor # so the user can add one in... if not reject_message: - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); editor = os.environ.get("EDITOR","vi") answer = 'E'; while answer == 'E': diff --git a/lisa b/lisa index 4adeeb5a..99319f55 100755 --- a/lisa +++ b/lisa @@ -2,7 +2,7 @@ # Handles NEW and BYHAND packages # Copyright (C) 2001, 2002, 2003, 2004 James Troup -# $Id: lisa,v 1.29 2004-03-11 00:14:32 troup Exp $ +# $Id: lisa,v 1.30 2004-04-01 17:13:11 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -37,12 +37,12 @@ ################################################################################ -import copy, errno, os, readline, stat, sys, tempfile; +import copy, errno, os, readline, stat, sys; import apt_pkg, apt_inst; import db_access, fernanda, katie, logging, utils; # Globals -lisa_version = "$Revision: 1.29 $"; +lisa_version = "$Revision: 1.30 $"; Cnf = None; Options = None; @@ -394,9 +394,7 @@ def index_range (index): def edit_new (new): # Write the current data to a temporary file - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); temp_file = utils.open_file(temp_filename, 'w'); print_new (new, 0, temp_file); temp_file.close(); @@ -542,9 +540,7 @@ def edit_overrides (new): def edit_note(note): # Write the current data to a temporary file - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); temp_file = utils.open_file(temp_filename, 'w'); temp_file.write(note); temp_file.close(); @@ -648,9 +644,7 @@ def add_overrides (new): def prod_maintainer (): # Here we prepare an editor and get them ready to prod... - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); editor = os.environ.get("EDITOR","vi") answer = 'E'; while answer == 'E': @@ -670,7 +664,7 @@ def prod_maintainer (): answer = answer[:1].upper(); os.unlink(temp_filename); if answer == 'A': - return 1; + return; elif answer == 'Q': sys.exit(0); # Otherwise, do the proding... @@ -678,7 +672,7 @@ def prod_maintainer (): Cnf["Dinstall::MyAdminAddress"]); Subst = Katie.Subst; - + Subst["__FROM_ADDRESS__"] = user_email_address; Subst["__PROD_MESSAGE__"] = prod_message; Subst["__CC__"] = "Cc: " + Cnf["Dinstall::MyEmailAddress"]; diff --git a/melanie b/melanie index 38b81b37..23b89e54 100755 --- a/melanie +++ b/melanie @@ -1,8 +1,8 @@ #!/usr/bin/env python # General purpose package removal tool for ftpmaster -# Copyright (C) 2000, 2001, 2002, 2003 James Troup -# $Id: melanie,v 1.39 2003-05-02 13:53:57 troup Exp $ +# Copyright (C) 2000, 2001, 2002, 2003, 2004 James Troup +# $Id: melanie,v 1.40 2004-04-01 17:13:11 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -40,8 +40,8 @@ ################################################################################ -import commands, os, pg, re, sys, tempfile -import utils, db_access +import commands, os, pg, re, sys; +import utils, db_access; import apt_pkg, apt_inst; ################################################################################ @@ -262,9 +262,7 @@ def main (): # If we don't have a reason; spawn an editor so the user can add one # Write the rejection email out as the .reason file if not Options["Reason"] and not Options["No-Action"]: - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); editor = os.environ.get("EDITOR","vi") result = os.system("%s %s" % (editor, temp_filename)) if result != 0: @@ -377,7 +375,7 @@ def main (): Subst["__BCC__"] = "Bcc: " + ", ".join(bcc); else: Subst["__BCC__"] = "X-Filler: 42"; - Subst["__CC__"] = "X-Katie: melanie $Revision: 1.39 $"; + Subst["__CC__"] = "X-Katie: melanie $Revision: 1.40 $"; if carbon_copy: Subst["__CC__"] += "\nCc: " + ", ".join(carbon_copy); Subst["__SUITE_LIST__"] = suites_list; diff --git a/neve b/neve index a1f6c8cf..9b89b6b6 100755 --- a/neve +++ b/neve @@ -1,8 +1,8 @@ #!/usr/bin/env python # Populate the DB -# Copyright (C) 2000, 2001, 2002, 2003 James Troup -# $Id: neve,v 1.18 2003-02-07 14:53:42 troup Exp $ +# Copyright (C) 2000, 2001, 2002, 2003, 2004 James Troup +# $Id: neve,v 1.19 2004-04-01 17:13:11 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ ############################################################################### -import commands, os, pg, re, sys, tempfile, time; +import commands, os, pg, re, sys, time; import apt_pkg; import db_access, utils; @@ -474,9 +474,7 @@ def process_packages (filename, suite, component, archive): ############################################################################### def do_sources(sources, suite, component, server): - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (sources, temp_filename)); if (result != 0): utils.fubar("Gunzip invocation failed!\n%s" % (output), result); diff --git a/tea b/tea index 21b6c43f..3430b123 100755 --- a/tea +++ b/tea @@ -1,8 +1,8 @@ #!/usr/bin/env python # Various different sanity checks -# Copyright (C) 2000, 2001, 2002, 2003 James Troup -# $Id: tea,v 1.26 2003-10-17 11:20:47 troup Exp $ +# Copyright (C) 2000, 2001, 2002, 2003, 2004 James Troup +# $Id: tea,v 1.27 2004-04-01 17:13:11 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -27,7 +27,7 @@ ################################################################################ -import commands, os, pg, stat, string, sys, tempfile, time; +import commands, os, pg, stat, string, sys, time; import db_access, utils; import apt_pkg, apt_inst; @@ -284,9 +284,7 @@ def validate_sources(suite, component): filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component); print "Processing %s..." % (filename); # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance... - temp_filename = tempfile.mktemp(); - fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700); - os.close(fd); + temp_filename = utils.temp_filename(); (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename)); if (result != 0): sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output)); @@ -298,8 +296,7 @@ def validate_sources(suite, component): directory = Sources.Section.Find('Directory'); files = Sources.Section.Find('Files'); for i in files.split('\n'): - s = i.split(); - (md5, size, name) = s; + (md5, size, name) = i.split(); filename = "%s/%s/%s" % (Cnf["Dir::Root"], directory, name); if not os.path.exists(filename): if directory.find("potato") == -1: @@ -364,7 +361,7 @@ def check_files_not_symlinks(): # q = projectB.query("BEGIN WORK"); for i in q_files: filename = os.path.normpath(i[0] + i[1]); - file_id = i[2]; +# file_id = i[2]; if os.access(filename, os.R_OK) == 0: utils.warn("%s: doesn't exist." % (filename)); else: diff --git a/utils.py b/utils.py index c537bf8f..8445fd4d 100644 --- a/utils.py +++ b/utils.py @@ -2,7 +2,7 @@ # Utility functions # Copyright (C) 2000, 2001, 2002, 2003, 2004 James Troup -# $Id: utils.py,v 1.64 2004-03-11 00:20:51 troup Exp $ +# $Id: utils.py,v 1.65 2004-04-01 17:13:10 troup Exp $ ################################################################################ @@ -928,6 +928,29 @@ def clean_symlink (src, dest, root): ################################################################################ +def temp_filename(directory=None, dotprefix=None, perms=0700): + """Return a secure and unique filename by pre-creating it. +If 'directory' is non-null, it will be the directory the file is pre-created in. +If 'dotprefix' is non-null, the filename will be prefixed with a '.'.""" + + if directory: + old_tempdir = tempfile.tempdir; + tempfile.tempdir = directory; + + filename = tempfile.mktemp(); + + if dotprefix: + filename = "%s/.%s" % (os.path.dirname(filename), os.path.basename(filename)); + fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, perms); + os.close(fd); + + if directory: + tempfile.tempdir = old_tempdir; + + return filename; + +################################################################################ + apt_pkg.init(); Cnf = apt_pkg.newConfiguration();