| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | """Create the PEP 376-compliant .dist-info directory.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Forked from the former install_egg_info command by Josip Djolonga | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2011-09-17 03:31:51 +02:00
										 |  |  | import csv | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | import hashlib | 
					
						
							| 
									
										
										
										
											2011-09-17 03:31:51 +02:00
										 |  |  | from shutil import rmtree | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from packaging import logger | 
					
						
							| 
									
										
										
										
											2011-09-17 03:31:51 +02:00
										 |  |  | from packaging.command.cmd import Command | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class install_distinfo(Command): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     description = 'create a .dist-info directory for the distribution' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     user_options = [ | 
					
						
							|  |  |  |         ('distinfo-dir=', None, | 
					
						
							|  |  |  |          "directory where the the .dist-info directory will be installed"), | 
					
						
							|  |  |  |         ('installer=', None, | 
					
						
							|  |  |  |          "the name of the installer"), | 
					
						
							|  |  |  |         ('requested', None, | 
					
						
							|  |  |  |          "generate a REQUESTED file"), | 
					
						
							|  |  |  |         ('no-requested', None, | 
					
						
							|  |  |  |          "do not generate a REQUESTED file"), | 
					
						
							|  |  |  |         ('no-record', None, | 
					
						
							|  |  |  |          "do not generate a RECORD file"), | 
					
						
							|  |  |  |         ('no-resources', None, | 
					
						
							| 
									
										
										
										
											2011-09-18 20:36:19 +02:00
										 |  |  |          "do not generate a RESOURCES file"), | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boolean_options = ['requested', 'no-record', 'no-resources'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     negative_opt = {'no-requested': 'requested'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize_options(self): | 
					
						
							|  |  |  |         self.distinfo_dir = None | 
					
						
							|  |  |  |         self.installer = None | 
					
						
							|  |  |  |         self.requested = None | 
					
						
							|  |  |  |         self.no_record = None | 
					
						
							|  |  |  |         self.no_resources = None | 
					
						
							| 
									
										
										
										
											2011-08-20 09:19:25 +02:00
										 |  |  |         self.outfiles = [] | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def finalize_options(self): | 
					
						
							|  |  |  |         self.set_undefined_options('install_dist', | 
					
						
							|  |  |  |                                    'installer', 'requested', 'no_record') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.set_undefined_options('install_lib', | 
					
						
							|  |  |  |                                    ('install_dir', 'distinfo_dir')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.installer is None: | 
					
						
							|  |  |  |             # FIXME distutils or packaging? | 
					
						
							|  |  |  |             # + document default in the option help text above and in install | 
					
						
							|  |  |  |             self.installer = 'distutils' | 
					
						
							|  |  |  |         if self.requested is None: | 
					
						
							|  |  |  |             self.requested = True | 
					
						
							|  |  |  |         if self.no_record is None: | 
					
						
							|  |  |  |             self.no_record = False | 
					
						
							|  |  |  |         if self.no_resources is None: | 
					
						
							|  |  |  |             self.no_resources = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         metadata = self.distribution.metadata | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-12 11:12:42 -06:00
										 |  |  |         basename = metadata.get_fullname(filesafe=True) + ".dist-info" | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.distinfo_dir = os.path.join(self.distinfo_dir, basename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run(self): | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         target = self.distinfo_dir | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         if os.path.isdir(target) and not os.path.islink(target): | 
					
						
							|  |  |  |             if not self.dry_run: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                 rmtree(target) | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         elif os.path.exists(target): | 
					
						
							|  |  |  |             self.execute(os.unlink, (self.distinfo_dir,), | 
					
						
							|  |  |  |                          "removing " + target) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         self.execute(os.makedirs, (target,), "creating " + target) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         installer_path = os.path.join(self.distinfo_dir, 'INSTALLER') | 
					
						
							|  |  |  |         logger.info('creating %s', installer_path) | 
					
						
							|  |  |  |         if not self.dry_run: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             with open(installer_path, 'w') as f: | 
					
						
							|  |  |  |                 f.write(self.installer) | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         self.outfiles.append(installer_path) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         if self.requested: | 
					
						
							|  |  |  |             requested_path = os.path.join(self.distinfo_dir, 'REQUESTED') | 
					
						
							|  |  |  |             logger.info('creating %s', requested_path) | 
					
						
							|  |  |  |             if not self.dry_run: | 
					
						
							| 
									
										
										
										
											2011-05-19 15:52:59 +02:00
										 |  |  |                 open(requested_path, 'wb').close() | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |             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) | 
					
						
							|  |  |  |                 if not self.dry_run: | 
					
						
							| 
									
										
										
										
											2011-10-14 16:58:23 +02:00
										 |  |  |                     with open(resources_path, 'w') as f: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                         writer = csv.writer(f, delimiter=',', | 
					
						
							| 
									
										
										
										
											2011-05-21 19:45:48 +02:00
										 |  |  |                                             lineterminator='\n', | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                                             quotechar='"') | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |                         for row in install_data.get_resources_out(): | 
					
						
							|  |  |  |                             writer.writerow(row) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |                 self.outfiles.append(resources_path) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |         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: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                 with open(record_path, 'w', encoding='utf-8') as f: | 
					
						
							|  |  |  |                     writer = csv.writer(f, delimiter=',', | 
					
						
							| 
									
										
										
										
											2011-05-21 19:45:48 +02:00
										 |  |  |                                         lineterminator='\n', | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                                         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) | 
					
						
							| 
									
										
										
										
											2011-05-19 15:09:57 +02:00
										 |  |  |                             with open(fpath, 'rb') as fp: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                                 hash = hashlib.md5() | 
					
						
							| 
									
										
										
										
											2011-05-19 15:09:57 +02:00
										 |  |  |                                 hash.update(fp.read()) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                             md5sum = hash.hexdigest() | 
					
						
							|  |  |  |                             writer.writerow((fpath, md5sum, size)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     # add the RECORD file itself | 
					
						
							|  |  |  |                     writer.writerow((record_path, '', '')) | 
					
						
							| 
									
										
										
										
											2011-09-10 18:10:23 +02:00
										 |  |  |             self.outfiles.append(record_path) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_outputs(self): | 
					
						
							| 
									
										
										
										
											2011-08-20 09:19:25 +02:00
										 |  |  |         return self.outfiles |