2004-09-21 Akim Demaille <akim(a)epita.fr>
Have a single tmp dir for each run.
* prcs2svn/prcs2svn.py (TempDir): New.
Use it instead of tempfile functions.
The calling convention is not nice, if someone can improve it
(using class methods), be my guest.
Index: prcs2svn/prcs2svn.py
--- prcs2svn/prcs2svn.py (revision 102)
+++ prcs2svn/prcs2svn.py (working copy)
@@ -14,6 +14,28 @@
## -------------------------------------
+## Temporary directory
+class TempDir:
+
+ base = tempfile.mkdtemp ("", "prcs2svn")
+
+ def mkdir (self, name):
+ dirname = os.path.join (self.base, name)
+ # Ignore rm errors: the file might be fresh.
+ shutil.rmtree (dirname, True)
+ os.mkdir (dirname)
+ return dirname
+
+ def mkfile (self, name):
+ fname = os.path.join (self.base, name)
+ # Ignore rm errors: the file might be fresh.
+ shutil.rmtree (fname, True)
+ return (open (fname, "wb"), fname)
+
+ def mksfile (self, name):
+ return tempfile.mkstemp ("", name, self.base)
+
+## -------------------------------------
## Errors
class AbstractError:
@@ -429,7 +451,7 @@
svn_revision=""):
self.prcs_project = prcs_project
self.svn_project = svn_project
- self.tmp = tempfile.mkdtemp(".diff", "prcs2svn")
+ self.tmp = TempDir().mkdir ("%s.prcs-%s.svn.diff" % (prcs_project,
svn_project))
xchdir(self.tmp)
if svn_revision != "":
svn_revision = " -r " + svn_revision + " "
@@ -527,7 +549,7 @@
if stat.S_ISDIR(st.st_mode):
self.change_keywords_(os.path.join(path, f))
else:
- tmp = tempfile.mkdtemp(".keywords", "prcs2svn")
+ tmp = TempDir().mkdir ("prcs2svn.keywords")
atime = st[stat.ST_ATIME]
mtime = st[stat.ST_MTIME]
xcopy(f, tmp)
@@ -555,9 +577,6 @@
def changes_commit(self, checkin_login, version_log, checkin_time, update=True):
info("> Commit changes")
# Write version log in a temporary file.
- log_fd, log_path = tempfile.mkstemp(".log", "prcs2svn")
- os.write(log_fd, version_log)
- os.close(log_fd)
# Do commit.
# FIXME: `encoding' must be a command line option.
self.change_keywords(self.workdir)
@@ -575,13 +594,13 @@
env = "LD_LIBRARY_PATH=" + infos.timelib_path + " "
env += "LD_PRELOAD=libprcs2svntime.so "
r = xtee(env + "svn commit --encoding latin1 " + \
- "--username " + checkin_login + " --file " + log_path)
+ "--username " + quote (checkin_login) + \
+ " --message " + quote (version_log))
if r > 0:
self.revision += 1
# Refresh local copy (should be useless, may be a bug of svn).
if update:
xsystem("svn update")
- xrm(log_path)
# Replace symbolic links with the files they point to.
def changes_apply_links_replace(self, diffs):
@@ -628,15 +647,17 @@
backup = {}
# Process the sorted list backward for copy consistency.
for i in reversed(range(len(diffs.renamed))):
+ to = diffs.renamed[i][0]
+ frm = diffs.renamed[i][1]
# Backup files that may be overwritten.
- if os.path.exists(diffs.renamed[i][0]):
+ if os.path.exists (to):
# Destination exists: PRCS renamed then added an homonym file.
- bak_fd, bak_path = tempfile.mkstemp(".bak", "prcs2svn")
+ bak_fd, bak_path = TempDir().mksfile ("%s.bak" % to)
os.close(bak_fd)
- xmove(diffs.renamed[i][0], bak_path)
- backup[diffs.renamed[i][0]] = bak_path
+ xmove (to, bak_path)
+ backup[to] = bak_path
# FIXED: was copy().
- xmove(diffs.renamed[i][1], diffs.renamed[i][0])
+ xmove (frm, to)
# Do Subversion rename.
for r in diffs.renamed:
# Delete files that may be overwritten (--force is a joke).
@@ -821,7 +842,7 @@
# Create workspace.
def convert_init(self):
self.svn = Svn()
- self.topdir = tempfile.mkdtemp(".migrate", "prcs2svn")
+ self.topdir = TempDir().mkdir ("current-svn-checkout")
self.workdir = os.path.join(self.topdir, infos.subdir)
debug("> Create working directory: " + self.topdir)
xchdir(self.topdir)
@@ -1117,7 +1138,7 @@
if not infos.copy_prcs:
return
info("> Copy prcs repository")
- tmp = tempfile.mkdtemp(".prcs", "prcs2svn")
+ tmp = TempDir().mkdir ("prcs-repository")
pwd = os.getcwd()
xchdir(tmp)
for proj in infos.prcs_projects:
@@ -1132,7 +1153,7 @@
def copy_svn_repository(self):
if not infos.copy_svn:
return
- tmp = tempfile.mkdtemp(".svn", "prcs2svn")
+ tmp = TempDir().mkdir ("%s.svn-repo" % infos.svn_project)
info("> Copy svn repository to " + tmp)
new_repo = os.path.join(tmp, infos.svn_project)
xmkdir(new_repo)