[Python-checkins] distutils2: Fix usage of dry-run in bdist_wininst and install_distinfo.
eric.araujo
python-checkins at python.org
Mon Sep 19 15:12:39 CEST 2011
http://hg.python.org/distutils2/rev/8695472d3009
changeset: 1176:8695472d3009
user: Éric Araujo <merwok at netwok.org>
date: Mon Sep 19 04:53:51 2011 +0200
summary:
Fix usage of dry-run in bdist_wininst and install_distinfo.
In dry-run mode, commands should log the same info as in real operation
and should collect the same files in self.outputs, so that users can run
a command in verbose and dry-run mode to see exactly what operations
will be done in the real run.
files:
distutils2/command/bdist_wininst.py | 5 +-
distutils2/command/install_distinfo.py | 148 ++++++------
2 files changed, 80 insertions(+), 73 deletions(-)
diff --git a/distutils2/command/bdist_wininst.py b/distutils2/command/bdist_wininst.py
--- a/distutils2/command/bdist_wininst.py
+++ b/distutils2/command/bdist_wininst.py
@@ -187,9 +187,8 @@
os.remove(arcname)
if not self.keep_temp:
- if self.dry_run:
- logger.info('removing %s', self.bdist_dir)
- else:
+ logger.info('removing %s', self.bdist_dir)
+ if not self.dry_run:
rmtree(self.bdist_dir)
def get_inidata(self):
diff --git a/distutils2/command/install_distinfo.py b/distutils2/command/install_distinfo.py
--- a/distutils2/command/install_distinfo.py
+++ b/distutils2/command/install_distinfo.py
@@ -2,18 +2,17 @@
# Forked from the former install_egg_info command by Josip Djolonga
+import os
+import csv
import codecs
-import csv
-import os
-import re
+from shutil import rmtree
try:
import hashlib
except ImportError:
from distutils2._backport import hashlib
+from distutils2 import logger
from distutils2.command.cmd import Command
-from distutils2 import logger
-from shutil import rmtree
class install_distinfo(Command):
@@ -72,81 +71,90 @@
self.distinfo_dir = os.path.join(self.distinfo_dir, basename)
def run(self):
- # FIXME dry-run should be used at a finer level, so that people get
- # useful logging output and can have an idea of what the command would
- # have done
+ target = self.distinfo_dir
+
+ if os.path.isdir(target) and not os.path.islink(target):
+ if not self.dry_run:
+ rmtree(target)
+ elif os.path.exists(target):
+ self.execute(os.unlink, (self.distinfo_dir,),
+ "removing " + target)
+
+ self.execute(os.makedirs, (target,), "creating " + target)
+
+ metadata_path = os.path.join(self.distinfo_dir, 'METADATA')
+ self.execute(self.distribution.metadata.write, (metadata_path,),
+ "creating " + metadata_path)
+ self.outfiles.append(metadata_path)
+
+ installer_path = os.path.join(self.distinfo_dir, 'INSTALLER')
+ logger.info('creating %s', installer_path)
if not self.dry_run:
- target = self.distinfo_dir
+ f = open(installer_path, 'w')
+ try:
+ f.write(self.installer)
+ finally:
+ f.close()
+ self.outfiles.append(installer_path)
- if os.path.isdir(target) and not os.path.islink(target):
- rmtree(target)
- elif os.path.exists(target):
- self.execute(os.unlink, (self.distinfo_dir,),
- "removing " + target)
+ if self.requested:
+ requested_path = os.path.join(self.distinfo_dir, 'REQUESTED')
+ logger.info('creating %s', requested_path)
+ if not self.dry_run:
+ open(requested_path, 'wb').close()
+ self.outfiles.append(requested_path)
- self.execute(os.makedirs, (target,), "creating " + target)
+ if not self.no_resources:
+ install_data = self.get_finalized_command('install_data')
+ if install_data.get_resources_out() != []:
+ resources_path = os.path.join(self.distinfo_dir,
+ 'RESOURCES')
+ logger.info('creating %s', resources_path)
+ if not self.dry_run:
+ f = open(resources_path, 'wb')
+ try:
+ writer = csv.writer(f, delimiter=',',
+ lineterminator='\n',
+ quotechar='"')
+ for row in install_data.get_resources_out():
+ writer.writerow(row)
+ finally:
+ f.close()
- metadata_path = os.path.join(self.distinfo_dir, 'METADATA')
- logger.info('creating %s', metadata_path)
- self.distribution.metadata.write(metadata_path)
- self.outfiles.append(metadata_path)
+ self.outfiles.append(resources_path)
- installer_path = os.path.join(self.distinfo_dir, 'INSTALLER')
- logger.info('creating %s', installer_path)
- f = open(installer_path, 'w')
- f.write(self.installer)
- f.close()
- self.outfiles.append(installer_path)
-
- if self.requested:
- requested_path = os.path.join(self.distinfo_dir, 'REQUESTED')
- logger.info('creating %s', requested_path)
- open(requested_path, 'wb').close()
- self.outfiles.append(requested_path)
-
-
- if not self.no_resources:
- install_data = self.get_finalized_command('install_data')
- if install_data.get_resources_out() != []:
- resources_path = os.path.join(self.distinfo_dir,
- 'RESOURCES')
- logger.info('creating %s', resources_path)
- f = open(resources_path, 'wb')
+ if not self.no_record:
+ record_path = os.path.join(self.distinfo_dir, 'RECORD')
+ logger.info('creating %s', record_path)
+ if not self.dry_run:
+ f = codecs.open(record_path, 'w', encoding='utf-8')
+ try:
writer = csv.writer(f, delimiter=',',
lineterminator='\n',
quotechar='"')
- for tuple in install_data.get_resources_out():
- writer.writerow(tuple)
+ install = self.get_finalized_command('install_dist')
+
+ for fpath in install.get_outputs():
+ if fpath.endswith('.pyc') or fpath.endswith('.pyo'):
+ # do not put size and md5 hash, as in PEP-376
+ writer.writerow((fpath, '', ''))
+ else:
+ size = os.path.getsize(fpath)
+ fp = open(fpath, 'rb')
+ try:
+ hash = hashlib.md5()
+ hash.update(fp.read())
+ finally:
+ fp.close()
+ md5sum = hash.hexdigest()
+ writer.writerow((fpath, md5sum, size))
+
+ # add the RECORD file itself
+ writer.writerow((record_path, '', ''))
+ finally:
f.close()
- self.outfiles.append(resources_path)
-
- if not self.no_record:
- record_path = os.path.join(self.distinfo_dir, 'RECORD')
- logger.info('creating %s', record_path)
- f = codecs.open(record_path, 'w', encoding='utf-8')
- writer = csv.writer(f, delimiter=',',
- lineterminator='\n',
- quotechar='"')
-
- install = self.get_finalized_command('install_dist')
-
- for fpath in install.get_outputs():
- if fpath.endswith('.pyc') or fpath.endswith('.pyo'):
- # do not put size and md5 hash, as in PEP-376
- writer.writerow((fpath, '', ''))
- else:
- size = os.path.getsize(fpath)
- fp = open(fpath, 'rb')
- hash = hashlib.md5()
- hash.update(fp.read())
- fp.close()
- md5sum = hash.hexdigest()
- writer.writerow((fpath, md5sum, size))
-
- # add the RECORD file itself
- writer.writerow((record_path, '', ''))
- self.outfiles.append(record_path)
+ self.outfiles.append(record_path)
def get_outputs(self):
return self.outfiles
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list