Created
July 24, 2015 19:37
-
-
Save xerp/79f388b70e532156530a to your computer and use it in GitHub Desktop.
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 | |
import sys | |
import subprocess as proc | |
import zipfile as zipf | |
GIT_COMMAND = 'git diff-tree --no-commit-id --name-only -r {commit_id}' | |
EXPLORER_CMD = r'explorer /select,{0}' | |
DESTINATION_DIR = 'C:/Users/santiago.depedro/Desktop' | |
def get_git_files(commits_id): | |
def get_files(commit_id): | |
process = proc.Popen( | |
GIT_COMMAND.format(commit_id=commit_id), stdout=proc.PIPE, shell=True) | |
output = process.communicate()[0] | |
if output: | |
return filter(lambda v: v, output.split('\n')) | |
else: | |
return [] | |
if isinstance(commits_id, list): | |
files = [] | |
for commit in commits_id: | |
files.append(get_files(commit)) | |
files = [y for x in files for y in x] | |
return list(set(files)) | |
elif commits_id: | |
return get_files(commits_id) | |
def write_files_zip(files, zipf): | |
if isinstance(files, list): | |
zipfs = zipf.namelist() | |
for fi in files: | |
if fi not in zipfs: | |
zipf.write(fi) | |
elif isinstance(files, str): | |
for root, dirs, files_lst in os.walk(files): | |
for name in files_lst: | |
fil = os.path.join(root, name) | |
if '.git' not in fil: | |
fil = fil.split( | |
'{0}\\'.format(files) if '\\' in files else files)[-1] | |
zipf.write(fil) | |
try: | |
if len(sys.argv) > 1: | |
source_path = sys.argv[1] | |
commits = sys.argv[2::] | |
else: | |
print '*****GIT must be in system path*****\n' | |
source_path = raw_input('Source directory: ') | |
commits = raw_input('Commits: ') | |
if source_path and os.path.isdir(source_path) and commits: | |
os.chdir(source_path) | |
commits = commits.split(',') if isinstance(commits, str) else commits | |
files = filter( | |
lambda f: os.path.isfile(f), get_git_files(commits)) | |
source_name = os.path.split(source_path)[-1] | |
zip_file = os.path.join( | |
DESTINATION_DIR, '{0}.zip'.format(source_name)) | |
with zipf.ZipFile(zip_file, 'w') as myzip: | |
if files: | |
write_files_zip(files, myzip) | |
else: | |
write_files_zip(source_path, myzip) | |
else: | |
print 'Nothing to do!' | |
except KeyboardInterrupt: | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment