Exemple #1
0
 def __init__(self, args):
     parser = argparse.ArgumentParser()
     parser.add_argument("-d", "--db_name", help = "database name")
     # [...] d'autres arguments
     # accessible via options.code_argument
     options = parser.parse_args(args)
     self.db_name = options.db_name
     self.pkg_path_1 = glob.cog_pkg_path(self.db_name)
     self.mod_path = self.pkg_path_1.replace("/", ".")
     in_cog_repo_db_name = glob._config('db', 'name')
     if in_cog_repo_db_name:
         sys.stderr.write(
             """you are in the '%s' collorg repository\n"""
             """you can't create a new repository in it\n"""
             """aborting\n""" % (
                 in_cog_repo_db_name))
         sys.exit(1)
     if os.path.exists("%s" % (self.db_name)):
         sys.stderr.write(
             "directory or file %s already in the way\n"
             "try cog make in that directory instead\n"
             "aborting\n" % (self.db_name))
         sys.exit(1)
     db_exists = os.popen(
         """psql --list | egrep '^ %s '""" % (
             self.db_name)).read().strip()
     if not db_exists:
         self.__create_db()
     self.__initdb()
     self.db = Controller(self.db_name).db
     self.charset = self.db._cog_controller._charset
     self.__make_cog_tree()
     self.db.table('collorg.core.data_type')._populate()
Exemple #2
0
 def __init__(self, nop, *args):
     self.charset = 'utf-8'
     self.__curdir = os.path.abspath(os.path.curdir)
     parser = argparse.ArgumentParser(prog="cog init")
     parser.add_argument(
         "-d", "--db_name", required = True, help = "database name")
     options = parser.parse_args(*args)
     self.db_name = options.db_name
     self.__collorg_db = Controller('collorg_db').db
     self.__cursor = Cursor(self.__collorg_db.db)
     self.__check()
     self.__db_connection_params()
     db_exists = os.popen(
         """psql --list -U %s | egrep '^ %s '""" % (
         self.user, self.db_name)).read().strip()
     if not db_exists:
         self.__create_db()
     else:
         sys.stderr.write(
             "The database already exists\n"
             "Drop if you really want to proceed\n" )
         sys.exit(1)
     self.db = Controller(self.db_name).db
     self.charset = self.db._cog_controller._charset
     self.__make_cog_repos()
Exemple #3
0
 def __add_missing_modules(self):
     self.db = Controller().db
     for fqtn in self.db.fqtns:
         cog_table = self.db.table('collorg.core.data_type')
         cog_table.fqtn_.value = fqtn
         tablename = fqtn.rsplit('.', 1)[1]
         cog_table.name_.value = tablename
         if not cog_table.count():
             print("+ adding table %s to data_type" % fqtn)
             cog_table.insert()
     sys.path.insert(0, self.__repos_base_dir)
     cog_app_pkg_dir = "%s/%s" % (self.__repos_base_dir, self.pkg_path)
     if not os.path.exists(cog_app_pkg_dir):
         os.makedirs(cog_app_pkg_dir)
     open("%s/__init__.py" % (cog_app_pkg_dir), "w")
     for schema in self.db.schemas:
         if (schema.name.find("collorg.") == 0 and
             self.db.name != 'collorg_db'):
             continue
         os.chdir(cog_app_pkg_dir)
         schema_path = schema.name.replace(".", "/")
         if schema_path.find("collorg/") == 0:
             schema_path = schema_path.replace("collorg/", "")
         if not os.path.exists(schema_path):
             print("+ new schema: %s" % schema_path)
             os.makedirs(schema_path)
             open("%s/__init__.py" % (schema_path), "w")
         os.chdir(schema_path)
         for tablename in schema.tables:
             fqtn = "%s.%s" % (schema.name, tablename)
             module = self.db.table(
                 'collorg.core.data_type', fqtn_ = fqtn, name_ = tablename)
             if module.is_empty():
                 module.insert()
             if not os.path.exists("%s/__init__.py" % (tablename)):
                 print("+ adding package %s.%s" % (schema.name, tablename))
                 fd = open("%s/__init__.py" % (tablename), "w")
                 inh_classes, import_statment = self.__get_inh_infos(fqtn)
                 fd.write(glob.module_template % (
                     self.__ctrl._charset,
                     import_statment,
                     tablename.capitalize(),
                     inh_classes,
                     schema.name, tablename,
                     tablename.capitalize()))
                 fd.close()
                 open("%s/cog/__init__.py" % (tablename), "w")
                 open("%s/%s/__init__.py" % (
                         tablename, glob.templates_dir), "w")
     Make(self.__ctrl, self.__db_name)
Exemple #4
0
 def __init__(self, controller, *args):
     self.__ctrl = controller
     if self.__ctrl is None:
         self.__ctrl = Controller()
     self.db_ = self.__ctrl.db
     self.__db_name = self.db_.name
     self.__collorg_db = None
     if self.__db_name != "collorg_db":
         self.__collorg_db_ctrl = Controller("collorg_db")
         self.__collorg_db = self.__collorg_db_ctrl.db
     self.pkg_path = glob.cog_pkg_path(self.db_.name)
     self.mod_path = self.pkg_path.replace("/", ".")
     self.__repos_base_dir = self.__ctrl.repos_path
     sys.path.insert(0, self.__repos_base_dir)
     sys.path.insert(0, ".")
     self.__rerun = 0
     self.__parse_args()
     # XXX recopie brute de collorg.utils.make.py # REMOVE ME WHEN DONE
     self.template_module = None
     self.l_schemas = [self.mod_path]
     self.db_ = Controller(self.db_.name).db
     self.db_.table("collorg.core.data_type")._populate()
     self.charset = self.db_._cog_controller._charset
     System_users(self.__ctrl).check()
     self.make_cog_tree()
     cog_table = self.db_.table("collorg.core.data_type")
     for fqtn in self.db_.fqtns:
         cog_table = self.db_.table("collorg.core.data_type")
         cog_table.fqtn_.value = fqtn
         tablename = fqtn.rsplit(".", 1)[1]
         cog_table.name_.value = tablename
         if not cog_table.count():
             cog_table.insert()
     self.__gen_templates()
     GenRelationalPart(self.__ctrl)
     os.system(
         "cd {};make clean_install > /dev/null" ";sudo python setup.py -q install".format(self.__repos_base_dir)
     )
     #        os.system("sudo service apache2 restart")
     if self.__rerun and self.__rerun < 2:
         os.system("cog make")
     #            print("Please rerun cog make to finish install.")
     if self.db_.name != "collorg_db":
         self.__sync_collorg_actions()
     Action_requirement(self.__ctrl).update_check()
     os.system("sudo service apache2 restart")
Exemple #5
0
class Init():
    def __init__(self, args):
        parser = argparse.ArgumentParser()
        parser.add_argument("-d", "--db_name", help = "database name")
        # [...] d'autres arguments
        # accessible via options.code_argument
        options = parser.parse_args(args)
        self.db_name = options.db_name
        self.pkg_path_1 = glob.cog_pkg_path(self.db_name)
        self.mod_path = self.pkg_path_1.replace("/", ".")
        in_cog_repo_db_name = glob._config('db', 'name')
        if in_cog_repo_db_name:
            sys.stderr.write(
                """you are in the '%s' collorg repository\n"""
                """you can't create a new repository in it\n"""
                """aborting\n""" % (
                    in_cog_repo_db_name))
            sys.exit(1)
        if os.path.exists("%s" % (self.db_name)):
            sys.stderr.write(
                "directory or file %s already in the way\n"
                "try cog make in that directory instead\n"
                "aborting\n" % (self.db_name))
            sys.exit(1)
        db_exists = os.popen(
            """psql --list | egrep '^ %s '""" % (
                self.db_name)).read().strip()
        if not db_exists:
            self.__create_db()
        self.__initdb()
        self.db = Controller(self.db_name).db
        self.charset = self.db._cog_controller._charset
        self.__make_cog_tree()
        self.db.table('collorg.core.data_type')._populate()

    def __check_pg_extensions(self):
        pg_config_present = os.popen('which pg_config').read().strip()
        try:
            assert pg_config_present
        except:
            sys.stderr.write("can't find pg_config!\naborting\n")
            sys.exit(1)
        pg_version = os.popen(sql_version % self.db_name).read()
        pg_release, pg_sub_release = pg_version.strip().split(".")[:2]
        self.pg_release = int(pg_release)
        self.pg_sub_release = int(pg_sub_release)
        self.pg_sharedir = os.popen("pg_config --sharedir").read().strip()

    def __create_db(self):
        self.__check_pg_extensions()
        create = input("will try to create %s database [y/N]? " % (
            self.db_name))
        if create.upper() != 'Y':
            print("aborting")
            sys.exit()
        exit_status = os.system(
            "createdb %s --owner collorg" % (self.db_name))
        if exit_status != 0:
            sys.stderr.write("unable to create database %s\n%s\n" % (
                    self.db_name, exit_status))
            sys.exit(exit_status)

    def __check_uuid(self):
        import uuid
        try:
            uuid_val = os.popen(
                """psql %s -qtc "SELECT uuid_generate_v1()" """
                """2> /dev/null """ % (
                    self.db_name)).read().strip()
            uuid.UUID(uuid_val)
            return True
        except:
            return False

    def __check_intarray(self):
        try:
            os.popen("""psql %s -qtc "SELECT icount('{1,2,3}'::int[])" """ % (
                self.db_name))
            return True
        except:
            return False

    def __initdb(self):
        """
        ajoute à la base les infos nécessaires pour un usage collorg
        * extension intarray
        * extension uuid-ossp
        * plpgsql
        """
        if self.__check_uuid() and self.__check_intarray():
            return
        self.__check_pg_extensions()
        print("adding the necessary extensions to the database")
        try:
            assert self.pg_release >= 9 or (
                self.pg_release >= 8 and self.pg_sub_release >= 4)
        except:
            sys.stderr.write("need postgresql 8.4 or greater\n"
                              "got %s.%s instead. aborting\n" % (
                    self.pg_release, self.pg_sub_release))
            sys.exit(1)
        os.popen("psql %s -qtc '%s'" % (self.db_name, sql_create_greater_9))
        print("please, re-run 'cog init -d %s' to finish the installation" % (
                self.db_name))
        sys.exit()

    def __make_cog_tree(self):
        db_name = self.db_name
        base_dir = "%s" % (db_name)
        if not os.path.exists(base_dir):
            os.mkdir(base_dir)
            open("%s/__init__.py" % (base_dir), "w")
            if not os.path.exists("%s/.cog" % (base_dir)):
                os.mkdir("%s/.cog" % (base_dir))
                open("%s/.cog/config" % (base_dir), "w").write(
                    glob.cog_config % (db_name))
        os.chdir(base_dir)
        abs_base_dir = os.path.abspath(os.path.curdir)
        sys.path.insert(0, abs_base_dir)
        cog_app_pkg_dir = self.pkg_path_1
        if not os.path.exists(cog_app_pkg_dir):
            os.makedirs(cog_app_pkg_dir)
            os.makedirs("%s/_cog_web_site/__src" % cog_app_pkg_dir)
            open("%s/__init__.py" % (self.pkg_path_1), "w")
            open("%s/_cog_web_site/__init__.py" % (
                cog_app_pkg_dir), "w")
        open("%s/__init__.py" % (cog_app_pkg_dir), "w")
        os.chdir(cog_app_pkg_dir)
        for schema in self.db.schemas:
            if schema.name.find("collorg.") == 0:
                continue
            if not os.path.exists(schema.name):
                os.mkdir(schema.name)
                open("%s/__init__.py" % (schema.name), "w")
                os.makedirs("%s/%s" % (schema.name, glob.templates_dir))
                open("%s/%s/__init__.py" % (
                        schema.name, glob.templates_dir), "w")
            os.chdir(schema.name)
            for tablename in schema.tables:
                fqtn = "%s.%s" % (schema.name, tablename)
                module = self.db.table(
                    'collorg.core.data_type',
                    fqtn_ = fqtn,
                    name_ = tablename)
                if module.is_empty():
                    module.insert()
                if not os.path.exists("%s.py" % (tablename)):
                    print("+ %s.%s" % (schema.name, tablename))
                    fd = open("__init__.py", "w")
                    fd.write(glob.module_template % (
                            self.charset,
                            tablename.capitalize(),
                            schema.name, tablename,
                            tablename.capitalize()))
                    fd.close()
                if not os.path.exists("%s/%s" % (
                        glob.templates_dir, tablename)):
                    os.makedirs("%s/%s" % (
                            glob.templates_dir, tablename))
                    open("%s/%s/__init__.py" % (
                            glob.templates_dir, tablename), "w")
                    open("%s/cog/__init__.py" % (tablename), "w")
                    os.makedirs("%s/__src/%s" % (
                            glob.templates_dir, tablename))
            os.chdir('..')
        os.chdir(abs_base_dir)
        if False:#not os.path.exists("setup.py"):
            #FIXME this is not working
            if not os.path.exists(".cog"):
                os.mkdir('.cog')
                open('.cog/config', "w").write(
                    glob.cog_config_template % (self.db_name))
            fd = open("setup.py", "w")
            fd.write(glob.setup_template % (self.pkg_path_1, self.db_name))
            fd.close()
        print("%s library generated.\n"
               "you can now go to the %s directory and\n"
               "'python setup.py -q install' to install it" % (
                self.db_name, self.db_name))
Exemple #6
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

db = Controller().db
table = db.table

data_types = table('collorg.core.data_type')

for dt in data_types:
    try:
        table(dt.fqtn_.value)
    except Exception as err:
        print("{}: {}".format(dt.fqtn_, err))
        ok = input('Remove entry from database [N/y] ? ')
        if ok.upper() == 'Y':
            dt._rev_field_.delete()
            dt.delete()
            db.raw_sql('drop table {}'.format(
                    db._fqtn_2_sql_fqtn(dt.fqtn_.value)))

Exemple #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Displays the access
"""

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

if __name__ == '__main__':
    ctrl = Controller()
    print(ctrl._d_anonymous_access)
    print(ctrl._d_visitor_access)

if False:#ÇA MARCHE PAS! LE SET_INTENTION SUR PVIEW!!!!
    action = None
    tv = ctrl._get_tasks_view()
    while action != "":
        action = input("action: ")
        if action == '':
            sys.exit()
        fqtn = input("fqtn: ")
        tv.name_.value = action
        tv.fqtn_.value = fqtn
        print(tv.select(just_return_sql = True))
        for elt in tv:
            print("%s" % (elt.task_name_))
Exemple #8
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

db = Controller().db
oid_post = input('post oid: ')
post = db.get_elt_by_oid(oid_post)
print(post.cog_label())
oid_data = input('data oid: ')
data = db.get_elt_by_oid(oid_data)
print(data.cog_label())

ok = input('ok [o/N]')
if ok.upper() == 'O':
    apd = db.table('collorg.communication.blog.a_post_data')
    apd._post_ = post
    apd._data_ = data
    apd.insert()

Exemple #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

if __name__ == '__main__':
    db = Controller().db
    function = db.table('collorg.actor.function')
    function.order_by(function.long_name_)
    for fctn in function:
        print(fctn.long_name_)
    fctn_lng_name = input("name: ")
    the_function = function()
    the_function.long_name_.value = fctn_lng_name
    data = the_function._data_type_.get()()
    for dt in data:
        print("%s : %s" % (dt.cog_oid_, dt.cog_label()))
    data_oid = input("oid data: ")
    data.cog_oid_.value = data_oid
    role = the_function._rev_role_
    user = db.table('collorg.actor.user')
    pseudo = input('pseudo: ')
    user.pseudo_.value = pseudo
    access = user._rev_access_
    access._data_ = data
    access.granted()
    if not access.is_granted():
Exemple #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
attach an existing function to a task
"""

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

if __name__ == '__main__':
    db = Controller().db
    f_long_name = sys.argv[1]
    function = db.table('collorg.actor.function')
    function.long_name_.value = f_long_name
    if function.count() != 1:
        sys.stderr.write("No such function (%s)\nAborting\n" % (f_long_name))
        sys.exit()
    task_name = sys.argv[2]
    task = db.table('collorg.application.task')
    task.name_.value = task_name
    if task.count() != 1:
        sys.stderr.write("No such task %s\nAborting\n" % (task_name))
        sys.exit()
    ok = input('ok [y/N]: ')
    if ok.upper() != 'Y':
        sys.stderr.write("exiting\n")
        sys.exit()
Exemple #11
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Tags write, moderate, admin according to a csv file
method_name, data_type, write, moderate, admin
"""

from collorg.controller.controller import Controller
import sys

if __name__ == "__main__":
    db = Controller().db
    tags_file = sys.argv[1]
    for line in open(tags_file).readlines():
        name, data_type, tags = line.split(";", 3)
        tags = tags.strip().split(";")
        action = db.table("collorg.application.action")
        action.name_.value = name
        action.data_type_.value = data_type
        naction = db.table("collorg.application.action")
        l_rights = [naction.write_, naction.moderate_, naction.admin_]
        idx = 0
        update = False
        for tag in tags:
            tag.strip()
            assert tag in ("t", "f", "")
            if tag:
                update = True
                l_rights[idx].value = tag
            idx += 1
Exemple #12
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Test: associates the actions not in tasks to the task "Anonymous navigation"
to allow navigation.
"""

from collorg.controller.controller import Controller

if __name__ == '__main__':
    db = Controller().db
    for label, function in [
        ('Authenticated navigation','Authenticated user'),
        ('Anonymous navigation', 'Anonymous user')]:
        goal = db.table('collorg.application.goal')
        goal.name_.value = label
        if goal.count() == 0:
            goal.insert()
        task = db.table('collorg.application.task')
        task.name_.value = label
        if task.count() == 0:
            task.insert()
            # link anonymous navigation to anonymous role
            func = db.table('collorg.actor.function')
            func.name_.value = function
            atf = task._rev_a_task_function_
            atf._function_ = func
            atf.insert()
        atg = task._rev_a_task_goal_
        atg._goal_ = goal
Exemple #13
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from collorg.controller.controller import Controller

"""
give information on the elements with a cog_oid begining with sys.argv[1]
"""

if __name__ == '__main__':
    db = Controller().db
    ot = db.table('collorg.core.oid_table')
    ot.cog_oid_.value = '%s%%' % (sys.argv[1]), 'like'
    for elt in ot:
        print("%s: %s" % (elt.cog_oid_, elt.cog_fqtn_))
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
attach an existing function to an existing inst_group (collorg.actor.inst_group)
"""

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

if __name__ == "__main__":
    db = Controller().db
    f_long_name = sys.argv[1]
    function = db.table("collorg.actor.function")
    function.long_name_.value = f_long_name
    if function.count() == 0:
        sys.stderr.write("No such function (%s)\nAborting\n" % (f_long_name))
        sys.exit()
    ig_name = sys.argv[2]
    inst_group = db.table("collorg.actor.inst_group")
    inst_group.name_.value = ig_name
    if inst_group.count() == 0:
        sys.stderr.write("No such inst_group %s\nAborting\n" % (ig_name))
        sys.exit()
    ok = input("ok [y/N]: ")
    if ok.upper() != "Y":
        sys.stderr.write("exiting\n")
        sys.exit()
Exemple #15
0
Add a new function to the database.
The arguments are:
    * name
    * long_name
    * data_type (fqtn)
    * optional: inst_group name
"""

import sys
from collorg.controller.controller import Controller

if sys.version_info.major < 3:
    input = raw_input

if __name__ == '__main__':
    db = Controller().db
    inst_group = None
    if len(sys.argv) == 5:
        ig_name = sys.argv[4]
        inst_group = db.table('collorg.actor.inst_group')
        inst_group.name_.value = ig_name
        if inst_group.count() == 0:
            sys.stderr.write("No such inst_group %s\nAborting\n" % (ig_name))
            sys.exit()
    name = sys.argv[1]
    long_name = sys.argv[2]
    fqtn = sys.argv[3]
    function = db.table('collorg.actor.function')
    function.long_name_.value = long_name
    if function.count():
        sys.stderr.write(
Exemple #16
0
#!/usr/bin/env python

import sys
from collorg.controller.controller import Controller
import gettext

def get_page_ref(method, oid):
    return "{}-{}".format(method, oid)

ctrl = Controller()
ctrl.cog_exec_env = None
ctrl.user = None
ctrl.get_page_ref = get_page_ref
ctrl.i18n = gettext.translation('messages', '/usr/share/collorg/locale', ['fr'])
ctrl._url = sys.argv[1]
#ctrl._url_scheme, nop = ctrl._url.split('://')
db = ctrl.db
table = db.table

posts = table('collorg.communication.blog.post')
posts.visibility_.value = 'public'
for elt in posts:
    if elt.cog_fqtn_.value != 'collorg.communication.blog.post':
        elt = elt.get()
    if elt.cog_fqtn_.value == 'collorg.web.topic':
        elt.data_type_.set_null()
        elt.action_.set_null()
    try:
        print("{} {}".format(elt.cog_fqtn_, elt.cog_oid_))
        elt._cog_get_cache('w3display')
    except:
Exemple #17
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from datetime import datetime
from random import random
from collorg.controller.controller import Controller

path = sys.argv[0].rsplit("/", 1)[0]

ctrl = Controller()
table = ctrl.db.table
post_table = table("collorg.communication.blog.post")
topics = table("collorg.web.topic")
topics.site_.set_not_null()
topics_oids = []
for topic in topics:
    topics_oids.append(topic.cog_oid_.value)

print (int(random() * 3) + 1)

visibilities = ["private", "protected", "public"]
users = []
for user in table("collorg.actor.user"):
    users.append(user)

texts = open("{}/lipsum.txt".format(path)).readlines()
titles = open("{}/lipsum_titles.txt".format(path)).readlines()

last = datetime.now()
for i in range(100000):
Exemple #18
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

from collorg.controller.controller import Controller

l_fqtn = ['organization.department', 'organization.team', 'collorg.actor.user']

db = Controller().db
groups = db.table('collorg.group.group')
for group in groups:
    data = group._data_.get()
    function = group._rev_a_group_role_._role_._function_
    if function.count() > 1:
        continue
    function.get()
    ng = group()
    if data.cog_fqtn_ in ['organization.department', 'organization.team']:
        ng.name_.value = '{} {}'.format(function.name_, data.acronym_)
    if data.cog_fqtn_ in ['collorg.actor.user']:
        ng.name_.value = "{} {}'s private group".format(
            data.first_name_, data.last_name_)
    group.update(ng)
Exemple #19
0
class Cmd:
    """
    * Generates the code to handle new tables in the database.
    * Generates the templates modules when cog templates are modified or
      created
    """

    def __init__(self, controller, *args):
        self.__ctrl = controller
        if self.__ctrl is None:
            self.__ctrl = Controller()
        self.db_ = self.__ctrl.db
        self.__db_name = self.db_.name
        self.__collorg_db = None
        if self.__db_name != "collorg_db":
            self.__collorg_db_ctrl = Controller("collorg_db")
            self.__collorg_db = self.__collorg_db_ctrl.db
        self.pkg_path = glob.cog_pkg_path(self.db_.name)
        self.mod_path = self.pkg_path.replace("/", ".")
        self.__repos_base_dir = self.__ctrl.repos_path
        sys.path.insert(0, self.__repos_base_dir)
        sys.path.insert(0, ".")
        self.__rerun = 0
        self.__parse_args()
        # XXX recopie brute de collorg.utils.make.py # REMOVE ME WHEN DONE
        self.template_module = None
        self.l_schemas = [self.mod_path]
        self.db_ = Controller(self.db_.name).db
        self.db_.table("collorg.core.data_type")._populate()
        self.charset = self.db_._cog_controller._charset
        System_users(self.__ctrl).check()
        self.make_cog_tree()
        cog_table = self.db_.table("collorg.core.data_type")
        for fqtn in self.db_.fqtns:
            cog_table = self.db_.table("collorg.core.data_type")
            cog_table.fqtn_.value = fqtn
            tablename = fqtn.rsplit(".", 1)[1]
            cog_table.name_.value = tablename
            if not cog_table.count():
                cog_table.insert()
        self.__gen_templates()
        GenRelationalPart(self.__ctrl)
        os.system(
            "cd {};make clean_install > /dev/null" ";sudo python setup.py -q install".format(self.__repos_base_dir)
        )
        #        os.system("sudo service apache2 restart")
        if self.__rerun and self.__rerun < 2:
            os.system("cog make")
        #            print("Please rerun cog make to finish install.")
        if self.db_.name != "collorg_db":
            self.__sync_collorg_actions()
        Action_requirement(self.__ctrl).update_check()
        os.system("sudo service apache2 restart")

    def __dup_task(self, task_to_dup, duped_task):
        """
        duplicates task_to_dup which is the result of a .get leaving
        the cog_* fields at None into duped_task.
        """
        t2d = task_to_dup
        dt_ = duped_task
        dt_.name_.value = t2d.name_.value
        dt_.delegable_.value = t2d.delegable_.value
        dt_.description_.value = t2d.description_.value
        return dt_

    def __dup_action(self, action_to_dup, duped_action):
        """
        action_to_dup is the action to duplicate. duped_action is the
        returned duplicated action
        """
        da_ = duped_action
        a2d = action_to_dup
        da_.data_type_.value = a2d.data_type_.value
        da_.name_.value = a2d.name_.value
        da_.label_.value = a2d.label_.value
        da_.description_.value = a2d.description_.value
        da_.format_.value = a2d.format_.value
        da_.source_.value = a2d.source_.value
        da_.raw_.value = a2d.raw_.value
        da_.protected_.value = a2d.protected_.value
        da_.in_menu_.value = a2d.in_menu_.value
        da_.in_header_.value = a2d.in_header_.value
        da_.in_nav_.value = a2d.in_nav_.value
        da_.write_.value = a2d.write_.value
        da_.moderate_.value = a2d.moderate_.value
        da_.admin_.value = a2d.admin_.value
        da_.icon_.value = a2d.icon_.value
        return da_

    def __sync_collorg_actions(self):
        """
        Synchonize application actions with collorg reference database.
        1. remove removed collorg actions
        2. add missing collorg actions
        """
        aa_ = self.db_.table("collorg.application.action")
        ca_ = self.__collorg_db.table("collorg.application.action")
        for action in aa_:
            sca = ca_()
            sca.data_type_.value = ca_.data_type_.value
            sca.name_.value = ca_.name_.value
            if sca.is_empty():
                print("- {}.{} missing".format(ca_.data_type_, ca_.name_))
                rat = action._rev_a_action_task_
                if not rat.is_empty():
                    rat.delete()
                action.delete()
        for action in ca_():
            aa_ = self.__dup_action(action, aa_())
            tasks = action._rev_a_action_task_._task_
            saa = aa_()
            saa.data_type_.value = aa_.data_type_.value
            saa.name_.value = aa_.name_.value
            if saa.is_empty():
                print("++ action {}.{}".format(aa_.data_type_, aa_.name_))
                aa_.insert()
                aa_.get()
                for task in tasks:
                    ta_ = self.db_.table("collorg.application.task")
                    ta_.name_.value = task.name_.value
                    if ta_.is_empty():
                        # XXX  mettre une méthode pour faire ce qui suit
                        ta_.delegable_.value = task.delegable_.value
                        ta_.description_.value = task.description_.value
                        ta_.insert()
                        orig_goal = task._rev_a_task_goal_._goal_
                        if not orig_goal.is_empty():
                            orig_goal.get()
                            goal = self.db_.table("collorg.application.goal")
                            goal.name_.value = orig_goal.name_.value
                            goal.insert()
                            atg = goal._rev_a_task_goal_
                            atg._task_ = ta_
                            atg.insert()
                        orig_function = task._rev_a_task_function_._function_
                        orig_function.get()
                        function = self.db_.table("collorg.actor.function")
                        function.name_.value = orig_function.name_.value
                        if function.is_empty():
                            function.fname_.value = orig_function.fname_.value
                            function.long_name_.value = orig_function.long_name_.value
                            function.advertise_.value = orig_function.advertise_.value
                            # XXX check if data_type exists
                            function.data_type_.value = orig_function.data_type_.value
                            function.insert()
                        atf = function._rev_a_task_function_
                        atf._task_ = ta_
                        atf.insert()
                    ta_.get()
                    aat = aa_._rev_a_action_task_
                    aat._task_ = ta_
                    print("++ link to task {}".format(task.name_))
                    aat.insert()
                    aat.get()
            else:
                saa.update(aa_)

    def __parse_args(self):
        """arguments parser"""
        parser = argparse.ArgumentParser(prog="cog make")
        parser.add_argument("-a", "--an_arg", help="an argument", required=False)
        self.__args = parser.parse_args()

    def __get_pkg_path(self, pkg_path):
        """returns package path according to the collorg convention"""
        if pkg_path.find("collorg/") == 0 and self.db_.name == "collorg_db":
            pkg_path = pkg_path.replace("collorg/", "")
        return pkg_path

    def make_cog_tree(self):
        """
        Constructs the collorg tree when new tables are created in the
        database.
        """
        cog_app_pkg_dir = "{}/{}".format(self.__repos_base_dir, self.pkg_path)
        if not os.path.exists(cog_app_pkg_dir):
            os.makedirs(cog_app_pkg_dir)
            the_dir = self.__repos_base_dir
            for dir_ in self.pkg_path.split("/"):
                the_dir = "{}/{}".format(the_dir, dir_)
                open("{}/__init__.py".format(the_dir), "w")
        for schema in self.db_.schemas:
            os.chdir(cog_app_pkg_dir)
            if schema.name.find("collorg.") == 0 and self.db_.name != "collorg_db":
                continue
            self.l_schemas.append("{}.{}".format(self.mod_path, schema.name))
            self.l_schemas.append("{}.{}.{}".format(self.mod_path, schema.name, glob.templates_dir))
            pkg_path = self.__get_pkg_path(schema.name.replace(".", "/"))
            if not os.path.exists(pkg_path):
                os.makedirs(pkg_path)
                open("{}/__init__.py".format(pkg_path), "w")
            os.chdir(pkg_path)
            for tablename in schema.tables:
                fqtn = "{}.{}".format(schema.name, tablename)
                module = self.db_.table("collorg.core.data_type", fqtn_=fqtn, name_=tablename)
                if module.is_empty():
                    module.insert()
                if not os.path.exists("{}/__init__.py".format(tablename)):
                    print("+ adding package {}.{}".format(schema.name, tablename))
                    os.makedirs("{}/{}".format(tablename, glob.templates_dir))
                    fd_ = open("{}/__init__.py".format(tablename), "w")
                    inh_classes, import_statment = self.__get_inh_infos(fqtn)
                    fd_.write(
                        glob.module_template.format(
                            self.__ctrl._charset,
                            import_statment,
                            tablename.capitalize(),
                            inh_classes,
                            schema.name,
                            tablename,
                            tablename.capitalize(),
                        )
                    )
                    fd_.close()
                    open("{}/cog/__init__.py".format(tablename), "w")
                    open("{}/{}/__init__.py".format(tablename, glob.templates_dir), "w")
                self.l_schemas.append("{}.{}.{}.{}".format(self.mod_path, schema.name, tablename, glob.templates_dir))
        os.chdir(self.__repos_base_dir)
        if not os.path.exists("setup.py"):
            fd_ = open("setup.py", "w")
            fd_.write(glob.setup_template.format(self.db_.name))
            fd_.close()

    def __get_inh_infos(self, fqtn):
        """return the list of inherited classes"""
        import_line = "from {} import {}"
        l_imports = []
        l_class_names = []
        inh_fqtns = self.db_._sql_inherits(fqtn)
        for fqtn in inh_fqtns:
            module_path = fqtn
            class_name = fqtn.split(".")[-1].capitalize()
            if fqtn.find("collorg.") == 0:
                module_path = fqtn.replace("collorg.", "collorg.db.")
            else:
                module_path = "{}.{}".format(self.mod_path, fqtn)
            l_imports.append(import_line.format(module_path, class_name))
            l_class_names.append(class_name)
        if not l_imports:
            l_imports.append("from collorg.orm.table import Table")
            l_class_names.append("Table")
        return ", ".join(l_class_names), "\n".join(l_imports)

    def __treat_pragmas(self, action):
        """
        before saving action
        """
        #        template_module = ".".join(self.template_module.split(".")[:-1])
        task = None
        try:
            module = __import__(self.template_module, globals(), locals(), [action.name_], -1)
        except:
            # the module is not installed yet. cog make is to be run again
            print("Unable to load {}. Please, rerun cog make.".format(self.template_module))
            self.__rerun += 1
            print("ERR", self.template_module)
            return
        if hasattr(module, "PRAGMA"):
            pragma = module.PRAGMA
            goals = pragma.get("goals", [])
            assert type(goals) is list
            tasks = pragma.get("tasks", [])
            assert type(tasks) is list
            #            if not tasks:
            #                print("Warning! No task defined.")
            functions = pragma.get("functions", [])
            #            if not functions:
            #                print("Warning! No function defined.")
            assert type(functions) is list
            attrs = [
                "description",
                "label",
                "icon",
                "in_menu",
                "in_header",
                "in_nav",
                "raw",
                # XXX TODO
                "write",
                "moderate",
                "admin",
            ]
            for attr in attrs:
                if attr in pragma:
                    action.__dict__["{}_".format(attr)].value = pragma[attr]
            action.insert()
            if not tasks and not functions:
                print("Linking action to 'Anonymous navigation'")
                task = self.db_.table("collorg.application.task", name_="Anonymous navigation")
                aat = task._rev_a_action_task_
                aat._action_ = action
                if aat.is_empty():
                    aat.insert()
            for goal_ in goals:
                for task_ in tasks:
                    goal = self.db_.table("collorg.application.goal", name_=goal_)
                    task = self.db_.table("collorg.application.task", name_=task_)
                    if goal.is_empty():
                        print("+ new goal {}".format(goal.name_.value))
                        goal.insert()
                    if task.is_empty():
                        print("+ new task {}".format(task.name_.value))
                        task.insert()
                    atg = task._rev_a_task_goal_
                    atg._goal_ = goal
                    if atg.is_empty():
                        atg.insert()
                    try:
                        action.link_to_task(task)
                    except:
                        print("ERR! could not link action to task")
            for task_ in tasks:
                task = self.db_.table("collorg.application.task", name_=task_)
                if task.is_empty():
                    print("+ new task {}".format(task.name_.value))
                    task.insert()
                for function_ in functions:
                    function = self.db_.table("collorg.actor.function", long_name_=function_)
                    atf = task._rev_a_task_function_
                    atf._function_ = function
                    if atf.is_empty():
                        print("+ task<->function: {}<->{}".format(task.name_, function.long_name_))
                        atf.insert()
                aat = task._rev_a_action_task_
                aat._action_ = action
                if aat.is_empty():
                    aat.insert()

    def __set_template_module_string(self, schemaname, tablename, template_name):
        """
        returns the template module string according to the collorg
        source tree organization.
        """
        if self.db_.name == "collorg_db" or (self.this_application and schemaname.find("collorg.") == 0):
            schemaname = schemaname.replace("collorg.", "")
            self.template_module = "collorg.db.{}.{}.templates.{}".format(schemaname, tablename, template_name)
        else:
            self.template_module = "{}.{}.{}.templates.{}".format(self.mod_path, schemaname, tablename, template_name)

    def __add_action(self, schemaname, tablename, tsn, template_code=None):
        """adds a new action in collorg.application.action"""
        self.__set_template_module_string(schemaname, tablename, tsn)
        fqtn = "{}.{}".format(schemaname, tablename)
        module = self.db_.table("collorg.core.data_type", fqtn_=fqtn)
        if module.is_empty():
            module.insert()
        action = self.db_.table("collorg.application.action", name_=tsn)
        action._data_type_ = module
        if action.is_empty():
            print("+ new action {}.{} {}".format(schemaname, tablename, tsn))
            action.source_.value = template_code
            self.__treat_pragmas(action)

    def __remove_cog_templates(self):
        """
        For each action in the database, check if the source is still
        here and removes it from the database if not.
        """
        import collorg

        cog_base_dir = collorg.__path__[0]
        cog_app_pkg_dir = "{}/{}".format(self.__repos_base_dir, self.pkg_path)
        action = self.db_.table("collorg.application.action")
        action.this_application_.value = True
        for act in action:
            missing = False
            data_type = act.data_type_.value
            data_type_path = self.__get_pkg_path(data_type.replace(".", "/"))
            src_path = "{}/{}/templates/__src/{}".format(cog_app_pkg_dir, data_type_path, act.name_)
            src_path_cog = "{}/{}/templates/{}.cog".format(cog_app_pkg_dir, data_type_path, act.name_)
            if (data_type.find("collorg.") == 0 and self.db_.name == "collorg_db") or (
                data_type.find("collorg.") == -1 and self.db_.name != "collorg_db"
            ):
                if not os.path.exists(src_path) and not os.path.exists(src_path_cog):
                    print("MISSING: {}".format(src_path))
                    missing = True
            elif data_type.find("collorg.") == 0 and self.db_.name != "collorg_db":
                act_cog = self.__collorg_db.table("collorg.application.action")
                act_cog.name_.value = act.name_.value
                act_cog.data_type_.value = data_type
                if act_cog.is_empty():
                    print("MISSING: {}, {}".format(data_type, act.name_))
                    missing = True
            if missing:
                module_path = [
                    "{}/{}/templates/{}".format(cog_app_pkg_dir, data_type_path, act.name_),
                    "{}/db/{}/templates/{}*".format(cog_base_dir, data_type_path, act.name_),
                    "{}/build/*/db/{}/templates/{}".format(self.__repos_base_dir, data_type_path, act.name_),
                ]
                for mp_ in module_path:
                    for ext in ["py", "pyc", "pyo"]:
                        file_ = "{}.{}".format(mp_, ext)
                        if os.path.exists(file_):
                            os.popen("sudo rm -f {}".format(file_))
                            print("-{}".format(file_))
                act._rev_check_requires_.delete()
                act._rev_check_required_.delete()
                act._rev_a_action_task_.delete()
                act.delete()
                print("- action removed")

    def __get_templates(self, path):
        l_templates = []
        try:
            l_files = os.listdir(path)
        except:
            # first time. the path doesn't exist
            return l_templates
        for file_ in l_files:
            if file_.find(".cog") != -1 or path.find("__src") != -1 and file_.find(".") == -1:
                l_templates.append((file_.replace(".cog", ""), path.replace("/__src", "")))
                if path.find("__src") != -1:
                    new_path = path.replace("__src", "")
                    template_name = "{}/{}.cog".format(new_path, file_)
                    open(template_name, "w").write(open("{}/{}".format(path, file_)).read())
        # if l_templates is empty we might be with an old __src...
        if not l_templates and path.find("__src") == -1:
            return self.__get_templates("{}/__src".format(path))
        return l_templates

    def __gen_templates(self):
        """
        Check les rép. <db>/<schema>/<module>/templates/__src/<fichier>
        Parse chaque fichier et génère un module "<fichier>.py" dans le rép.
        <db>/<schema>/templates/<module>/.
        Insert dans la table action la réf. à la template
        """
        self.this_application = True
        self.__remove_cog_templates()
        cog_app_pkg_dir = "{}/{}".format(self.__repos_base_dir, self.pkg_path)
        for schema in self.db_.schemas:
            os.chdir(cog_app_pkg_dir)
            schema_path = schema.name.replace(".", "/")
            if self.db_.name == "collorg_db":
                schema_path = schema_path.replace("collorg/", "")
            if not os.path.exists(schema_path):
                continue
            os.chdir(schema_path)
            for tablename in schema.tables:
                # parcours du rép. <tablename>/templates/
                path = "{}/{}".format(tablename, glob.templates_dir)
                init_content = []
                init_file = "{}/__init__.py".format(path)
                # tsp: template source path
                # tsn: template source name
                for tsn, tsp in self.__get_templates(path):
                    parser = Parser()
                    # tsn must be compatible with a python method name
                    if not ((tsn[0].isalpha() or tsn[0] == "_") and (tsn[1:].replace("_", "").isalnum())):
                        continue
                    src_file = "{}/{}.cog".format(tsp, tsn)
                    template_src = open(src_file).read()
                    template_code = parser.parse(tsn, template_src, "{}.cog".format(tsn))
                    template_sha = hashlib.sha1(template_code).hexdigest()
                    template_module_name = "{}/{}.py".format(tsp, tsn)
                    self.this_application = True
                    if self.db_.name != "collorg_db" and schema.name.find("collorg.") == 0:
                        self.this_application = False
                    module_sha = None
                    if os.path.exists(template_module_name):
                        module_sha = hashlib.sha1(open(template_module_name).read()).hexdigest()
                    if template_sha != module_sha:
                        print("+ template modified: {}".format(src_file))
                        if os.path.exists(template_module_name):
                            os.unlink(template_module_name)
                        open(template_module_name, "w").write(template_code)
                        os.chmod(template_module_name, 0444)
                    self.__add_action(schema.name, tablename, tsn, template_src)
                    init_content.append(tsn)
                try:
                    init_content.sort()
                    open(init_file, "w").write(
                        "__all__ = [{}]".format(", ".join(['"{}"'.format(elt) for elt in init_content]))
                    )
                except:
                    pass
        os.chdir(self.__repos_base_dir)
Exemple #20
0
class Cmd():
    global help_
    __known_items = [
        'stage',
        'label', 'description',
        'tickets',
        'pre', 'sql', 'post']
    __stages = ['alpha', 'beta', 'candidate', 'release', 'unsupported']
    cmd_help = (help_)
    def __init__(self, controller, *args):
        self.__ctrl = controller
        if self.__ctrl is None:
            self.__ctrl = Controller()
        self.db = self.__ctrl.db
        self.__database = self.db.table('collorg.core.database')
        self.__db_name = self.db.name
        self.__database.name_.value = self.__db_name
        assert self.__database.count() == 1
        self.__patch = None
        self.__patch_dir = None
        self.__major = None
        self.__minor = None
        self.__revision = None
        self.__cfg_file = None
        self.__output = []
        self.__error = []
        self.pkg_path = glob.cog_pkg_path(self.db.name)
        self.mod_path = self.pkg_path.replace("/", ".")
        self.__repos_base_dir = self.__ctrl.repos_path
        self.__parse_args()
        self.__execute()

    def reload_db(self):
        self.__ctrl = Controller()
        self.db = self.__ctrl.db

    def __check_revision(self):
        """
        We will check here if the patch can be applied...
        """
        # the revision must be greater than the already applied patches
        # TODO
        # is the patch config file accessible
        self.__patch_dir = "%s/patches/%s/%s/%s" % (
            self.db._cog_params['application_basedir'],
            self.__major, self.__minor, self.__revision)
        self.__cfg_file = "%s/patch.cfg" % (self.__patch_dir)
        # all the files mentioned in the ini file must be accessible
        self.__config.read(self.__cfg_file)
        self.__items = {}
        if not self.__config.has_section('patch'):
            sys.stderr.write(
                "No section: patch in patch.cfg\n"
                "Have you run 'python setup.py install'?\n")
            sys.exit()
        for key, val in self.__config.items('patch'):
            self.__items[key] = val[1:].strip()
        assert self.__items['stage'] in self.__stages
        assert 'label' in self.__items
        assert 'description' in self.__items
        for key, val in self.__items.items():
            if key not in self.__known_items:
                raise ValueError("Unknown item: %s" % key)

    def __parse_args(self):
        parser = argparse.ArgumentParser(prog="cog patch")
        parser.add_argument(
            "-r", "--revision", help = self.cmd_help, required = True)
        self.__args = parser.parse_args()
        self.__config = ConfigParser.ConfigParser()
        self.__major, self.__minor, self.__revision = \
            self.__args.revision.split('.')
        self.__check_revision()
        os.path.abspath(os.path.dirname(self.__cfg_file))
        sys.argv = []

    def __execute(self):
        for section in self.__config.sections():
            if section == 'patch':
                patch_res = self.__ex_patch()
                GenRelationalPart(self.__ctrl)
                return patch_res
        print("nothing")

    def __ex_patch(self):
        if 'pre' in self.__items:
            self.__pre_patch(self.__items['pre'])
        self.__patch = self.db.table("collorg.core.patch.changelog")
        self.__patch.major_.value = self.__major
        self.__patch.minor_.value = self.__minor
        self.__patch.revision_.value = self.__revision
        if self.__patch.count() == 1:
            sys.exit("Patch already applied\n")
        self.__patch._database_ = self.__database
        self.__patch.stage_.value = \
            self.__stages.index(self.__items['stage'])
        self.__patch.label_.value = self.__items['label']
        self.__patch.description_.value = \
            self.__items['description']
        if 'sql' in self.__items:
            self.__sql_patch(self.__items['sql'])
        if 'post' in self.__items:
            self.__post_patch(self.__items['post'])
        self.__patch.error_.value = "\n".join(self.__error)
        self.__patch.output_.value = "\n".join(self.__output)
        if self.__patch is not None:
            self.__patch.insert()

    def __pre_patch(self, items):
        print("Pre patch")
        for item in items.split('\n'):
            print(item)
            sp = subprocess.Popen(
                ["%s/%s" % (self.__patch_dir, item), self.db.name])
            return_code = sp.wait()
            out, err = sp.communicate()
            self.__output.append("==%s==\n%s" % (item, out))
            if err:
                self.__error.append("==%s==\n%s" % (item, err))
            if return_code != 0:
                print("+++%s---" % return_code)
                raise RuntimeError("%s\n%s\nErr: %s" % (item, out, err))
        self.reload_db()

    def __sql_patch(self, items):
        print("SQL patch")
        for item in items.split('\n'):
            if item[0] == '#':
                continue
            print(item)
            sql_file = "%s/%s" % (self.__patch_dir, item)
            os.system("psql %s -f %s" % (self.db.name, sql_file))
        self.reload_db()
        self.__add_missing_modules()

    def __post_patch(self, items):
        print("Post patch")
        for item in items.strip().split('\n'):
            print(item)
            os.system("%s/%s" % (self.__patch_dir, item))

    def __get_inh_infos(self, fqtn):
        # héritage sql
        import_line = "from %s import %s"
        l_imports = []
        l_class_names = []
        inh_fqtns = self.db._sql_inherits(fqtn)
        for fqtn in inh_fqtns:
            module_path = fqtn
            class_name = fqtn.split('.')[-1].capitalize()
            if fqtn.find('collorg.') == 0:
                module_path = fqtn.replace('collorg.', 'collorg.db.')
            else:
                module_path = "%s.%s" % (self.mod_path, fqtn)
            l_imports.append(import_line % (module_path, class_name))
            l_class_names.append(class_name)
        if not l_imports:
            l_imports.append("from collorg.orm.table import Table")
            l_class_names.append("Table")
        return ", ".join(l_class_names), "\n".join(l_imports)

    def __add_missing_modules(self):
        self.db = Controller().db
        for fqtn in self.db.fqtns:
            cog_table = self.db.table('collorg.core.data_type')
            cog_table.fqtn_.value = fqtn
            tablename = fqtn.rsplit('.', 1)[1]
            cog_table.name_.value = tablename
            if not cog_table.count():
                print("+ adding table %s to data_type" % fqtn)
                cog_table.insert()
        sys.path.insert(0, self.__repos_base_dir)
        cog_app_pkg_dir = "%s/%s" % (self.__repos_base_dir, self.pkg_path)
        if not os.path.exists(cog_app_pkg_dir):
            os.makedirs(cog_app_pkg_dir)
        open("%s/__init__.py" % (cog_app_pkg_dir), "w")
        for schema in self.db.schemas:
            if (schema.name.find("collorg.") == 0 and
                self.db.name != 'collorg_db'):
                continue
            os.chdir(cog_app_pkg_dir)
            schema_path = schema.name.replace(".", "/")
            if schema_path.find("collorg/") == 0:
                schema_path = schema_path.replace("collorg/", "")
            if not os.path.exists(schema_path):
                print("+ new schema: %s" % schema_path)
                os.makedirs(schema_path)
                open("%s/__init__.py" % (schema_path), "w")
            os.chdir(schema_path)
            for tablename in schema.tables:
                fqtn = "%s.%s" % (schema.name, tablename)
                module = self.db.table(
                    'collorg.core.data_type', fqtn_ = fqtn, name_ = tablename)
                if module.is_empty():
                    module.insert()
                if not os.path.exists("%s/__init__.py" % (tablename)):
                    print("+ adding package %s.%s" % (schema.name, tablename))
                    fd = open("%s/__init__.py" % (tablename), "w")
                    inh_classes, import_statment = self.__get_inh_infos(fqtn)
                    fd.write(glob.module_template % (
                        self.__ctrl._charset,
                        import_statment,
                        tablename.capitalize(),
                        inh_classes,
                        schema.name, tablename,
                        tablename.capitalize()))
                    fd.close()
                    open("%s/cog/__init__.py" % (tablename), "w")
                    open("%s/%s/__init__.py" % (
                            tablename, glob.templates_dir), "w")
        Make(self.__ctrl, self.__db_name)
Exemple #21
0
def add_data(schema,table):
    file_ = "./data/%s/%s" % ( schema, table )
    obj = database.table('%s.%s'%(schema, table))
    lines = open(file_).readlines()
    if len(lines):
        insert_data(obj,lines)

if __name__ == '__main__':
    config = ConfigParser.ConfigParser()
    config.read( 'init.cfg' )
    if len(sys.argv) == 1:
        db_name = config.sections()[0]
        items = config.items(db_name)
    else:
        items = [tuple(sys.argv[1].rsplit("."))]
    database = Controller().db
    db_name = database.name
    for item in items:
        schemaname = item[0]
        tablenames = item[1]
        print( "schema: %s" % ( schemaname ) )
        for tablename in tablenames.split( '\n' ):
            if tablename:
                fqtn = "%s.%s" % ( schemaname, tablename )
                print( " + %s" % ( fqtn ) )
                type_ = database.table(
                    'collorg.core.data_type', fqtn_ = fqtn )
                if type_.count() == 0:
                    print( "   NEW" )
                    output = os.popen( "psql %s -f ./sql/%s/%s.sql 2>& 1" % (
                            db_name, schemaname, tablename ) ).readlines()
Exemple #22
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

from collorg.controller.controller import Controller
import sys

db = Controller().db
table = db.table

alter_table = """
alter table {} add column cog_from timestamp(0);
alter table {} add column cog_to timestamp(0);
"""
alter_inh = """
alter table {} inherit "collorg.time".duration;
"""

if __name__ == '__main__':
    fqtn = sys.argv[1]
    obj = table(fqtn)
    if 'collorg.time.duration' in obj._cog_fqtn_inherits:
        sys.stderr.write('Already inherited!')
    sql_cmd = alter_table.format(obj.sql_fqtn, obj.sql_fqtn)
    if len(sys.argv) > 2:
        sql_cmd += alter_inh.format(obj.sql_fqtn)
    db.raw_sql(sql_cmd)
    db.commit()
Exemple #23
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-

from collorg.controller.controller import Controller

db = Controller().db
apd = db.table('collorg.communication.blog.a_post_data')
apd.order_by(apd.data_)
data = None
for elt in apd:
    if elt.data_.value != data:
        data = elt.data_.value
        i = 0
    nelt = elt()
    nelt.order_.value = i
    elt.update(nelt)
    i += 1

Exemple #24
0
class Cmd():
    def __init__(self, nop, *args):
        self.charset = 'utf-8'
        self.__curdir = os.path.abspath(os.path.curdir)
        parser = argparse.ArgumentParser(prog="cog init")
        parser.add_argument(
            "-d", "--db_name", required = True, help = "database name")
        options = parser.parse_args(*args)
        self.db_name = options.db_name
        self.__collorg_db = Controller('collorg_db').db
        self.__cursor = Cursor(self.__collorg_db.db)
        self.__check()
        self.__db_connection_params()
        db_exists = os.popen(
            """psql --list -U %s | egrep '^ %s '""" % (
            self.user, self.db_name)).read().strip()
        if not db_exists:
            self.__create_db()
        else:
            sys.stderr.write(
                "The database already exists\n"
                "Drop if you really want to proceed\n" )
            sys.exit(1)
        self.db = Controller(self.db_name).db
        self.charset = self.db._cog_controller._charset
        self.__make_cog_repos()

    def __check(self):
        try:
            collorg_repos = Controller()
        except RuntimeError:
            print("%s/%s" % (self.__curdir, self.db_name))
            if os.path.exists("%s/%s" % (self.__curdir, self.db_name)):
                sys.stderr.write(
                    "Directory or file %s already in the way.\n"
                    "Aborting!\n" % (self.db_name))
                sys.exit(1)
            self.__check_db_access()
            return
        sys.stderr.write(
            "Can't create a repos in a repos! "
            "You are currently in %s repos.\n" % collorg_repos.db.name)
        sys.exit()

    def __remove_check(self):
            try:
                self.__cursor.execute(drop_db % ('collorg_check'))
                self.__cursor.execute(drop_user % ('collorg_check'))
            except:
                pass

    def __check_db_access(self):
        try:
            # retreive postgresql version
            self.__pg_version = int(self.__cursor.fetchone(
                sql_version)[0].split()[1].split('.')[0])
            if self.__pg_version < 9:
                raise ValueError("Colorgs needs postgresql 9 or greater")
            # are we able to create a database
            self.__remove_check()
            self.__cursor.execute(create_user % ('collorg_check'))
            self.__cursor.execute(create_db % ('collorg_check','collorg_check'))
            assert(self.__cursor.fetchone(
                sql_db_exists, ('collorg_check',))[0] == 'collorg_check')
        except Exception as err:
            sys.stderr.write("%s\n" % err)
            # something went wrong. We stop here
            sys.exit()
        finally:
            self.__remove_check()

    def __db_connection_params(self):
        """
        sets the connection parameters to postgresql.
        the default paramaters come from self.__collorg_db.
        """
        self.user = _input('user', self.__collorg_db._cog_params['user'])
        self.host = _input('host', self.__collorg_db._cog_params['host'])
        self.port = _input('port', self.__collorg_db._cog_params['port'])
        if self.user != self.__collorg_db._cog_params['user']:
            while True:
                password1 = getpass.getpass('Enter password: '******'Retype password: '******'password']
        self.cog_psql_cmd = "psql collorg_db -h {host} -U collorg -p {port} -w"\
            .format(
                host = self.host, user = self.user, port = self.port,
                name = self.db_name)
        self.__psql_cmd = "psql -h {host} -U {user} -p {port} -w".format(
            host = self.host, user = self.user, port = self.port,
            name = self.db_name)
        self.db_psql_cmd = "psql -h {host} -U {user} -p {port} {name} -w"\
            .format(
                host = self.host, user = self.user, port = self.port,
                name = self.db_name)

    def __create_db(self):
        self.__collorg_db.close()
        self.__create_cog_ini_file()
        exit_status = os.system(
            "export PGPASSWORD={password};"
            "createdb {db_name} -O {user} -U {user} "
            "-T collorg_db -h {host}".format(
                password = self.password, db_name = self.db_name,
                user = self.user, host = self.host))
        if exit_status != 0:
            sys.stderr.write("unable to create database %s\n%s\n" % (
                self.db_name, exit_status))
            sys.exit(exit_status)

    def __create_cog_ini_file(self):
        try:
            open("/etc/collorg/%s" % (self.db_name))
            return
        except:
            pass
        file_ = app_ini_file.format(
            user = self.user, password = self.password,
            host = self.host, port = self.port,
            charset = self.charset, db_name = self.db_name)
        os.popen('sudo su - -c "cat>/etc/collorg/{}<<EOF\n{}\nEOF\n"'.format(
            self.db_name, file_))

    def __make_cog_repos(self):
        db_name = self.db.name
        base_dir = "%s/%s" % (self.__curdir, db_name)
        if not os.path.exists(base_dir):
            os.mkdir(base_dir)
            open("%s/__init__.py" % (base_dir), "w")
            if not os.path.exists("%s/.cog" % (base_dir)):
                os.mkdir("%s/.cog" % (base_dir))
                open("%s/.cog/config" % (base_dir), "w").write(
                    cog_config_file % (db_name))
        os.chdir(base_dir)
        self.abs_base_dir = os.path.abspath(os.path.curdir)
        sys.path.insert(0, self.abs_base_dir)
        cog_app_pkg_dir = "%s" % (glob.cog_pkg_path(self.db_name))
        if not os.path.exists(cog_app_pkg_dir):
            os.makedirs(cog_app_pkg_dir)
            for dir_ in ("../..", "..", "."):
                open("%s/%s/__init__.py" % (cog_app_pkg_dir, dir_), "w")
        open("%s/__init__.py" % (cog_app_pkg_dir), "w")
        os.chdir(cog_app_pkg_dir)
        self.post_init()

    def add_user(self):
        db = self.db
        user = db.table('collorg.actor.user')
        kwargs = {}
        kwargs['first_name_'] = input('First name: ')
        kwargs['last_name_'] = input('Last name: ')
        kwargs['email_'] = input('email: ')
        kwargs['pseudo_'] = input('pseudo: ')
        pass1 = ''
        pass2 = 'x'
        while pass1 != pass2:
            pass1 = getpass.getpass('password: '******'password (retype): ')
            if pass1 == pass2:
                if pass1 == '':
                    print("Empty password. aborting!")
                    sys.exit()
                break
            else:
                print("password mismatch!")
        kwargs['password_'] = pass1
        user.new_account(**kwargs)
        return user.get()

    def __make_dirs(self):
        os.chdir(self.abs_base_dir)
        app_base_dir = self.db._cog_params['application_basedir']
        setup = open("{}/examples/setup.py".format(app_base_dir)).read()
        setup = setup.replace('__DB_NAME__', self.db_name)
        makefile = open("{}/examples/Makefile".format(app_base_dir)).read()
        makefile = makefile.replace('__DB_NAME__', self.db_name)
        os.mkdir('sql')
        os.mkdir('scripts')
        open("setup.py", "w").write(setup)
        open("Makefile", "w").write(makefile)

        document_root = self.db._cog_params['document_root']
        apache_user = "******"
        os.system("sudo mkdir {}".format(document_root))
        os.system("sudo mkdir {}/download".format(document_root))
        os.system("sudo chown -R {}:{} {}".format(
            apache_user, apache_user, document_root))
        os.system("sudo chmod -R 700 {}".format(document_root))

        upload_dir = self.db._cog_params['upload_dir']
        os.system("sudo mkdir {}".format(upload_dir))
        os.system("sudo mkdir {}/tmp".format(upload_dir))
        os.system("sudo mkdir {}/uploaded_files".format(upload_dir))
        os.system("sudo chown -R {}:{} {}".format(
            apache_user, apache_user, upload_dir))
        os.system("sudo chmod -R 700 {}".format(upload_dir))

    def post_init(self):
        self.__make_dirs()

        table = self.db.table
        #db.set_auto_commit(False)
        user = self.add_user()
        assert user.count() == 1
        db = table('collorg.core.database').get()
        ndb = db()
        ndb.name_.value = self.db.name
        db.update(ndb)
        site = table('collorg.web.site')
        site.url_.value = '{}/collorg'.format(self.db.name)
        if site.is_empty():
            site.insert()
        assert site.count() == 1
        unit = table('collorg.organization.unit')
        unit.acronym_.value = 'Collorg'
        unit.name_.value = 'Collorg unit'
        unit.cog_fqtn_.value = 'collorg.organization.unit'
        if unit.is_empty():
            unit.insert()
        assert unit.count() == 1
        topic = table('collorg.web.topic')
        topic._site_ = site
        topic._author_ = user
        topic._cog_environment_ = unit
        topic.title_.value = 'Home'
        topic.path_info_.value = '/'
        topic.author_.value = user.cog_oid_.value
        if topic.is_empty():
            topic.insert()
        assert topic.count() == 1
        topic.get()
        user.grant_access(unit, True)
        user.grant_access(topic, True)