Skip to content

Instantly share code, notes, and snippets.

@zhanghang1989
Last active April 4, 2020 03:53
Show Gist options
  • Save zhanghang1989/ecea6ab91f5fb0ae3907af19f9dad086 to your computer and use it in GitHub Desktop.
Save zhanghang1989/ecea6ab91f5fb0ae3907af19f9dad086 to your computer and use it in GitHub Desktop.
automatically create zip file for model_zoo upload
import os
from os import listdir
from os.path import isfile, join
import hashlib
import shutil
import zipfile
mypath = './'
newpath = './zip'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for f in onlyfiles:
if '.pth' not in f and '.params' not in f: continue
print(f'doing {f}')
filename = join(mypath, f)
basename, extension = os.path.splitext(f)
sha1 = hashlib.sha1()
with open(filename, 'rb') as fi:
while True:
data = fi.read(1048576)
if not data:
break
sha1.update(data)
sha1str = sha1.hexdigest()[:8]
newname = join(newpath, basename + '-' + sha1str + extension)
zipname = join(newpath, basename + '-' + sha1str + '.zip')
shutil.copyfile(filename, newname)
zipfile.ZipFile(zipname, mode='w').write(newname, os.path.basename(newname))
import os
from os import listdir
from os.path import isfile, join
import hashlib
import shutil
import zipfile
mypath = './'
newpath = './hub'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for f in onlyfiles:
if '.pth' not in f: continue
print(f'doing {f}')
filename = join(mypath, f)
basename, extension = os.path.splitext(f)
sha1 = hashlib.sha256()
with open(filename, 'rb') as fi:
while True:
data = fi.read(1048576)
if not data:
break
sha1.update(data)
sha1str = sha1.hexdigest()[:8]
print(sha1str, basename)
newname = join(newpath, basename + '-' + sha1str + extension)
shutil.copyfile(filename, newname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment