addes tests and cleaned up export_module
This commit is contained in:
parent
3ee3a7ef4d
commit
fcd77ee512
6 changed files with 92 additions and 181 deletions
|
|
@ -125,6 +125,9 @@ def get_files_path():
|
|||
return os.path.join(get_index_path(), 'user_files', conn.cur_db_name)
|
||||
|
||||
def create_folder(path):
|
||||
"""
|
||||
Wrapper function for os.makedirs (does not throw exception if directory exists)
|
||||
"""
|
||||
import os
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,165 +1,27 @@
|
|||
# ==============================================================================
|
||||
# export to files
|
||||
# ==============================================================================
|
||||
|
||||
updated_modules = []
|
||||
|
||||
from webnotes.modules import scrub, get_module_path
|
||||
|
||||
def export_to_files(modules = [], record_list=[], from_db=None, from_ac=None, verbose=1, record_module=None):
|
||||
# Multiple doctype and multiple modules export to be done
|
||||
# for Module Def, right now using a hack..should consider table update in the next version
|
||||
# all modules transfer not working, because source db not known
|
||||
# get the items
|
||||
|
||||
global updated_modules
|
||||
|
||||
if from_ac or from_db:
|
||||
init_db_login(from_ac, from_db)
|
||||
|
||||
out = []
|
||||
import webnotes.model.doc
|
||||
def export_to_files(record_list=[], record_module=None, verbose=0):
|
||||
module_doclist =[]
|
||||
if record_list:
|
||||
for record in record_list:
|
||||
module_doclist.append([d.fields for d in webnotes.model.doc.get(record[0], record[1])])
|
||||
|
||||
# build the doclist
|
||||
if modules:
|
||||
for m in modules:
|
||||
module_doclist +=get_module_doclist(m)
|
||||
|
||||
# write files
|
||||
for doclist in module_doclist:
|
||||
if verbose:
|
||||
out.append("Writing for " + doclist[0]['doctype'] + " / " + doclist[0]['name'])
|
||||
write_document_file(doclist, record_module)
|
||||
|
||||
# write out attachments
|
||||
for m in modules:
|
||||
write_attachments(m)
|
||||
|
||||
doclist = [d.fields for d in webnotes.model.doc.get(record[0], record[1])]
|
||||
write_document_file(doclist, record_module)
|
||||
|
||||
return out
|
||||
|
||||
# ==============================================================================
|
||||
# write module.info file with last updated timestamp
|
||||
# ==============================================================================
|
||||
|
||||
def write_attachments(m):
|
||||
import webnotes, os
|
||||
from webnotes.utils.file_manager import get_file
|
||||
|
||||
try:
|
||||
fl = webnotes.conn.sql("select name from `tabFile Data` where module=%s", m)
|
||||
except Exception, e:
|
||||
if e.args[0]==1054: # no field called module
|
||||
return
|
||||
else:
|
||||
raise e
|
||||
|
||||
# write the files
|
||||
if fl:
|
||||
folder = os.path.join(webnotes.defs.modules_path, m, 'files')
|
||||
webnotes.create_folder(folder)
|
||||
for f in fl:
|
||||
file_det = get_file(f)
|
||||
file = open(os.path.join(folder, file_det[0]), 'w+')
|
||||
file.write(file_det[1])
|
||||
file.close()
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# write module.info file with last updated timestamp
|
||||
# ==============================================================================
|
||||
|
||||
def write_module_info(mod):
|
||||
import webnotes.utils, os
|
||||
|
||||
file = open(os.path.join(get_module_path(mod), 'module.info'), 'w')
|
||||
file.write(str({'update_date': webnotes.utils.now()}))
|
||||
file.close()
|
||||
|
||||
# ==============================================================================
|
||||
# prepare a list of items in a module
|
||||
# ==============================================================================
|
||||
|
||||
def get_module_items(mod):
|
||||
import webnotes
|
||||
from webnotes.modules import transfer_types
|
||||
from webnotes.modules import scrub
|
||||
|
||||
dl = []
|
||||
for dt in transfer_types:
|
||||
try:
|
||||
dl2 = webnotes.conn.sql('select name, modified from `tab%s` where module="%s"' % (dt,mod))
|
||||
for e in dl2:
|
||||
dl += [dt + ',' + e[0] + ',0']
|
||||
|
||||
if e[0] == 'Control Panel':
|
||||
dl += [e[0]+','+e[0]+',1']
|
||||
except:
|
||||
pass
|
||||
dl1 = webnotes.conn.sql('select doctype_list from `tabModule Def` where name=%s', mod)
|
||||
dl1 = dl1 and dl1[0][0] or ''
|
||||
if dl1:
|
||||
dl1 = dl1.split('\n')
|
||||
dl += [t+',1' for t in dl1]
|
||||
dl += ['Module Def,'+mod+',0']
|
||||
# build finally
|
||||
dl = [e.split(',') for e in dl]
|
||||
dl = [[e[0].strip(), e[1].strip(), e[2]] for e in dl] # remove blanks
|
||||
return dl
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# build a list of doclists of items in that module and send them
|
||||
# ==============================================================================
|
||||
|
||||
def get_module_doclist(module):
|
||||
import webnotes
|
||||
import webnotes.model.doc
|
||||
item_list = get_module_items(module)
|
||||
|
||||
# build the super_doclist
|
||||
super_doclist = []
|
||||
for i in item_list:
|
||||
dl = webnotes.model.doc.get(i[0], i[1])
|
||||
if i[2]=='1':
|
||||
dl[0].module = module
|
||||
# remove compiled code (if any)
|
||||
if dl[0].server_code_compiled:
|
||||
dl[0].server_code_compiled = None
|
||||
|
||||
# add to super
|
||||
super_doclist.append([d.fields for d in dl])
|
||||
|
||||
return super_doclist
|
||||
|
||||
# ==============================================================================
|
||||
# Create __init__.py files
|
||||
# ==============================================================================
|
||||
|
||||
def create_init_py(modules_path, dt, dn):
|
||||
def create_init_py(modules_path, module, dt, dn):
|
||||
import os
|
||||
from webnotes.modules import scrub
|
||||
|
||||
# in module
|
||||
if not '__init__.py' in os.listdir(modules_path):
|
||||
open(os.path.join(modules_path, '__init__.py'), 'w').close()
|
||||
|
||||
# in type and name folders
|
||||
if dt in ['doctype', 'page', 'search_criteria']:
|
||||
if not '__init__.py' in os.listdir(os.path.join(modules_path, dt)):
|
||||
open(os.path.join(modules_path, dt, '__init__.py'), 'w').close()
|
||||
|
||||
if not '__init__.py' in os.listdir(os.path.join(modules_path, dt, dn)):
|
||||
open(os.path.join(modules_path, dt, dn, '__init__.py'), 'w').close()
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Create module folders
|
||||
# ==============================================================================
|
||||
|
||||
def create_if_not_exists(path):
|
||||
initpy = os.path.join(path, '__init__.py')
|
||||
if not os.path.exists(initpy):
|
||||
open(initpy, 'w').close()
|
||||
|
||||
create_if_not_exists(os.path.join(modules_path, module))
|
||||
create_if_not_exists(os.path.join(modules_path, module, dt))
|
||||
create_if_not_exists(os.path.join(modules_path, module, dt, dn))
|
||||
|
||||
def create_folder(module, dt, dn):
|
||||
import webnotes, os
|
||||
|
||||
|
|
@ -175,31 +37,28 @@ def create_folder(module, dt, dn):
|
|||
|
||||
# create init_py_files
|
||||
if code_type:
|
||||
create_init_py(modules_path, scrub(dt), scrub(dn))
|
||||
create_init_py(modules_path, module, scrub(dt), scrub(dn))
|
||||
|
||||
return folder
|
||||
|
||||
# ==============================================================================
|
||||
# Write doclist into file
|
||||
# ==============================================================================
|
||||
|
||||
def write_document_file(doclist, record_module=None):
|
||||
import os
|
||||
from webnotes.utils import pprint_dict
|
||||
|
||||
global updated_modules
|
||||
|
||||
def get_module_name(doclist, record_module=None):
|
||||
# module name
|
||||
if doclist[0]['doctype'] == 'Module Def':
|
||||
module = doclist[0]['name']
|
||||
elif doclist[0]['doctype']=='Control Panel':
|
||||
module = 'System'
|
||||
module = 'Core'
|
||||
elif record_module:
|
||||
module = record_module
|
||||
else:
|
||||
module = doclist[0]['module']
|
||||
|
||||
updated_modules.append(module)
|
||||
return module
|
||||
|
||||
def write_document_file(doclist, record_module=None):
|
||||
import os
|
||||
from webnotes.utils import pprint_dict
|
||||
|
||||
module = get_module_name()
|
||||
|
||||
# create the folder
|
||||
code_type = doclist[0]['doctype'] in ['DocType','Page','Search Criteria']
|
||||
|
|
@ -218,11 +77,6 @@ def write_document_file(doclist, record_module=None):
|
|||
txtfile.write('[\n' + ',\n'.join(dict_list) + '\n]')
|
||||
txtfile.close()
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Create seperate files for code
|
||||
# ==============================================================================
|
||||
|
||||
def clear_code_fields(doclist, folder, code_type):
|
||||
|
||||
import os
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
# Account/Domain Name to Database Mapping file
|
||||
# --------------------------------------------
|
||||
# last updated on: 2011-02-02 14:31:14
|
||||
|
||||
default_db_name = "webnotesdb"
|
||||
|
||||
db_name_map = {'wnframework':'webnotesdb'}
|
||||
#{'main_acc_name';'db_name'}
|
||||
domain_name_map = {'localhost':'webnotesdb'}
|
||||
|
||||
64
cgi-bin/webnotes/tests.py
Normal file
64
cgi-bin/webnotes/tests.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
"""
|
||||
Run tests from modules. Sets up database connection, modules path and session before running test
|
||||
|
||||
Usage: from shell, run
|
||||
|
||||
python tests.py [test modules]
|
||||
|
||||
Options:
|
||||
test modules: list of modules separated by space
|
||||
|
||||
if no modules are specified, it will run all "tests.py" files from all modules
|
||||
"""
|
||||
|
||||
import sys, os
|
||||
import unittest
|
||||
|
||||
# webnotes path
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
# modules path
|
||||
import webnotes
|
||||
import webnotes.defs
|
||||
|
||||
if webnotes.defs.__dict__.get('modules_path'):
|
||||
sys.path.append(webnotes.defs.modules_path)
|
||||
|
||||
def get_tests():
|
||||
"""
|
||||
Returns list of test modules identified by "tests.py"
|
||||
"""
|
||||
ret = []
|
||||
for walk_tuple in os.walk(webnotes.defs.modules_path):
|
||||
if 'tests.py' in walk_tuple[2]:
|
||||
dir_path = os.path.relpath(walk_tuple[0], webnotes.defs.modules_path)
|
||||
if dir_path=='.':
|
||||
ret.append('tests')
|
||||
else:
|
||||
ret.append(dir_path.replace('/', '.') + '.tests')
|
||||
|
||||
return ret
|
||||
|
||||
def setup():
|
||||
"""
|
||||
Sets up connection and session
|
||||
"""
|
||||
from webnotes.db import Database
|
||||
webnotes.conn = Database()
|
||||
webnotes.session = {'user':'Administrator'}
|
||||
|
||||
if __name__=='__main__':
|
||||
setup()
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
tests_list = sys.argv[1:]
|
||||
|
||||
# for unittest.main
|
||||
sys.argv = sys.argv[:1]
|
||||
else:
|
||||
tests_list = get_tests()
|
||||
|
||||
for tests in tests_list:
|
||||
exec 'from %s import *' % str(tests)
|
||||
|
||||
unittest.main()
|
||||
|
|
@ -10,7 +10,7 @@ try:
|
|||
|
||||
import sys, os, cgi
|
||||
|
||||
sys.path.append(os.getcwd()+'/cgi-bin')
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'cgi-bin'))
|
||||
|
||||
import webnotes
|
||||
import webnotes.defs
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue