예제 #1
0
def content_hook():
    auth_key = current_app.config.get('GIT_UPDATE_HOOK_TOKEN')

    if not auth_key:
        # no key configured (default) → feature not enabled
        abort(404)

    key = request.args.get('token')
    if not key:
        logger.debug("`update-content` called without Token",
                     extra={'data': {'request_args': request.args}})
        abort(401)

    if key != auth_key:
        logger.warning("`update-content` called with wrong Token",
                       extra={'data': {'request_args': request.args,
                                       'auth_key': auth_key}})
        abort(403)

    logger.info("Update hook triggered. Fetching content.")
    reload_necessary = update_repo(current_app.config['FLATPAGES_ROOT'])
    if reload_necessary:
        try:
            import uwsgi
        except ImportError:
            logger.debug("UWSGI not present, skipping reload")
            pass
        else:
            logger.debug("Reloading UWSGI...")
            uwsgi.reload()

    # 204: No content
    # https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#204
    return "", 204
예제 #2
0
 def get(self, request):
     try:
         import uwsgi
         uwsgi.reload()
     except ImportError:
         pass  # Probably the django started with runserver
     return HttpResponseRedirect(reverse('rosetta-home'))
예제 #3
0
    def _update_settings(self, values):
        """Modify the settings associated with the given dictionary."""
        for name, value in values.iteritems():
            if name in tmpl_context.settings:
                setting = tmpl_context.settings[name]
            else:
                setting = Setting(key=name, value=value)
            if value is None:
                value = u''
            else:
                value = unicode(value)
            if setting.value != value:
                setting.value = value
                DBSession.add(setting)
        DBSession.flush()

        # Clear the settings cache unless there are multiple processes.
        # We have no way of notifying the other processes that they need
        # to clear their caches too, so we've just gotta let it play out
        # until all the caches expire.
        if not request.environ.get('wsgi.multiprocess', False):
            app_globals.settings_cache.clear()
        else:
            # uWSGI provides an automagically included module
            # that we can use to call a graceful restart of all
            # the uwsgi processes.
            # http://projects.unbit.it/uwsgi/wiki/uWSGIReload
            try:
                import uwsgi
                uwsgi.reload()
            except ImportError:
                pass
예제 #4
0
 def reload_uwsgi_on_code_change(sig):
     """This function will check every five seconds to see whether
     the Django code has changed, and if it has uWSGI will reload.
     This mimics the autoreload functionality
     of manage.py runserver."""
     if autoreload.code_changed():
         uwsgi.reload()
예제 #5
0
 def update_uwsgi(signum):
     flatpages_root = app.config["FLATPAGES_ROOT"]
     logger.debug("Udpating git repository at %s", flatpages_root)
     hasToReload = update_repo(flatpages_root)
     if hasToReload:
         logger.debug("Reloading flatpages…")
         uwsgi.reload()
예제 #6
0
파일: views.py 프로젝트: 20tab/uwsgi
def reload(request):
	if uwsgi.masterpid() > 0:
		uwsgi.reload()
		request.user.message_set.create(message="uWSGI reloaded")
	else:
		request.user.message_set.create(message="The uWSGI master process is not active")

	return HttpResponseRedirect(reverse(index))
예제 #7
0
def reload_server():
    try:
        import uwsgi
        uwsgi.reload()
    except ImportError:
        try:
            path = inspect.getsourcefile(reload_server)
            os.utime(path, None)
        except Exception:
            pass
예제 #8
0
    def change_code_graceful_reload(sig):
        """
        This method allows for the django runserver's autoreload
        capability in uWSGI.
        """
        if (getattr(settings, 'DEBUG', False)
            and getattr(settings, 'UWSGI_AUTORELOAD', False)
            and autoreload.code_changed()):

            uwsgi.reload()
예제 #9
0
 def delete(self):
     name = self.request.path_info.strip(' /')
     # Delete document
     docStore = get_userDocumentStore(self.session.user.username)
     id_ = docStore.outIdNormalizer.process_string(session, name)
     docStore.delete_document(session, id_)
     self.response.status = "204 Deleted"
     if uwsgi:
         uwsgi.reload()
     return ''
def overwrite_data(request):
    data = ''
    if request.method == 'POST':
        data = request.POST.get('text', '')
    if data != '':
        file = open('static/contractor_info.js', 'w')
        file.write(data)
        file.close
        uwsgi.reload()
    return HttpResponse('')
예제 #11
0
파일: api.py 프로젝트: ArchiGithub/zynq-sdk
def upgrade_app():
    if request.method == 'POST':
        file_ = next((file_ for file_ in request.files if api_app.is_valid_app_file(file_)), None)
        if file_ is not None:
            filename = secure_filename(file_)
            request.files[file_].save(os.path.join(api_app.config['UPLOAD_FOLDER'], filename))
            tmp_file = os.path.join('/tmp/', filename)
            subprocess.call(['/usr/bin/unzip', '-o', tmp_file, '-d', '/usr/local/flask'])
            uwsgi.reload()
            return make_response('Updating app')
예제 #12
0
 def update_uwsgi(signum):
     flatpages_root = app.config["FLATPAGES_ROOT"]
     logger.debug("Udpating git repository at %s", flatpages_root)
     hasToReload = update_repo(flatpages_root)
     if hasToReload:
         logger.debug("Reloading flatpages and uwsgi", extra={'data': {
             'uwsgi.numproc': uwsgi.numproc,
             'uwsgi.opt': uwsgi.opt,
             'uwsgi.applications': uwsgi.applications,
         }})
         uwsgi.reload()
예제 #13
0
파일: views.py 프로젝트: revealit/libreveal
def uwsgi_reload(request):
    allowed_ips = getattr(settings, 'INTERNAL_IPS', ('127.0.0.1',),)

    if request.META['REMOTE_ADDR'] in allowed_ips:
        try:
            import uwsgi
            uwsgi.reload()
            return HttpResponse('Reload OK')
        except ImportError:
            return HttpResponseServerError('Failed to import uwsgi')

    return HttpResponseForbidden('Access denied. %s is not on the INTERNAL_IPS list.' % request.META['REMOTE_ADDR'])
예제 #14
0
def reload_uwsgi():
    if not have_uwsgi:
        return "you aren't running web2py with uwsgi"
    masterpid = uwsgi.masterpid()
    if masterpid > 0:
        form = FORM(INPUT(_type="submit", _value="Reload uWSGI"))
        if form.process().accepted:
            uwsgi.reload()
            response.flash = "uWSGI reloaded correctly"
    else:
        form = ''
        response.flash = "The uWSGI master process is not active"
    return form
예제 #15
0
파일: option.py 프로젝트: fivef/bartendro
def ws_options():
    if request.method == 'GET':
        options = Option.query.order_by(asc(func.lower(Option.key)))
        data = {}
        for o in options:
            try:    
                if isinstance(bartendro_options[o.key], types.IntType):
                   value = int(o.value)
                elif isinstance(bartendro_options[o.key], types.UnicodeType):
                   value = unicode(o.value)
                elif isinstance(bartendro_options[o.key], types.FloatType):
                   value = float(o.value)
                elif isinstance(bartendro_options[o.key], types.BooleanType):
                   value = boolean(o.value)
                else:
                   raise InternalServerError
            except KeyError:
                pass

            data[o.key] = value

        return json.dumps({ 'options' : data });

    if request.method == 'POST':
        try:
            data = request.json['options']
            logout = request.json['logout']
        except KeyError:
            raise BadRequest

        if logout: logout_user()

        Option.query.delete()

        for key in data:
            option = Option(key, data[key])
            db.session.add(option)

        db.session.commit()
        try:
            import uwsgi
            uwsgi.reload()
            reload = True
        except ImportError:
            reload = False
            
        app.options = load_options()
        return json.dumps({ 'reload' : reload });

    raise BadRequest
예제 #16
0
def reload(request):
    if uwsgi.masterpid() > 0:
        uwsgi.reload()
        messages.add_message(request,
                             messages.SUCCESS,
                             _('uWSGI reloaded'),
                             fail_silently=True)
    else:
        messages.add_message(request,
                             messages.ERROR,
                             _('The uWSGI master process is not active'),
                             fail_silently=True)

    return HttpResponseRedirect(reverse(index))
예제 #17
0
def reload(request):
    import uwsgi
    if uwsgi.masterpid() > 0:
        uwsgi.reload()
        messages.add_message(request,
                             messages.SUCCESS,
                             _('uWSGI reloaded'),
                             fail_silently=True)
    else:
        messages.add_message(request,
                             messages.ERROR,
                             _('The uWSGI master process is not active'),
                             fail_silently=True)

    return HttpResponseRedirect(reverse("admin:uwsgi_status_changelist"))
예제 #18
0
파일: reload.py 프로젝트: palankai/xadrpy
def reload_wsgi(request):
    if 'mod_wsgi.process_group' in request.environ and \
        request.environ.get('mod_wsgi.process_group', None) and \
        'SCRIPT_FILENAME' in request.environ and \
        int(request.environ.get('mod_wsgi.script_reloading', '0')):
            try:
                os.utime(request.environ.get('SCRIPT_FILENAME'), None)
            except OSError:
                pass
    # Try auto-reloading via uwsgi daemon reload mechanism
    try:
        import uwsgi #@UnresolvedImport
        # pretty easy right?
        uwsgi.reload()
    except:
        # we may not be running under uwsgi :P
        pass
예제 #19
0
    def auto_reload(request):
        if defaults.WSGI_AUTO_RELOAD is True:
            env = request.environ.get
            if env('mod_wsgi.process_group') and env('SCRIPT_FILENAME'):
                if int(env('mod_wsgi.script_reloading', 0)):
                    try:
                        if os.path.exists(env('SCRIPT_FILENAME')):
                            os.utime(env('SCRIPT_FILENAME'), None)
                    except OSError:
                        pass

        if defaults.UWSGI_AUTO_RELOAD is True:
            try:
                import uwsgi

                uwsgi.reload()
            except ImportError:
                pass
예제 #20
0
    def auto_reload(request):
        if defaults.WSGI_AUTO_RELOAD is True:
            env = request.environ.get
            if env('mod_wsgi.process_group') and env('SCRIPT_FILENAME'):
                if int(env('mod_wsgi.script_reloading', 0)):
                    try:
                        if os.path.exists(env('SCRIPT_FILENAME')):
                            os.utime(env('SCRIPT_FILENAME'), None)
                    except OSError:
                        pass

        if defaults.UWSGI_AUTO_RELOAD is True:
            try:
                import uwsgi

                uwsgi.reload()
            except ImportError:
                pass
예제 #21
0
def update_all(request):
    if True:
        import uwsgi
        from git import Repo
        from git import Git

        git_ssh_identity_file = '/srv/deployment.key'
        os.system(
            'ssh-keyscan -t rsa github.com >> /home/django/.ssh/known_hosts;')
        git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
        repo = Repo('/srv/application')
        with repo.git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
            repo.remotes.origin.pull()

        call_command('collectstatic', interactive=False, verbosity=3)
        uwsgi.reload()
        return HttpResponse('Done')
    else:
        return HttpResponse('Not supported')
예제 #22
0
파일: views.py 프로젝트: velrest/modelreg
def system_update(req):
    import uwsgi
    import git
    from django.core.management import call_command

    # This does not work properly yet, so disabling the code for now
    # if req.POST['ref'] != 'refs/heads/master':
    #     response = HttpResponseBadRequest()
    #     response.write("Not master branch, not updating")
    #     return response


    git_cmd = git.cmd.Git(settings.BASE_DIR)
    git_cmd.pull()

    call_command('migrate')

    uwsgi.reload()
    return render(req, 'system_update.html')
예제 #23
0
def gitpull():
    if request.headers.get('X-GitHub-Event') == "push":
        payload = json.loads(request.data)
        if payload['ref'] == "refs/heads/master":
            try:
                subprocess.check_output(['git', 'pull'])
                subprocess.check_output([
                    'pip', 'install', '-U', '--user', '-r', 'requirements.txt',
                    '--no-cache'
                ])
                import uwsgi
                uwsgi.reload()
                return return_json({'rebooting': True})
            except ImportError:
                pass
    return return_json({
        'rebooting': False,
        'commit-id': APP.config['commit-id']
    })
예제 #24
0
def ws_options():
    if request.method == 'GET':
        options = Option.query.order_by(asc(func.lower(Option.key)))
        data = {}
        for o in options:
            if isinstance(bartendro_options[o.key], int):
                value = int(o.value)
            elif isinstance(bartendro_options[o.key], unicode):
                value = unicode(o.value)
            elif isinstance(bartendro_options[o.key], boolean):
                value = boolean(o.value)
            else:
                raise InternalServerError
            data[o.key] = value

        return json.dumps({'options': data})

    if request.method == 'POST':
        try:
            data = request.json['options']
            logout = request.json['logout']
        except KeyError:
            raise BadRequest

        if logout: logout_user()

        Option.query.delete()

        for key in data:
            option = Option(key, data[key])
            db.session.add(option)

        db.session.commit()
        try:
            import uwsgi
            uwsgi.reload()
            reload = True
        except ImportError:
            reload = False
        return json.dumps({'reload': reload})

    raise BadRequest
예제 #25
0
파일: views.py 프로젝트: syl0735/itops
def restartuwsgi(request):
    try:
        username = request.session.get('username')
        if username == returnadminusernamevalue:
            try:
                import uwsgi
                uwsgi.reload()
                insert_log(username, request, 'True', str('restartuwsgi'), '重启 uwsgi服务 成功')
                result = {'isSuccess': True, "message": '重启 uwsgi服务 成功'}
            except Exception as e:
                insert_log(username, request, 'False', str('restartuwsgi'), '重启 uwsgi服务 失败'+str(e))
                result = {'isSuccess': False, "message": '重启 uwsgi服务 失败'+str(e)}
        else:
            result = {'isSuccess': False, "message": '权限不足'}
    except Exception as e:
        result = {'isSuccess': False, "message": str(e)}
    response = HttpResponse()
    response['Content-Type'] = "text/javascript"
    response.write(json.dumps(result))
    return response
예제 #26
0
def on_pull_request(data):
    org, repo, pr_number = webhooks.parse_pr_url(data['pull_request']['url'])
    print([org, repo, pr_number])
    if data['action'] == 'synchronize' or data['action'] == 'opened':
        webhooks.set_check(data, 'pending', 'Waiting for tests')
    if data['action'] == 'labeled' or data['action'] == 'unlabeled':
        pr = webhooks.load_pr(data)
        if pr.state == 'open':
            return webhooks.check_pr_for_mergability(pr)
        elif pr.state == 'closed' and 'Overdue-on-GH' in [
                l.name for l in pr.as_issue().labels
        ]:
            # We can't actually reboot when `master` is pushed like the other sites.
            # So this is a lovely hack to reboot ourselves when we absolutely need to.
            try:
                import uwsgi
                uwsgi.reload()
            except ImportError:
                pass
            return 'I need to be reloaded'
    return ''
예제 #27
0
파일: views.py 프로젝트: Splawik/pytigon
def restart_server(request):
    
    restarted = False
    try:
        if platform.system() == "Linux":
            if platform.system() == "Linux":
                if type(request).__name__=="AsgiRequest":
                    os.kill(os.getpid(), signal.SIGINT)
                    restarted = True
                elif 'mod_wsgi.process_group' in request.environ:
                    if request.environ['mod_wsgi.process_group'] != '':
                        os.kill(os.getpid(), signal.SIGINT)
                        restarted = True
                else:
                    try:
                        import uwsgi
                        uwsgi.reload()
                        restarted = True
                    except:
                        pass
            else:
                try:
                    import uwsgi
                    uwsgi.reload()
                    restarted = True
                except:
                    pass
        else:
            ctypes.windll.libhttpd.ap_signal_parent(1)
            restarted = True
    except:
        pass
    
    script = "<script>jQuery('#ModalLabel').html('Restart');</script>"
    
    if restarted:
        return HttpResponse("<html>%s<body>Restarted</body></html>" % script)
    else:
        return HttpResponse("<html>%s<body>I can't restart server</body></html>" % script)
예제 #28
0
파일: __init__.py 프로젝트: abhijitr/giza
def check_for_modifications(signum):
    global file_mtimes

    def iter_module_files():
        for module in sys.modules.values():
            filename = getattr(module, '__file__', None)
            if filename:
                old = None
                while not os.path.isfile(filename):
                    old = filename
                    filename = os.path.dirname(filename)
                    if filename == old:
                        break
                else:
                    if filename[-4:] in ('.pyc', '.pyo'):
                        filename = filename[:-1]
                    yield filename

    def iter_assets():
        base = os.path.dirname(os.path.dirname(__file__))
        for root, dirnames, filenames in os.walk(base + 'static'):
            for filename in filenames:
                if filename.endswith('.js') or filename.endswith('.less'):
                    yield os.path.join(root, filename)

    mtimes = file_mtimes
    for filename in itertools.chain(iter_module_files(), iter_assets()):
        try:
            mtime = os.stat(filename).st_mtime
        except OSError:
            continue

        old_time = mtimes.get(filename)
        if old_time is None:
            mtimes[filename] = mtime
            continue
        elif mtime > old_time:
            uwsgi.reload()
            return
예제 #29
0
def restart_server(request):
    
    restarted = False
    try:
        if platform.system() == "Linux":
            if platform.system() == "Linux":
                if type(request).__name__=="AsgiRequest":
                    os.kill(os.getpid(), signal.SIGINT)
                    restarted = True
                elif 'mod_wsgi.process_group' in request.environ:
                    if request.environ['mod_wsgi.process_group'] != '':
                        os.kill(os.getpid(), signal.SIGINT)
                        restarted = True
                else:
                    try:
                        import uwsgi
                        uwsgi.reload()
                        restarted = True
                    except:
                        pass
            else:
                try:
                    import uwsgi
                    uwsgi.reload()
                    restarted = True
                except:
                    pass
        else:
            ctypes.windll.libhttpd.ap_signal_parent(1)
            restarted = True
    except:
        pass
    
    script = "<script>jQuery('#ModalLabel').html('Restart');</script>"
    
    if restarted:
        return HttpResponse("<html>%s<body>Restarted</body></html>" % script)
    else:
        return HttpResponse("<html>%s<body>I can't restart server</body></html>" % script)
예제 #30
0
 def post(self):
     name = self.request.path_info.strip(' /')
     if not name:
         # Accidental re-submit of login form?
         # Return index page
         return self.get()
     data = self.request.body
     # Create a Document
     doc = StringDocument(
         data,
         creator=self.session.user.username,
         filename=name,
         tagName="ead"
     )
     # TODO: Validate
     # Store document
     docStore = get_userDocumentStore(self.session.user.username)
     doc.id = id_ = docStore.outIdNormalizer.process_string(session, name)
     docStore.store_document(session, doc)
     self.response.status = "201 Created"
     if uwsgi:
         uwsgi.reload()
     return ""
예제 #31
0
def process_github_webhook() -> Response:
    if request.headers.get('X-GitHub-Event') == 'push':
        payload = json.loads(request.data)
        expected = 'refs/heads/{0}'.format(current_app.config['branch'])
        if payload['ref'] == expected:
            try:
                subprocess.check_output(['git', 'fetch'])
                subprocess.check_output(['git', 'reset', '--hard', 'origin/{0}'.format(current_app.config['branch'])])
                try:
                    subprocess.check_output(['pip', 'install', '-U', '--user', '-r', 'requirements.txt', '--no-cache'])
                except subprocess.CalledProcessError:
                    pass
                import uwsgi
                uwsgi.reload()
                return return_json({'rebooting': True})
            except ImportError:
                pass
    return return_json({
        'rebooting': False,
        'commit-id': current_app.config['commit-id'],
        'current_branch': current_app.config['branch'],
        'ref': payload['ref'],
        'expected': expected
        })
예제 #32
0
def appliance_restart():
    uwsgi.reload()
예제 #33
0
 def p_work():
     time.sleep(2)
     # 重启server
     print("重启server")
     uwsgi.reload()
예제 #34
0
파일: models.py 프로젝트: glomium/elmnt.de
def server_restart(**kwargs):
    if uwsgi is not None and uwsgi.masterpid() > 0:
        uwsgi.reload()
예제 #35
0
 def change_code_gracefull_reload(sig):
     if autoreload.code_changed():
         print("RELOADING...")
         uwsgi.reload()
예제 #36
0
 def reload_on_code_change(sig):
     if autoreload.code_changed():
         uwsgi.reload()
예제 #37
0
 def run(self) -> None:
     sleep(self.delay / 1000)
     uwsgi.reload()
예제 #38
0
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        print "reloading"
        uwsgi.reload()
예제 #39
0
def home(request):
    """
    Displays a list of messages to be translated
    """

    def fix_nls(in_, out_):
        """Fixes submitted translations by filtering carriage returns and pairing
        newlines at the begging and end of the translated string with the original
        """
        if 0 == len(in_) or 0 == len(out_):
            return out_

        if "\r" in out_ and "\r" not in in_:
            out_ = out_.replace("\r", '')

        if "\n" == in_[0] and "\n" != out_[0]:
            out_ = "\n" + out_
        elif "\n" != in_[0] and "\n" == out_[0]:
            out_ = out_.lstrip()
        if "\n" == in_[-1] and "\n" != out_[-1]:
            out_ = out_ + "\n"
        elif "\n" != in_[-1] and "\n" == out_[-1]:
            out_ = out_.rstrip()
        return out_

    storage = get_storage(request)
    version = rosetta.get_version(True)
    if storage.has('rosetta_i18n_fn'):
        rosetta_i18n_fn = storage.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split('-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = storage.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(rosetta_i18n_fn, wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    entry.msgid.encode("utf8") +
                    entry.msgstr.encode("utf8") +
                    (entry.msgctxt and entry.msgctxt.encode("utf8") or "")
                ).hexdigest()

        else:
            rosetta_i18n_pofile = storage.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated', 'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                storage.set('rosetta_i18n_filter', filter_)
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = storage.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = unicode(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.find(md5hash, 'md5hash')
                    # If someone did a makemessage, some entries might
                    # have been removed, so we need to check.
                    if entry:
                        old_msgstr = entry.msgstr
                        if plural_id is not None:
                            #plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                            plural_string = fix_nls(entry.msgid_plural, value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(request.POST.get('f_%s' % md5hash, False))
                        old_fuzzy = 'fuzzy' in entry.flags

                        if old_fuzzy and not is_fuzzy:
                            entry.flags.remove('fuzzy')
                        elif not old_fuzzy and is_fuzzy:
                            entry.flags.append('fuzzy')

                        file_change = True

                        if old_msgstr != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(sender=entry,
                                               user=request.user,
                                               old_msgstr=old_msgstr,
                                               old_fuzzy=old_fuzzy,
                                               pofile=rosetta_i18n_fn,
                                               language_code=rosetta_i18n_lang_code,
                                               )

                    else:
                        storage.set('rosetta_last_save_error', True)

            if file_change and rosetta_i18n_write:
                try:
                    # Provide defaults in case authorization is not required.
                    request.user.first_name = getattr(request.user, 'first_name', 'Anonymous')
                    request.user.last_name = getattr(request.user, 'last_name', 'User')
                    request.user.email = getattr(request.user, 'email', '*****@*****.**')

                    rosetta_i18n_pofile.metadata['Last-Translator'] = unicodedata.normalize('NFKD', u"%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore')
                    rosetta_i18n_pofile.metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False)
                    rosetta_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass

                try:
                    rosetta_i18n_pofile.save()
                    po_filepath, ext = os.path.splitext(rosetta_i18n_fn)
                    save_as_mo_filepath = po_filepath + '.mo'
                    rosetta_i18n_pofile.save_as_mofile(save_as_mo_filepath)

                    post_save.send(sender=None, language_code=rosetta_i18n_lang_code, request=request)
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.WSGI_AUTO_RELOAD and \
                        'mod_wsgi.process_group' in request.environ and \
                        request.environ.get('mod_wsgi.process_group', None) and \
                        'SCRIPT_FILENAME' in request.environ and \
                        int(request.environ.get('mod_wsgi.script_reloading', '0')):
                            try:
                                os.utime(request.environ.get('SCRIPT_FILENAME'), None)
                            except OSError:
                                pass
                    # Try auto-reloading via uwsgi daemon reload mechanism
                    if rosetta_settings.UWSGI_AUTO_RELOAD:
                        try:
                            import uwsgi
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    storage.set('rosetta_i18n_write', False)
                storage.set('rosetta_i18n_pofile', rosetta_i18n_pofile)

                # Retain query arguments
                query_arg = '?_next=1'
                if 'query' in request.GET or 'query' in request.POST:
                    query_arg += '&query=%s' % request.REQUEST.get('query')
                if 'page' in request.GET:
                    query_arg += '&page=%d&_next=1' % int(request.GET.get('page'))
                return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
        rosetta_i18n_lang_name = _(storage.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')

        if 'query' in request.REQUEST and request.REQUEST.get('query', '').strip():
            query = request.REQUEST.get('query').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(smart_unicode(e.msgstr) + smart_unicode(e.msgid) + u''.join([o[0] for o in e.occurrences]))], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([e for e in rosetta_i18n_pofile.fuzzy_entries() if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE)

        if 'page' in request.GET and int(request.GET.get('page')) <= paginator.num_pages and int(request.GET.get('page')) > 0:
            page = int(request.GET.get('page'))
        else:
            page = 1

        if '_next' in request.GET or '_next' in request.POST:
            page += 1
            if page > paginator.num_pages:
                page = 1
            query_arg = '?page=%d' % page
            return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_messages = paginator.page(page).object_list

        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in rosetta_messages:
                message.main_lang = po.find(message.msgid).msgstr

        needs_pagination = paginator.num_pages > 1
        if needs_pagination:
            if paginator.num_pages >= 10:
                page_range = pagination_range(1, paginator.num_pages, page)
            else:
                page_range = range(1, 1 + paginator.num_pages)
        try:
            ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/admin/'
        except AttributeError:
            ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/'
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.BING_APP_ID and rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
        BING_APP_ID = rosetta_settings.BING_APP_ID
        MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
        MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE
        if storage.has('rosetta_last_save_error'):
            storage.delete('rosetta_last_save_error')
            rosetta_last_save_error = True

        for message in rosetta_messages:
            if not message.msgid_plural:
                continue

            tmp = SortedDict()
            keylist = sorted(map(int, message.msgstr_plural.keys()))
            for k in keylist:
                tmp[k] = message.msgstr_plural[str(k)]

            message.msgstr_plural = tmp

        tmpl = 'rosetta/pofile.html'
        if 'singlepage' in rosetta_i18n_fn:
            tmpl = 'rosetta/pofile_singlepage.html'

        return render_to_response(tmpl, locals(), context_instance=RequestContext(request))
    else:
        return list_languages(request, do_session_warn=True)
예제 #40
0
def code_monitor_reload(sig):
    if server.code_monitor and server.code_monitor.check_changed():
        uwsgi.reload()
예제 #41
0
def monitor_py(num):
    logging.info("main.py has been modified,reboot")
    uwsgi.reload()
예제 #42
0
 def reload_on_code_change(sig):
     if autoreload.code_changed():
         uwsgi.reload()
예제 #43
0
def home(request):
    """
    Displays a list of messages to be translated
    """
    def fix_nls(in_, out_):
        """Fixes submitted translations by filtering carriage returns and pairing
        newlines at the begging and end of the translated string with the original
        """
        if 0 == len(in_) or 0 == len(out_):
            return out_

        if "\r" in out_ and "\r" not in in_:
            out_ = out_.replace("\r", '')

        if "\n" == in_[0] and "\n" != out_[0]:
            out_ = "\n" + out_
        elif "\n" != in_[0] and "\n" == out_[0]:
            out_ = out_.lstrip()
        if "\n" == in_[-1] and "\n" != out_[-1]:
            out_ = out_ + "\n"
        elif "\n" != in_[-1] and "\n" == out_[-1]:
            out_ = out_.rstrip()
        return out_

    def _request_request(key, default=None):
        if key in request.GET:
            return request.GET.get(key)
        elif key in request.POST:
            return request.POST.get(key)
        return default

    storage = get_storage(request)
    query = ''
    if storage.has('rosetta_i18n_fn'):
        rosetta_i18n_fn = storage.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split(
            '-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = storage.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(
                rosetta_i18n_fn, wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    (six.text_type(entry.msgid) + six.text_type(entry.msgstr) +
                     six.text_type(entry.msgctxt
                                   or "")).encode('utf8')).hexdigest()

        else:
            rosetta_i18n_pofile = storage.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated',
                                             'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                storage.set('rosetta_i18n_filter', filter_)
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = storage.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = six.text_type(rx_plural.match(key).groups()[1])

                    # Above no longer true as of Polib 1.0.4
                    if plural_id and plural_id.isdigit():
                        plural_id = int(plural_id)

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.find(md5hash, 'md5hash')
                    # If someone did a makemessage, some entries might
                    # have been removed, so we need to check.
                    if entry:
                        old_msgstr = entry.msgstr
                        if plural_id is not None:
                            #plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                            plural_string = fix_nls(entry.msgid_plural, value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(
                            request.POST.get('f_%s' % md5hash, False))
                        old_fuzzy = 'fuzzy' in entry.flags

                        if old_fuzzy and not is_fuzzy:
                            entry.flags.remove('fuzzy')
                        elif not old_fuzzy and is_fuzzy:
                            entry.flags.append('fuzzy')

                        file_change = True

                        if old_msgstr != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(
                                sender=entry,
                                user=request.user,
                                old_msgstr=old_msgstr,
                                old_fuzzy=old_fuzzy,
                                pofile=rosetta_i18n_fn,
                                language_code=rosetta_i18n_lang_code,
                            )

                    else:
                        storage.set('rosetta_last_save_error', True)

            if file_change and rosetta_i18n_write:
                try:
                    rosetta_i18n_pofile.metadata[
                        'Last-Translator'] = unicodedata.normalize(
                            'NFKD', u"%s %s <%s>" %
                            (getattr(request.user, 'first_name', 'Anonymous'),
                             getattr(request.user, 'last_name', 'User'),
                             getattr(request.user, 'email',
                                     '*****@*****.**'))).encode(
                                         'ascii', 'ignore')
                    rosetta_i18n_pofile.metadata[
                        'X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(
                            False)
                    rosetta_i18n_pofile.metadata[
                        'PO-Revision-Date'] = timestamp_with_timezone()
                except UnicodeDecodeError:
                    pass

                try:
                    rosetta_i18n_pofile.save()
                    po_filepath, ext = os.path.splitext(rosetta_i18n_fn)
                    save_as_mo_filepath = po_filepath + '.mo'
                    rosetta_i18n_pofile.save_as_mofile(save_as_mo_filepath)

                    post_save.send(sender=None,
                                   language_code=rosetta_i18n_lang_code,
                                   request=request)
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if rosetta_settings.WSGI_AUTO_RELOAD and \
                        'mod_wsgi.process_group' in request.environ and \
                        request.environ.get('mod_wsgi.process_group', None) and \
                        'SCRIPT_FILENAME' in request.environ and \
                            int(request.environ.get('mod_wsgi.script_reloading', '0')):
                        try:
                            os.utime(request.environ.get('SCRIPT_FILENAME'),
                                     None)
                        except OSError:
                            pass
                    # Try auto-reloading via uwsgi daemon reload mechanism
                    if rosetta_settings.UWSGI_AUTO_RELOAD:
                        try:
                            import uwsgi
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    storage.set('rosetta_i18n_write', False)
                storage.set('rosetta_i18n_pofile', rosetta_i18n_pofile)

                # Retain query arguments
                query_arg = '?_next=1'
                if _request_request('query', False):
                    query_arg += '&query=%s' % _request_request('query')
                if 'page' in request.GET:
                    query_arg += '&page=%d&_next=1' % int(
                        request.GET.get('page'))
                return HttpResponseRedirect(
                    reverse('rosetta-home') + iri_to_uri(query_arg))
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')

        if _request_request('query', False) and _request_request('query',
                                                                 '').strip():
            query = _request_request('query', '').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([
                e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(
                    six.text_type(e.msgstr) + six.text_type(e.msgid) +
                    u''.join([o[0] for o in e.occurrences]))
            ], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(
                    rosetta_i18n_pofile.untranslated_entries(),
                    rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(),
                                      rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([
                    e for e in rosetta_i18n_pofile.fuzzy_entries()
                    if not e.obsolete
                ], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator(
                    [e for e in rosetta_i18n_pofile if not e.obsolete],
                    rosetta_settings.MESSAGES_PER_PAGE)

        if 'page' in request.GET and int(
                request.GET.get('page')) <= paginator.num_pages and int(
                    request.GET.get('page')) > 0:
            page = int(request.GET.get('page'))
        else:
            page = 1

        if '_next' in request.GET or '_next' in request.POST:
            page += 1
            if page > paginator.num_pages:
                page = 1
            query_arg = '?page=%d' % page
            return HttpResponseRedirect(
                reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_messages = paginator.page(page).object_list
        main_language = None
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(
                rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            for message in rosetta_messages:
                message.main_lang = po.find(message.msgid).msgstr

        needs_pagination = paginator.num_pages > 1
        if needs_pagination:
            if paginator.num_pages >= 10:
                page_range = pagination_range(1, paginator.num_pages, page)
            else:
                page_range = range(1, 1 + paginator.num_pages)
        try:
            ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/admin/'
        except AttributeError:
            ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/'

        if storage.has('rosetta_last_save_error'):
            storage.delete('rosetta_last_save_error')
            rosetta_last_save_error = True
        else:
            rosetta_last_save_error = False

        return render_to_response(
            'rosetta/pofile.html',
            dict(version=rosetta.get_version(True),
                 ADMIN_MEDIA_PREFIX=ADMIN_MEDIA_PREFIX,
                 ADMIN_IMAGE_DIR=ADMIN_IMAGE_DIR,
                 rosetta_settings=rosetta_settings,
                 rosetta_i18n_lang_name=_(
                     storage.get('rosetta_i18n_lang_name')),
                 rosetta_i18n_lang_code=rosetta_i18n_lang_code,
                 rosetta_i18n_lang_bidi=rosetta_i18n_lang_bidi,
                 rosetta_last_save_error=rosetta_last_save_error,
                 rosetta_i18n_filter=rosetta_i18n_filter,
                 rosetta_i18n_write=rosetta_i18n_write,
                 rosetta_messages=rosetta_messages,
                 page_range=needs_pagination and page_range,
                 needs_pagination=needs_pagination,
                 main_language=main_language,
                 rosetta_i18n_app=rosetta_i18n_app,
                 page=page,
                 query=query,
                 paginator=paginator,
                 rosetta_i18n_pofile=rosetta_i18n_pofile),
            context_instance=RequestContext(request))
    else:
        return list_languages(request, do_session_warn=True)
예제 #44
0
def monitor_py(num):
    logging.info("configfile has been modified,reboot")
    uwsgi.reload()
예제 #45
0
파일: wrapper.py 프로젝트: fossabot/beecell
 def reload(self):
     '''
     Gracefully reload the uWSGI portal2 stack.
     '''
     return uwsgi.reload()
예제 #46
0
파일: wsgi.py 프로젝트: bernytt/yunwei
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        print 'reload now [%s]' % time.strftime('%Y-%m-%d %T', time.localtime(time.time()))
        uwsgi.reload()
    else:
        pass
예제 #47
0
def routing(signum):
    state = getmemorystate()
    current_workers_tasks = state["current_workers_tasks"]
    workers_tasks = state["workers_tasks"]
    # reload
    if state.has_key("stoping"):
        print "seems we are reload"
        print "let's start"
        if not len(current_workers_tasks.keys()):
            print "oo there is no tasks"
            print uwsgi.workers()
            del state["stoping"]
            setmemorystate(state)
            uwsgi.reload()
        return

    # stop
    if state.has_key("stop"):
        print "seems we are stoping"
        print "let's start"
        if not len(current_workers_tasks.keys()):
            print "oo there is not tasks"
            print uwsgi.workers()
            uwsgi.stop()
        return
    # working or not
    if state.has_key("working") and state["working"]:
        print "working"
    else:
        print "idle"
        return

    # choose command to start
    tmp_commands = commands.copy()
    for working_task in current_workers_tasks.keys():
        print "delete %s " % current_workers_tasks[working_task]["comand_key"]
        del tmp_commands[current_workers_tasks[working_task]
                         ["comand_key"]]  # there is no possible exception here

    # if comman is existed in past half of length also do not start
    cmds = tmp_commands.keys()
    past_length = len(cmds) / 2
    for past_work in workers_tasks[-1 * past_length:]:
        try:
            del tmp_commands[past_work]  #but here it seems to be
        except KeyError:
            print "%s is already deleted  from possible executin" % past_work

    cmds = tmp_commands.keys()
    to_start_command = None
    if len(cmds):
        to_start_command = random.choice(cmds)
    else:
        print "it's seems that everything is working recently, do not let workers to be lazy"
        to_start_command = random.choice(commands.keys())

    workers_signal_tmp = workers_signal[:]
    for busy_worker in current_workers_tasks.keys():
        print "delete from choice busy worker %i" % busy_worker
        workers_signal_tmp.remove(busy_worker)

    if len(workers_signal_tmp) > 0:
        print "choosing from the workers"
        print workers_signal_tmp
        number_of_signal = random.choice(workers_signal_tmp)
        workers_tasks.append(to_start_command)

        # write to shard memory index of  command
        state["to_start"] = to_start_command
        current_workers_tasks[number_of_signal] = {
            "started": datetime.now(),
            "comand_key": to_start_command,
            "command": commands[to_start_command]
        }
        state["workers_tasks"] = workers_tasks[
            -300:]  # only 300 save in history
        state["current_workers_tasks"] = current_workers_tasks
        setmemorystate(state)

        try:
            print "ok sending signal %i" % number_of_signal
            print "and going start %s" % str(commands[to_start_command])
            print "and going start %s" % to_start_command
            uwsgi.signal(number_of_signal)
            print workers_tasks
        except:
            traceback.print_exc()
            print "oh no %i busy" % number_of_signal
    else:
        print "=" * 64
        print "there is no free workers ?!"
        print "=" * 64

    print "busy workers "
    nw = datetime.now()
    for working_id in current_workers_tasks.keys():
        print "%i -> %s" % (working_id, str(current_workers_tasks[working_id]))
        working_delta = nw - current_workers_tasks[working_id]["started"]
        if working_delta > dt(minutes=5):
            print "this process seems to be stuck"
            print "%i -> %s" % (
                working_id, str(current_workers_tasks[working_id]["command"]))
예제 #48
0
def reloaded(num):
    uwsgi.reload()
예제 #49
0
파일: main.py 프로젝트: tenggeli/py_weixin
 def GET(self):
     uwsgi.reload()
     return "uwsgi is reload success"
예제 #50
0
def do_reload_uwsgi():
    uwsgi.reload()
예제 #51
0
 def change_code_gracefull_reload(sig):
     uwsgi.reload()
예제 #52
0
    def post(self, request, *args, **kwargs):
        """The only circumstances when we POST is to submit the main form, both
        updating translations (if any changed) and advancing to the next page of
        messages.

        There is no notion of validation of this content; as implemented, unknown
        fields are ignored and a generic failure message is shown.

        Submitted changes are saved out to the specified .po file on the
        filesystem if that file is writable, otherwise the cached version of the
        file is updated (so it can be downloaded). Then the user is redirected
        to the next page of messages (if there is one; otherwise they're
        redirected back to the current page).
        """
        # The message text inputs are captured as hashes of their initial
        # contents, preceded by "m_". Messages with plurals end with their
        # variation number.
        single_text_input_regex = re.compile(r'^m_([0-9a-f]+)$')
        plural_text_input_regex = re.compile(r'^m_([0-9a-f]+)_([0-9]+)$')
        file_change = False
        for field_name, new_msgstr in request.POST.items():
            md5hash = None

            if plural_text_input_regex.match(field_name):
                md5hash, plural_id = plural_text_input_regex.match(
                    field_name).groups()
                md5hash = str(md5hash)
                # polib parses .po files into unicode strings, but
                # doesn't bother to convert plural indexes to int,
                # so we need unicode here.
                plural_id = six.text_type(plural_id)

                # Above no longer true as of Polib 1.0.4
                if plural_id and plural_id.isdigit():
                    plural_id = int(plural_id)

            elif single_text_input_regex.match(field_name):
                md5hash = str(
                    single_text_input_regex.match(field_name).groups()[0])
                plural_id = None

            if md5hash is not None:  # Empty string should be processed!
                entry = self.po_file.find(md5hash, 'md5hash')
                # If someone did a makemessage, some entries might
                # have been removed, so we need to check.
                if entry:
                    old_msgstr = entry.msgstr
                    if plural_id is not None:  # 0 is ok!
                        entry.msgstr_plural[plural_id] = self.fix_nls(
                            entry.msgid_plural, new_msgstr)
                    else:
                        entry.msgstr = self.fix_nls(entry.msgid, new_msgstr)

                    is_fuzzy = bool(
                        self.request.POST.get('f_%s' % md5hash, False))
                    old_fuzzy = 'fuzzy' in entry.flags

                    if old_fuzzy and not is_fuzzy:
                        entry.flags.remove('fuzzy')
                    elif not old_fuzzy and is_fuzzy:
                        entry.flags.append('fuzzy')

                    file_change = True

                    if old_msgstr != new_msgstr or old_fuzzy != is_fuzzy:
                        entry_changed.send(
                            sender=entry,
                            user=request.user,
                            old_msgstr=old_msgstr,
                            old_fuzzy=old_fuzzy,
                            pofile=self.po_file_path,
                            language_code=self.language_id,
                        )
                else:
                    messages.error(
                        self.request,
                        _("Some items in your last translation block couldn't "
                          "be saved: this usually happens when the catalog file "
                          "changes on disk after you last loaded it."),
                    )

        if file_change and self.po_file_is_writable:
            try:
                self.po_file.metadata[
                    'Last-Translator'] = unicodedata.normalize(
                        'NFKD',
                        u"%s %s <%s>" % (
                            getattr(self.request.user, 'first_name',
                                    'Anonymous'),
                            getattr(self.request.user, 'last_name', 'User'),
                            getattr(self.request.user, 'email',
                                    '*****@*****.**'),
                        ),
                    ).encode('ascii', 'ignore')
                self.po_file.metadata[
                    'X-Translated-Using'] = u"django-rosetta %s" % (
                        get_rosetta_version())
                self.po_file.metadata[
                    'PO-Revision-Date'] = timestamp_with_timezone()
            except UnicodeDecodeError:
                pass

            try:
                self.po_file.save()
                po_filepath, ext = os.path.splitext(self.po_file_path)

                if rosetta_settings.AUTO_COMPILE:
                    self.po_file.save_as_mofile(po_filepath + '.mo')

                post_save.send(sender=None,
                               language_code=self.language_id,
                               request=self.request)
                # Try auto-reloading via the WSGI daemon mode reload mechanism
                should_try_wsgi_reload = (
                    rosetta_settings.WSGI_AUTO_RELOAD
                    and 'mod_wsgi.process_group' in self.request.environ and
                    self.request.environ.get('mod_wsgi.process_group', None)
                    and 'SCRIPT_FILENAME' in self.request.environ and int(
                        self.request.environ.get('mod_wsgi.script_reloading',
                                                 0)))
                if should_try_wsgi_reload:
                    try:
                        os.utime(self.request.environ.get('SCRIPT_FILENAME'),
                                 None)
                    except OSError:
                        pass
                # Try auto-reloading via uwsgi daemon reload mechanism
                if rosetta_settings.UWSGI_AUTO_RELOAD:
                    try:
                        import uwsgi

                        uwsgi.reload()  # pretty easy right?
                    except:
                        pass  # we may not be running under uwsgi :P
                # XXX: It would be nice to add a success message here!
            except Exception as e:
                messages.error(self.request, e)

        if file_change and not self.po_file_is_writable:
            storage = get_storage(self.request)
            storage.set(self.po_file_cache_key, self.po_file)

        # Reconstitute url to redirect to. Start with determining whether the
        # page number can be incremented.
        paginator = Paginator(self.get_entries(),
                              rosetta_settings.MESSAGES_PER_PAGE)
        try:
            page = int(self._request_request('page', 1))
        except ValueError:
            page = 1  # fall back to page 1
        else:
            if not (0 < page <= paginator.num_pages):
                page = 1
        if page < paginator.num_pages:
            page += 1
        query_string_args = {
            'msg_filter': self.msg_filter,
            'query': self.query,
            'ref_lang': self.ref_lang,
            'page': page,
        }
        # Winnow down the query string args to non-blank ones
        query_string_args = {k: v for k, v in query_string_args.items() if v}
        return HttpResponseRedirect("{url}?{qs}".format(
            url=reverse('rosetta-form', kwargs=self.kwargs),
            qs=urlencode_safe(query_string_args),
        ))
예제 #53
0
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        print('code change detected. autoreload -----------------')
        uwsgi.reload()
예제 #54
0
def home(request):
    """
    Displays a list of messages to be translated
    """
    def fix_nls(in_, out_):
        """Fixes submitted translations by filtering carriage returns and pairing
        newlines at the begging and end of the translated string with the original
        """
        if 0 == len(in_) or 0 == len(out_):
            return out_

        if "\r" in out_ and "\r" not in in_:
            out_ = out_.replace("\r", '')

        if "\n" == in_[0] and "\n" != out_[0]:
            out_ = "\n" + out_
        elif "\n" != in_[0] and "\n" == out_[0]:
            out_ = out_.lstrip()
        if "\n" == in_[-1] and "\n" != out_[-1]:
            out_ = out_ + "\n"
        elif "\n" != in_[-1] and "\n" == out_[-1]:
            out_ = out_.rstrip()
        return out_

    version = rosetta.get_version(True)
    if 'rosetta_i18n_fn' in request.session:
        rosetta_i18n_fn = request.session.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code']
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split(
            '-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = request.session.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(rosetta_i18n_fn)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    entry.msgid.encode("utf8") +
                    entry.msgstr.encode("utf8")).hexdigest()

        else:
            rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated',
                                             'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                request.session['rosetta_i18n_filter'] = filter_
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = request.session.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = unicode(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.find(md5hash, 'md5hash')
                    # If someone did a makemessage, some entries might
                    # have been removed, so we need to check.
                    if entry:
                        old_msgstr = entry.msgstr

                        if plural_id is not None:
                            plural_string = fix_nls(
                                entry.msgstr_plural[plural_id], value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(
                            request.POST.get('f_%s' % md5hash, False))
                        old_fuzzy = 'fuzzy' in entry.flags

                        if old_fuzzy and not is_fuzzy:
                            entry.flags.remove('fuzzy')
                        elif not old_fuzzy and is_fuzzy:
                            entry.flags.append('fuzzy')

                        file_change = True

                        if old_msgstr != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(
                                sender=entry,
                                user=request.user,
                                old_msgstr=old_msgstr,
                                old_fuzzy=old_fuzzy,
                                pofile=rosetta_i18n_fn,
                                language_code=rosetta_i18n_lang_code,
                            )

                    else:
                        request.session['rosetta_last_save_error'] = True

            if file_change and rosetta_i18n_write:

                try:
                    rosetta_i18n_pofile.metadata[
                        'Last-Translator'] = unicodedata.normalize(
                            'NFKD', u"%s %s <%s>" %
                            (request.user.first_name, request.user.last_name,
                             request.user.email)).encode('ascii', 'ignore')
                    rosetta_i18n_pofile.metadata[
                        'X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(
                            False)
                    rosetta_i18n_pofile.metadata[
                        'PO-Revision-Date'] = datetime.datetime.now().strftime(
                            '%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                try:
                    rosetta_i18n_pofile.save()
                    rosetta_i18n_pofile.save_as_mofile(
                        rosetta_i18n_fn.replace('.po', '.mo'))

                    post_save.send(sender=None,
                                   language_code=rosetta_i18n_lang_code,
                                   request=request)

                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.WSGI_AUTO_RELOAD and \
                        request.environ.has_key('mod_wsgi.process_group') and \
                        request.environ.get('mod_wsgi.process_group',None) and \
                        request.environ.has_key('SCRIPT_FILENAME') and \
                        int(request.environ.get('mod_wsgi.script_reloading', '0')):
                        try:
                            os.utime(request.environ.get('SCRIPT_FILENAME'),
                                     None)
                        except OSError:
                            pass
                    # Try auto-reloading via uwsgi daemon reload mechanism
                    if rosetta_settings.UWSGI_AUTO_RELOAD:
                        try:
                            import uwsgi
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    request.session['rosetta_i18n_write'] = False

                request.session['rosetta_i18n_pofile'] = rosetta_i18n_pofile

                # Retain query arguments
                query_arg = ''
                if 'query' in request.REQUEST:
                    query_arg = '?query=%s' % request.REQUEST.get('query')
                if 'page' in request.GET:
                    if query_arg:
                        query_arg = query_arg + '&'
                    else:
                        query_arg = '?'
                    query_arg = query_arg + 'page=%d' % int(
                        request.GET.get('page'))

                return HttpResponseRedirect(
                    reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_i18n_lang_name = _(
            request.session.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code')

        if 'query' in request.REQUEST and request.REQUEST.get('query',
                                                              '').strip():
            query = request.REQUEST.get('query').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([
                e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(
                    smart_unicode(e.msgstr) + smart_unicode(e.msgid) +
                    u''.join([o[0] for o in e.occurrences]))
            ], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(
                    rosetta_i18n_pofile.untranslated_entries(),
                    rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(),
                                      rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([
                    e for e in rosetta_i18n_pofile.fuzzy_entries()
                    if not e.obsolete
                ], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator(
                    [e for e in rosetta_i18n_pofile if not e.obsolete],
                    rosetta_settings.MESSAGES_PER_PAGE)

        if 'page' in request.GET and int(
                request.GET.get('page')) <= paginator.num_pages and int(
                    request.GET.get('page')) > 0:
            page = int(request.GET.get('page'))
        else:
            page = 1
        messages = paginator.page(page).object_list
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(
                rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in messages:
                message.main_lang = po.find(message.msgid).msgstr

        needs_pagination = paginator.num_pages > 1
        if needs_pagination:
            if paginator.num_pages >= 10:
                page_range = pagination_range(1, paginator.num_pages, page)
            else:
                page_range = range(1, 1 + paginator.num_pages)
        ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS

        MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
        MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE

        if 'rosetta_last_save_error' in request.session:
            del (request.session['rosetta_last_save_error'])
            rosetta_last_save_error = True

        return render_to_response('rosetta/pofile.html',
                                  locals(),
                                  context_instance=RequestContext(request))

    else:
        return list_languages(request)
예제 #55
0
def signal_reload():
    """ trigger graceful reload via uWSGI """
    uwsgi.reload()
    return "reloading"
예제 #56
0
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        uwsgi.reload()
예제 #57
0
파일: app.py 프로젝트: yueyoum/dianjing
def config_change(**kwargs):
    import uwsgi
    uwsgi.reload()
예제 #58
0
 def execute(self):
     import uwsgi
     uwsgi.reload()
예제 #59
0
    def change_code_gracefull_reload(sig):
	    if autoreload.code_changed():
		    uwsgi.reload()