import re
from zipfile import *
from operator import truth
from os import listdir
from os import path
from os import chdir
from time import *

# this variable is the filter for file extensions of files that will be in archive
filter_src = re.compile('\.h|\.c|\.cpp|\.hcc|\.sln|\.suo|\.vcproj|\.cu|\.py|\.doc|\.rc|\.vert|\.frag|\.txt', re.I)

# it is the list of file extensions for project name
proj_name_ext = [re.compile('\.sln', re.I), \
                 re.compile('\.vcproj', re.I), \
                 re.compile('\.doc', re.I)]

# this is the path from where program start scan folders
curr_path = "."


def find_file_with_ext(folder, filter):

  arch_name = folder
  files = listdir(folder)
  
  for	file_name in files:
    la_name = file_name
    file_name = folder + "/" + file_name
      
    #print ("look: " + file_name)
    if truth(filter.search(file_name)):
      #print ("found:" + file_name)
      arch_name = re.split(filter,la_name)[0]
      return arch_name
          
    elif path.isdir(file_name):
      #print ("in: " + file_name)
      arch_name = find_file_with_ext(file_name, filter)
      if len(arch_name) > 0:
        return arch_name
        
  return ""

def find_project_name(folder, filters):
 
  for filter in filters:
    arch_name = find_file_with_ext(folder, filter)
    if arch_name != "":
      return arch_name
  return ""
  
def arch_recursive(folder, zip):

	files  = listdir(folder)
		
	for file in files:
		file_name = folder + "\\" + file
		if path.isdir(file_name): 
			print (file_name)
			arch_recursive(file_name,zip)
		elif truth(filter_src.search(file)):
			zip.write(file_name)
		
arch_name = find_project_name(curr_path, proj_name_ext)
print("found: " + arch_name)



if arch_name == "":
  arch_name = "DEFAULT"

folder_name = curr_path
arch_name += strftime("_%d_%B_%Y",localtime())		
arch_name += ".zip"
zip = ZipFile(arch_name,'w', ZIP_DEFLATED)
print ("Archive name: " + arch_name)
arch_recursive(folder_name, zip)
