|
| 1 | +import os |
| 2 | +from datetime import datetime, timezone, tzinfo, timedelta |
| 3 | +from random import randint |
| 4 | + |
| 5 | +current_year = datetime.now().year |
| 6 | + |
| 7 | + |
| 8 | +def get_random_date_underscored(date_timezone: tzinfo = None): |
| 9 | + return datetime(year=randint(2005, current_year), |
| 10 | + month=randint(1, 12), |
| 11 | + day=randint(1, 28), |
| 12 | + tzinfo=date_timezone)\ |
| 13 | + .strftime('%Y_%m_%d') |
| 14 | + |
| 15 | + |
| 16 | +def get_random_date_iso(date_timezone=None): |
| 17 | + |
| 18 | + return datetime(year=randint(2005, current_year), |
| 19 | + month=randint(1, 12), |
| 20 | + day=randint(1, 28), |
| 21 | + tzinfo=date_timezone)\ |
| 22 | + .isoformat() |
| 23 | + |
| 24 | + |
| 25 | +def mkdir_if_not_exists(path): |
| 26 | + try: |
| 27 | + # create the directory if it doesn't already exist |
| 28 | + os.mkdir(path) |
| 29 | + except FileExistsError: |
| 30 | + # ignore the error, this means the path exists |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +def mock_dir(path, project_key='ABC', num_issues=10, num_attachments_per_issue=2, |
| 35 | + fileattachment_table_csv_export_file_name='fileattachment.csv', dates_hours_diff=0): |
| 36 | + |
| 37 | + if not project_key: |
| 38 | + raise ValueError('project_key cannot be empty') |
| 39 | + |
| 40 | + if 1 > num_issues > 10000: |
| 41 | + raise ValueError('num_issues cannot be less than 1 or more than 10 000') |
| 42 | + |
| 43 | + if 1 > num_attachments_per_issue > 100: |
| 44 | + raise ValueError('num_attachments_per_issue cannot be less than 1 or more than 100') |
| 45 | + |
| 46 | + if not fileattachment_table_csv_export_file_name: |
| 47 | + raise ValueError('fileattachment_table_csv_export cannot be empty') |
| 48 | + |
| 49 | + mkdir_if_not_exists(path) |
| 50 | + |
| 51 | + dates_timezone = timezone(timedelta(hours=dates_hours_diff)) |
| 52 | + with open(f'{path}/{fileattachment_table_csv_export_file_name}', 'w') as f: |
| 53 | + # write the header for the csv |
| 54 | + f.write('id,issueid,mimetype,filename,created,filesize,author,zip,thumbnailable\n') |
| 55 | + |
| 56 | + issue_id = 100 |
| 57 | + attachment_id = 1 |
| 58 | + for issue_key in range(1, num_issues + 1): |
| 59 | + issue_path = f'{path}/{project_key}-{issue_key}' |
| 60 | + mkdir_if_not_exists(issue_path) |
| 61 | + |
| 62 | + for _ in range(num_attachments_per_issue): |
| 63 | + with open(f'{issue_path}/{attachment_id}', 'w') as _: |
| 64 | + pass |
| 65 | + |
| 66 | + mime_type = 'image/png' |
| 67 | + file_name = f'Screenshot_{get_random_date_underscored(dates_timezone)}.png' |
| 68 | + created = get_random_date_iso(dates_timezone) |
| 69 | + file_size = randint(1000, 10000) |
| 70 | + f.write(f'{attachment_id},{issue_id},{mime_type},{file_name},{created},{file_size},unknown,,1\n') |
| 71 | + attachment_id += 1 |
| 72 | + |
| 73 | + issue_id += 1 |
0 commit comments