# Path: infrastructure/models/server.py
# def custom_sudo(command, user=False, group=False, dont_raise=False):
#     # TODO esto habria que pasrlo con *args, **kwargs
#     env.warn_only = True
#     if user and group:
#         res = sudo(command, user=user, group=group)
#     elif user:
#         res = sudo(command, user=user)
#     elif group:
#         res = sudo(command, group=group)
#     else:
#         res = sudo(command)
#     if res.failed:
#         if dont_raise:
#             _logger.warning((
#                 "Can not run command:\n%s\nThis is what we get:\n%s") % (
#                 res.real_command, unicode(res.stdout, 'utf-8')))
#         else:
#             raise ValidationError(_(
#                 "Can not run command:\n%s\nThis is what we get:\n%s") % (
#                 res.real_command, unicode(res.stdout, 'utf-8')))
#     env.warn_only = False
#     return res


import string
import os
from openerp import models, fields, api, _
from openerp.exceptions import ValidationError
from .server import custom_sudo as sudo
from fabric.contrib.files import exists

    # TODO si no vamos a usar el sufijo entonces borrar lo comentado aca
    @api.onchange('partner_id')
    # @api.onchange('partner_id', 'odoo_version_id')
    def _get_name(self):
        name = False
        if self.partner_id:
            # if self.partner_id and self.odoo_version_id:
            name = self.partner_id.commercial_partner_id.name
            # partner_name = self.partner_id.commercial_partner_id.name
            # sufix = self.odoo_version_id.sufix
            # name = '%s-%s' % (partner_name, sufix)
            valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
            name = ''.join(c for c in name if c in valid_chars)
            name = name.replace(' ', '').replace('.', '').lower()
        self.name = name

    @api.onchange('name', 'server_id')
    def _get_path(self):
        path = False
        if self.server_id.base_path and self.name:
            path = os.path.join(self.server_id.base_path, self.name)
        self.path = path

    @api.one
    def make_env_paths(self):
        self.server_id.get_env()
        if exists(self.path, use_sudo=True):
            raise ValidationError(_("Folder '%s' already exists") %
                          (self.path))

# Path: infrastructure/models/server.py
# def custom_sudo(command, user=False, group=False, dont_raise=False):
#     # TODO esto habria que pasrlo con *args, **kwargs
#     env.warn_only = True
#     if user and group:
#         res = sudo(command, user=user, group=group)
#     elif user:
#         res = sudo(command, user=user)
#     elif group:
#         res = sudo(command, group=group)
#     else:
#         res = sudo(command)
#     if res.failed:
#         if dont_raise:
#             _logger.warning((
#                 "Can not run command:\n%s\nThis is what we get:\n%s") % (
#                 res.real_command, unicode(res.stdout, 'utf-8')))
#         else:
#             raise ValidationError(_(
#                 "Can not run command:\n%s\nThis is what we get:\n%s") % (
#                 res.real_command, unicode(res.stdout, 'utf-8')))
#     env.warn_only = False
#     return res


# Path: infrastructure/models/environment.py
import string
import os
from openerp import models, fields, api, _
from openerp.exceptions import ValidationError
from .server import custom_sudo as sudo
from fabric.contrib.files import exists


    # TODO si no vamos a usar el sufijo entonces borrar lo comentado aca
    @api.onchange('partner_id')
    # @api.onchange('partner_id', 'odoo_version_id')
    def _get_name(self):
        name = False
        if self.partner_id:
            # if self.partner_id and self.odoo_version_id:
            name = self.partner_id.commercial_partner_id.name
            # partner_name = self.partner_id.commercial_partner_id.name
            # sufix = self.odoo_version_id.sufix
            # name = '%s-%s' % (partner_name, sufix)
            valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
            name = ''.join(c for c in name if c in valid_chars)
            name = name.replace(' ', '').replace('.', '').lower()
        self.name = name

    @api.onchange('name', 'server_id')
    def _get_path(self):
        path = False
        if self.server_id.base_path and self.name:
            path = os.path.join(self.server_id.base_path, self.name)
        self.path = path

    @api.one
    def make_env_paths(self):
        self.server_id.get_env()
        if exists(self.path, use_sudo=True):
            raise ValidationError(_("Folder '%s' already exists") %
                          (self.path))

        sudo('mkdir -p ' + self.path)