--- /dev/null
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (C) 2017 Samsung Electronics, Co., Ltd.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+import os
+import sys
+import re
+from time import sleep
+from datetime import datetime
+import requests
+import yaml
+import ast
+import copy
+
+from common.buildtrigger import trigger_next, trigger_info
+from common.mapping import git_obs_map, git_virtual_branch_map, git_obs_map_full_list
+from common import utils
+from gbp.rpm import SpecFile
+from gbp.errors import GbpError
+from common.git import Git, clone_gitproject
+from common.gerrit import GerritEnv
+
+from common.workflow import get_manifest_filelists_snapshot, get_obs_package_name
+
+# set default char-set endcoding to utf-8
+reload(sys)
+sys.setdefaultencoding('utf-8') # pylint: disable-msg=E1101
+
+HIGHLIGHT = '+'
+GREYEDOUT = '-'
+
+def request_sr_list_for_snapshot(project, snapshot, reason):
+
+ ret_data = {'project': project, \
+ 'project_to_create': None, \
+ 'requests': []
+ }
+ # 'requests':
+ # [{
+ # 'snapshot': 'snapshot-version-1',
+ # 'submit_list': [
+ # {'sr-tag-name-1': ['git-path-11']},
+ # {'sr-tag-name-2': ['git-path-21','git-path-22']},
+ # }]
+
+ # Request dashboard information
+ if True:
+ path = os.path.join(os.getenv('PUBLIC_DASHBOARD_API_URL'), \
+ 'snapshot.code?snapshot=%s' % snapshot)
+ try:
+ board_data = requests.get(os.path.join(os.getenv('PUBLIC_DASHBOARD_API_URL'), \
+ 'snapshot.code?snapshot=%s' % snapshot)).text
+ except Exception, err:
+ os.system('wget %s -O myfile' % path)
+ with open('myfile', 'r') as sf:
+ board_data = sf.read()
+ for line in board_data.replace('\\', '').split('\n'):
+ try:
+ sr_list_from_snapshot = ast.literal_eval(line)
+ except Exception, err:
+ continue
+ print sr_list_from_snapshot
+ sr_list = []
+ for sr in sr_list_from_snapshot:
+ sr_list.append({sr.get('sr'): [ x.get('repo') for x in sr.get('packages') ]})
+ ret_data['requests'].append({'snapshot': snapshot, 'submit_list': sr_list})
+ return ret_data
+
+def touch_trigger_data(raw_data):
+
+ ret_data = {'project': raw_data.get('source'), \
+ 'project_to_create': None, \
+ 'requests': []
+ }
+ sr_list = []
+ #for snapshot in raw_data.get('snapshots'):
+ if True:
+ for sr in raw_data.get('sr'):
+ sr_list.append({sr.get('sr'): [ x.get('repo') for x in sr.get('packages') ]})
+ ret_data['requests'].append({'snapshot': 'SR-SYNC', 'submit_list': sr_list})
+ return ret_data
+
+def main():
+ """
+ Script entry point.
+ """
+
+ print '---[JOB STARTED]-------------------------'
+
+ if os.getenv('SR_SYNC_ENABLED', '0') != '1':
+ return
+
+ bm_start_datetime = datetime.now()
+
+ enabled_projects = ast.literal_eval(os.getenv('SR_SYNC_PROJECTS'))
+
+ reason = os.getenv('UPSTREAM_REASON')
+ if os.getenv('TRIGGER_INFO', None):
+ manual_trigger = trigger_info(os.getenv('TRIGGER_INFO'))
+ snapshot = 'SR-SYNC'
+ project = manual_trigger.get('source')
+ target_project = manual_trigger.get('target')
+ # Parse triggered data
+ official_data = touch_trigger_data(manual_trigger)
+ else:
+ snapshot = os.getenv('UPSTREAM_SNAPSHOT')
+ project = os.getenv('UPSTREAM_OBS_PROJECT')
+ target_project = os.getenv('TARGET_PROJECT')
+ # Retrieve SR list of the snapshot from dashboard
+ official_data = request_sr_list_for_snapshot(project, snapshot, reason)
+
+ if target_project != os.getenv('TARGET_PROJECT'):
+ print 'Skip target project %s' % target_project
+ return
+
+ for source_project in enabled_projects.keys():
+ if source_project != project:
+ del enabled_projects[source_project]
+ continue
+ enabled_projects[source_project] = \
+ [ x for x in enabled_projects[source_project] \
+ if x.get('target') == target_project ]
+ print enabled_projects
+ if project not in enabled_projects:
+ return
+
+ git_url = 'ssh://%s:%s' % (os.getenv('PUBLIC_GERRIT_HOSTNAME'),
+ os.getenv('PUBLIC_GERRIT_SSHPORT'))
+
+ print '==== OFFICIAL_DATA ===='
+ print 'project: %s' % official_data['project']
+ print 'project_to_create: %s' % official_data['project_to_create']
+ print 'requests:'
+ for rq in official_data['requests']:
+ print ' snapshot:%s' % rq['snapshot']
+ print ' submit_list:'
+ for s in rq['submit_list']:
+ print ' %s' % s.keys()[0]
+ for g in s[s.keys()[0]]:
+ print ' %s' % g
+
+ for target in enabled_projects[project]:
+ target_project = target['target']
+ target_fork_prefix = target['fork']
+ branch = target['branch']
+ source_branch = target['source_branch']
+
+ ret_data = {'project': official_data['project'],
+ 'project_to_create': official_data['project_to_create'],
+ 'requests': []}
+ if 'project_to_create' in ret_data and ret_data['project_to_create'] is not None:
+ project_to_create = ret_data['project_to_create']
+ else:
+ project_to_create = 'home:prerelease:%s:submit:%s:%s' \
+ % (target_project, \
+ branch, \
+ datetime.utcnow().strftime("%Y%m%d.%H%M%S"))
+ requests_item = {}
+ requests_item[project_to_create] = []
+ print '\n >>>> Processing target %s\n >>>> %s\n' % (target_project, project_to_create)
+
+ # Remove forked git repo
+ mapping = git_obs_map_full_list(target['target'])
+ for rq in official_data['requests']:
+ _conv = {'snapshot': rq['snapshot'], 'submit_list': []}
+ for sr in rq['submit_list']:
+ _sr_tag = sr.keys()[0]
+ _sr_list = {_sr_tag: []}
+ for repo in sr[_sr_tag]:
+ bSetHighlight = False
+ for item in mapping:
+ # We only care about defined git repositories
+ if item.get('OBS_project', '') == target['target'] and \
+ item.get('Project_name', '') == target_fork_prefix + repo:
+ bSetHighlight = True
+ if bSetHighlight:
+ marked_repo = HIGHLIGHT + repo
+ else:
+ marked_repo = GREYEDOUT + repo
+ if marked_repo not in _sr_list[_sr_tag]:
+ _sr_list[_sr_tag].append(marked_repo)
+ if _sr_list[_sr_tag]:
+ _conv['submit_list'].append(_sr_list)
+ if _conv['submit_list']:
+ ret_data['requests'].append(_conv)
+ print '\nret_data:\n%s\n' % ret_data
+
+ # Find OBS package name and trigger OBS project creation job
+ for req in ret_data['requests']:
+ print ' + Make requests: [%s]\n %s\n' \
+ % (req['snapshot'], [ x.keys()[0] for x in req['submit_list'] ])
+ for sr in req['submit_list']:
+ for submit in sr.keys():
+ for git in sr[submit]:
+ _package = ''
+ if git.startswith(HIGHLIGHT):
+ _package = get_obs_package_name(git[1:], source_branch, project, \
+ submit, GerritEnv('PUBLIC_'))
+ requests_item[project_to_create].append({'package': _package, \
+ 'git_tag': submit, \
+ 'gerrit_project': git})
+
+ if True: #os.getenv('label') == 'SR-SYNC':
+ trigger_next('PRE_RELEASE_REQUEST_%s' % target['target'], \
+ {'requests': [requests_item], \
+ 'obs_target_prj': project, \
+ 'url': git_url, \
+ 'reason': reason})
+ #FIXME: remove fake entries
+ fake_date = str(bm_start_datetime).split('.')[0] + ' +0900'
+ fake_git_tag = project_to_create.split(':'+target['target']+':')[-1].replace(':','/')
+ #TODO: move BUILD-MONITOR after pre-release-request
+ trigger_next('BUILD-MONITOR_%s' % target['target'], \
+ {'bm_stage' : 'Submit',
+ 'commit_date' : fake_date,
+ 'commit_msg' : '',
+ 'submit_date' : fake_date,
+ 'submit_msg' : 'SR sync from %s' % project,
+ 'submitter' : '<tizen.build@samsung.com>',
+ 'git_tag' : fake_git_tag,
+ 'gerrit_project' : '',
+ 'gerrit_newrev' : '',
+ 'gerrit_account_name' : 'Tizen Build',
+ 'bm_start_datetime': str(bm_start_datetime),
+ 'bm_end_datetime': str(datetime.now()),
+ 'bm_src_project_lst': [project_to_create]
+ })
+
+if __name__ == '__main__':
+ sys.exit(main())
+