File: resetversion.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 (91 lines) | stat: -rw-r--r-- 2,604 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
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

# lots of borrowing from http://code.activestate.com/recipes/252508-file-unzip/

import getopt, sys, os, os.path, re, zipfile, shutil

def usage():
    print """
usage:
    resetversion.py -f [from directory]

Change the version number of the fz file in the fzz files in from directory.  Probably safest to make a copy of from directory first
"""

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

    inputdir = None

    for o, a in opts:
        #print o
        #print a
        if o in ("-f", "--from"):
            inputdir = a
        else:
            print "unhandled option",o
            usage()
            return

    if not(inputdir):
        print "missing 'from' argument"
        usage()
        return

    try:
        import zlib
        compression = zipfile.ZIP_DEFLATED
    except:
        compression = zipfile.ZIP_STORED

    for root, dirs, files in os.walk(inputdir, topdown=False):
        for fzz in files:
            if not fzz.endswith('.fzz'):
                continue

            print fzz
            fzzpath = os.path.join(root, fzz)

            tempDir = inputdir + os.sep + "___temp___"
            shutil.rmtree(tempDir, 1)
            os.mkdir(tempDir)

            zf = zipfile.ZipFile(fzzpath)
            zf.extractall(tempDir)
            zf.close()

            for fz in os.listdir(tempDir):
                if not fz.endswith(".fz"):
                    continue

                try:
                    fzpath = os.path.join(tempDir, fz)
                    infile = open(fzpath, "rb")
                    #print "opened", fzpath
                    xml = infile.read()
                    infile.close()
                    os.remove(fzpath)
                    outfile = open(fzpath, 'wb')
                    outfile.write( xml.replace('fritzingVersion="0.8.6', 'fritzingVersion="0.8.5'))
                    outfile.close()
                except:
                    print "exception", fzpath, sys.exc_info()[0]
                    pass

            # helpful examples in http://www.doughellmann.com/PyMOTW/zipfile/

            os.remove(fzzpath)
            zf = zipfile.ZipFile(fzzpath, mode='w')
            for fn in os.listdir(tempDir):
                zf.write(os.path.join(tempDir, fn), fn, compression)
            zf.close()

            shutil.rmtree(tempDir, 1)

if __name__ == "__main__":
    main()