File: copyif.py

package info (click to toggle)
fritzing 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,144 kB
  • sloc: cpp: 98,203; xml: 2,203; python: 803; sh: 274; ansic: 26; makefile: 22
file content (59 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download | duplicates (2)
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


import getopt, sys, os, re, time, shutil

def usage():
    print """
usage:
    copyif.py -s <source directory> -d <dest directory>

    copies files from source into dest, if the file already exists in dest

    """



def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hs:d:", ["help", "source","dest"])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        return

    source = None
    dest = None

    for o, a in opts:
        #print o
        #print a
        if o in ("-d", "--dest"):
            dest = a
        elif o in ("-s", "--source"):
            source = a
        elif o in ("-h", "--help"):
            usage()
            return
        else:
            print "unknown option", o
            return

    if not dest:
        print "missing -d (dest) argument"
        usage()
        return

    if not source:
        print "missing -s (source) argument"
        usage()
        return

    fs = []
    for f in os.listdir(source):
        df = os.path.join(dest, f)
        if os.path.exists(df) and os.path.isfile(df):
            shutil.copy(os.path.join(source,f), df)

if __name__ == "__main__":
    main()