<commit_before>#!/usr/bin/env python2.7

# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import sys, os

import webnotes

site_arg_optional = ['serve']

def main():
	parsed_args = webnotes._dict(vars(setup_parser()))
	fn = get_function(parsed_args)
	if not parsed_args.get("sites_path"):
		parsed_args["sites_path"] = "."
	
	if not parsed_args.get("make_app"):
		if not parsed_args.get("site") and not fn in site_arg_optional:
			print "Site argument required"
			exit(1)
		if fn not in site_arg_optional and (fn != 'install' and not os.path.exists(parsed_args.get("site"))):
			print "Did not find folder '{}'. Are you in sites folder?".format(parsed_args.get("site"))
			exit(1)
			
		if parsed_args.get("site")=="all":
			for site in get_sites():
				args = parsed_args.copy()
				args["site"] = site
				webnotes.init(site)
				run(fn, args)
		else:
			if not fn in site_arg_optional:
				webnotes.init(parsed_args.get("site"))
			run(fn, parsed_args)
	else:
		run(fn, parsed_args)

def cmd(fn):
	def new_fn(*args, **kwargs):
		import inspect
		fnargs, varargs, varkw, defaults = inspect.getargspec(fn)
		new_kwargs = {}
		for i, a in enumerate(fnargs):
			# should not pass an argument more than once
			if i >= len(args) and a in kwargs:
				new_kwargs[a] = kwargs.get(a)
		
		return fn(*args, **new_kwargs)
	
	return new_fn
	

def run(fn, args):
	if isinstance(args.get(fn), (list, tuple)):
		out = globals().get(fn)(*args.get(fn), **args)
	else:
		out = globals().get(fn)(**args)
	
	return out
		
def get_function(args):
	for fn, val in args.items():
		if (val or isinstance(val, list)) and globals().get(fn):
			return fn
	
def get_sites():
	import os
	import conf
	return [site for site in os.listdir(conf.sites_dir)
			if not os.path.islink(os.path.join(conf.sites_dir, site)) 
				and os.path.isdir(os.path.join(conf.sites_dir, site))]
	
def setup_parser():
	import argparse
	parser = argparse.ArgumentParser(description="Run webnotes utility functions")
	
	setup_install(parser)
	setup_utilities(parser)
	setup_translation(parser)
	setup_test(parser)
	
	parser.add_argument("site", nargs="?")
	<commit_msg>fix get_sites for 4.0.0<commit_after>#!/usr/bin/env python2.7

# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import sys, os

import webnotes

site_arg_optional = ['serve']

def main():
	parsed_args = webnotes._dict(vars(setup_parser()))
	fn = get_function(parsed_args)
	if not parsed_args.get("sites_path"):
		parsed_args["sites_path"] = "."
	
	if not parsed_args.get("make_app"):
		if not parsed_args.get("site") and not fn in site_arg_optional:
			print "Site argument required"
			exit(1)
		if fn not in site_arg_optional and (fn != 'install' and not os.path.exists(parsed_args.get("site"))):
			print "Did not find folder '{}'. Are you in sites folder?".format(parsed_args.get("site"))
			exit(1)
			
		if parsed_args.get("site")=="all":
			for site in get_sites(parsed_args["sites_path"]):
				args = parsed_args.copy()
				args["site"] = site
				webnotes.init(site)
				run(fn, args)
		else:
			if not fn in site_arg_optional:
				webnotes.init(parsed_args.get("site"))
			run(fn, parsed_args)
	else:
		run(fn, parsed_args)

def cmd(fn):
	def new_fn(*args, **kwargs):
		import inspect
		fnargs, varargs, varkw, defaults = inspect.getargspec(fn)
		new_kwargs = {}
		for i, a in enumerate(fnargs):
			# should not pass an argument more than once
			if i >= len(args) and a in kwargs:
				new_kwargs[a] = kwargs.get(a)
		
		return fn(*args, **new_kwargs)
	
	return new_fn
	

def run(fn, args):
	if isinstance(args.get(fn), (list, tuple)):
		out = globals().get(fn)(*args.get(fn), **args)
	else:
		out = globals().get(fn)(**args)
	
	return out
		
def get_function(args):
	for fn, val in args.items():
		if (val or isinstance(val, list)) and globals().get(fn):
			return fn
	
def get_sites(sites_path=None):
	import os
	if not sites_path:
		sites_path = '.'
	return [site for site in os.listdir(sites_path)
			if not os.path.islink(os.path.join(sites_path, site))
				and os.path.isdir(os.path.join(sites_path, site))]
	
def setup_parser():
	import argparse
	parser = argparse.ArgumentParser(description="Run webnotes utility functions")
	
	setup_install(parser)
	setup_utilities(parser)
	setup_translation(parser)
	setup_test(parser)
	
	parser.add_argument("site", nargs="?")
	
{'fraction_non_alphanumeric': 0.13776986423325172, 'fraction_numerical': 0.0042288003561095034, 'mean_word_length': 4.343638525564804, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7342804', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1595, 'n_words': 415}