# general utility module
from __future__ import print_function
from __future__ import with_statement

import os
import sys
import re
import subprocess

def list_autogen_files(dirPath):
	autogenFiles = []
	for (root, subdirs, files) in os.walk(dirPath):
		files = filter(lambda f : re.search(r"AutoGenerated", f), files)
		autogenFiles.extend([os.path.join(root, f) for f in files])
	return autogenFiles

#checkout files with p4 if available
def try_checkout_files(files):
	
	print("checking p4 connection parameter...")
	
	# checking p4
	cmd = "p4"
	(stdout, stderr) = run_cmd(cmd)
	
	if stderr == "":
		print("p4 available.")
	else:
		print("p4 unavailable.")
		return
	
	cmd = "p4 edit " + " " + " ".join(files)
	(stdout, stderr) = run_cmd(cmd)
	print(stderr)
	print(stdout)

# check files writability
def check_files_writable(files):
	for file in files:
		if not os.access(file, os.W_OK):
			return False
	return True

# find a root directory containing a known directory (as a hint)
def find_root_path(startDir, containedDir):
	
	currentDir = startDir
	
	# search directory tree
	mergedDir = os.path.join(currentDir, containedDir)
	while not os.path.isdir(mergedDir):
		(currentDir, dir) = os.path.split(currentDir)
		if not dir:
			return None
			
		mergedDir = os.path.join(currentDir, containedDir)
	
	return currentDir
	
def run_cmd(cmd, stdin = ""):
	process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	
	(stdoutRaw, stderrRaw) = process.communicate(stdin.encode('utf-8'))
	stdout = stdoutRaw.decode(encoding='utf-8')
	stderr = stderrRaw.decode(encoding='utf-8')
	return (stdout, stderr)
	
# clears content of files
def clear_files(files):
	for file in files:
		open(file, 'w').close()

##############################################################################
# internal functions
##############################################################################
	


# general utility module
from __future__ import print_function
from __future__ import with_statement

import os
import sys
import re
import subprocess

def list_autogen_files(dirPath):
	autogenFiles = []
	for (root, subdirs, files) in os.walk(dirPath):
		files = filter(lambda f : re.search(r"AutoGenerated", f), files)
		autogenFiles.extend([os.path.join(root, f) for f in files])
	return autogenFiles

#checkout files with p4 if available
def try_checkout_files(files):
	
	print("checking p4 connection parameter...")
	
	# checking p4
	cmd = "p4"
	(stdout, stderr) = run_cmd(cmd)
	
	if stderr == "":
		print("p4 available.")
	else:
		print("p4 unavailable.")
		return
	
	cmd = "p4 edit " + " " + " ".join(files)
	(stdout, stderr) = run_cmd(cmd)
	print(stderr)
	print(stdout)

# check files writability
def check_files_writable(files):
	for file in files:
		if not os.access(file, os.W_OK):
			return False
	return True

# find a root directory containing a known directory (as a hint)
def find_root_path(startDir, containedDir):
	
	currentDir = startDir
	
	# search directory tree
	mergedDir = os.path.join(currentDir, containedDir)
	while not os.path.isdir(mergedDir):
		(currentDir, dir) = os.path.split(currentDir)
		if not dir:
			return None
			
		mergedDir = os.path.join(currentDir, containedDir)
	
	return currentDir
	
def run_cmd(cmd, stdin = ""):
	process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	
	(stdoutRaw, stderrRaw) = process.communicate(stdin.encode('utf-8'))
	stdout = stdoutRaw.decode(encoding='utf-8')
	stderr = stderrRaw.decode(encoding='utf-8')
	return (stdout, stderr)
	
# clears content of files
def clear_files(files):
	for file in files:
		open(file, 'w').close()

##############################################################################
# internal functions
##############################################################################
	


# general utility module #checkout files with p4 if available # checking p4 # check files writability # find a root directory containing a known directory (as a hint) # search directory tree # clears content of files ############################################################################## # internal functions ##############################################################################