aboutsummaryrefslogtreecommitdiff
blob: 42b6e7d4201abdfc7cfc27b478abbe9167790a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/usr/bin/python -O
# Copyright 2004 Ian Leitch
# Copyright 1999-2004 Gentoo Foundation
# $Header$
#
# Author:
#  Ian Leitch <port001@gentoo.org>
#

import os
import re
import sys
import signal
import commands
from time import sleep
from random import randint
from optparse import OptionParser

import portage
try:
	from portage.output import *
except ImportError:
	from output import *

__author__	=	"Ian Leitch"
__email__ 	=	"port001@gentoo.org"
__productname__ =	"epkgmove"
__version__	=	"1.3.1 - \"Moving Fusion + Bandages + Plasters\""
__description__ =	"A tool for moving and renaming packages in CVS"

def print_usage():

    print
    print "%s %s [ %s ] [ %s ] [ %s ]" % (white("Usage:"), turquoise(__productname__), green("option"), green("origin"), green("destination"))
    print "   '%s' and '%s' are expected as a full package name, e.g. net-im/gaim" % (green("origin"), green("destination"))
    print "   See %s --help for a list of options" % __productname__
    print

def check_cwd(portdir):

    if os.getcwd() != portdir:
        print
        print "%s Not in PORTDIR!" % yellow(" *")
        print "%s Setting to: %s" % (yellow("   *"), os.getcwd())
	os.environ["PORTDIR"]=os.getcwd()
        return os.getcwd()
	
    return portdir

def check_args(args, portdir, options, cvscmd):

    booboo = False
    re_expr = "^[\da-zA-Z-]{,}/[\d\w0-9-]{,}$"
    o_re_expr = re.compile(re_expr)

    if len(args) == 0:
        print "\n%s ERROR: errr, didn't you forget something" % red("!!!"),
	count = range(3)
	for second in count:
	    sys.stdout.write(".")
	    sys.stdout.flush()
	    sleep(1)
        sys.stdout.write("?")
	sys.stdout.flush()
	sleep(1)
	print "\n\n%s %s hits you with a clue stick %s" % (green("    *"), __productname__, green("*"))
	sleep(1)
	print_usage()
	sys.exit(1)

    if options.remove:
        if len(args) > 1:
	    error("Please remove packages one at a time")
	    sys.exit(1)
	elif not o_re_expr.match(args[0].rstrip("/")):
	    error("Expected full package name as argument")
	    sys.exit(1)
        elif not os.path.exists(os.path.join(portdir, args[0].rstrip("/"))):
	    error("No such package '%s'" % args[0].rstrip("/"))
	    sys.exit(1)
	else:
            if not options.cvs_up:
                update_categories(portdir, args, cvscmd["update"])
            return (args[0].rstrip("/"), None)

    if len(args) == 2:
        if not o_re_expr.match(args[0].rstrip("/")):
	    error("Expected full package name as origin argument")
	    booboo = True
	elif not o_re_expr.match(args[1].rstrip("/")):
	    error("Expected full package name as destination argument")
	    booboo = True

	if booboo == True:
	    sys.exit(1)
    else:
        error("Expected two arguments as input.")
        print_usage()
	sys.exit(1)

    if not options.cvs_up:
        update_categories(portdir, args, cvscmd["update"])

    if not os.path.exists(os.path.join(portdir, args[0].rstrip("/"))):
        error("No such package '%s'" % args[0].rstrip("/"))
	booboo = True
    elif os.path.exists(os.path.join(portdir, args[1].rstrip("/"))):
        error("Package '%s' already exists" % args[1].rstrip("/"))
        booboo = True

    if booboo == True:
        sys.exit(1)
    
    return (args[0].rstrip("/"), args[1].rstrip("/"))

def check_repos(portdir):

    files = os.listdir(portdir)

    for file in files:
        if not os.path.isdir(file):
            files.remove(file)
                                                           
    if "CVS" not in files:
        error("Current directory doesn't look like a CVS repository")
	sys.exit(1)

def check_commit_queue():

    empty = True

    print
    print "%s Checking commit queue for outstanding changes..." % green(" *")
    print "%s Note: This may take a VERY long time" % yellow("   *")
    print
        
    output = commands.getoutput("cvs diff")
    
    for line in output.split("\n"):
        if not line.startswith("?"):
	    empty = False
	    break
	    
    if empty == False:
        error("Commit queue not empty! Please commit all outstanding changes before using %s." % __productname__)
	sys.exit(1)

def update_categories(portdir, catpkgs, cvsupcmd):

    my_catpkgs = []

    print
    print "%s Updating categories: " % green(" *")

    if len(catpkgs) >= 2:
        if catpkgs[0].split("/", 1)[0] == catpkgs[1].split("/", 1)[0]:
            my_catpkgs.append(catpkgs[0])
        else:
            my_catpkgs.append(catpkgs[0])
	    my_catpkgs.append(catpkgs[1])
    else:
        my_catpkgs.append(catpkgs[0])

    for catpkg in my_catpkgs:
        (category, package) = catpkg.split("/", 1)
        if os.path.exists(os.path.join(portdir, category)):
	    os.chdir(os.path.join(portdir, category))
	    print "   %s %s" % (green("*"), category)
            do_cmd(cvsupcmd)
	else:
	    print "   %s %s" % (red("!"), category)

    os.chdir(portdir)
    
def error(msg):

    sys.stderr.write("\n%s ERROR: %s\n" % (red("!!!"), msg))

def signal_handler(signal_number=None, stack_frame=None):

    error("Caught SIGINT; exiting...")
    sys.exit(1)
    os.kill(0, signal.SIGKILL)

def do_cmd(cmd):

    (status, output) = commands.getstatusoutput(cmd)
    if status != 0:
        error("Command '%s' failed with exit status %d." % (cmd, status))
	for line in output.split("\n"):
	    if line != "":
	        print "    %s %s" % (red("!"), line)
	sys.exit(1)
       
class CVSAbstraction:

    def __init__(self, portdir, oldcatpkg, newcatpkg, cvscmd, options):
    
        self._portdir = portdir
	self._cvscmd = cvscmd
	self._options = options
        self._ignore = ("CVS")

	self._old_category = ""
	self._old_package = ""
	self._new_category = ""
	self._new_package = ""
        self._old_catpkg = oldcatpkg
	self._new_catpkg = newcatpkg

        self._action = ""
	
	self._distinguish_action(oldcatpkg, newcatpkg)

    def _distinguish_action(self, oldcatpkg, newcatpkg):
                
        (self._old_category, self._old_package) = oldcatpkg.split("/")
        if newcatpkg:
            (self._new_category, self._new_package) = newcatpkg.split("/")

        if self._old_category != self._new_category and self._new_category:
	    if self._old_package != self._new_package and self._new_package:
	        self._action = "MOVE & RENAME"
            else:
	        self._action = "MOVE"
        elif self._old_package != self._new_package and self._new_package:
	    self._action = "RENAME"
	elif not self._new_package:
	    self._action = "REMOVE"
	else:
	    error("Unable to distingush required action.")
	    sys.exit(1)

    def __backup(self):

        print "%s Backing up %s..." % (green("   *"), turquoise(self._old_catpkg))
	
	if not os.path.exists("/tmp/%s" % __productname__):
	    os.mkdir("/tmp/%s" % __productname__)
	
	if os.path.exists("/tmp/%s/%s" % (__productname__, self._old_package)):
	    do_cmd("rm -rf /tmp/%s/%s" % (__productname__, self._old_package))
	    
	do_cmd("cp -R %s /tmp/%s/%s" % (os.path.join(self._portdir, self._old_catpkg), __productname__, self._old_package))

    def perform_action(self):

        count_down = 5

        print
	if self._action == "REMOVE":
	    print "%s Performing a '%s' of %s..." % (green(" *"), green(self._action), turquoise(self._old_catpkg))
        else:
            print "%s Performing a '%s' of %s to %s..." % (green(" *"), green(self._action), turquoise(self._old_catpkg), yellow(self._new_catpkg))
	
	if not self._options.countdown:
	    print "%s Performing in: " % green(" *"),
            count = range(count_down)
	    count.reverse()
	    for second in count:
	        sys.stdout.write("%s " % red(str(second + 1)))
	        sys.stdout.flush()
	        sleep(1)
	    print

        if not self._action == "REMOVE":
            self.__backup()

        if self._action == "MOVE & RENAME":
	    self._perform_move_rename()
	elif self._action == "MOVE":
	    self._perform_move()
	elif self._action == "RENAME":
	    self._perform_rename()
	elif self._action == "REMOVE":
	    self._perform_remove()

    def _perform_remove(self):

        deps = self.__get_reverse_deps()

	if deps:
	    print "%s The following ebuild(s) depend on this package:" % red("     *")
	    for dep in deps:
	        print "%s %s" % (red("       !"), dep)
	    if self._options.force:
	        print "%s Are you sure you wish to force removal of this package?" % yellow("   *"),
		try:
		    choice = raw_input("(Yes/No): ")
		except KeyboardInterrupt:
		    error("Interrupted by user.")
		    sys.exit(1)
		if choice.strip().lower() != "yes":
		    error("Bailing on forced removal.")
		    sys.exit(1)
	    else:
	        error("Refusing to remove from CVS, package has dependents.")
                sys.exit(1)

	self.__remove_old_package()

    def _perform_move(self):

        self.__add_new_package()
        self.__regen_manifest()
        self.__update_dependents(self.__get_reverse_deps())
	self.__remove_old_package()
	
    def _perform_move_rename(self):

        self._perform_rename()
 
    def _perform_rename(self):
        
        self.__rename_files()
	self.__add_new_package()
        self.__regen_digests()
	self.__update_dependents(self.__get_reverse_deps())
	self.__remove_old_package()

    def __rename_files(self):

        def rename_files(arg, dir, files):
        
	    if os.path.basename(dir) not in self._ignore:
	        if os.path.basename(dir) != self._old_package:
	            for file in files:
	                new_file = ""
	                if file.find(self._old_package) >= 0:
	   	            new_file = file.replace(self._old_package, self._new_package)
			    do_cmd("mv %s %s" % (os.path.join(dir, file), os.path.join(dir, new_file)))
		        if not os.path.isdir(os.path.join(dir, new_file)) and not file.startswith("digest-"):
		            self.__rename_file_contents(os.path.join(dir, new_file))
                else:
	            for file in files:
	                if file.endswith(".ebuild"):
	   	            new_file = file.replace(self._old_package, self._new_package)
		            do_cmd("mv %s %s" % (os.path.join(dir, file), os.path.join(dir, new_file)))
                            self.__rename_file_contents(os.path.join(dir, new_file))
			elif file.endswith(".xml"):
			    self.__rename_file_contents(os.path.join(dir, file))

	print "%s Renaming files..." % green("   *")
	os.path.walk("/tmp/%s/%s" % (__productname__, self._old_package), rename_files , None)
	do_cmd("mv /tmp/%s/%s /tmp/%s/%s" % (__productname__, self._old_package, __productname__, self._new_package))

    def __regen_manifest(self, path=None, dep=False):

        if dep:
	    print "%s Regenerating Manifest..." % green("      *")
        else:
            print "%s Regenerating Manifest..." % green("   *")
	if path:
	    os.chdir(path)
	else:
            os.chdir(os.path.join(self._portdir, self._new_catpkg))
        for ebuild in os.listdir("."):
            if ebuild.endswith(".ebuild"):
                do_cmd("/usr/lib/portage/bin/ebuild %s manifest" % ebuild)
                break

        self.__gpg_sign(dep)

    def __regen_digests(self):

        print "%s Regenerating digests:" % green("   *")
        os.chdir(os.path.join(self._portdir, self._new_catpkg))
	for digest in os.listdir("files/"):
	    if digest.startswith("digest-"):
	        os.unlink("files/%s" % digest)
	for ebuild in os.listdir("."):
	    if ebuild.endswith(".ebuild"):
                print "      >>> %s" % ebuild	
	        do_cmd("/usr/lib/portage/bin/ebuild %s digest" % ebuild)

        self.__gpg_sign()

    def __gpg_sign(self, dep=False):

        gpg_cmd = ""

        os.chdir(os.path.join(self._portdir, self._new_catpkg))

        if "sign" in portage.features:
            if dep:
                print "%s GPG Signing Manifest..." % green("      *")
            else:
                print "%s GPG Signing Manifest..." % green("   *")
	    gpg_cmd = "gpg --quiet --sign --clearsign --yes --default-key %s" % portage.settings["PORTAGE_GPG_KEY"]
	    if portage.settings.has_key("PORTAGE_GPG_DIR"):
	        gpg_cmd = "%s --homedir %s" % (gpg_cmd, portage.settings["PORTAGE_GPG_DIR"])
	    do_cmd("%s Manifest" % gpg_cmd)
	    do_cmd("mv Manifest.asc Manifest")
	do_cmd("%s 'Manifest recommit'" % self._cvscmd["commit"])

    def __rename_file_contents(self, file):
        
	def choice_loop(line, match_count, choice_list, type="name", replace=""):

            new_line = line
	    accepted = False
	    skipp = False

	    while(not accepted):
		print "         ",
		if len(choice_list) > 0:
                    for choice in choice_list:
	                print "%s: Replace with '%s'," % (white(choice), green(choice_list[choice])),
		if match_count == 0:
		    print "%s: Pass, %s: Skip this file, %s: Custom" % (white(str(len(choice_list)+1)), white(str(len(choice_list)+2)), white(str(len(choice_list)+3))),
		else:
		    print "%s: Pass, %s: Custom" % (white(str(len(choice_list)+1)), white(str(len(choice_list)+2))),
		try:
		    input = raw_input("%s " % white(":"))
    		except KeyboardInterrupt:
		    print
		    error("Interrupted by user.")
		    sys.exit(1)
		if choice_list.has_key(input):
		    if type == "name":
		        new_line = new_line.replace(self._old_package, choice_list[input])
		        accepted = True
		    elif type == "P":
		        new_line = new_line.replace("${P}", choice_list[input])
                        accepted = True
		    elif type == "PN":
		        new_line = new_line.replace("${PN}", choice_list[input])
                        accepted = True
		    elif type == "PV":
		        new_line = new_line.replace("${PV}", choice_list[input])
                        accepted = True
		    elif type == "PVR":
		        new_line = new_line.replace("${PVR}", choice_list[input])
                        accepted = True
		    elif type == "PR":
		        new_line = new_line.replace("${PR}", choice_list[input])
                        accepted = True
		    elif type == "PF":
		        new_line = new_line.replace("${PF}", choice_list[input])
                        accepted = True
		elif input == str(len(choice_list)+1):
		    accepted = True
		elif input == str(len(choice_list)+2) and match_count == 0:
		    accepted = True
		    skipp = True
		elif input == str(len(choice_list)+3) and match_count == 0 or input == str(len(choice_list)+2) and match_count != 0:
		    
		    input_accepted = False
		    
		    while(not input_accepted):
		        try:
		            custom_input = raw_input("            %s Replacement string: " % green("*"))
    		        except KeyboardInterrupt:
		            print
		            error("Interrupted by user.")
		            sys.exit(1)
			while(1):
			    print "              %s Replace '%s' with '%s'? (Y/N)" % (yellow("*"), white(replace), white(custom_input)),
		            try:
		                yes_no = raw_input(": ")
    		            except KeyboardInterrupt:
		                print
		                error("Interrupted by user.")
		                sys.exit(1)
			    if yes_no.lower() == "y":
			        input_accepted = True
				break
			    elif yes_no.lower() == "n":
			        break

                    new_line = new_line.replace(replace, custom_input)
                    accepted = True
		else:
		    accepted = False
		    
		if skipp:
		    break

	    return (new_line, skipp)

        found = False
	contents = []
	new_contents = []
	match_count = 0
	skipp = False

        try:
	    readfd = open(file, "r")
	    contents = readfd.readlines()
	    readfd.close()
	except IOError, e:
	    error(e)
	    sys.exit(1)
	
	for line in contents:
	    if line.find(self._old_package) >= 0:
	        found = True
	        break

	if found == True:
	    print "%s Editing %s:" % (green("     *"), white(file.split("/tmp/%s/%s/" % (__productname__, self._old_package))[1]))
	    try:
	        writefd = open(file, "w")
	    except IOError, e:
	        error(e)
		sys.exit(1)
	    for line in contents:
		tmp_line = line	
		if not line.startswith("# $Header:"):
                    if line.find(self._old_package) >= 0:
		        print "%s %s" % (green("       !"), line.strip().replace(self._old_package, yellow(self._old_package)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": self._new_package,
                                                                                "2": "${PN}"}, type="name", replace=self._old_package)
		        match_count += 1
			if skipp:
			    break
		    if line.find("${P}") >= 0:
                        print "%s %s" % (green("       !"), line.strip().replace("${P}", yellow("${P}")))
			(pkg, version, revising) = portage.pkgsplit(os.path.split(file)[1][:-7])
			(tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": "%s-%s" % (pkg, version),
                                                                                "2": "%s-%s" % (self._old_package, version)}, type="P", replace="${P}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PN}") >= 0:
		        print "%s %s" % (green("       !"), line.strip().replace("${PN}", yellow("${PN}")))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": self._new_package,
			                                                        "2": self._old_package}, type="PN", replace="${PN}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PV}") >= 0:
                        print "%s %s" % (green("       !"), line.strip().replace("${PV}", yellow("${PV}")))
			(pkg, version, revision) = portage.pkgsplit(os.path.split(file)[1][:-7])
			(tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": version}, type="PV", replace="${PV}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PVR}") >= 0:
                        print "%s %s" % (green("       !"), line.strip().replace("${PVR}", yellow("${PVR}")))
			(pkg, version, revision) = portage.pkgsplit(os.path.split(file)[1][:-7])
			(tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": "%s-%s" % (version, revision)}, type="PVR", replace="${PVR}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PR}") >= 0:
                        print "%s %s" % (green("       !"), line.strip().replace("${PR}", yellow("${PR}")))
			(pkg, version, revision) = portage.pkgsplit(os.path.split(file)[1][:-7])
			(tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": revision}, type="PR", replace="${PR}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PF}") >= 0:
                        print "%s %s" % (green("       !"), line.strip().replace("${PF}", yellow("${PF}")))
			(pkg, version, revision) = portage.pkgsplit(os.path.split(file)[1][:-7])
			(tmp_line, skipp) = choice_loop(tmp_line, match_count, {"1": "%s-%s-%s" % (pkg, version, revision),
			                                                        "2": "%s-%s-%s" % (self._old_package, version, revision)}, type="PF", replace="${PF}")
                        match_count += 1
			if skipp:
			    break
		    if line.find("${P/") >= 0:
		        start = line.find("${P/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
                        print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PN/") >= 0:
		        start = line.find("${PN/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
			print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PV/") >= 0:
		        start = line.find("${PV/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
			print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PVR/") >= 0:
		        start = line.find("${PVR/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
			print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PR/") >= 0:
		        start = line.find("${PR/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
			print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break
		    if line.find("${PF/") >= 0:
		        start = line.find("${PF/")
		        i = 0
		        hi_str = ""
			while(line[start + (i - 1)] != "}"):
			    hi_str += line[start + i]
                            i += 1
			print "%s %s" % (green("       !"), line.strip().replace(hi_str, red(hi_str)))
		        (tmp_line, skipp) = choice_loop(tmp_line, match_count, {}, type=None, replace=hi_str)
                        match_count += 1
			if skipp:
			    break

		new_contents.append(tmp_line)

            if not skipp:
                for line in new_contents:
                    writefd.write(line)
	    else:
	        for line in contents:
		    writefd.write(line)
            
	    writefd.close()

    def __update_dependents(self, dep_list):

        if len(dep_list) <= 0:
	    return
        
	print "%s Updating dependents:" % green("   *")
        os.chdir(self._portdir)

        for dep in dep_list:
	    print "      >>> %s" % dep
	    new_contents = []
	    (category, pkg) = dep.split("/")
	    pkg_split = portage.pkgsplit(pkg)
	    os.chdir(self._portdir)
	    do_cmd("%s %s" % (self._cvscmd["update"], os.path.join(category, pkg_split[0])))

            try:
	        readfd = open(os.path.join(self._portdir, category, pkg_split[0], "%s.ebuild" % pkg), "r")
	        contents = readfd.readlines()
	        readfd.close()
	    except IOError, e:
	        error(e)
		sys.exit(1)
	    
	    for line in contents:
	        if self._old_catpkg in line:
		    new_contents.append(line.replace(self._old_catpkg, self._new_catpkg))
		else:
		    new_contents.append(line)

	    try:
	        writefd = open(os.path.join(self._portdir, category, pkg_split[0], "%s.ebuild" % pkg), "w")
	        writefd.write("".join(new_contents))
	        writefd.close()
	    except IOError, e:
	        error(e)
		sys.exit(1)
	
	    os.chdir(os.path.join(self._portdir, category, pkg_split[0]))
            do_cmd("echangelog 'Dependency update: %s -> %s.'" % (self._old_catpkg, self._new_catpkg))
	    do_cmd("%s 'Dependency update: %s -> %s.'" % (self._cvscmd["commit"], self._old_catpkg, self._new_catpkg))
	    self.__regen_manifest(path=os.path.join(self._portdir, category, pkg_split[0]), dep=True)
            
    def __get_reverse_deps(self):

        dep_list = []
        conf_portdir = "/usr/portage"

	def scan_for_dep(arg, dir, files):

            (null, category) = os.path.split(dir)

            for file in files:
	        if self._old_catpkg not in os.path.join(category, file):
		    if not os.path.isdir(os.path.join(dir, file)):
		        try:
	                    fd = open(os.path.join(dir, file), "r")
		            contents = fd.readlines()
			    fd.close()
			except IOError, e:
			    error(e)
			    sys.exit(1)
		        if self._old_catpkg in contents[0] or self._old_catpkg in contents[1] or self._old_catpkg in contents[12]:
		            dep_list.append(os.path.join(category, file))
        
        print "%s Resolving reverse dependencies..." % green("   *")

	try:
	    fd = open("/etc/make.conf", "r")
	    contents = fd.readlines()
	    fd.close()
	except IOError, e:
	    error(e)
	    sys.exit(1)
	
	for line in contents:
	    if line.startswith("PORTDIR="):
	        (null, conf_portdir) = line.strip("\"\n").split("=")
		break

	os.path.walk(os.path.join(conf_portdir, "metadata/cache"), scan_for_dep, None)

	return dep_list

    def __remove_old_package(self):

        def remove_files(arg, dir, files):

	   if os.path.basename(dir) not in self._ignore:
	       for file in files: 
	           if not os.path.isdir(os.path.join(dir, file)): 
		       print "      <<< %s" % (os.path.join(dir.strip("./"), file))
		       os.unlink(os.path.join(dir, file))
		       do_cmd("%s %s" % (self._cvscmd["remove"], os.path.join(dir, file)))

        print "%s Removing %s from CVS:" % (green("   *"), turquoise(self._old_catpkg))
        os.chdir(os.path.join(self._portdir, self._old_catpkg))
        os.path.walk('.', remove_files , None)
        os.chdir("..")

        print "%s Commiting changes..." % green("     *")
	if self._options.remove:
	    explanation = ""
	    while(1):
	        print "%s Please provide an explanation for this removal:" % yellow("       *"),
		try:
		    explanation = raw_input("")
		    explanation = explanation.replace("'", "\\'").replace('"', '\\"')
    		except KeyboardInterrupt:
		    print
		    error("Interrupted by user.")
		    sys.exit(1)
		if explanation != "":
		    break
            do_cmd("""%s "Removed from %s: %s" """ % (self._cvscmd["commit"], self._old_category, explanation))
        else:
	    do_cmd("%s 'Moved to %s.'" % (self._cvscmd["commit"], self._new_catpkg))
        do_cmd("rm -rf %s" % os.path.join(self._portdir, self._old_catpkg))

        print "%s Checking for remnant files..." % (green("     *"))
        do_cmd(self._cvscmd["update"])
                                                                                                                  
        if not os.path.exists(os.path.join(self._portdir, self._old_catpkg)):
	    if not self._action == "REMOVE":
                print "%s %s successfully removed from CVS." % (green("   *"), turquoise(self._old_catpkg))
        else:
            error("Remnants of %s still remain in CVS." % (turquoise(self._old_catpkg)))

    def __add_new_package(self):

        def add_files(arg, dir, files):

	    (null, null, null, dirs) = dir.split("/", 3)
	
            if os.path.basename(dir) not in self._ignore:
	        os.chdir(os.path.join(self._portdir, self._new_category))
                if os.path.basename(dir) != self._new_package:
                    print "      >>> %s/" % dirs   
                    os.mkdir(dirs)
		    do_cmd("%s %s" % (self._cvscmd["add"], dirs))
                for file in files:
                    if not os.path.isdir(os.path.join(dir, file)):
                        print "      >>> %s" % os.path.join(dirs, file)
                        do_cmd("cp %s %s" % (os.path.join(dir, file), dirs))
                        do_cmd("%s %s" % (self._cvscmd["add"], os.path.join(dirs, file)))

        print "%s Adding %s to CVS:" % (green("   *"), turquoise(self._new_catpkg))
        os.chdir(os.path.join(self._portdir, self._new_category))
        print "      >>> %s/" % self._new_package
        os.mkdir(self._new_package)
        do_cmd("%s %s" % (self._cvscmd["add"], self._new_package))
        os.chdir(self._new_package)
        os.path.walk("/tmp/%s/%s" % (__productname__, self._new_package), add_files , None)
        os.chdir(os.path.join(self._portdir, self._new_catpkg))

        print "%s Adding ChangeLog entry..." % green("     *")
        do_cmd("echangelog 'Moved from %s to %s.'" % (self._old_catpkg, self._new_catpkg))

        print "%s Commiting changes..." % green("     *")
	do_cmd("%s 'Moved from %s to %s.'" % (self._cvscmd["commit"], self._old_catpkg, self._new_catpkg))

        print "%s %s successfully added to CVS." % (green("   *"), turquoise(self._new_catpkg))

    def log_move(self):

        if not self._action == "REMOVE":
	
	    update_files = []
            update_regex = "^[\d]Q-[\d]{4}$"

            p_file_regex = re.compile(update_regex)

            print "%s Logging move:" % (green("   *"))
            os.chdir(os.path.join(self._portdir, "profiles/updates"))
            do_cmd(self._cvscmd["update"])

            for file in os.listdir("."):
                o_file_regex = p_file_regex.match(file)
                if file not in self._ignore and o_file_regex:
       	            (q, y) = file.split("-")
                    update_files.append("%s-%s" % (y, q))

	    update_files.sort()
	    (y, q) = update_files[-1].split("-")
	    upfile = "%s-%s" % (q, y)
    	    print "      >>> %s" % upfile

            try:
	        fd = open(upfile, "a")
	        fd.write("move %s %s\n" % (self._old_catpkg, self._new_catpkg))
                fd.close()
	    except IOError, e:
	        error(e)
		sys.exit(1)

	    do_cmd("%s 'Moved %s to %s'" % (self._cvscmd["commit"], self._old_catpkg, self._new_catpkg))
	    os.chdir(self._portdir)

    def clean_up(self):

        if not self._action == "REMOVE":
            print "%s Removing back-up..." % (green("   *"))
            do_cmd("rm -rf /tmp/%s/%s" % (__productname__, self._old_package))
            if len(os.listdir("/tmp/%s" % __productname__)) == 0:
                do_cmd("rmdir /tmp/%s" % __productname__)

	os.chdir(self._portdir)
	    
if __name__ == "__main__":
    
    signal.signal(signal.SIGINT, signal_handler)

    cvscmd = {"remove": "cvs -Qf rm",
    	      "commit": "cvs -Qf commit -m",
    	      "update": "cvs -Qf up -dPC",
    	      "add":    "cvs -Qf add"}

    parser = OptionParser(usage="%prog [ option ] [ origin ] [ destination ]", version="%s-%s" % (__productname__, __version__))
    parser.add_option("--usage", action="store_true", dest="usage", default=False, help="Pint usage information")
    parser.add_option("-q", "--queue-check", action="store_true", dest="commit_queue", default=False, help="Check the cvs tree for files awaiting commit")
    parser.add_option("-u", "--no-cvs-up", action="store_true", dest="cvs_up", default=False, help="Skip running cvs up in the origin and destination categories")
    parser.add_option("-c", "--no-countdown", action="store_true", dest="countdown", default=False, help="Skip countdown before performing")
    parser.add_option("-R", "--remove", action="store_true", dest="remove", default=False, help="Remove package")
    parser.add_option("-F", "--force", action="store_true", dest="force", default=False, help="Force removal of package, ignoring any reverse deps")
    (options, args) = parser.parse_args()

    if options.usage:
        print_usage()
	sys.exit(0)

    if randint(1, 100) == 50:
        print "%s I put on my robe and wizard hat..." % green(" *")
 
    portdir = check_cwd(portage.settings["PORTDIR"].rstrip("/"))
    check_repos(portdir)
    (oldcatpkg, newcatpkg) = check_args(args, portdir, options, cvscmd)
    
    if options.commit_queue:
        check_commit_queue()

    ThisPackage = CVSAbstraction(portdir, oldcatpkg, newcatpkg, cvscmd, options)

    ThisPackage.perform_action()
    ThisPackage.log_move()
    ThisPackage.clean_up()

    if options.remove:
        print "%s %s successfully removed from CVS." % (green(" *"), turquoise(oldcatpkg))
    else:
        print "%s %s successfully moved to %s." % (green(" *"), turquoise(oldcatpkg), yellow(newcatpkg))