Skip to content

Instantly share code, notes, and snippets.

@dumpmycode
Created March 30, 2016 00:55
Show Gist options
  • Select an option

  • Save dumpmycode/573bd8578cd754c3b87dcd6e347d0044 to your computer and use it in GitHub Desktop.

Select an option

Save dumpmycode/573bd8578cd754c3b87dcd6e347d0044 to your computer and use it in GitHub Desktop.
Google Python Class - copyspecial.py
#!/usr/bin/python
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/
'''
Used argparse instead of using sys.argv and also opted for
zipfile.ZipFile instead of using the deprecated command module.
subprocess would be a better choice for calling commands.
e.g. subprocess.call('ls')
'''
import re
import os
import shutil
import zipfile
import argparse
def gsp(dirpath):
# search current dir for any 'special' file with __file__ style format
# and print its abspath if found
abspath = []
try:
dirlist = ' '.join(os.listdir(dirpath))
match = re.findall(r'\w*__\w+__\.\w+', dirlist)
if match:
for line in match:
#print('Files found: {}'.format(line))
abspath.append(os.path.abspath(line))
else:
print('No special files found in this directory')
sys.exit(0)
except Exception as e:
print('gsp Error: {}'.format(e))
sys.exit(0)
return abspath
def cp2(dstpath, dirpath):
# copy special files located in dirpath to paths, and create directories
# if it doesn't exist along paths.
f2cp = gsp(dirpath)
try:
if not os.path.exists(dstpath):
print('{} does not exist, creating directory path..'.format(dstpath))
os.makedirs(dstpath)
for filesrc in f2cp:
shutil.copy2(filesrc, dstpath)
print('Files copied to {}'.format(dstpath))
except Exception as e:
print('cp2 Error: {}'.format(e))
def callzip(name, files):
with zipfile.ZipFile(name, 'w') as zip:
for file in files:
zip.write(file)
def zip2(zname, zsrcpath):
# zip all special files in zsrcpath to dstpath.
f2zip = gsp(zsrcpath)
dirpath = os.path.dirname(zname)
try:
# if zname contains directory path and that path exists
# proceed with zip process
if dirpath != '' and os.path.exists(dirpath):
callzip(zname, f2zip)
# if directory path doesnt exist, create the path
# if the zname doesnt contain dir path, proceed with zip process
elif not os.path.exists(dirpath):
if not dirpath:
callzip(zname, f2zip)
os.makedirs(dirpath)
callzip(zname, f2zip)
print('Zip complete. Files zipped to {}'.format(zname))
except Exception as e:
print('zip2 Error: {}'.format(e))
def main():
parser = argparse.ArgumentParser(usage='\n#./%(prog)s <optional arguments> [srcdir]')
parser.add_argument('srcdir', help='source directory', default='.')
parser.add_argument('-t', '--todir', help='destination directory')
parser.add_argument('-z', '--zipto', help='zip files to dest. dir')
args = parser.parse_args()
srcdir = args.srcdir
try:
if not args.todir and not args.zipto:
files = gsp(srcdir)
for file in files: print file
else:
if not args.todir:
zip2(args.zipto, srcdir)
else:
cp2(args.todir, srcdir)
except Exception as e:
print(e)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment