Skip to content

Instantly share code, notes, and snippets.

@chapter09
Last active November 19, 2015 21:19
Show Gist options
  • Save chapter09/cd9793e66e0a738923c5 to your computer and use it in GitHub Desktop.
Save chapter09/cd9793e66e0a738923c5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Author: wh.sjtu@gmail.com
import os, sys
import subprocess
#
# Copy this script to the directory containing submission packages
# excute python ext_convt.py .
#
path = sys.argv[1]
out_path = path + "/output"
if "output" not in os.listdir(path):
os.mkdir(out_path)
def untar(f_name, path):
tmp_dir = f_name + "_tmp"
cmd = "mkdir %s; tar -xf %s -C %s"%(tmp_dir, f, tmp_dir)
print cmd
p = subprocess.Popen(cmd, shell=True)
p.wait()
f_path = check_dir(path + "/" + tmp_dir)
cmd = "tar -czf %s.tar.gz -C \"%s\" ."%(out_path+"/"+f_name, f_path)
print cmd
p = subprocess.Popen(cmd, shell=True)
p.wait()
print "SUCCESS"
def check_dir(path):
p = None
if "Makefile" in os.listdir(path):
print path, "is the root directory for PA1 source code"
return path
else:
print "-", path
if "router" in os.listdir(path):
p = check_dir(path + "/router")
else:
p = check_dir(path + "/" + os.listdir(path)[0])
return p
for f in os.listdir(path):
f_name, f_ext = os.path.splitext(f)
print "Processing ", f_name, f_ext, "......"
if f_ext.strip() == ".tar.gz":
untar(f_name, path)
elif f_ext.strip() == ".xz":
untar(f_name, path)
elif f_ext.strip() == ".zip":
tmp_dir = f_name + "_tmp"
cmd = "mkdir %s; unzip %s -d %s"%(tmp_dir, f, tmp_dir)
print cmd
p = subprocess.Popen(cmd, shell=True)
p.wait()
f_path = check_dir(path+"/"+tmp_dir)
tar_dir(f_path)
elif f_ext.strip() == ".tar":
untar(f_name, path)
elif f_ext.strip() == ".gz":
untar(f_name, path)
elif f_ext.strip() == ".tgz":
untar(f_name, path)
else:
print "Unexpected format: ", f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment