# Copyright 2017 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


import os
import sqlite3

from zvmsdk import config
from zvmsdk import constants as const

CONF = config.CONF

_DB_OPERATOR = None


def get_DbOperator():
    global _DB_OPERATOR

    if _DB_OPERATOR is not None:
        return _DB_OPERATOR

    _DB_OPERATOR = DbOperator()
    return _DB_OPERATOR


class DbOperator(object):

    def __init__(self):

        database = self._prepare()
        self._conn = sqlite3.connect(database)
        # autocommit
        self._conn.isolation_level = None

    def _prepare(self):
        file_mode = 777

        if not os.path.exists(CONF.database.path):
            os.makedirs(CONF.database.path, file_mode)
        else:
            mode = oct(os.stat(CONF.database.path).st_mode)[-3:]
            if mode != '777':
                os.chmod(CONF.database.path, file_mode)

        return ''.join((CONF.database.path, "/", const.DATABASE_NAME))

    def create_table(self, table_name, attribute):

        create_table_sql = ' '.join((
                        'create table if not exists %s (' % self.table_name,
                        '%s);' % attribute))
        self._conn.execute(create_table_sql)

@@ -620,16 +620,28 @@
  sqlite3
+%0Aimport stat
 %0A%0Afrom z
@@ -963,27 +963,15 @@
 f):%0A
-%0A
        
- database =
  sel
@@ -987,166 +987,224 @@
 e()%0A
-   
+%0A
     
- self._conn = sqlite3.connect(database)%0A        # autocommit%0A        self._conn.isolation_level = None%0A%0A    def _prepare(self):%0A        file_mode = 777
+def _prepare(self):%0A        path_mode = stat.S_IRWXU + stat.S_IRWXG + stat.S_IRWXO%0A        file_mode = stat.S_IRUSR + stat.S_IWUSR + stat.S_IRGRP +%5C%0A                    stat.S_IWGRP + stat.S_IROTH + stat.S_IWOTH
 %0A%0A  
@@ -1292,28 +1292,28 @@
 abase.path, 
-file
+path
 _mode)%0A     
@@ -1461,20 +1461,20 @@
 e.path, 
-file
+path
 _mode)%0A%0A
@@ -1481,22 +1481,26 @@
         
-return
+database =
  ''.join
@@ -1552,262 +1552,373 @@
 E))%0A
-%0A
+  
     
-def create_table(self, table_name, attribute):%0A%0A        create_table_sql = ' '.join((%0A                        'create table if not exists %25s (' %25 self.table_name,%0A                        '%25s);' %25 attribute))%0A        self._conn.execute(create_table_sql)
+  self._conn = sqlite3.connect(database)%0A%0A        db_mode = os.stat(database).st_mode%0A%0A        mu = (db_mode & stat.S_IRWXU) %3E%3E 6%0A        mg = (db_mode & stat.S_IRWXG) %3E%3E 3%0A        mo = db_mode & stat.S_IRWXO%0A%0A        if ((mu %3C 6) or (mg %3C 6) or (mo %3C 6)):%0A            os.chmod(database, file_mode)%0A%0A        # autocommit%0A        self._conn.isolation_level = None
 %0A

fix bug about file mode