2
import pathlib
import subprocess
import argparse
import os
from _datetime import datetime


def get_unique_run_id():

    if os.environ.get("BUILD_NUMBER"):
        unique_run_id = os.environ.get("BUILD_NUMBER")
    elif os.environ.get("CUSTOM_BUILD_NUMBER"):
        unique_run_id = os.environ.get("CUSTOM_BUILD_NUMBER")
    else:
        unique_run_id = datetime.now().strftime('%Y%M%D%H%M%S')

    os.environ['UNIQUE_RUN_ID'] = unique_run_id

    return unique_run_id


def create_output_directory(prefix='results_'):

    global run_id
    if not run_id:
        raise Exception("Variable 'run_id' is not set. Unable to create output directory")

    curr_file_path = pathlib.Path(__file__).parent.absolute()
    dir_to_create = os.path.join(curr_file_path, prefix + str(run_id))
    os.mkdir(dir_to_create)

    print(f"Created output directory: {dir_to_create}")

    return dir_to_create


if __name__ == "__main__":
    run_id = get_unique_run_id()
    output_dir = create_output_directory()
    json_out_dir = os.path.join(output_dir, 'json_report_out.json')
    junit_out_dir = os.path.join(output_dir, 'junit_report_out')
    # import pdb; pdb.set_trace()
    parser = argparse.ArgumentParser()
    parser.add_argument('--test_directory', required=False, help='Specify the location of the test file')
    parser.add_argument('--behave_options', type=str, required=False, help='String of behave options')
    args = parser.parse_args()
    test_directory = '' if not args.test_directory else args.test_directory
    behave_options = '' if not args.behave_options else args.behave_options
    command = f'behave -k--no-capture -f json.pretty -o {json_out_dir} ' \
              f'--junit --junit-directory {junit_out_dir}' \
              f'{behave_options} ' \
              f'{test_directory}'
    print(f"Running command : {command}")
    rs = subprocess.run(command, shell=True)

When I try to run this I'm getting an error as follows: FileNotFoundError: [WinError 3] The system cannot find the path specified: 'E:\Projects\results_20204710/11/20194751'. Please help me to find a solution for this.

Thought it could be installer error. So tried both 32bit and 64bit python installers. I'm totally lost here.

  • If you're creating a directory tree, try `pathlib.Path.mkdir`: https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory – Mike67 Oct 11 '20 at 14:37
  • Can also try: `os.makedirs` instead of `os.mkdir` – Daniel Oct 11 '20 at 14:46
  • It's hard to say, but the path looks definitely wrong `E:\Projects\results_20204710/11/20194751`. You have to use either forward slashes or backslashes (within f-string), but never both of them at the same time. – Yuri Khristich Oct 11 '20 at 17:43
  • @Daniel. Thanks for your comment. It worked fine and post this as an answer and I'll verify it. – Guru Prasad Oct 13 '20 at 14:17

1 Answers1

2

For a single directory:

os.mkdir(...)

For nested directories:

os.makedirs(...)

You can also check if a diretory exists:

os.path.exists(...)
Daniel
  • 3,228
  • 1
  • 7
  • 23