Changeset 63 for Mulberry/trunk

Show
Ignore:
Timestamp:
07/08/07 13:07:58 (2 years ago)
Author:
daboo
Message:

tabs->spaces. Add option to undo license change.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Mulberry/trunk/license-change.py

    r59 r63  
    11#!/usr/bin/python 
    22 
     3import getopt 
    34import os 
    45import sys 
     
    2223""" 
    2324 
    24 def process_directory(path): 
    25         for file in os.listdir(path): 
    26                 fpath = os.path.join(path, file) 
    27                 if os.path.isfile(fpath): 
    28                         process_file(fpath) 
    29                 elif os.path.isdir(fpath): 
    30                         process_directory(fpath) 
     25def process_directory(path, delete_unlicensed, undo): 
     26    for file in os.listdir(path): 
     27        fpath = os.path.join(path, file) 
     28        if os.path.isfile(fpath): 
     29            if undo: 
     30                undo_file(fpath) 
     31            else: 
     32                process_file(fpath, delete_unlicensed) 
     33        elif os.path.isdir(fpath): 
     34            process_directory(fpath, delete_unlicensed, undo) 
    3135 
    32 def process_file(fpath): 
    33         if (fpath.endswith(".cp") or 
    34                 fpath.endswith(".cc") or 
    35                 fpath.endswith(".cpp") or 
    36                 fpath.endswith(".c") or 
    37                 fpath.endswith(".h")): 
    38                 replace_license(fpath) 
     36def undo_file(fpath): 
     37    if (fpath.endswith(".cp.unlicensed") or 
     38        fpath.endswith(".cc.unlicensed") or 
     39        fpath.endswith(".cpp.unlicensed") or 
     40        fpath.endswith(".c.unlicensed") or 
     41        fpath.endswith(".h.unlicensed")): 
     42        fpath = fpath[:-11] 
     43        print fpath 
     44        os.rename(fpath, "%s.licensed" % fpath) 
     45        os.rename("%s.unlicensed" % fpath, fpath) 
     46        os.remove("%s.licensed" % fpath) 
     47 
     48def process_file(fpath, delete_unlicensed): 
     49    if delete_unlicensed and \ 
     50       (fpath.endswith(".cp.unlicensed") or 
     51        fpath.endswith(".cc.unlicensed") or 
     52        fpath.endswith(".cpp.unlicensed") or 
     53        fpath.endswith(".c.unlicensed") or 
     54        fpath.endswith(".h.unlicensed")): 
     55        os.remove(fpath) 
     56    elif not delete_unlicensed and \ 
     57         (fpath.endswith(".cp") or 
     58          fpath.endswith(".cc") or 
     59          fpath.endswith(".cpp") or 
     60          fpath.endswith(".c") or 
     61          fpath.endswith(".h")): 
     62        replace_license(fpath) 
    3963 
    4064def replace_license(fpath): 
    41         print fpath 
    42         fin = open(fpath, "r") 
    43         fout = open("%s.licensed" % fpath, "w") 
     65    print fpath 
     66    fin = open(fpath, "rU") 
     67    fout = open("%s.licensed" % fpath, "w") 
    4468 
    45         first = True 
    46         ignore = False 
    47         reallyignore = False 
    48         ignored = [] 
    49         for line in fin: 
    50                 if first: 
    51                         fout.write(newlicense) 
    52                         if line.startswith("/*"): 
    53                                 ignore = True 
    54                 first = False 
    55                 if not ignore: 
    56                         fout.write(line) 
    57                 else: 
    58                         ignored.append(line) 
    59                 if ignore: 
    60                         if line.find("Copyright") != -1: 
    61                                 reallyignore = True 
    62                         if line.find("*/") != -1: 
    63                                 ignore = False 
    64                                 if not reallyignore: 
    65                                         fout.write("".join(ignored)) 
    66          
    67         fin.close() 
    68         fout.close() 
    69         os.rename(fpath, "%s.unlicensed" % fpath) 
    70         os.rename("%s.licensed" % fpath, fpath) 
     69    first = True 
     70    ignore = False 
     71    reallyignore = False 
     72    ignored = [] 
     73    for line in fin: 
     74        if first: 
     75            fout.write(newlicense) 
     76            if line.startswith("/*"): 
     77                ignore = True 
     78        first = False 
     79        if not ignore: 
     80            fout.write(line) 
     81        else: 
     82            ignored.append(line) 
     83        if ignore: 
     84            if line.find("Copyright") != -1: 
     85                reallyignore = True 
     86            if line.find("*/") != -1: 
     87                ignore = False 
     88                if not reallyignore: 
     89                    fout.write("".join(ignored)) 
     90     
     91    fin.close() 
     92    fout.close() 
     93    os.rename(fpath, "%s.unlicensed" % fpath) 
     94    os.rename("%s.licensed" % fpath, fpath) 
    7195 
    7296if __name__ == "__main__": 
    73         paths = ("Sources_Common", "Linux_v2",) 
    74         #paths = ("Plug-ins", ) 
    7597 
    76         for path in paths: 
    77                 process_directory(path) 
    78                          
     98    delete_unlicensed = False 
     99    undo = False 
     100 
     101    options, args = getopt.getopt(sys.argv[1:], "ru") 
     102 
     103    for option, value in options: 
     104        if option == "-r": 
     105            delete_unlicensed = True 
     106        elif option == "-r": 
     107            undo = True 
     108 
     109    paths = args 
     110 
     111    for path in paths: 
     112        process_directory(path, delete_unlicensed, undo) 
     113