1# -*- coding: utf-8 -*-
2#
3# (c) Copyright @ 2015 HP Development Company, L.P.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18#
19# Author: Amarnath Chitumalla, Goutam Kodu
20#
21
22# Global import
23import os
24import os.path
25import locale
26import stat
27
28#Local
29from . import logger
30
31log = logger.Logger('', logger.Logger.LOG_LEVEL_INFO, logger.Logger.LOG_TO_CONSOLE)
32
33def execute(cmd):
34    if cmd:
35        return os.system(cmd)
36    else:
37        log.error("Command not found \n" % cmd)
38        return 127
39
40
41# Returns the file size in bytes.
42# If file is not exists, returns size as -1.
43#
44def getFileSize(filename):
45    if not os.path.exists(filename):
46        return -1
47
48    return os.path.getsize(filename)
49
50def getHPLIPDir():
51    homedir = os.path.expanduser('~')
52    hplipdir = os.path.join(homedir, ".hplip")
53    status = 0
54    if not os.path.exists(hplipdir):
55        try:
56            os.umask(0o022)
57            s = os.stat(homedir)
58            os.mkdir(hplipdir, 0o755)
59            os.chown(hplipdir, s[stat.ST_UID], s[stat.ST_GID])
60        except OSError:
61            status = 1
62            log.error("Failed to create %s" % hplipdir)
63    return status, hplipdir
64def changeOwner(path, user, group, Recursive = False ):
65    status = 0
66    try:
67        if Recursive:
68            for root, dirs, files in os.walk(path):
69                for dr in dirs:
70                    os.chown(os.path.join(root, dr), user, group)
71                for fl in files:
72                    os.chown(os.path.join(root, fl), user, group)
73        else:
74            os.chown(path, user, group)
75    except OSError:
76        status = 1
77        log.error("Failed to change ownership of %s" %path)
78    return status
79