58 elif object.type == 'project': |
58 elif object.type == 'project': |
59 Snapshot(object, targetdir) |
59 Snapshot(object, targetdir) |
60 else: |
60 else: |
61 object.to_file(os.path.join(targetdir, object.name)) |
61 object.to_file(os.path.join(targetdir, object.name)) |
62 |
62 |
63 class __FileSnapshot: |
63 |
64 def __init__(self, object, targetdir): |
64 class _FastSnapshot: |
65 self.object = object |
65 """ Snapshot Job executed by the thread pool. """ |
66 self.targetdir = targetdir |
66 def __init__(self, pool, project, targetdir, callback, exc_hld): |
67 def __call__(self): |
67 """ Construtor, will store the parameter for the checkout. """ |
68 _logger.info("Getting %s (%s)" % (os.path.join(self.targetdir, self.object.name), self.object)) |
68 self.pool = pool |
69 self.object.to_file(os.path.join(self.targetdir, self.object.name)) |
|
70 return "Getting %s (%s)" % (os.path.join(self.targetdir, self.object.name), self.object) |
|
71 |
|
72 class __ProjectSnapshot: |
|
73 def __init__(self, project, targetdir): |
|
74 self.project = project |
69 self.project = project |
75 self.targetdir = targetdir |
70 self.targetdir = targetdir |
|
71 self.callback = callback |
|
72 self.exc_hld = exc_hld |
|
73 |
76 def __call__(self): |
74 def __call__(self): |
77 _logger.info("Snapshotting '%s' under '%s'" % (self.project, self.targetdir)) |
75 """ Do the checkout, and then walkthrough the project hierarchy to find subproject to snapshot. """ |
78 status = self.project['status'] |
76 _logger.info("Snapshotting %s under %s" % (self.project, self.targetdir)) |
79 if status == 'released' or status == 'sqa' or status == 'test' or status == 'integrate': |
77 self.project.snapshot(self.targetdir, False) |
80 _logger.info("Using Synergy wa_snapshot") |
78 def walk(dir, targetdir): |
81 return self.project.snapshot(self.targetdir, True) |
79 for object in dir.children(self.project): |
82 else: |
80 if isinstance(object, ccm.Dir): |
83 _logger.info("Non static project, using custom snapshot.") |
81 walk(object, os.path.join(targetdir, object.name)) |
84 Snapshot(self.project, self.targetdir) |
82 elif isinstance(object, ccm.Project): |
85 return "" |
83 _logger.info("Adding project %s" % object.objectname) |
86 |
84 self.pool.addWork(_FastSnapshot(self.pool, object, targetdir, self.callback, self.exc_hld)) |
87 def __FastSnapshot(pool, project, targetdir, callback, exc_hld, dir=None): |
85 |
88 if not dir: |
86 if len(self.project.subprojects) > 0: |
89 dir = project.root_dir() |
87 rootdir = self.project.root_dir() |
90 targetdir = os.path.join(targetdir, dir.name) |
88 walk(rootdir, os.path.join(self.targetdir, rootdir.name)) |
91 os.makedirs(targetdir) |
89 return "" |
92 for object in dir.children(project): |
90 |
93 if isinstance(object, ccm.Dir): |
|
94 __FastSnapshot(pool, project, targetdir, callback, exc_hld, object) |
|
95 elif isinstance(object, ccm.Project): |
|
96 pool.addWork(__ProjectSnapshot(object, targetdir), callback=callback, exc_callback=exc_hld) |
|
97 else: |
|
98 _logger.info("Getting %s (%s)" % (os.path.join(targetdir, object.name), object)) |
|
99 object.to_file(os.path.join(targetdir, object.name)) |
|
100 # Can't snapshot files in parallel. |
|
101 #pool.addWork(__FileSnapshot(object, targetdir), callback=callback, exc_callback=exc_hld) |
|
102 |
|
103 def FastSnapshot(project, targetdir, threads=4): |
91 def FastSnapshot(project, targetdir, threads=4): |
104 """ Create snapshot running by running sbsnapshot concurently. """ |
92 """ Create snapshot running by running snapshots concurrently. |
105 assert (threads > 0, "Number of threads must be > 0.") |
93 Snapshot will be made recursively top-down, and each sub project will |
106 assert (project != None, "a project object must be supplied.") |
94 be snapshotted in parallel. |
107 assert (project.type == "project", "project must be of project type.") |
95 """ |
|
96 assert threads > 0, "Number of threads must be > 0." |
|
97 assert project != None, "a project object must be supplied." |
|
98 assert project.type == "project", "project must be of project type." |
108 |
99 |
109 # error handling |
100 # error handling |
110 exceptions = [] |
101 exceptions = [] |
111 results = [] |
102 results = [] |
112 def handle_exception(request, exc_info): |
103 def handle_exception(request, exc_info): |
113 _logger.error( "Exception occured in request #%s: %s" % (request.requestID, exc_info[1])) |
104 _logger.error( "Exception occurred in request #%s: %s" % (request.requestID, exc_info[1])) |
114 exceptions.append(exc_info[1]) |
105 exceptions.append(exc_info[1]) |
115 |
106 |
116 def handle_result(request, result): |
107 def handle_result(request, result): |
117 results.append(result) |
108 results.append(result) |
118 |
109 |
119 pool = threadpool.ThreadPool(threads) |
110 pool = threadpool.ThreadPool(threads) |
120 __FastSnapshot(pool, project, targetdir, handle_result, handle_exception) |
111 pool.addWork(_FastSnapshot(pool, project, targetdir, handle_result, handle_exception)) |
121 pool.wait() |
112 pool.wait() |
122 |
113 |
123 if len(exceptions): |
114 if len(exceptions): |
124 raise CCMExtraException("Errors occured during snapshot.", exceptions) |
115 raise CCMExtraException("Errors occurred during snapshot.", exceptions) |
125 |
116 |
126 return "\n".join(results) |
117 return "\n".join(results) |
127 |
118 |
128 |
119 |
129 |
120 |
130 def FastMaintainWorkArea(project, path, pst=None, threads=4, wat=False): |
121 def FastMaintainWorkArea(project, path, pst=None, threads=4, wat=False): |
131 """ Maintain the workarea of a project in parallel. """ |
122 """ Maintain the workarea of a project in parallel. """ |
132 assert (threads > 0, "Number of threads must be > 0.") |
123 assert threads > 0, "Number of threads must be > 0." |
133 assert (isinstance(project, ccm.Project), "a valid project object must be supplied.") |
124 assert isinstance(project, ccm.Project), "a valid project object must be supplied." |
134 |
125 |
135 # error handling |
126 # error handling |
136 exceptions = [] |
127 exceptions = [] |
137 results = [] |
128 results = [] |
138 def handle_exception(request, exc_info): |
129 def handle_exception(request, exc_info): |