[Pypt-offline-general] SF.net SVN: pypt-offline: [185] trunk/pypt_core.py
Status: Beta
                
                Brought to you by:
                
                    riteshsarraf
                    
                
            | 
     
      
      
      From: <rit...@us...> - 2007-08-13 20:51:02
      
     
   | 
Revision: 185
          http://pypt-offline.svn.sourceforge.net/pypt-offline/?rev=185&view=rev
Author:   riteshsarraf
Date:     2007-08-13 13:51:03 -0700 (Mon, 13 Aug 2007)
Log Message:
-----------
rrs@learner:~/devel/eclipse/pypt-offline$ hg log -l 6 -v
changeset:   164:0e25f5b1fd9a
branch:      trunk
tag:         tip
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Tue Aug 14 02:10:06 2007 +0530
files:       pypt_core.py
description:
* Updated all magic_check_and_uncompress() call's with proper arguments
* Catch IOError in Archiver
changeset:   163:6305da9154bd
branch:      trunk
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Tue Aug 14 01:47:37 2007 +0530
files:       pypt_core.py
description:
* We were checking for the wrong file in pypt_magic.file()
* Same was for shutil.copy
* Fixed the temporary file write stull. tempfile is much better to do it.
changeset:   162:97589b94d83a
branch:      trunk
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Tue Aug 14 01:23:15 2007 +0530
files:       pypt_core.py
description:
* When the target path is not writable, we indeed need to errorout and exit.
changeset:   161:01818b3bab35
branch:      trunk
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Tue Aug 14 01:05:25 2007 +0530
files:       pypt_core.py
description:
* Let's hope that this way of trying to get the bug's subject works
* properly everywhere.
changeset:   160:ae2409fc7057
branch:      trunk
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Mon Aug 13 04:50:22 2007 +0530
files:       pypt_core.py
description:
* String Formatiing updates
changeset:   159:93d78fc4d770
branch:      trunk
user:        Ritesh Raj Sarraf <rr...@re...>
date:        Mon Aug 13 02:33:20 2007 +0530
files:       pypt_core.py
description:
* Fixed variable vame error
Modified Paths:
--------------
    trunk/pypt_core.py
Modified: trunk/pypt_core.py
===================================================================
--- trunk/pypt_core.py	2007-08-12 20:31:08 UTC (rev 184)
+++ trunk/pypt_core.py	2007-08-13 20:51:03 UTC (rev 185)
@@ -29,6 +29,7 @@
 import optparse
 import array
 import socket
+import tempfile
 
 from array import array
 
@@ -55,7 +56,7 @@
 version = "0.7.0"
 copyright = "(C) 2005 - 2007 Ritesh Raj Sarraf - RESEARCHUT (http://www.researchut.com/)"
 terminal_license = "This program comes with ABSOLUTELY NO WARRANTY.\n\
-This is free software, and you are welcome to redistribute it under certain conditions.\n"
+This is free software, and you are welcome to redistribute it under certain conditions.\n\n\n"
         
 errlist = []
 supported_platforms = ["Linux", "GNU/kFreeBSD", "GNU"]
@@ -378,15 +379,15 @@
             except ImportError:
                 return False
             
-            #try:
-            read_from = bz2.BZ2File(archive_file, 'r')
-            #except:
-            #    return False
+            try:
+                read_from = bz2.BZ2File(archive_file, 'r')
+            except IOError:
+                return False
                             
-            #try:
-            write_to = open (os.path.join(path, target_file), 'wb')
-            #except:
-            #    return False
+            try:
+                write_to = open (os.path.join(path, target_file), 'wb')
+            except IOError:
+                return False
                             
             if self.TarGzipBZ2_Uncompress(read_from, write_to) != True:
                 raise ArchiveError
@@ -400,15 +401,15 @@
             except ImportError:
                 return False
             
-            #try:
-            read_from = gzip.GzipFile(file, 'r')
-            #except:
-            #    return False
+            try:
+                read_from = gzip.GzipFile(archive_file, 'r')
+            except IOError:
+                return False
                             
-            #try:
-            write_to = open(os.path.join(path,filename), 'wb')
-            #except:
-            #    return False            
+            try:
+                write_to = open(os.path.join(path,target_file), 'wb')
+            except IOError:
+                return False            
             
             if self.TarGzipBZ2_Uncompress(read_from, write_to) != True:
                 raise ArchiveError
@@ -1292,19 +1293,19 @@
             retval = archive.decompress_the_file(archive_file, target_path, filename, 2)
         elif pypt_magic.file(archive_file) == "application/zip":
             retval = archive.decompress_the_file(os.path.join(install_file_path, eachfile), target_path, eachfile, 3)
-        elif pypt_magic.file(filename) == "PGP armored data" or pypt_magic.file(filename) == "application/x-dpkg":
+        elif pypt_magic.file(archive_file) == "PGP armored data" or pypt_magic.file(archive_file) == "application/x-dpkg":
             if os.access(target_path, os.W_OK):
-                shutil.copy(filename, target_path)
+                shutil.copy(archive_file, target_path + filename)
                 retval = True
             else:
-                log.err("ERROR: Cannot write to target path %s.\n" % (target_path) )
+                log.err("ERROR: Cannot write to target path %s\n" % (target_path) )
+                sys.exit(1)
         elif filename.endswith(pypt_bug_file_format):
             retval = False # We intentionally put the bug report files as not printed.
         else:
             log.err("ERROR: I couldn't understand file type %s.\n" % (filename) )
         if retval is True:
             log.msg("%s file synced.\n" % (filename))
-        os.unlink(archive_file)
         
     if path_type == 1:
         
@@ -1320,14 +1321,17 @@
             bugs_number = {}
             for filename in file.namelist():
                 if filename.endswith(pypt_bug_file_format):
-                    bug_subject_file = file.read(filename)
-                    bug_subject = bug_subject_file.split('\r')
-                    del bug_subject_file
-                    for subject in bug_subject:
-                        if subject.startswith('#'):
-                            subject = subject.lstrip(subject.split(":")[0])
+                    temp = tempfile.NamedTemporaryFile()
+                    temp.file.write(file.read(filename))
+                    temp.file.flush()
+                    temp.file.seek(0) #Let's go back to the start of the file
+                    for bug_subject_identifier in temp.file.readlines():
+                        if bug_subject_identifier.startswith('#'):
+                            subject = bug_subject_identifier.lstrip(bug_subject_identifier.split(":")[0])
+                            subject = subject.rstrip("\n")
                             break
                     bugs_number[filename] = subject
+                    temp.file.close()
                     
             if bugs_number:
                 # Display the list of bugs
@@ -1343,23 +1347,13 @@
                     elif response.startswith('y') or response.startswith('Y'):
                         for filename in file.namelist():
                             
-                            data = open(filename, "wb")
-                            data.write(file.read(filename))
-                            data.close()
+                            data = tempfile.NamedTemporaryFile()
+                            data.file.write(file.read(filename))
+                            data.file.flush()
+                            archive_file = data.name
                             
-                            #FIXME: Fix this tempfile feature
-                            # Access to the temporary file is not being allowed
-                            # It's throwing a Permission denied exception
-                            #try:
-                            #    import tempfile
-                            #except ImportError:
-                            #    sys.stderr.write("Aieeee! Module pypt_magic not found.\n")
-                            #    sys.exit(1)
-                            #data = tempfile.NamedTemporaryFile('wb', -1, '', '', os.curdir)
-                            #data.write(file.read(filename))
-                            #data = file.read(filename)
-                            
-                            magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                            magic_check_and_uncompress(archive_file, target_path, filename)
+                            data.file.close()
                         sys.exit(0)
                             
                     elif response.startswith('n') or response.startswith('N'):
@@ -1401,11 +1395,13 @@
                     log.verbose("Continuing with syncing the files.\n")
                     for filename in file.namelist():
                         
-                        data = open(filename, "wb")
-                        data.write(file.read(filename))
-                        data.close()
-                        
-                        magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                        data = tempfile.NamedTemporaryFile()
+                        data.file.write(file.read(filename))
+                        data.file.flush()
+                        archive_file = data.name
+                            
+                        magic_check_and_uncompress(archive_file, target_path, filename)
+                        data.file.close()
                 else:
                     log.msg("Exiting gracefully on user request.\n")
                     sys.exit(0)
@@ -1413,22 +1409,13 @@
                 
             for filename in file.namelist():
                 
-                data = open(filename, "wb")
-                data.write(file.read(filename))
-                data.close()
+                data = tempfile.NamedTemporaryFile()
+                data.file.write(file.read(filename))
+                data.file.flush()
+                archive_file = data.name
                             
-                #FIXME: Fix this tempfile feature
-                # Access to the temporary file is not being allowed
-                # It's throwing a Permission denied exception
-                #try:
-                #    import tempfile
-                #except ImportError:
-                #    sys.stderr.write("Aieeee! Module pypt_magic not found.\n")
-                #    sys.exit(1)
-                #data = tempfile.NamedTemporaryFile('wb', -1, '', '', os.curdir)
-                #data.write(file.read(filename))
-                #data = file.read(filename)
-                magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                magic_check_and_uncompress(archive_file, target_path, filename)
+                data.file.close()
         else:
             log.err("ERROR: Inappropriate argument sent to syncer during data fetch. Do you need to fetch bugs or not?\n")    
             sys.exit(1)
@@ -1458,7 +1445,7 @@
                         for eachfile in os.listdir(install_file_path):
                             archive_type = None
                                 
-                            magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                            magic_check_and_uncompress(archive_file, target_path, filename)
                             
                     elif response.startswith('n') or response.startswith('N'):
                         log.err("Exiting gracefully on user request.\n\n")
@@ -1501,7 +1488,7 @@
                     for eachfile in os.listdir(install_file_path):
                         archive_type = None
                             
-                        magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                        magic_check_and_uncompress(archive_file, target_path, filename)
                 else:
                     log.msg("Exiting gracefully on user request.\n")
                     sys.exit(0)
@@ -1509,7 +1496,7 @@
             for eachfile in os.listdir(install_file_path):
                 archive_type = None
                     
-                magic_check_and_uncompress(os.path.abspath(filename), target_path, filename)
+                magic_check_and_uncompress(archive_file, target_path, filename)
         else:
             log.err("ERROR: Inappropriate argument sent to syncer during data fetch. Do you need to fetch bugs or not?\n")    
             sys.exit(1)
@@ -1593,7 +1580,7 @@
                 if os.geteuid() != 0:
                     parser.error("This option requires super-user privileges. Execute as root or use sudo/su")
                 else:
-                    log.msg("Generating database of files that are needed for an update.\n")
+                    log.msg("\n\nGenerating database of files that are needed for an update.\n")
                     
                     #FIXME: Unicode Fix
                     # This is only a workaround.
@@ -1622,17 +1609,17 @@
                     parser.error("This option requires super-user privileges. Execute as root or use sudo/su")
                 #TODO: Use a more Pythonic way for it
                 if options.upgrade_type == "upgrade":
-                    log.msg("Generating database of files that are needed for an upgrade.\n")
+                    log.msg("\n\nGenerating database of files that are needed for an upgrade.\n")
                     os.environ['__pypt_set_upgrade'] = options.set_upgrade
                     if os.system('/usr/bin/apt-get -qq --print-uris upgrade > $__pypt_set_upgrade') != 0:
                         log.err("FATAL: Something is wrong with the apt system.\n")
                 elif options.upgrade_type == "dist-upgrade":
-                    log.msg("Generating database of files that are needed for a dist-upgrade.\n")
+                    log.msg("\n\nGenerating database of files that are needed for a dist-upgrade.\n")
                     os.environ['__pypt_set_upgrade'] = options.set_upgrade
                     if os.system('/usr/bin/apt-get -qq --print-uris dist-upgrade > $__pypt_set_upgrade') != 0:
                         log.err("FATAL: Something is wrong with the apt system.\n")
                 elif options.upgrade_type == "dselect-upgrade":
-                    log.msg("Generating database of files that are needed for a dselect-upgrade.\n")
+                    log.msg("\n\nGenerating database of files that are needed for a dselect-upgrade.\n")
                     os.environ['__pypt_set_upgrade'] = options.set_upgrade
                     if os.system('/usr/bin/apt-get -qq --print-uris dselect-upgrade > $__pypt_set_upgrade') != 0:
                         log.err("FATAL: Something is wrong with the apt system.\n")
@@ -1650,7 +1637,7 @@
                 if os.geteuid() != 0:
                     parser.error("This option requires super-user privileges. Execute as root or use sudo/su")
                     
-                log.msg("Generating database of the package and its dependencies.\n")
+                log.msg("\n\nGenerating database of the package and its dependencies.\n")
                 os.environ['__pypt_set_install'] = options.set_install
                 os.environ['__pypt_set_install_packages'] = ''
                 
@@ -1726,13 +1713,13 @@
             
         if options.install_upgrade:
             #INFO: Comment these lines to do testing on Windows machines too
-            try:
-                if os.geteuid() != 0:
-                    log.err("\nYou need superuser privileges to execute this option\n")
-                    sys.exit(1)
-            except AttributeError:
-                log.err("Are you really running the install command on a Debian box?\n")
-                sys.exit(1)
+            #try:
+            #    if os.geteuid() != 0:
+            #        log.err("\nYou need superuser privileges to execute this option\n")
+            #        sys.exit(1)
+            #except AttributeError:
+            #    log.err("Are you really running the install command on a Debian box?\n")
+            #    sys.exit(1)
             if os.path.isfile(options.install_upgrade) is True:
                 syncer(options.install_upgrade, apt_package_target_path, 1, bug_parse_required = True)
             elif os.path.isdir(options.install_upgrade) is True:
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |