Skip to content

Instantly share code, notes, and snippets.

@pfmoore
Created January 10, 2015 10:57
Show Gist options
  • Select an option

  • Save pfmoore/fd09fff11b4fa5f8cb25 to your computer and use it in GitHub Desktop.

Select an option

Save pfmoore/fd09fff11b4fa5f8cb25 to your computer and use it in GitHub Desktop.
import zipfile
import hashlib
import csv
import sys
import os
import io
oldproj = 'pywin32'
newproj = 'pypiwin32'
filename = sys.argv[1]
newfilename = filename.replace(oldproj, newproj)
print("Processing:", filename, "to", newfilename)
hashes = {}
def process_metadata(info, data):
newdata = data.replace(oldproj.encode('ascii'), newproj.encode('ascii'), 1)
newhash = "sha256=" + hashlib.sha256(newdata).hexdigest()
newlen = len(newdata)
print("Remember:", info.filename)
hashes[info.filename] = (newhash, newlen)
return newdata
def process_record(info, data):
inb = io.BytesIO(data)
outb = io.BytesIO()
intx = io.TextIOWrapper(inb, encoding='utf-8', newline='')
outtx = io.TextIOWrapper(outb, encoding='utf-8', newline='')
incsv = csv.reader(intx)
outcsv = csv.writer(outtx)
for name, h, l in incsv:
if name.startswith(oldproj):
name = newproj + name[len(oldproj):]
if name in hashes:
print("Fixup:", name)
h, l = hashes[name]
outcsv.writerow([name, h, l])
return outb.getvalue()
def process_pathfile(info, data):
extraline = b'''import os;os.environ["PATH"]+=(';'+os.path.join(sitedir,"pypiwin32_system32"))'''
newdata = data + b'\n' + extraline
newhash = "sha256=" + hashlib.sha256(newdata).hexdigest()
newlen = len(newdata)
print("Remember:", info.filename)
hashes[info.filename] = (newhash, newlen)
return newdata
special_process = {
'pypiwin32.pth': process_pathfile,
'METADATA': process_metadata,
'metadata.json': process_metadata,
'RECORD': process_record,
}
with zipfile.ZipFile(filename) as zin:
with zipfile.ZipFile(newfilename, 'w', zipfile.ZIP_DEFLATED) as zout:
for info in zin.infolist():
try:
data = zin.read(info)
except zipfile.BadZipFile:
print("Error processing", filename, "entry", info.filename)
break
if info.filename.startswith(oldproj):
info.filename = newproj + info.filename[len(oldproj):]
for nm, fn in special_process.items():
if info.filename.endswith(nm):
data = fn(info, data)
zout.writestr(info, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment