87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
"""
|
|
Utilities for using modules
|
|
"""
|
|
|
|
transfer_types = ['Role', 'Print Format','DocType','Page','DocType Mapper','GL Mapper','Search Criteria', 'Patch']
|
|
|
|
def scrub(txt):
|
|
return txt.replace(' ','_').replace('-', '_').replace('/', '_').lower()
|
|
|
|
def scrub_dt_and_dn(dt, dn):
|
|
"""
|
|
Returns in lowercase and code friendly names of doctype and name for certain types
|
|
"""
|
|
ndt, ndn = dt, dn
|
|
if dt.lower() in ('doctype', 'search criteria', 'page'):
|
|
ndt, ndn = scrub(dt), scrub(dn)
|
|
|
|
return ndt, ndn
|
|
|
|
def get_item_file(module, dt, dn):
|
|
"""
|
|
Returns the path of the item file
|
|
"""
|
|
import os
|
|
ndt, ndn = scrub_dt_and_dn(dt, dn)
|
|
|
|
return os.path.join(get_module_path(module), ndt, ndn, ndn + '.txt')
|
|
|
|
def get_item_timestamp(module, dt, dn):
|
|
"""
|
|
Return ths timestamp of the given item (if exists)
|
|
"""
|
|
return get_file_timestamp(get_item_file(module, dt, dn))
|
|
|
|
def get_file_timestamp(fn):
|
|
"""
|
|
Returns timestamp of the given file
|
|
"""
|
|
import os
|
|
from webnotes.utils import cint
|
|
|
|
try:
|
|
return str(cint(os.stat(fn).st_mtime))
|
|
except OSError, e:
|
|
if e.args[0]!=2:
|
|
raise e
|
|
else:
|
|
return None
|
|
|
|
def get_module_path(module):
|
|
"""
|
|
Returns path of the given module (imports it and reads it from __file__)
|
|
"""
|
|
import webnotes.defs, os, os.path
|
|
|
|
try:
|
|
exec ('import ' + scrub(module)) in locals()
|
|
modules_path = eval(scrub(module) + '.__file__')
|
|
|
|
modules_path = os.path.sep.join(modules_path.split(os.path.sep)[:-1])
|
|
except ImportError, e:
|
|
# get module path by importing the module
|
|
modules_path = os.path.join(webnotes.defs.modules_path, scrub(module))
|
|
|
|
return modules_path
|
|
|
|
def switch_module(dt, dn, to, frm=None, export=None):
|
|
"""
|
|
Change the module of the given doctype, if export is true, then also export txt and copy
|
|
code files from src
|
|
"""
|
|
import os
|
|
webnotes.conn.sql("update `tab"+dt+"` set module=%s where name=%s", (to, dn))
|
|
|
|
if export:
|
|
export_doc(dt, dn)
|
|
|
|
# copy code files
|
|
if dt in ('DocType', 'Page', 'Search Criteria'):
|
|
from_path = os.path.join(get_module_path(frm), scrub(dt), scrub(dn), scrub(dn))
|
|
to_path = os.path.join(get_module_path(to), scrub(dt), scrub(dn), scrub(dn))
|
|
|
|
# make dire if exists
|
|
os.system('mkdir -p %s' % os.path.join(get_module_path(to), scrub(dt), scrub(dn)))
|
|
|
|
for ext in ('py','js','html','css'):
|
|
os.system('cp %s %s')
|