Daniel Holth avatar Daniel Holth committed 5cd471f Merge

Merged in pmoore/wheel (pull request #18)

Comments (0)

Files changed (2)

                 target = get_path(key)
             else:
                 # Normal file. Target destination is root
+                key = ''
                 target = root
                 filename = name
 
             # Map the actual filename from the zipfile to its intended target
             # directory and the pathname relative to that directory.
             dest = os.path.normpath(os.path.join(target, filename))
-            name_trans[name] = (target, filename, dest)
+            name_trans[name] = (key, target, filename, dest)
 
         # We're now ready to start processing the actual install. The process
         # is as follows:
         
         if not force:
             for k, v in name_trans.items():
-                target, filename, dest = v
+                key, target, filename, dest = v
                 if os.path.exists(dest):
                     raise ValueError("Wheel file {} would overwrite an existing file. Use force if this is intended".format(k))
 
+        # Get the name of our executable, for use when replacing script
+        # wrapper hashbang lines.
+        # We encode it using getfilesystemencoding, as that is "the name of
+        # the encoding used to convert Unicode filenames into system file
+        # names".
+        exename = sys.executable.encode(sys.getfilesystemencoding())
         record_data = []
-        for name, (target, filename, dest) in name_trans.items():
-            source = HashingFile(self.zipfile.open(name))
+        for name, (key, target, filename, dest) in name_trans.items():
+            source = self.zipfile.open(name)
             # Skip the RECORD file
             if name == self.distinfo_name + '/RECORD':
                 continue
             ddir = os.path.dirname(dest)
             if not os.path.isdir(ddir):
                 os.makedirs(ddir)
-            destination = open(dest, 'wb')
+            destination = HashingFile(open(dest, 'wb'))
+            if key == 'scripts' and filename.endswith('.py'):
+                hashbang = source.readline()
+                if hashbang.startswith(b'#!python'):
+                    hashbang = b'#!' + exename + b'\r\n'
+                destination.write(hashbang)
             shutil.copyfileobj(source, destination)
-            destination.close()
             reldest = os.path.relpath(dest, root)
             reldest.replace(os.sep, '/')
-            record_data.append((reldest, source.digest(), source.length))
+            record_data.append((reldest, destination.digest(), destination.length))
+            destination.close()
             source.close()
 
         record_name = os.path.join(root, self.distinfo_name, 'RECORD')
         self.hashtype = hashtype
         self.hash = hashlib.new(hashtype)
         self.length = 0
-    def read(self, n):
-        data = self.fd.read(n)
+    def write(self, data):
         self.hash.update(data)
         self.length += len(data)
-        return data
+        self.fd.write(data)
     def close(self):
         self.fd.close()
     def digest(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.