def unzip(source_filename, dest_dir): with zipfile.ZipFile(source_filename) as zf: zf.extractall(dest_dir) #borrowed from http://bytes.com/topic/python/answers/851018-how-zip-directory-python-using-zipfile def recursive_zip(zipf, directory, folder=""): for item in os.listdir(directory): if os.path.isfile(os.path.join(directory, item)): zipf.write(os.path.join(directory, item), folder + os.sep + item) elif os.path.isdir(os.path.join(directory, item)): recursive_zip( zipf, os.path.join(directory, item), folder + os.sep + item) #read web2py version from VERSION file web2py_version_line = readlines_file('VERSION')[0] #use regular expression to get just the version number v_re = re.compile('[0-9]+\.[0-9]+\.[0-9]+') web2py_version = v_re.search(web2py_version_line).group(0) #pull in preferences from config file import ConfigParser Config = ConfigParser.ConfigParser() Config.read('setup_exe.conf') remove_msft_dlls = Config.getboolean("Setup", "remove_microsoft_dlls") copy_apps = Config.getboolean("Setup", "copy_apps") copy_site_packages = Config.getboolean("Setup", "copy_site_packages") copy_scripts = Config.getboolean("Setup", "copy_scripts") make_zip = Config.getboolean("Setup", "make_zip") zip_filename = Config.get("Setup", "zip_filename") remove_build_files = Config.getboolean("Setup", "remove_build_files")
""" from distutils.core import setup import py2exe from gluon.import_all import base_modules, contributed_modules from gluon.fileutils import readlines_file from glob import glob import fnmatch import os import shutil import sys import re import zipfile #read web2py version from VERSION file web2py_version_line = readlines_file('VERSION')[0] #use regular expression to get just the version number v_re = re.compile('[0-9]+\.[0-9]+\.[0-9]+') web2py_version = v_re.search(web2py_version_line).group(0) #pull in preferences from config file import ConfigParser Config = ConfigParser.ConfigParser() Config.read('setup_exe.conf') remove_msft_dlls = Config.getboolean("Setup", "remove_microsoft_dlls") copy_apps = Config.getboolean("Setup", "copy_apps") copy_site_packages = Config.getboolean("Setup", "copy_site_packages") copy_scripts = Config.getboolean("Setup", "copy_scripts") make_zip = Config.getboolean("Setup", "make_zip") zip_filename = Config.get("Setup", "zip_filename") remove_build_files = Config.getboolean("Setup", "remove_build_files")
def crondance(applications_parent, ctype='hard', startup=False, apps=None): """ Does the periodic job of cron service: read the crontab(s) and launch the various commands. """ apppath = os.path.join(applications_parent, 'applications') token = Token(applications_parent) cronmaster = token.acquire(startup=startup) if not cronmaster: return try: now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) logger = getLogger(logger_name) if not apps: apps = [ x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x)) ] full_apath_links = set() if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable base_commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): base_commands.append(w2p_path) base_commands.extend( ('--cron_job', '--no_banner', '--no_gui', '--plain')) for app in apps: if _stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = [ line.strip() for line in fileutils.readlines_file(crontab, 'rt') ] lines = [ line for line in cronlines if line and not line.startswith('#') ] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('crontab read error %s', e) continue for task in tasks: if _stopping: break if not task: continue task_min = task.get('min', []) if not startup and task_min == [-1]: continue citems = [(k in task and not v in task[k]) for k, v in checks] if task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('%s cron: %s executing %r in %s at %s', ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now()) action = models = False command = task['cmd'] if command.startswith('**'): action = True command = command[2:] elif command.startswith('*'): action = models = True command = command[1:] if action: commands = base_commands[:] if command.endswith('.py'): commands.extend(('-S', app, '-R', command)) else: commands.extend(('-S', app + '/' + command)) if models: commands.append('-M') else: commands = shlex.split(command) try: if not _launcher(commands): logger.warning( 'no thread available, cannot execute %r', task['cmd']) except Exception: logger.exception('error executing %r', task['cmd']) finally: token.release()
def crondance(applications_parent, ctype='soft', startup=False, apps=None): # TODO: docstring apppath = os.path.join(applications_parent, 'applications') token = Token(applications_parent) cronmaster = token.acquire(startup=startup) if not cronmaster: return now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) if apps is None: apps = [x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x))] full_apath_links = set() if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable base_commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): base_commands.append(w2p_path) if applications_parent != global_settings.gluon_parent: base_commands.extend(('-f', applications_parent)) base_commands.extend(('-J', # FIXME: this should not be needed since we are # not launching the web server '-a', '"<recycle>"')) for app in apps: if _cron_stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = [line.strip() for line in fileutils.readlines_file(crontab, 'rt')] lines = [line for line in cronlines if line and not line.startswith('#')] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('crontab read error %s', e) continue for task in tasks: if _cron_stopping: break citems = [(k in task and not v in task[k]) for k, v in checks] task_min = task.get('min', []) if not task: continue elif not startup and task_min == [-1]: continue elif task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('%s cron: %s executing %s in %s at %s', ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now()) action = models = False command = task['cmd'] if command.startswith('**'): action = True command = command[2:] elif command.startswith('*'): action = models = True command = command[1:] if action: commands = base_commands[:] if command.endswith('.py'): commands.extend(('-S', app, '-R', command)) else: commands.extend(('-S', app + '/' + command)) if models: commands.append('-M') else: commands = command try: cronlauncher(commands).start() except Exception as e: logger.warning('execution error for %s: %s', task.get('cmd'), e) token.release()
def crondance(applications_parent, ctype='soft', startup=False, apps=None): apppath = os.path.join(applications_parent, 'applications') cron_path = os.path.join(applications_parent) token = Token(cron_path) cronmaster = token.acquire(startup=startup) if not cronmaster: return now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) if apps is None: apps = [ x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x)) ] full_apath_links = set() for app in apps: if _cron_stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = fileutils.readlines_file(crontab, 'rt') lines = [ x.strip() for x in cronlines if x.strip() and not x.strip().startswith('#') ] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('WEB2PY CRON: crontab read error %s' % e) continue for task in tasks: if _cron_stopping: break if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): commands.append(w2p_path) if applications_parent != global_settings.gluon_parent: commands.extend(('-f', applications_parent)) citems = [(k in task and not v in task[k]) for k, v in checks] task_min = task.get('min', []) if not task: continue elif not startup and task_min == [-1]: continue elif task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('WEB2PY CRON (%s): %s executing %s in %s at %s' % (ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now())) action, command, models = False, task['cmd'], '' if command.startswith('**'): (action, models, command) = (True, '', command[2:]) elif command.startswith('*'): (action, models, command) = (True, '-M', command[1:]) else: action = False if action and command.endswith('.py'): commands.extend(( '-J', # cron job models, # import models? '-S', app, # app name '-a', '"<recycle>"', # password '-R', command)) # command elif action: commands.extend(( '-J', # cron job models, # import models? '-S', app + '/' + command, # app name '-a', '"<recycle>"')) # password else: commands = command # from python docs: # You do not need shell=True to run a batch file or # console-based executable. shell = False try: cronlauncher(commands, shell=shell).start() except Exception as e: logger.warning('WEB2PY CRON: Execution error for %s: %s' % (task.get('cmd'), e)) token.release()
def crondance(applications_parent, ctype='soft', startup=False, apps=None): apppath = os.path.join(applications_parent, 'applications') cron_path = os.path.join(applications_parent) token = Token(cron_path) cronmaster = token.acquire(startup=startup) if not cronmaster: return now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) if apps is None: apps = [x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x))] full_apath_links = set() for app in apps: if _cron_stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = fileutils.readlines_file(crontab, 'rt') lines = [x.strip() for x in cronlines if x.strip( ) and not x.strip().startswith('#')] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('WEB2PY CRON: crontab read error %s' % e) continue for task in tasks: if _cron_stopping: break if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): commands.append(w2p_path) if applications_parent != global_settings.gluon_parent: commands.extend(('-f', applications_parent)) citems = [(k in task and not v in task[k]) for k, v in checks] task_min = task.get('min', []) if not task: continue elif not startup and task_min == [-1]: continue elif task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('WEB2PY CRON (%s): %s executing %s in %s at %s' % (ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now())) action, command, models = False, task['cmd'], '' if command.startswith('**'): (action, models, command) = (True, '', command[2:]) elif command.startswith('*'): (action, models, command) = (True, '-M', command[1:]) else: action = False if action and command.endswith('.py'): commands.extend(('-J', # cron job models, # import models? '-S', app, # app name '-a', '"<recycle>"', # password '-R', command)) # command elif action: commands.extend(('-J', # cron job models, # import models? '-S', app + '/' + command, # app name '-a', '"<recycle>"')) # password else: commands = command # from python docs: # You do not need shell=True to run a batch file or # console-based executable. shell = False try: cronlauncher(commands, shell=shell).start() except Exception as e: logger.warning( 'WEB2PY CRON: Execution error for %s: %s' % (task.get('cmd'), e)) token.release()
def crondance(applications_parent, ctype='soft', startup=False, apps=None): # TODO: docstring apppath = os.path.join(applications_parent, 'applications') token = Token(applications_parent) cronmaster = token.acquire(startup=startup) if not cronmaster: return now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) if apps is None: apps = [ x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x)) ] full_apath_links = set() if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable base_commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): base_commands.append(w2p_path) if applications_parent != global_settings.gluon_parent: base_commands.extend(('-f', applications_parent)) base_commands.extend(( '-J', # FIXME: this should not be needed since we are # not launching the web server '-a', '"<recycle>"')) for app in apps: if _cron_stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = [ line.strip() for line in fileutils.readlines_file(crontab, 'rt') ] lines = [ line for line in cronlines if line and not line.startswith('#') ] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('crontab read error %s', e) continue for task in tasks: if _cron_stopping: break citems = [(k in task and not v in task[k]) for k, v in checks] task_min = task.get('min', []) if not task: continue elif not startup and task_min == [-1]: continue elif task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('%s cron: %s executing %s in %s at %s', ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now()) action = models = False command = task['cmd'] if command.startswith('**'): action = True command = command[2:] elif command.startswith('*'): action = models = True command = command[1:] if action: commands = base_commands[:] if command.endswith('.py'): commands.extend(('-S', app, '-R', command)) else: commands.extend(('-S', app + '/' + command)) if models: commands.append('-M') else: commands = command try: cronlauncher(commands).start() except Exception as e: logger.warning('execution error for %s: %s', task.get('cmd'), e) token.release()
def crondance(applications_parent, ctype='soft', startup=False, apps=None): """ Does the periodic job of cron service: read the crontab(s) and launch the various commands. """ apppath = os.path.join(applications_parent, 'applications') token = Token(applications_parent) cronmaster = token.acquire(startup=startup) if not cronmaster: return now_s = time.localtime() checks = (('min', now_s.tm_min), ('hr', now_s.tm_hour), ('mon', now_s.tm_mon), ('dom', now_s.tm_mday), ('dow', (now_s.tm_wday + 1) % 7)) logger = getLogger(logger_name) if not apps: apps = [x for x in os.listdir(apppath) if os.path.isdir(os.path.join(apppath, x))] full_apath_links = set() if sys.executable.lower().endswith('pythonservice.exe'): _python_exe = os.path.join(sys.exec_prefix, 'python.exe') else: _python_exe = sys.executable base_commands = [_python_exe] w2p_path = fileutils.abspath('web2py.py', gluon=True) if os.path.exists(w2p_path): base_commands.append(w2p_path) if applications_parent != global_settings.gluon_parent: base_commands.extend(('-f', applications_parent)) base_commands.extend(('--cron_job', '--no_banner', '--no_gui', '--plain')) for app in apps: if _cron_stopping: break apath = os.path.join(apppath, app) # if app is a symbolic link to other app, skip it full_apath_link = absolute_path_link(apath) if full_apath_link in full_apath_links: continue else: full_apath_links.add(full_apath_link) cronpath = os.path.join(apath, 'cron') crontab = os.path.join(cronpath, 'crontab') if not os.path.exists(crontab): continue try: cronlines = [line.strip() for line in fileutils.readlines_file(crontab, 'rt')] lines = [line for line in cronlines if line and not line.startswith('#')] tasks = [parsecronline(cline) for cline in lines] except Exception as e: logger.error('crontab read error %s', e) continue for task in tasks: if _cron_stopping: break if not task: continue task_min = task.get('min', []) if not startup and task_min == [-1]: continue citems = [(k in task and not v in task[k]) for k, v in checks] if task_min != [-1] and reduce(lambda a, b: a or b, citems): continue logger.info('%s cron: %s executing %r in %s at %s', ctype, app, task.get('cmd'), os.getcwd(), datetime.datetime.now()) action = models = False command = task['cmd'] if command.startswith('**'): action = True command = command[2:] elif command.startswith('*'): action = models = True command = command[1:] if action: commands = base_commands[:] if command.endswith('.py'): commands.extend(('-S', app, '-R', command)) else: commands.extend(('-S', app + '/' + command)) if models: commands.append('-M') else: commands = shlex.split(command) try: # FIXME: using a new thread every time there is a task to # launch is not a good idea in a long running process cronlauncher(commands).start() except Exception: logger.exception('error starting %r', task['cmd']) token.release()
""" from distutils.core import setup import py2exe from gluon.import_all import base_modules, contributed_modules from gluon.fileutils import readlines_file from glob import glob import fnmatch import os import shutil import sys import re import zipfile # read web2py version from VERSION file web2py_version_line = readlines_file("VERSION")[0] # use regular expression to get just the version number v_re = re.compile("[0-9]+\.[0-9]+\.[0-9]+") web2py_version = v_re.search(web2py_version_line).group(0) # pull in preferences from config file import ConfigParser Config = ConfigParser.ConfigParser() Config.read("setup_exe.conf") remove_msft_dlls = Config.getboolean("Setup", "remove_microsoft_dlls") copy_apps = Config.getboolean("Setup", "copy_apps") copy_site_packages = Config.getboolean("Setup", "copy_site_packages") copy_scripts = Config.getboolean("Setup", "copy_scripts") make_zip = Config.getboolean("Setup", "make_zip") zip_filename = Config.get("Setup", "zip_filename")