1import os
2import argparse
3import codecs
4from search_gxps import project_search
5
6#define a exception table that contain projects whose lib version should not change
7exception_projects=[
8"all_widgets_5_2_0.gxp",
9"all_widgets_5_2_5.gxp",
10"all_widgets_5_3_0.gxp",
11"all_widgets_5_3_2.gxp",
12"all_widgets_5_3_3.gxp",
13"all_widgets_5_3_4.gxp",
14"all_widgets_5_4_0.gxp",
15"all_widgets_5_4_1.gxp",
16"all_widgets_5_4_2.gxp",
17"all_widgets_synergy_5_4_2.gxp",
18"all_widgets_5_5_1.gxp",
19"multi_themes_16bpp_5_5_1.gxp",
20"old_language_name_test_5_6.gxp"
21]
22
23def set_project_lib_version(gxp_projects, version):
24
25    if version is None:
26        print('You must specify the GUIX library version')
27        parser.print_help()
28        exit()
29
30    updated = 0
31
32    major, minor, patch = version.split('.')
33    int_major = int(major)
34    int_minor = int(minor)
35    int_patch = int(patch)
36    new_version = str.format('%d%02d%02d' %(int_major, int_minor, int_patch))
37
38    for project in gxp_projects:
39        absolute_path = os.path.abspath(project)
40
41        if not os.path.isfile(absolute_path):
42            raise Exception("project was deleted")
43
44        (project_path, project_name) = os.path.split(absolute_path)
45
46        if project_name in exception_projects:
47            continue
48
49        # read project into memory
50        file = open(absolute_path, 'rb', -1)
51        src_lines = file.readlines()
52        file.close()
53        update = True
54
55        for line in src_lines:
56            if b'<guix_version>' in line:
57                start = line.find(b'>')
58                end = line.find(b'</')
59                if line[start+1:end] == bytearray(new_version, 'utf8'):
60                    update = False
61                    break
62
63        if not update:
64            continue
65
66        # now open project in write mode
67        file = open(absolute_path, 'wb', -1)
68
69        for line in src_lines:
70            if b'<guix_version>' in line:
71                start = line.find(b'>')
72                end = line.find(b'</')
73                line = line[:start+1] + bytearray(new_version, 'utf8') + line[end:]
74            file.write(line)
75        file.close()
76        updated += 1
77
78    print('Projects searched: %d  Projects updated: %d' %(len(gxp_projects), updated))
79
80