Last active
March 7, 2021 23:58
-
-
Save samuelmaddock/5519986 to your computer and use it in GitHub Desktop.
Simple FastDL script written using Python 2.7.3 with the bz2 library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start srcds_fastdl.py "/source" "/target" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, getopt, bz2 | |
_debug = 0 # Use -d switch to enable | |
source = None | |
target = None | |
# Directories to ignore searching in | |
ignoredir = [ '.svn', '.git' ] | |
# Valid file extensions to compress | |
validext = [ | |
'bsp', # maps | |
'mdl','vtx','vvd', # models | |
'vtf','vmt','png', # textures | |
'wav','mp3', # sounds | |
'pcf', # particles | |
'ttf','otf', # fonts | |
'txt' # misc | |
] | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv, "h:d", ["help"]) | |
except getopt.GetoptError: | |
usage() | |
sys.exit() | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
usage() | |
sys.exit() | |
elif opt == '-d': | |
global _debug | |
_debug = 1 | |
# Validate number of arguments | |
if len(args) < 2: | |
print "Invalid arguments" | |
usage() | |
sys.exit() | |
# Get arguments | |
global source, target | |
source = args[0] | |
target = args[1] | |
# Validate path directories | |
if not os.path.isdir(source): | |
print "Source path doesn't exist!" | |
sys.exit() | |
elif not os.path.isdir(target): | |
print "Target path doesn't exist!" | |
sys.exit() | |
# Begin compressing files | |
bzip2dir() | |
def usage(): | |
print "Usage: <source> <target>" | |
def bzip2dir(): | |
global source, target, ignoredir | |
sourcelen = len(source) | |
# Walk source path | |
for roots, dirlist, filelist in os.walk(source): | |
# Ignore version control directories | |
for v in ignoredir: | |
if v in roots: | |
continue | |
for filename in [os.path.join(roots,filegot) for filegot in filelist]: | |
bzip2file( filename[sourcelen:] ) | |
def bzip2file(filename): | |
global source, target, _debug | |
# Make sure file is of valid type before continuing | |
if not validfile(filename): | |
if _debug: | |
print "skip: %s" % filename | |
return | |
# Open input file | |
fin = open(source + filename, 'rb') | |
# Get output path | |
dest = target + filename + '.bz2' | |
# Ignore writing to file if it already exists | |
if os.path.exists(dest): | |
if _debug: | |
print "exists: %s" % dest | |
return | |
# Get absolute output folder | |
directory = os.path.dirname(os.path.abspath(dest)) | |
# Create folder if it doesn't exist | |
if not os.path.exists(directory): | |
if _debug: | |
print "makedir: %s" % directory | |
os.makedirs(directory) | |
# Create new file and write bz2 compressed data | |
fout = bz2.BZ2File(dest, 'w') | |
fout.write(fin.read()) | |
# Close files | |
fout.close() | |
fin.close() | |
if _debug: | |
print "compress: %s" % dest | |
def validfile(filename): | |
global validext | |
# Separate name and extension | |
name, ext = os.path.splitext(filename) | |
ext = ext[1:] # remove period | |
# Check for extension in valid extensions list | |
return ext in validext | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment