Changeset 1481

Show
Ignore:
Timestamp:
11/02/06 14:38:50 (2 years ago)
Author:
ttressieres
Message:

PerforceJobScript:

add new config parameter 'p4job_prefix' and clean-up code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • perforcejobscript/trunk/p4_trac_jobs.py

    r1454 r1481  
    1010          trac_project : /data/trac/test # REQUIRED path to Trac environment 
    1111          p4_to_trac   :                 # REQUIRED mapping from Perforce to Trac status 
     12            p4_status    : trac_status       # Subsequent lines are p4 to trac mapping 
    1213          trac_to_p4   :                 # REQUIRED mapping from Trac to Perforce status 
     14            trac_status  : p4_status         # Subsequent lines are trac to p4 mapping 
    1315          debug        : 0               # OPTIONAL (0:no message, 1:some, 2:all) 
    1416          p4port       : localhost:1666  # OPTIONAL (by default P4PORT variable) 
    1517          p4user       : MyName          # OPTIONAL (by default P4USER variable) 
    1618          p4passwd     : MyPassword      # OPTIONAL (by default P4PASSWD variable) 
     19          p4job_prefix : job             # OPTIONAL specify the prefix used to name job in Perforce 
    1720          check_delay  : 60              # OPTIONAL interval between checks (60 by default) 
    1821          first_check  : -1              # OPTIONAL specify how many history are  -1: all, 0:none 
     
    4750    """ 
    4851 
    49     OPTRE = re.compile(r'(?P<key>[^:\s][^:]*)'    # everything up to : 
    50                        r'\s*(?P<sep>[:])\s*'      # any # of space/tab, 
    51                                                   # followed by : 
    52                                                   # followed by any # space/tab 
    53                        r'(?P<value>.*)$')         # everything up to eol 
    54     SUBOPTRE = re.compile(r'(?P<key>[^:]*)'    # everything up to : 
    55                           r'\s*(?P<sep>[:])\s*'      # any # of space/tab, 
    56                                                      # followed by : 
    57                                                      # followed by any # space/tab 
    58                           r'(?P<value>.*)$')         # everything up to eol 
     52    OPTRE = re.compile(r'(?P<key>[^:\s][^:]*)' # everything up to : 
     53                       r'\s*[:]\s*'            # any # of space/tab, 
     54                                               # followed by : 
     55                                               # followed by any # space/tab 
     56                       r'(?P<value>.*)$')      # everything up to eol 
     57    SUBOPTRE = re.compile(r'(?P<key>[^:]*)'  # everything up to : 
     58                          r'\s*[:]\s*'       # any # of space/tab, 
     59                                             # followed by : 
     60                                             # followed by any # space/tab 
     61                          r'(?P<value>.*)$') # everything up to eol 
    5962 
    6063    if not os.path.isfile(file): 
     
    7376        mo = OPTRE.match(line) 
    7477        if mo: 
    75             optname, vi, optval = mo.group('key', 'sep', 'value') 
     78            optname, optval = mo.group('key', 'value') 
    7679            optval = optval.strip() 
    7780            optname = optname.rstrip().lower() 
     
    8790            mo = SUBOPTRE.match(line) 
    8891            if mo: 
    89                 subname, vi, subval = mo.group('key', 'sep', 'value') 
     92                subname, subval = mo.group('key', 'value') 
    9093                subval = subval.strip() 
    9194                subname = subname.strip() 
     
    102105    http://pyperforce.sourceforge.net/ 
    103106    """ 
    104  
    105107    def __init__(self, settings): 
    106108        """connect to the Perforce server using setting's parameters 
    107109        """ 
     110        self.job_prefix = settings.get('p4job_prefix', 'job') 
     111        self.job_re = re.compile(r'%s(?P<id>[0-9]+)' %  self.job_prefix) 
    108112        self.p4 = perforce.Connection(port=settings['p4port']) 
    109113        try: 
     
    126130        try: 
    127131            if nbsec == -1: 
    128                 results = self.p4.run('jobs', '-e', 'job=job*') 
     132                results = self.p4.run('jobs', '-e', 'job='+self.job_prefix+'*') 
    129133            else: 
    130134                olddate = time.strftime("%Y/%m/%d:%H:%M:%S", time.localtime(time.time() - nbsec)) 
    131                 results = self.p4.run('jobs', '-e', 'date>='+olddate+'&job=job*') 
     135                results = self.p4.run('jobs', '-e', 'date>='+olddate+'&job='+self.job_prefix+'*') 
    132136        except perforce.PerforceError, e: 
    133137            print str(e) 
     
    137141        for record in results.records: 
    138142            r = self._createRecord(record) 
    139             key = int(record['Job'][3:]) 
    140             tickets[key] = r 
     143            mo = self.job_re.match(record['Job']) 
     144            if mo: 
     145                key = int(mo.group('id')) 
     146                tickets[key] = r 
     147            else: 
     148                print 'error when parsing %s' % record['Job'] 
    141149 
    142150        # small bug in pyperforce, the first job is in form result 
    143151        for record in results.forms: 
    144152            r = self._createRecord(record) 
    145             key = int(record['Job'][3:]) 
    146             tickets[key] = r 
     153            mo = self.job_re.match(record['Job']) 
     154            if mo: 
     155                key = int(mo.group('id')) 
     156                tickets[key] = r 
     157            else: 
     158                print 'error when parsing %s' % record['Job'] 
    147159        return tickets 
    148160 
     
    151163        """ 
    152164        try: 
    153             jobname = 'job%06d' % (key) 
     165            jobname = '%s%06d' % (self.job_prefix, key) 
    154166            results = self.p4.run('fixes', '-j', str(jobname)) 
    155167        except PerforceError, e: 
     
    163175 
    164176    def updateTicket(self, id, ticket): 
    165         """update Perforce's job fields from a Trac ticket, the ticket can have 
     177        """update Perforce's job fields from a Trac ticket, the ticket must have 
    166178        three fields: Status, User and Description. The job created has a name 
    167         like jobXXXXXX. 
     179        like jobprefixXXXXXX. 
    168180        """ 
    169181        try: 
    170             jobname = 'job%06d' % (id) 
     182            jobname = '%s%06d' % (self.job_prefix, id) 
    171183            job = perforce.Job(self.p4, jobname) 
    172184 
    173             if 'Status' in ticket: 
    174                 job['Status'] = settings['trac_to_p4'][ticket['Status']] 
    175             if 'Description' in ticket: 
    176                 job['Description'] = ticket['Description'].encode('latin-1') 
    177             if 'User' in ticket: 
    178                 job['User'] = ticket['User'].encode('latin-1') 
    179             chgdate = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(ticket['Date'])) 
    180             job['Date'] = chgdate 
     185            job['Status'] = settings['trac_to_p4'][ticket['Status']] 
     186            job['Description'] = ticket['Description'].encode('latin-1') 
     187            job['User'] = ticket['User'].encode('latin-1') 
     188            job['Date'] = time.strftime("%Y/%m/%d %H:%M:%S", 
     189                                        time.localtime(ticket['Date'])) 
    181190 
    182191            if settings.get('debug', 0): 
     
    262271            return 
    263272 
    264         if 'Status' in ticket: 
    265             tkt['status'] = settings['p4_to_trac'][ticket['Status']] 
    266             if tkt['status'] == u'closed': 
    267                 tkt['resolution'] = u'fixed' 
    268             else: 
    269                 tkt['resolution'] = u'' 
    270         if 'User' in ticket: 
    271             tkt['owner'] = ticket['User'] 
    272         if 'Description' in ticket: 
    273             tkt['summary'] = ticket['Description'] 
     273        tkt['status'] = settings['p4_to_trac'][ticket['Status']] 
     274        if tkt['status'] == u'closed': 
     275            tkt['resolution'] = u'fixed' 
     276        else: 
     277            tkt['resolution'] = u'' 
     278        tkt['owner'] = ticket['User'] 
     279        tkt['summary'] = ticket['Description'] 
    274280 
    275281        comment = '' 
     
    286292            print "update Trac %d  %s" % (id, ticket) 
    287293        if settings.get('dry_run', 0) == 0: 
    288             tkt.save_changes('Perforce', comment, when) 
     294            tkt.save_changes(ticket['User'], comment, when) 
    289295 
    290296            try: 
     
    324330                nbRemoved = 0 
    325331                if rp['Status'] == settings['trac_to_p4'][rt['Status']]: 
    326                     del self.mergedTickets[key]['Status'] 
    327                     del rp['Status'] 
    328332                    nbRemoved += 1 
    329333                if rp['Description'] == rt['Description']: 
    330                     del self.mergedTickets[key]['Description'] 
    331                     del rp['Description'] 
    332334                    nbRemoved += 1 
    333335                if rp['User'] == rt['User']: 
    334                     del self.mergedTickets[key]['User'] 
    335                     del rp['User'] 
    336336                    nbRemoved += 1 
    337337