Subversion Repositories programming

Rev

Rev 220 | Rev 276 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 220 Rev 275
Line 56... Line 56...
56
#
56
#
57
# - 2005-11-06
57
# - 2005-11-06
58
# - Fixed the rar command so that it can extract files whose names begin
58
# - Fixed the rar command so that it can extract files whose names begin
59
#   with a hyphen.
59
#   with a hyphen.
60
#
60
#
-
 
61
# - 2006-03-08
-
 
62
# - Make an interactive mode which asks the user before deleting files.
-
 
63
#
61
 
64
 
62
################################################################################
65
################################################################################
63
# REQUIREMENTS:
66
# REQUIREMENTS:
64
#
67
#
65
# This code requires the programs cfv, par2repair, lxsplit, and rar to be able
68
# This code requires the programs cfv, par2repair, lxsplit, and rar to be able
Line 170... Line 173...
170
        self.__defaults = {
173
        self.__defaults = {
171
            ('directories', 'working_directory') : '~/downloads/usenet',
174
            ('directories', 'working_directory') : '~/downloads/usenet',
172
            ('options', 'recursive') : True,
175
            ('options', 'recursive') : True,
173
            ('options', 'check_required_programs') : False,
176
            ('options', 'check_required_programs') : False,
174
            ('options', 'extract_with_full_path') : False,
177
            ('options', 'extract_with_full_path') : False,
-
 
178
            ('options', 'interactive') : False,
175
            ('regular expressions', 'par2_regex') : '.*\.par2$',
179
            ('regular expressions', 'par2_regex') : '.*\.par2$',
176
            ('regular expressions', 'video_file_regex') : '.*\.(avi|ogm|mkv|mp4)$',
180
            ('regular expressions', 'video_file_regex') : '.*\.(avi|ogm|mkv|mp4)$',
177
            ('regular expressions', 'temp_repair_regex') : '.*\.1$',
181
            ('regular expressions', 'temp_repair_regex') : '.*\.1$',
178
            ('regular expressions', 'remove_regex') : '^.*\.(rar|r\d\d)$' }
182
            ('regular expressions', 'remove_regex') : '^.*\.(rar|r\d\d)$' }
179
 
183
 
180
        self.__user_config = self.__read_config()
184
        self.__user_config = self.__read_config()
181
 
185
 
182
# This is the global config variable.
186
# This is the global config variable.
183
config = rarslave_config()
187
config = rarslave_config()
184
 
188
 
-
 
189
# This is the global options variable. (to be set later)
-
 
190
options = None
-
 
191
 
185
################################################################################
192
################################################################################
186
# The rarslave_output class
193
# The rarslave_output class
187
#
194
#
188
# This class handles the nice output summary which is printed at the end
195
# This class handles the nice output summary which is printed at the end
189
# of a run
196
# of a run
Line 406... Line 413...
406
        print
413
        print
407
 
414
 
408
        print '=== extra_pars ==='
415
        print '=== extra_pars ==='
409
        for f in self.extra_pars:
416
        for f in self.extra_pars:
410
            print f
417
            print f
411
            
418
 
412
        print
419
        print
413
 
420
 
414
        print '=== files ==='
421
        print '=== files ==='
415
        for f in self.files:
422
        for f in self.files:
416
            print f
423
            print f
Line 506... Line 513...
506
 
513
 
507
        if not self.extracted:
514
        if not self.extracted:
508
            print 'Did not extract yet, not removing currentset'
515
            print 'Did not extract yet, not removing currentset'
509
            return
516
            return
510
 
517
 
-
 
518
        files_to_remove = []
-
 
519
 
511
        # remove the main par
520
        # remove the main par
512
        os.remove(self.parfile)
521
        files_to_remove.append(self.parfile)
513
 
522
 
514
        # remove all of the extra pars
523
        # remove all of the extra pars
515
        for i in self.extra_pars:
524
        for i in self.extra_pars:
516
            os.remove(i)
525
            files_to_remove.append(i)
517
 
526
 
518
        # remove any rars that are associated (leave EVERYTHING else)
527
        # remove any rars that are associated (leave EVERYTHING else)
519
        # This regex matches both old and new style rar(s) by default.
528
        # This regex matches both old and new style rar(s) by default.
520
        regex = re.compile(
529
        regex = re.compile(
521
                config.get_value('regular expressions', 'remove_regex'),
530
                config.get_value('regular expressions', 'remove_regex'),
522
                re.IGNORECASE)
531
                re.IGNORECASE)
523
 
532
 
524
        for i in self.files:
533
        for i in self.files:
525
            if regex.match(i):
534
            if regex.match(i):
526
                os.remove(i)
535
                files_to_remove.append(i)
527
 
536
 
528
        # remove any .{001,002,...} files (from parjoin)
537
        # remove any .{001,002,...} files (from parjoin)
529
        if self.used_parjoin:
538
        if self.used_parjoin:
530
            for i in os.listdir(os.getcwd()):
539
            for i in os.listdir(os.getcwd()):
531
                if i != self.files[0] and self.files[0] in i:
540
                if i != self.files[0] and self.files[0] in i:
532
                    os.remove(i)
541
                    files_to_remove.append(i)
533
 
542
 
534
        # remove any temp repair files
543
        # remove any temp repair files
535
        regex = re.compile(
544
        regex = re.compile(
536
                config.get_value('regular expressions', 'temp_repair_regex'),
545
                config.get_value('regular expressions', 'temp_repair_regex'),
537
                re.IGNORECASE)
546
                re.IGNORECASE)
538
        [os.remove(f) for f in os.listdir(os.getcwd()) if regex.match(f)]
547
        [files_to_remove.append(f) for f in os.listdir(os.getcwd()) if regex.match(f)]
-
 
548
 
-
 
549
        # interactively remove files
-
 
550
        if options.interactive:
-
 
551
 
-
 
552
            print # blank line
-
 
553
            for f in files_to_remove:
-
 
554
                print f
-
 
555
 
-
 
556
            print '========================================'
-
 
557
 
-
 
558
            done = False
-
 
559
            while not done:
-
 
560
                s = raw_input("Delete files [y,n]: ")
-
 
561
                s.lower()
-
 
562
 
-
 
563
                if s == 'y' or s == 'yes':
-
 
564
                    done = True
-
 
565
                    self.__remove_list_of_files(files_to_remove)
-
 
566
                elif s == 'n' or s == 'no':
-
 
567
                    done = True
-
 
568
                    print 'Not removing files'
-
 
569
                else:
-
 
570
                    print 'Bad selection, try again...'
-
 
571
        else:
-
 
572
            self.__remove_list_of_files(files_to_remove)
-
 
573
 
-
 
574
    def __remove_list_of_files(self, files_to_remove):
-
 
575
        """Remove all files in the list"""
-
 
576
 
-
 
577
        for f in files_to_remove:
-
 
578
            os.remove(f)
539
 
579
 
540
    def __get_extract_file(self):
580
    def __get_extract_file(self):
541
        """Find the first extractable file"""
581
        """Find the first extractable file"""
542
        for i in self.files:
582
        for i in self.files:
543
            if os.path.splitext(i)[1] == '.rar':
583
            if os.path.splitext(i)[1] == '.rar':
Line 637... Line 677...
637
            cur.files = filenames
677
            cur.files = filenames
638
            parsets.append(cur)
678
            parsets.append(cur)
639
 
679
 
640
    return parsets
680
    return parsets
641
 
681
 
642
def directory_worker(dir, options):
682
def directory_worker(dir):
643
    """Attempts to find, verify, and extract every parset in the directory
683
    """Attempts to find, verify, and extract every parset in the directory
644
    given as a parameter"""
684
    given as a parameter"""
645
 
685
 
646
    cwd = os.getcwd()
686
    cwd = os.getcwd()
647
    os.chdir(dir)
687
    os.chdir(dir)
Line 696... Line 736...
696
    parser.add_option('-o', '--output-debug-info',
736
    parser.add_option('-o', '--output-debug-info',
697
                       action='store_true', dest='debug_info',
737
                       action='store_true', dest='debug_info',
698
                       default=False,
738
                       default=False,
699
                       help="Output debug info for every parset, then exit")
739
                       help="Output debug info for every parset, then exit")
700
 
740
 
-
 
741
    parser.add_option('-i', '--interactive', dest='interactive', action='store_true',
-
 
742
                      default=config.get_value('options', 'interactive'),
-
 
743
                      help="Confirm before removing files")
-
 
744
 
701
    # Parse the given options
745
    # Parse the given options
-
 
746
    global options
702
    (options, args) = parser.parse_args()
747
    (options, args) = parser.parse_args()
703
 
748
 
704
    # Fix up the working directory
749
    # Fix up the working directory
705
    options.work_dir = os.path.abspath(os.path.expanduser(options.work_dir))
750
    options.work_dir = os.path.abspath(os.path.expanduser(options.work_dir))
706
 
751
 
Line 717... Line 762...
717
        config.write_config()
762
        config.write_config()
718
 
763
 
719
    # Run rarslave!
764
    # Run rarslave!
720
    if options.recursive:
765
    if options.recursive:
721
        for root, dirs, files in os.walk(options.work_dir):
766
        for root, dirs, files in os.walk(options.work_dir):
722
            directory_worker(root, options)
767
            directory_worker(root)
723
    else:
768
    else:
724
        directory_worker(options.work_dir, options)
769
        directory_worker(options.work_dir)
725
 
770
 
726
    # Print the results
771
    # Print the results
727
    output.print_results_table()
772
    output.print_results_table()
728
 
773
 
729
if __name__ == '__main__':
774
if __name__ == '__main__':