| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | """distutils.command.build_scripts
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Implements the Distutils 'build_scripts' command."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-19 13:12:28 +00:00
										 |  |  | # This module should be kept compatible with Python 1.5.2. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | __revision__ = "$Id$" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  | import sys, os, re | 
					
						
							| 
									
										
										
										
											2003-01-24 14:56:52 +00:00
										 |  |  | from stat import ST_MODE | 
					
						
							| 
									
										
										
										
											2001-12-10 16:15:44 +00:00
										 |  |  | from distutils import sysconfig | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | from distutils.core import Command | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  | from distutils.dep_util import newer | 
					
						
							| 
									
										
										
										
											2001-07-29 21:39:18 +00:00
										 |  |  | from distutils.util import convert_path | 
					
						
							| 
									
										
										
										
											2002-06-04 20:14:43 +00:00
										 |  |  | from distutils import log | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-25 20:20:11 +00:00
										 |  |  | # check if Python is called on the first line with this expression | 
					
						
							| 
									
										
										
										
											2002-02-28 09:16:21 +00:00
										 |  |  | first_line_re = re.compile(r'^#!.*python[0-9.]*(\s+.*)?$') | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class build_scripts (Command): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |     description = "\"build\" scripts (copy and fixup #! line)" | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     user_options = [ | 
					
						
							|  |  |  |         ('build-dir=', 'd', "directory to \"build\" (copy) to"), | 
					
						
							|  |  |  |         ('force', 'f', "forcibly build everything (ignore file timestamps"), | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 01:41:15 +00:00
										 |  |  |     boolean_options = ['force'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def initialize_options (self): | 
					
						
							|  |  |  |         self.build_dir = None | 
					
						
							|  |  |  |         self.scripts = None | 
					
						
							|  |  |  |         self.force = None | 
					
						
							|  |  |  |         self.outfiles = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def finalize_options (self): | 
					
						
							| 
									
										
										
										
											2000-09-30 18:27:54 +00:00
										 |  |  |         self.set_undefined_options('build', | 
					
						
							|  |  |  |                                    ('build_scripts', 'build_dir'), | 
					
						
							|  |  |  |                                    ('force', 'force')) | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  |         self.scripts = self.distribution.scripts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run (self): | 
					
						
							|  |  |  |         if not self.scripts: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |         self.copy_scripts() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-02 07:28:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |     def copy_scripts (self): | 
					
						
							|  |  |  |         """Copy each script listed in 'self.scripts'; if it's marked as a
 | 
					
						
							|  |  |  |         Python script in the Unix way (first line matches 'first_line_re', | 
					
						
							|  |  |  |         ie. starts with "\#!" and contains "python"), then adjust the first | 
					
						
							| 
									
										
										
										
											2000-07-27 02:13:20 +00:00
										 |  |  |         line to refer to the current Python interpreter as we copy. | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  |         self.mkpath(self.build_dir) | 
					
						
							| 
									
										
										
										
											2003-01-24 14:56:52 +00:00
										 |  |  |         outfiles = [] | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |         for script in self.scripts: | 
					
						
							|  |  |  |             adjust = 0 | 
					
						
							| 
									
										
										
										
											2001-07-29 21:39:18 +00:00
										 |  |  |             script = convert_path(script) | 
					
						
							| 
									
										
										
										
											2000-05-25 20:05:52 +00:00
										 |  |  |             outfile = os.path.join(self.build_dir, os.path.basename(script)) | 
					
						
							| 
									
										
										
										
											2003-01-24 14:56:52 +00:00
										 |  |  |             outfiles.append(outfile) | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not self.force and not newer(script, outfile): | 
					
						
							| 
									
										
										
										
											2002-06-04 20:14:43 +00:00
										 |  |  |                 log.debug("not copying %s (up-to-date)", script) | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Always open the file, but ignore failures in dry-run mode -- | 
					
						
							|  |  |  |             # that way, we'll get accurate feedback if we can read the | 
					
						
							|  |  |  |             # script. | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 f = open(script, "r") | 
					
						
							|  |  |  |             except IOError: | 
					
						
							|  |  |  |                 if not self.dry_run: | 
					
						
							|  |  |  |                     raise | 
					
						
							|  |  |  |                 f = None | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 first_line = f.readline() | 
					
						
							|  |  |  |                 if not first_line: | 
					
						
							|  |  |  |                     self.warn("%s is an empty file (skipping)" % script) | 
					
						
							| 
									
										
										
										
											2000-05-25 01:20:15 +00:00
										 |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 match = first_line_re.match(first_line) | 
					
						
							|  |  |  |                 if match: | 
					
						
							|  |  |  |                     adjust = 1 | 
					
						
							| 
									
										
										
										
											2001-12-11 20:44:42 +00:00
										 |  |  |                     post_interp = match.group(1) or '' | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if adjust: | 
					
						
							| 
									
										
										
										
											2002-06-04 20:14:43 +00:00
										 |  |  |                 log.info("copying and adjusting %s -> %s", script, | 
					
						
							|  |  |  |                          self.build_dir) | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |                 if not self.dry_run: | 
					
						
							|  |  |  |                     outf = open(outfile, "w") | 
					
						
							| 
									
										
										
										
											2001-12-10 16:15:44 +00:00
										 |  |  |                     if not sysconfig.python_build: | 
					
						
							|  |  |  |                         outf.write("#!%s%s\n" %  | 
					
						
							|  |  |  |                                    (os.path.normpath(sys.executable), | 
					
						
							|  |  |  |                                     post_interp)) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         outf.write("#!%s%s" % | 
					
						
							|  |  |  |                                    (os.path.join( | 
					
						
							|  |  |  |                             sysconfig.get_config_var("BINDIR"), | 
					
						
							|  |  |  |                             "python" + sysconfig.get_config_var("EXE")), | 
					
						
							|  |  |  |                                     post_interp)) | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |                     outf.writelines(f.readlines()) | 
					
						
							|  |  |  |                     outf.close() | 
					
						
							|  |  |  |                 if f: | 
					
						
							|  |  |  |                     f.close() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 f.close() | 
					
						
							|  |  |  |                 self.copy_file(script, outfile) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-24 14:56:52 +00:00
										 |  |  |         if os.name == 'posix': | 
					
						
							|  |  |  |             for file in outfiles: | 
					
						
							|  |  |  |                 if self.dry_run: | 
					
						
							|  |  |  |                     log.info("changing mode of %s", file) | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2003-01-29 16:58:31 +00:00
										 |  |  |                     oldmode = os.stat(file)[ST_MODE] & 07777 | 
					
						
							|  |  |  |                     newmode = (oldmode | 0555) & 07777 | 
					
						
							|  |  |  |                     if newmode != oldmode: | 
					
						
							|  |  |  |                         log.info("changing mode of %s from %o to %o", | 
					
						
							|  |  |  |                                  file, oldmode, newmode) | 
					
						
							|  |  |  |                         os.chmod(file, newmode) | 
					
						
							| 
									
										
										
										
											2003-01-24 14:56:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-25 02:03:56 +00:00
										 |  |  |     # copy_scripts () | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # class build_scripts |