Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90



First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write if some_boolean_expression != True:
instead prefer if not some_boolean_expression:.  Also, in your code
except (Except), ex: return ex the parentheses are redundant,
there's no Except Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that sys.exit(0) doing in there? And why exit with 0 if there
was a problem? Exit code 0 means no problem.)


Second, from what you've said backup_dest must be None:

  

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File pyshell#1, line 1, in module
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised as 
a NoneType ??


I'm using optparse in my script (that code for checking for permission 
is a part of my backup script).


Maybe i post my complete code:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print 

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you want 
to backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to backup 
and 'BCKP_NAME' is the name for backup output file. Default 'DEST_PATH' 
is placed in home directory, it can't be changed. 'BCKP_NAME' must be given;


   

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print Have no permissions on [%s] for reading 
operation.\nTrying to set them... % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(rd_obj_path)[1]

   else:
   print Have permissions on [%s] for reading. % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print Have no permissions on [%s] for writing 
operation.\nTrying to set them... % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(backup_dest)[1]

   else:
   print Have permissions on [%s] for writing. % 
os.path.split(backup_dest)[1]

   else:
   return Can't find specified path - [%s]. % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == nt:
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open(%s\\%s(%s).tar.bz2 % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open(%s/%s(%s).tar.bz2 % 
(backup_dest, 

Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread Dave Angel

ryniek90 wrote:

div class=moz-text-flowed style=font-family: -moz-fixed

First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write if some_boolean_expression != True:
instead prefer if not some_boolean_expression:.  Also, in your code
except (Except), ex: return ex the parentheses are redundant,
there's no Except Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that sys.exit(0) doing in there? And why exit with 0 if there
was a problem? Exit code 0 means no problem.)


Second, from what you've said backup_dest must be None:

 

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File pyshell#1, line 1, in module
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised 
as a NoneType ??


I'm using optparse in my script (that code for checking for permission 
is a part of my backup script).


Maybe i post my complete code:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print 

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you want 
to backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to 
backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print Have no permissions on [%s] for reading 
operation.\nTrying to set them... % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(rd_obj_path)[1]

   else:
   print Have permissions on [%s] for reading. % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print Have no permissions on [%s] for writing 
operation.\nTrying to set them... % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(backup_dest)[1]

   else:
   print Have permissions on [%s] for writing. % 
os.path.split(backup_dest)[1]

   else:
   return Can't find specified path - [%s]. % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == nt:
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open(%s\\%s(%s).tar.bz2 % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)

Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90



First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write if some_boolean_expression != True:
instead prefer if not some_boolean_expression:.  Also, in your code
except (Except), ex: return ex the parentheses are redundant,
there's no Except Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that sys.exit(0) doing in there? And why exit with 0 if there
was a problem? Exit code 0 means no problem.)


Second, from what you've said backup_dest must be None:

 

import os
os.access(None, os.W_OK)



Traceback (most recent call last):
  File pyshell#1, line 1, in module
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

  


No, backup_dest is destination for backup. It's this variable: 
*backup_dest = self.__backup_dest* .
*self.__backup_dest = os.getenv('HOME')*  -  as you can see, 
self.__backup_dest is a reference to the home directory which is a 
string. Then backup_dest is a reference to the variable 
self.__backup_dest. But that second reference seems to be recognised 
as a NoneType ??


I'm using optparse in my script (that code for checking for 
permission is a part of my backup script).


Maybe i post my complete code:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#MyBackUper

import os, sys, tarfile
import datetime
from optparse import OptionParser


class Except(Exception):
   pass


class Packer(Except):

   def __init__(self):
   self.__backup_dest = os.getenv('HOME')
   __current_time = datetime.datetime.today().strftime('%Y-%m-%d 
%H:%M:%S')

   Except.__init__(self)

   def doc_note(self):

   print 

   DOCUMENTATION

   'Backuper' is a script for doing backups.
   You can backup with it single files or whole directories.
   For available commands type 'backuper.py -h' or 'backuper.py 
--help'.


   Using it is simple:

   - to backup single file, type:
   'backuper.py -f FILE_PATH BCKP_NAME [DEST_PATH]',
   where 'FILE_PATH' is exact path to prefered file which you 
want to backup and 'BCKP_NAME' is the name for backup output file. 
Default 'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   - to backup whole directory, type:
   'backuper.py -d DIR_PATH BCKP_NAME [DEST_PATH]', where 
'DIR_PATH' is exact path to prefered directory, which you want to 
backup and 'BCKP_NAME' is the name for backup output file. Default 
'DEST_PATH' is placed in home directory, it can't be changed. 
'BCKP_NAME' must be given;


   

   def __check_set_perm(self, rd_obj_path, backup_dest):

   try:

   if os.path.exists(rd_obj_path):
   if not os.access(rd_obj_path, os.R_OK):
   print Have no permissions on [%s] for reading 
operation.\nTrying to set them... % os.path.split(rd_obj_path)[1]

   if not os.path.isdir(rd_obj_path):
   os.chmod(rd_obj_path, stat.S_IREAD)
   else:
   for root, dirs, files in os.walk(rd_obj_path):
   for f in files:
   os.chmod(os.path.join(root, f), 
stat.S_IREAD)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(rd_obj_path)[1]

   else:
   print Have permissions on [%s] for reading. % 
os.path.split(rd_obj_path)[1]


   if not os.access(backup_dest, os.W_OK):
   print Have no permissions on [%s] for writing 
operation.\nTrying to set them... % os.path.split(backup_dest)[1]

   os.chmod(backup_dest, stat.S_IWRITE)
   print Get permissions for reading on [%s] 
successfully. % os.path.split(backup_dest)[1]

   else:
   print Have permissions on [%s] for writing. % 
os.path.split(backup_dest)[1]

   else:
   return Can't find specified path - [%s]. % rd_obj_path
   sys.exit(1)

   except (Except), ex:
   return ex


   def backup_file(self, rd_obj_path, bkp_obj_name):

   try:

   if os.name == nt:
   rd_obj_path = rd_obj_path.replace('~/', '%s\\' % 
os.getenv('HOME')).replace('\\', '')

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open(%s\\%s(%s).tar.bz2 % 
(backup_dest, bkp_obj_name, __current_time), 'w:bz2')


   else:
   rd_obj_path = rd_obj_path.replace('~/', '%s/' % 
os.getenv('HOME'))

   backup_dest = self.__backup_dest
   print Checking permissions for reading and writing...
   self.__check_set_perm(rd_obj_path, backup_dest)
   backup_obj = tarfile.open(%s/%s(%s).tar.bz2 % 
(backup_dest, 

Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread Stephen Hansen


 WTF??
 Why on IDLE it works, but when i run this script in cmd.exe, the
 os.getenv('HOME') goes NoneType?
 I'm to newbie yet to understand this  :/


HOME is simply not a standard environment variable that Windows provides.
Any program can set/add environment variables as it deems fit; in this case
it seems IDLE is setting one, but you can't rely on it outside of IDLE.

If you do os.environ[HOMEDRIVE] + os.environ[HOMEPATH] it may be what
you want.

Start up cmd.exe, type 'set' -- that's the default/standard environment
variables you have to work with. That's it.

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-22 Thread ryniek90




WTF??
Why on IDLE it works, but when i run this script in cmd.exe, the
os.getenv('HOME') goes NoneType?
I'm to newbie yet to understand this  :/


HOME is simply not a standard environment variable that Windows 
provides. Any program can set/add environment variables as it deems 
fit; in this case it seems IDLE is setting one, but you can't rely on 
it outside of IDLE.


If you do os.environ[HOMEDRIVE] + os.environ[HOMEPATH] it may be 
what you want.


Start up cmd.exe, type 'set' -- that's the default/standard 
environment variables you have to work with. That's it.


--S


Yep, now that variable works fine. Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-21 Thread Simon Forman
On Aug 21, 1:33 pm, ryniek90 rynie...@gmail.com wrote:
 I've got some code that checks priviliges on two paths:
 First - chosen by user
 Second - hardcoded home directory represented by **os.getenv('HOME')** -
 (os.getenv('HOME') works both on Linux and Windows)

 Here's the code:
 
 def __check_set_perm(self, rd_obj_path, backup_dest):

         try:

             if os.path.exists(rd_obj_path):
                 if os.access(rd_obj_path, os.R_OK) != True:
                     print Have no permissions on [%s] for reading
 operation.\nTrying to set them... % os.path.split(rd_obj_path)[1]
                     if not os.path.isdir(rd_obj_path):
                         os.chmod(rd_obj_path, stat.S_IREAD)
                     else:
                         for root, dirs, files in os.walk(rd_obj_path):
                             for f in files:
                                 os.chmod(os.path.join(root, f),
 stat.S_IREAD)
                     print Get permissions for reading on [%s]
 successfully. % os.path.split(rd_obj_path)[1]
                 else:
                     print Have permissions on [%s] for reading. %
 os.path.split(rd_obj_path)[1]

                 if os.access(backup_dest, os.W_OK) != True:
                     print Have no permissions on [%s] for writing
 operation.\nTrying to set them... % os.path.split(backup_dest)[1]
                     os.chmod(backup_dest, stat.S_IWRITE)
                     print Get permissions for reading on [%s]
 successfully. % os.path.split(backup_dest)[1]
                 else:
                     print Have permissions on [%s] for writing. %
 os.path.split(backup_dest)[1]
             else:
                 return Can't find specified path - [%s]. % rd_obj_path
                 sys.exit(0)

         except (Except), ex:
             return ex
 

 This code is part of my backup script. When script checks for reading
 permission on 'user chosen path' it seems ok, but when it checks for
 writing permissions on 'home directory', (this line: **if
 os.access(backup_dest, os.W_OK) != True**), i get error:
 **TypeError: coercing to Unicode: need string or buffer, NoneType found**
 The second os.access check fails, but don't know why. When i turned
 os.W_OK to integer, i get the same error.
 But when changed os.W_OK or that int to string, i get error that integer
 is required. I'm launching this code on WinXP Prof+SP3, on Administrator
 account. Is it bug in os.acces() or my code is written wrong?
 Btw. i kow that os.chmod in windows can set only read-olny flag, so
 later i'll change the line with **os.chmod(path, stat.S_IWRITE)**

 Thanks and cheers.

First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write if some_boolean_expression != True:
instead prefer if not some_boolean_expression:.  Also, in your code
except (Except), ex: return ex the parentheses are redundant,
there's no Except Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that sys.exit(0) doing in there? And why exit with 0 if there
was a problem? Exit code 0 means no problem.)


Second, from what you've said backup_dest must be None:

 import os
 os.access(None, os.W_OK)

Traceback (most recent call last):
  File pyshell#1, line 1, in module
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

-- 
http://mail.python.org/mailman/listinfo/python-list