Skip to content

Instantly share code, notes, and snippets.

@Hamcha
Last active August 29, 2015 14:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
FastDL Done Right
#!/usr/bin/env python
# FastDL Done Right
# Edit below this
# I'm lazy and I didn't want to make prompts/cmdline stuff
# Base directory
basedir = "garrysmod"
# Target directory
outdir = "fastdl"
# Extensions to copy/compress over
exts = [".txt", ".lua"]
# Done with the editing!
# Just do "python fdr.py"
import os, shutil, bz2
outfiles = []
# Copy the file to the out directory
# Strip the addon path if necessary
def stripcopy(path):
# Get new relative path
pathparts = path.split(os.sep)
pathparts = pathparts[3:] if "addons" in path and len(pathparts) > 3 else pathparts[1:]
newpath = outdir + os.sep + os.sep.join(pathparts[:-1])
# Return new file path (for compression)
return (path, newpath, pathparts[-1])
# Compress (bz2) a file to a new directory
def bzipfile(oldpath, newpath, fname):
# Mkdir if necessary
if not os.path.isdir(newpath): os.makedirs(newpath)
# Open original file and read it
original = open(oldpath, "rb")
content = original.read()
original.close()
# Open target file and write to it
compressed = bz2.BZ2File(newpath+os.sep+fname+".bz2", "wb")
compressed.write(content)
compressed.close
# Cycle through the base directory for files to match
for root, dirs, filenames in os.walk(basedir):
dirfiles = [os.path.join(root, fname) for fname in filenames for e in exts if fname.endswith(e)]
outfiles.extend(map(stripcopy, dirfiles))
# Compress all copied files
for original, newpath, filename in outfiles:
bzipfile(original, newpath, filename)
print "Processed %d files" % len(outfiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment