def echo_status(self, status, url, desc, data="", header="", cookie=""): ''' 存储型xss信息输出,结果需要在触发的位置查看 ''' if status == 200: content = "[%s] [INFO] testing '%s' [RESULT] 'Saved successfully !' " % ( current_time(), desc) print_(content, B, W) else: content = "[%s] [INFO] testing '%s' " % (current_time(), desc) print_(content, G, W)
def syncToRedmine(time_entries, date, redmine): '''Push all given time_entries to Redmine''' def issue_exists(id, redmine): assert id issue_exists = True try: redmine.issue.get(id) except ResourceNotFoundError: issue_exists = False return issue_exists print_("-> Sending entries") try: time_entry = None for time_entry_infos in time_entries: print_('.') issue_id = time_entry_infos['issue_id'] time_entry_to_send = { 'spent_on': date.date.date(), # converts Moment date to Date 'issue_id': issue_id, 'hours': time_entry_infos['duration'], 'activity_id': time_entry_infos['activity_id'], 'comments': time_entry_infos['comment'] } try: # Send this activity to Redmine redmine.time_entry.create(**time_entry_to_send) except ServerError: # Error 500 # Check that issue id exists (may be a cause of 500 error on some Redmine versions) print if not issue_exists(issue_id, redmine): print "** ERROR ** Internal server error received from Redmine. "\ "This is probably because issue id {} doesn't exists ?".format(issue_id) else: print "** ERROR ** Internal server error received from Redmine !" print "\nLast time entry was:\n{}".format( pformat(time_entry_to_send)) sys.exit(-1) except ConnectionError as e: print "Connection Error: {}".format(e.message) print "\n"
def syncToRedmine(time_entries, date, redmine): '''Push all given time_entries to Redmine''' def issue_exists(id, redmine): assert id issue_exists = True try: redmine.issue.get(id) except ResourceNotFoundError: issue_exists = False return issue_exists print_("-> Sending entries") try: time_entry = None for time_entry_infos in time_entries: print_('.') issue_id = time_entry_infos['issue_id'] time_entry_to_send = { 'spent_on': date.date.date(), # converts Moment date to Date 'issue_id': issue_id, 'hours': time_entry_infos['duration'], 'activity_id': time_entry_infos['activity_id'], 'comments': time_entry_infos['comment'] } try: # Send this activity to Redmine redmine.time_entry.create(**time_entry_to_send) except ServerError: # Error 500 # Check that issue id exists (may be a cause of 500 error on some Redmine versions) print if not issue_exists(issue_id, redmine): print "** ERROR ** Internal server error received from Redmine. "\ "This is probably because issue id {} doesn't exists ?".format(issue_id) else: print "** ERROR ** Internal server error received from Redmine !" print "\nLast time entry was:\n{}".format(pformat(time_entry_to_send)) sys.exit(-1) except ConnectionError as e: print "Connection Error: {}".format(e.message) print "\n"
def syncToGitlab(times_by_issue_id, date): '''Push all given time_entries to Gitlab''' # Init Gitlab object gl = gitlab.Gitlab(config.get('gitlab', 'url'), config.get('gitlab', 'project_id'), config.get('gitlab', 'token')) # Synch print_("-> Sending entries") try: for issue_id, total in times_by_issue_id.items(): print_('.') try: # Send this activity to Gitlab gl.add_time_entry(issue_id, "{}h".format(total)) except gitlab.GitlabException: print() print("Error sending {!r}".format((issue_id, total))) raise except ConnectionError as e: print("Connection Error: {}".format(e.message)) print("\n")
def echo(self, flag, url, desc, data="", header="", cookie=""): if flag: content = "[%s] [INFO] testing '%s' " % (current_time(), desc) header = "Header: %s" % header if header else "" cookie = "Cookie: %s" % cookie if cookie else "" types = "Type: POST" if data else "Type: GET" if len(data) > 0: if len(header) > 0: info = "%sTitle: %s\n%s\nPayload: %s\nData: %s\n%s%s" % ( Y, desc, types, url, data, header, W) elif len(cookie) > 0: info = "%sTitle: %s\n%s\nPayload: %s\nData: %s\n%s%s" % ( Y, desc, types, url, data, cookie, W) else: info = "%sTitle: %s\n%s\nPayload: %s\nData: %s%s" % ( Y, desc, types, url, data, W) else: if len(header) > 0: info = "%sTitle: %s\n%s\nPayload: %s\n%s%s" % ( Y, desc, types, url, header, W) elif len(cookie) > 0: info = "%sTitle: %s\n%s\nPayload: %s\n%s%s" % ( Y, desc, types, url, cookie, W) else: info = "%sTitle: %s\n%s\nPayload: %s%s" % (Y, desc, types, url, W) print_(content, R, W) print_(info, G, W) else: content = "[%s] [INFO] testing '%s' " % (current_time(), desc) print_(content, G, W) return
question = "Sync tasks from {} to {} (included) ?".format( from_date.format(display_date_format), to_date.format(display_date_format) ) elif for_date: if args['<date>'] == '0': question = "Sync tasks for today ?" elif args['<date>'] == '1': question = "Sync tasks for yesterday ({}) ?".format(for_date.format(display_date_format)) else: question = "Sync tasks for {} ?".format(for_date.format(display_date_format)) assert question if not args['--no-date-confirm']: print(question) print_("\nPress ENTER to validate ...") try: input('') print("\n") except KeyboardInterrupt: print("\n") sys.exit() if for_date: # only one date will be parsed from_date = for_date to_date = for_date total_time = 0 total_sent_time = 0 date = from_date.clone() while date <= to_date:
else: question = "Delete tasks from {} to {} (included) ?".format( from_date.format(date_format), to_date.format(date_format)) elif for_date: if args['<date>'] == '0': question = "Delete tasks for today ?" elif args['<date>'] == '1': question = "Delete tasks for yesterday ({}) ?".format( for_date.format(date_format)) else: question = "Delete tasks for {} ?".format( for_date.format(date_format)) assert question print question print_("\nPress ENTER to validate ...") try: raw_input('') print "\n" except KeyboardInterrupt: print "\n" sys.exit() # Check that api_key or username (and eventually password) are given in config file api_key, login, password = common.get_api_key_or_login_password(config) # Connects to Redmine if api_key: redmine = Redmine(config.get('redmine', 'url'), key=api_key) else: if not password:
def getTimeEntries(date, config): '''Reads Sqlite Redmine DB file and return an array of explicit associative array for times entries, filtering out entries that do not match issue_id_regexp defined in config file Returns: - activities_array: array of dicts with 'description', 'label', 'issue_id', 'duration', 'comment', 'activity_id' keys - total_duration: sum of all activities duration ''' def fetchFromDatabase(db_filename, date): '''Fetch data from an SQLITE3 database Returns an iterable object with SELECT result''' _date = "%{}%".format(date.format('YYYY-MM-DD')) connection = sqlite3.connect(os.path.expanduser(db_filename)) dbCursor = connection.cursor() dbCursor.execute( """SELECT activities.name,facts.start_time,facts.end_time,facts.description,categories.name FROM activities JOIN facts ON activities.id = facts.activity_id LEFT JOIN categories ON activities.category_id = categories.id WHERE facts.start_time LIKE ? ORDER BY start_time""", (_date, )) return dbCursor db_filename = config.get('default', 'db') time_entries = fetchFromDatabase(db_filename, date) if os.path.exists(ACTIVITIES_CONFIG_FILE): categories_association = yaml.load(open(ACTIVITIES_CONFIG_FILE, 'r')) else: categories_association = None if config.has_option('default', 'redmine_default_activity_id'): default_activity_id = config.get('default', 'redmine_default_activity_id') else: default_activity_id = None activities = [] total_duration = 0 for time_entry in time_entries: label = time_entry[0] if not time_entry[2]: print(u'\n** Warning: ignoring "{}": Not completed yet\n'.format( label)) continue duration = ( moment.date(time_entry[2], DB_TIMESTAMP_FORMAT) - moment.date(time_entry[1], DB_TIMESTAMP_FORMAT)).seconds / 3600. assert duration > 0, "Duration for entry {} is not >0: {}".format( label, duration) total_duration += duration duration = round(duration, 1) comment = time_entry[3] # Try to find Redmine issue IDs from label using regexp defined in config file match = re.match(config.get('default', 'issue_id_regexp'), label) if match: issue_id = match.group(1) else: print u'\n** Warning: ignoring entry "{}" : not able to find issue ID\n'.format( label) continue print u"* [{duration}h #{id}]: {label}".format(duration=round( duration, 1), id=issue_id, label=label) if comment is not None: print u" {}".format(comment) # Try to find activity_id category_name = time_entry[4] if category_name is not None and categories_association is not None: if category_name in categories_association: activity_id = categories_association[category_name] else: print_(u' * Warning: unmatched category "{}"'.format( category_name)) if default_activity_id is not None: activity_id = default_activity_id print u'- assignated to default Redmine activity ID : {}'.format( default_activity_id) else: activity_id = None else: if default_activity_id is not None: activity_id = default_activity_id else: activity_id = None activities.append({ 'description': label, 'label': label, 'issue_id': issue_id, 'duration': duration, 'comment': comment, 'activity_id': activity_id }) if total_duration > 0: print "\n\nTotal : {}h".format(round(total_duration, 1)) return activities, total_duration
question = "Sync tasks from {} to {} (included) ?".format( from_date.format(date_format), to_date.format(date_format)) elif for_date: if args['<date>'] == '0': question = "Sync tasks for today ?" elif args['<date>'] == '1': question = "Sync tasks for yesterday ({}) ?".format( for_date.format(date_format)) else: question = "Sync tasks for {} ?".format( for_date.format(date_format)) assert question if not args['--no-date-confirm']: print question print_("\nPress ENTER to validate ...") try: raw_input('') print "\n" except KeyboardInterrupt: print "\n" sys.exit() # Check that api_key or username (and eventually password) are given in config file api_key, login, password = common.get_api_key_or_login_password(config) # Connects to Redmine if api_key: redmine = Redmine(config.get('redmine', 'url'), key=api_key) else: if not password:
else: question = "Delete tasks from {} to {} (included) ?".format( from_date.format(date_format), to_date.format(date_format) ) elif for_date: if args['<date>'] == '0': question = "Delete tasks for today ?" elif args['<date>'] == '1': question = "Delete tasks for yesterday ({}) ?".format(for_date.format(date_format)) else: question = "Delete tasks for {} ?".format(for_date.format(date_format)) assert question print question print_("\nPress ENTER to validate ...") try: raw_input('') print "\n" except KeyboardInterrupt: print "\n" sys.exit() # Check that api_key or username (and eventually password) are given in config file api_key, login, password = common.get_api_key_or_login_password(config) # Connects to Redmine if api_key: redmine = Redmine(config.get('redmine', 'url'), key=api_key) else: if not password:
def getTimeEntries(date, config): '''Reads Sqlite Redmine DB file and return an array of explicit associative array for times entries, filtering out entries that do not match issue_id_regexp defined in config file Returns: - activities_array: array of dicts with 'description', 'label', 'issue_id', 'duration', 'comment', 'activity_id' keys - total_duration: sum of all activities duration ''' def fetchFromDatabase(db_filename, date): '''Fetch data from an SQLITE3 database Returns an iterable object with SELECT result''' _date = "%{}%".format(date.format('YYYY-MM-DD')) connection = sqlite3.connect(os.path.expanduser(db_filename)) dbCursor = connection.cursor() dbCursor.execute("""SELECT activities.name,facts.start_time,facts.end_time,facts.description,categories.name FROM activities JOIN facts ON activities.id = facts.activity_id LEFT JOIN categories ON activities.category_id = categories.id WHERE facts.start_time LIKE ? ORDER BY start_time""", (_date,) ) return dbCursor db_filename = config.get('default', 'db') time_entries = fetchFromDatabase(db_filename, date) if os.path.exists(ACTIVITIES_CONFIG_FILE): categories_association = yaml.load(open(ACTIVITIES_CONFIG_FILE, 'r')) else: categories_association = None if config.has_option('default', 'redmine_default_activity_id'): default_activity_id = config.get('default', 'redmine_default_activity_id') else: default_activity_id = None activities = [] total_duration = 0 for time_entry in time_entries: label = time_entry[0] if not time_entry[2]: print(u'\n** Warning: ignoring "{}": Not completed yet\n'.format(label)) continue duration = (moment.date(time_entry[2], DB_TIMESTAMP_FORMAT) - moment.date(time_entry[1], DB_TIMESTAMP_FORMAT)).seconds / 3600. assert duration > 0, "Duration for entry {} is not >0: {}".format(label, duration) total_duration += duration duration = round(duration, 1) comment = time_entry[3] # Try to find Redmine issue IDs from label using regexp defined in config file match = re.match(config.get('default', 'issue_id_regexp'), label) if match: issue_id = match.group(1) else: print u'\n** Warning: ignoring entry "{}" : not able to find issue ID\n'.format(label) continue print u"* [{duration}h #{id}]: {label}".format( duration=round(duration, 1), id=issue_id, label=label ) if comment is not None: print u" {}".format(comment) # Try to find activity_id category_name = time_entry[4] if category_name is not None and categories_association is not None: if category_name in categories_association: activity_id = categories_association[category_name] else: print_(u' * Warning: unmatched category "{}"'.format(category_name)) if default_activity_id is not None: activity_id = default_activity_id print u'- assignated to default Redmine activity ID : {}'.format(default_activity_id) else: activity_id = None else: if default_activity_id is not None: activity_id = default_activity_id else: activity_id = None activities.append({ 'description': label, 'label': label, 'issue_id': issue_id, 'duration': duration, 'comment': comment, 'activity_id': activity_id }) if total_duration > 0: print "\n\nTotal : {}h".format(round(total_duration, 1)) return activities, total_duration
question = "Sync tasks from {} to {} (included) ?".format( from_date.format(date_format), to_date.format(date_format) ) elif for_date: if args['<date>'] == '0': question = "Sync tasks for today ?" elif args['<date>'] == '1': question = "Sync tasks for yesterday ({}) ?".format(for_date.format(date_format)) else: question = "Sync tasks for {} ?".format(for_date.format(date_format)) assert question if not args['--no-date-confirm']: print question print_("\nPress ENTER to validate ...") try: raw_input('') print "\n" except KeyboardInterrupt: print "\n" sys.exit() # Check that api_key or username (and eventually password) are given in config file api_key, login, password = common.get_api_key_or_login_password(config) # Connects to Redmine if api_key: redmine = Redmine(config.get('redmine', 'url'), key=api_key) else: if not password: